《C Primer Plus》第九章编程题
来源:互联网 发布:数据挖掘导论 mobi 编辑:程序博客网 时间:2024/06/05 03:23
1、设计函数min(x,y),返回两个double数值中较小的数值,同时用一个简单的驱动程序测试该函数。
#include <stdio.h>
double min(double ,double);
int main(void ){
double num1,num2;
printf("Please enter two nums(enter q to quit):\n");
while (scanf("%lf %lf" ,&num1,&num2)==2)
{
printf( "The smaller num is %.3f" ,min(num1,num2));
}
return 0;
}
double min(double num1 , double num2 ){
if(num1 >num2)
return num2 ;
else
return num1 ;
}
2、设计函数chline(ch,i,j),实现指定字符在i列到j列的输出,并用一个简单的驱动程序测试该函数。
#include <stdio.h>
void chline(char ,int ,int );
int main(void ){
char ch;
int r;
int l;
printf( "Please enter the char and the area you want to display:\n" );
while(scanf("%c %d %d" ,&ch,&r,&l)==3)
chline(ch,r,l);
return 0;
}
void chline(char ch,int r,int l){
int index_r;
int index_l;
for(index_r =0;index_r<r ;index_r++){
for(index_l=0;index_l<l ;index_l++){
printf( "%c",ch );
}
printf( "\n");
}
}
3、两数值的谐均值可以这样算:首先对两数值的倒数取平均数,然后再取倒数。编写一个带有一两个double参数的函数,计算这两个参数的谐均值。
#include <stdio.h>
const int SUM=2;
double cal(double ,double);
int main(void ){
double num1;
double num2;
printf( "Please enter the nums you want to cal(Enter q to quit):\n" );
while(scanf("%lf %lf" ,&num1,&num2)==2){
printf( "The special average is %.3lf" ,cal(num1,num2));
}
return 0;
}
double cal(double num1,double num2){
double temp;
temp = (1/ num1+1/num2 )/SUM;
return 1/temp;
}
4、编写一个程序,使其从标准输入读取字符,直到遇到文件结尾。对于每个字符,程序需要检查并报告该字符是否是一个字母,如果是的话,程序还应该报告字母表中的数值位置。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void ){
char c;
printf( "Please enter something(enter # to quit):\n" );
while((c=getchar())!=EOF ){
if(c>='A' &&c<='Z')
c = tolower(c);
if(c>='a' &&c<='z')
printf( "The position in alpha is %-3d.\n" ,(c-'a')+1);
else
printf( "Sorry,it isn't a word.\n" );
fflush( stdin);
printf( "Enter another word to check(enter # to quit):\n" );
}
return 0;
}
5、在第六章“C控制语句:循环”的程序清单6.20中,函数power()的功能是返回一个double类型数的某个正整数次幂。现在改进该循环,使其能正确地计算负幂。同时,用该函数实现0的任何次幂为0,并且任何数值的0次幂为1。使用循环的方法编写函数并在一个程序中测试它。
程序清单6-20:
#include <stdio.h>
double power(double ,int);
int main(void ){
double x,xpow;
int exp;
printf( "Enter a number and the positive integer power" );
printf( "to which \n the number will be raised.Enter q" );
printf( "to quit.\n");
while(scanf("%lf %d" ,&x,&exp)==2){
xpow = power(x,exp);
printf( "%.3g to the power %d is %.5g\n" ,x,exp,xpow);
printf( "Enter next pair of numbers or q to quit.\n" );
}
printf( "Hope you enjoyed this power trip--bye!\n" );
return 0;
}
double power(double n , int p){
double pow = 1;
int i;
for(i=1;i<=p ;i++)
pow*= n;
return pow;
}
改写:
#include <stdio.h>
#include <math.h>
double power(double , int);
int main(void ){
double x,xpow;
int exp;
printf( "Enter a number and the positive integer power" );
printf( "to which \n the number will be raised.Enter q" );
printf( "to quit.\n" );
while (scanf("%lf %d" ,&x,&exp)==2){
xpow = power(x,exp);
printf( "%.3g to the power %d is %.5g\n" ,x,exp,xpow);
printf( "Enter next pair of numbers or q to quit.\n" );
}
printf( "Hope you enjoyed this power trip--bye!\n" );
return 0;
}
double power(double n , int p){
double pow = 1;
int i;
int j;
if(p >0){
for (i=1;i<=p ;i++)
pow*= n ;
return pow;}
else if (p<0){
for(j=1;j<=abs(p );j++)
pow*= n;
return 1/pow;
} else if (n==0)
return 0;
else if (p==0)
return 1;
}
- 《C Primer Plus》第九章编程题
- C Primer Plus 第九章 编程练习 1-9题
- C Primer plus 第九章
- 记录——《C Primer Plus (第五版)》第九章编程练习第九题
- 《c primer plus》编程练习回顾-第九章
- C primer plus(第五版)编程练习第九章
- c++.primer.plus第五版第九章编程练习答案
- C.Primer.Plus(第六版)第九章 编程练习
- C ++ Primer Plus 第六版 第九章编程练习答案
- 记录——《C Primer Plus (第五版)》第七章编程练习第九题
- 记录——《C Primer Plus (第五版)》第九章编程练习第一题
- 记录——《C Primer Plus (第五版)》第九章编程练习第二题
- 记录——《C Primer Plus (第五版)》第九章编程练习第三题
- 记录——《C Primer Plus (第五版)》第九章编程练习第四题
- 记录——《C Primer Plus (第五版)》第九章编程练习第六题
- 记录——《C Primer Plus (第五版)》第九章编程练习第八题
- 记录——《C Primer Plus (第五版)》第九章编程练习第十题
- 记录——《C Primer Plus (第五版)》第十章编程练习第九题
- 我的4年程序生涯
- Launcher中拨号、短信图标加数字提示未接电话、未读短信数量
- 死锁
- 使用git进行版本控制
- ORACLE+10G+win7下载地址及安装方法
- 《C Primer Plus》第九章编程题
- db2错误代码信息
- 七大排序算法收藏版
- 《C Primer Plus》第十章编程题
- Git使用教程
- 《威博文件管理系统》从JSF1.2升级到JSF2的过程
- ImageMagick和OpenCV文件格式互转
- linux如何发送电子邮件
- 为什么css中float:right右对齐会换行