void initializecomponent( char name[ ], int * age ) { name = ""; age = 0; }

为了督促我学习完《C Primer Plus》,以后的一段时间我会经常在这篇博客里更新课后的编程练习,呵呵,加油!!!
11月22日  记:
今天看完了前两章的内容,0&31页&&
第二章  编程练习
#include&stdio.h&int main(void){printf("Anton Bruckner\n");printf("Anton\n Bruckner\n");printf("Anton\n");printf("Bruckner\n");return 0;}
#include&stdio.h&int main(void){printf("姓名:秦云豪\n");printf("地址:杭州电子科技大学\n");return 0;}
#include&stdio.h&int main(){age=23;days=age*365;printf("My age is %d,that is %d days!\n",age,days);return 0;}
#include&stdio.h&
void p1();void p2();int main(){p1();p2();&return 0;}void p1(){printf("For he's a jolly good fellow!\n");&printf("For he's a jolly good fellow!\n");printf("For he's a jolly good fellow!\n");}void p2(){printf("Which nobody can deny!\n");}
#include&stdio.h&int main(){toes=10;int toes_int toes_toes_double=toes +toes_square=toes*printf("toes is %d\ntoes_double is %d\ntoes_square is %d\n",toes,toes_double,toes_square);return 0;}
#include&stdio.h&
void smile();int main(){smile();smile();smile();printf("\n");&smile();smile();printf("\n");smile();printf("\n");}void smile(){printf("Smile!");}
#include&stdio.h&
void one();void two();void three();int main(){printf("Starting now:\n");one();two();three();printf("Done!\n");return 0;}void one(){printf("One\n");}void two(){printf("Two\n");}void three(){printf("Three\n");}
由于程序很简单,所以都没有加注释,以上就是第二章的编程练习啦,啦啦&&
11月23日  记:
今天从32页看到86页,共54页呢&&呵呵
第三章 编程练习
#include&stdio.h&int main() /*本机使用的是32位系统 */{&int i=;&float f_up=1.;float f_down=1.234567;&printf("%d\n",i+1); //有符号整型最大表示 ,故溢出&printf("%f\n",f_up*10); //单精度浮点数最大指数为38,故上溢出&printf("%f\n",f_down/10); //单精度浮点保留6位有效数字,故下溢出&return 0;}
#include&stdio.h&int main() //数值和ASCII码的转换&{ //char类型保证只读入8位&printf("Please input an ASCII number between 0-127:");scanf("%d",&a); //必须以整型读入&printf("The ASCII number means:%c\n",a);return 0;}
#include&stdio.h&int main() //响铃和&&的输出&{printf("\aStartled by the sudden sound, Sally shouted,");printf(" \"By the Great Pumpkin, what was that! \"\n");return 0;}
#include&stdio.h&int main() //浮点数的两种格式输出&{printf("Please input a float number:");scanf("%f",&a);printf("The input is %f or %e\n",a,a); //第一次用%e来输出,呵呵&return 0;}
#include&stdio.h&int main() //年龄到秒的转换&{ //用short足以&printf("Please input your age:");scanf("%hd",&age); //short类型的输入是%hd哈&printf("%e\n",age*3.156e7);}
#include&stdio.h&int main() //水的质量和分子数目转换&{printf("Please input the weight:"); //单位为夸脱&scanf("%f",&k);printf("It includes %e molecules\n",k*950/3.0e-23); //输出分子数&}
#include&stdio.h&int main() //身高在不同单位间的转换&{ //这里只实现cm到inch的转换&printf("Please input your height(cm):");scanf("%f",&height);printf("Your height is:%f(inch)\n",height/2.54);}
第四章 编程练习
#include&stdio.h&int main() //连续输入两个字符串&{char f_name[20],l_name[20];printf("Please input your first name and last name:");scanf("%s %s",f_name,l_name); //中间用个空白符搞定&printf("%s,%s\n",f_name,l_name);}
#include&stdio.h&int main() //格式化输出练习&{char name[20];printf("Please input your name:");scanf("%s",name);&printf("Your name is \"%s\"\n",name);printf("Your name is \"%20s\"\n",name);printf("Your name is \"%-20s\"\n",name);printf("Your name is %s\n",name);}
#include&stdio.h&int main() //浮点数的格式化输出&{&printf("Please input a float:");scanf("%f",&a);printf("The input is %.1f or %.1e\n",a,a); //值得一提的是浮点数的.num是小数点后保留num位&printf("The input is %+.3f or %.3E\n",a,a); //而整型的.num是指至少保留num位的数字输出,无数字则补0&return 0;}
#include&stdio.h&int main() //依然是输出&&&{ //看不出来这道题有啥意思&char name[20];printf("Please input your height(inch) and your name:");scanf("%f %s",&height,name);&printf("%s,you are %.3f feet tall\n",name,height);printf("Please input your height(cm) and your name:");scanf("%f %s",&height,name);&printf("%s,you are %.3f meters tall\n",name,height/100);&}
#include&stdio.h&#include&string.h&int main() //格式化输出的又一例&&&{ //对齐有点难度&char f_name[20];char l_name[20];short f,l;printf("Please input your last name and your first name:");scanf("%s %s",l_name,f_name);&f=strlen(f_name);l=strlen(l_name);printf("%s %s\n",f_name,l_name);printf("%*d %*d\n",f,f,l,l); //经高人指点,豁然开朗,*来表示字段宽度,呵呵&}
#include&stdio.h&#include&float.h&#include&limits.h&int main() //有关精度的测试&{double a=1.0/3.0;float b=1.0/3.0;printf("%.4lf %.4f\n",a,b);printf("%.12lf %.12f\n",a,b);printf("%.16lf %.16f\n",a,b);&printf("%f %lf\n",FLT_DIG,DBL_DIG);}
#include&stdio.h&#define GALLON_LITRE 3.785#define MILE_KILOMETRE 1.609int main() //油耗的计算&{float m,g;printf("Please input the miles and gallons:");scanf("%f%f",&m,&g);printf("That is %f miles/gallon\n",m/g);printf("That is %f litres/hkm\n",GALLON_LITRE&*g/(MILE_KILOMETRE&*m*100));&}
好,今天的任务圆满完成,好开心,啦啦啦&&
11月24日  记:
今天从87页到145页,这是&&这是&&58页哟!
第五章 编程练习
#include&stdio.h&#define H_PER_M 60 //每小时有60分钟&int main() //分钟到小时的转化程序&{ //不能同时连续声明两个long类型变量 ?&printf("Please input the minutes(a positive number):");scanf("%ld",&minutes);while(minutes&0){hours=minutes/H_PER_M;&minutes=minutes%H_PER_M; //此处要求minutes不能为浮点型&printf("That is %ld hours and %ld minutes\n",hours,minutes);printf("Again:");scanf("%ld",&minutes);}return 0;}
#include&stdio.h&int main() //依次输出大于等于输入值的11个数&{int i=10;printf("Please input a integer:");scanf("%ld",&integer);while(i--) //这种用法是比较推荐的哦&&&printf("%ld ",integer++);printf("%ld\n",integer);return 0;}
#include&stdio.h&#define DAYS_PER_WEEK 7 //每个星期7天&int main() //天数到周的转化程序&{printf("Please input the days(a positive number):");scanf("%ld",&days);while(days&0){weeks=days/DAYS_PER_WEEK;&temp=days%DAYS_PER_WEEK; //这里要求使用一个临时变量&printf("%ld days are %ld weeks,%ld days.\n",days,weeks,temp);printf("Again:");scanf("%ld",&days);}return 0;}
#include&stdio.h&#define INCHES_PER_FEET 12#define INCHES_PER_CM 0.393700int main() //厘米到英尺英寸的&{float centimeters,printf("Enter a height in centimeters:");scanf("%f",&centimeters);while(centimeters&0){inches=centimeters*INCHES_PER_CM;feet=inches/INCHES_PER_FEET; //浮点型到长整型的自动转换&inches=inches-feet*INCHES_PER_FEET; //不能用&%&号哈&&&printf("%.1f cm = %ld feet, %.1f inches\n",centimeters,feet,inches);printf("Enter a height in centimeters(&=0 to quit):");scanf("%f",&centimeters);}printf("Bye\n");return 0;}
#include&stdio.h&int main() //计算前20个整数的和&{int count,i=20,sum=0;printf("Please input 20 integers:\n");while(i--) //这个貌似是唯一的亮点了&&&{scanf("%d",&count);&sum=sum+&}printf("The sum of the 20 integers is:%d\n",sum);return 0;}
#include&stdio.h&int main() //计算前20个整数的平方和&{int count,i=20,sum=0;printf("Please input 20 integers:\n");while(i--) //这依然是唯一的亮点了&&&{scanf("%d",&count);sum=sum+count*}printf("The quadratic sum of the 20 integers is:%d\n",sum);return 0;}
#include&stdio.h&float cube(float);int main() //计算一个浮点数的立方&{&float base,printf("Please input a float:");scanf("%f",&base);cube(base); //参数传递&return 0;&}float cube(float n){printf("The number's cube is:%f\n",n*n*n);return 0;}
#include&stdio.h&void temperature(double); //声明时也要有&;&&int main() //实现华氏温度和摄氏温度及绝对温度的转化&{printf("Please input a temperature(Fahrenheit):");while(scanf("%lf",&f)){temperature(f);&printf("Again(input q to quit):"); //输入非数字字符都可以退出&}return 0;&}void temperature(double f){const double F_C_MULT=1.8; //const后要有数据类型以及&=&号&const double F_C_PLUS=32.0;const double C_K_PLUS=273.16;c=f*F_C_MULT+F_C_PLUS;k=c+C_K_PLUS;printf("%.2lf Fahrenheit is %.2lf Celsius or %.2lf Kelvin\n",f,c,k);}
这就是今天的成果啦,精读了五、六两章的内容,至于第六章的编程练习呵,明天更新,尽请期待吧&&
11月25日  记:
今天把第六章的编程练习更新一下&&从做题的消耗上看出我编程真有待提高&&
第六章 编程练习
#include&stdio.h&int main() //二十六个字母的输出&{char i=0,c='a',ch[26];while(c&='z'){ch[i]=c;printf("%c ",ch[i]); //printf()放在里面可以少用个循环&i++;c++;}printf("\n");return 0;}
#include&stdio.h&int main() //嵌套循环的使用&{for(int i=0;i&5;i++){for(int j=0;j&i+1;j++) //一般是在初始化和判断语句中使用i和j的关联的&printf("%c",'$');printf("\n");}&return 0;}
#include&stdio.h&int main() //依然嵌套循环&{for(int i=0;i&6;i++){for(int j=0;j&i+1;j++)&printf("%c",'F'-j); //在输出中使用j可以有规则的改变输出的值&printf("\n");}&return 0;}
#include&stdio.h&int main() //输出字母金字塔&{int i,space,up,printf("Please input a capital letter:");scanf("%c",&letter);for(i=0;i&=letter-'A';i++){ //在每一行里使用三个与行数相关的循环来实现&for(space='A';space&letter-i;space++){printf(" ");}for(up='A';up&='A'+i;up++){printf("%c",up);&}for(down=up-2;down&='A';down--) //这里的&&&失误的写成&&&导致很大的错误,切记切记&&&{printf("%c",down);}printf("\n");}return 0;}
#include&stdio.h&int main() //用循环输出输入值及其平方和立方&{int super,printf("Please input the lower limit and superior limit(lower&=super):");scanf("%d%d",&lower,&super); //在输入时,%d%d之间多加了一个&,&结果又出错了&&笨死喽&for(int i=i&=i++) //还有上次的忘记&&&号,吃一堑长一智,以后不要犯类似的低级错误啦&printf("%d,%d,%d\n",i,i*i,i*i*i);return 0;}
#include&stdio.h&#include&string.h&int main() //单词在逆序输出&{char ch[100];printf("Please input a word:");scanf("%s",ch); //直接用字符串输入形式&for(int j=strlen(ch);j&0;j--) //逆序输出&printf("%c",ch[j-1]);printf("\n");return 0;}
#include&stdio.h&int main() //循环进行浮点数的运算&{float first,printf("Please input two floats:");while(scanf("%f",&first)) //这也算亮点?&{scanf("%f",&second);printf("%f\n",(first-second)/(first*second));printf("Again:");&}return 0;}
#include&stdio.h&float f_test(float,float);int main() //对函数的返回值的练习&{float first,printf("Please input two floats:");while(scanf("%f",&first)){&scanf("%f",&second);printf("%f\n",f_test(first,second)); //直接利用函数的返回值作为printf的参数&printf("Again:");&}return 0;}float f_test(float f,float s){return (f-s)/(f*s);}
#include&stdio.h&int main() //用循环求平方的和&{int super,lower,printf("Enter lower and upper integer limits:");scanf("%d%d",&lower,&super);while(lower&super) //while也可用来当做判断的语句&{for(int i=i&=i++)&sum+=i*i;printf("The sums of the squares ""from %d to %d is %d\n",lower*lower,super*super,sum);sum=0; //sum切记要清零 !!!printf("Enter next set of limits:");scanf("%d%d",&lower,&super);}printf("Done\n");&return 0;}
#include&stdio.h&#define SIZE 8int main() //整数逆序输出&{int num[SIZE];printf("Please input eight integers:");for(int i=0;i&8;i++)scanf("%d",&num[i]); //怎么又忘了&&&啦&&&for(int j=SIZE;j&0;j--)&printf("%d ",num[j-1]); //逆序输出,注意是j-1哈&printf("\n");return 0;}
#include&stdio.h&int main() //数列的收敛性测试&{int t,i,j;float sign=-1.0;float sum_plus_sub=0.0;float sum_all_plus=0.0;printf("Please input the times:");while(scanf("%d",&t)){for(i=1;i&=t;i++){sum_all_plus+=1.0/i;for(j=0;j&i;j++)sign*=-1; //符号位复位&sum_plus_sub+=sign/i;sign=-1.0;&}printf("%f,%f\n",sum_all_plus,sum_plus_sub);&sum_all_plus=sum_plus_sub=0.0; //在输出后记得把sum清零&}&return 0;}
#include&stdio.h&int main() //2的8次幂的依次输出&{int i,j,k=0;int array[8];for(i=0;i&8;i++) //C编译器要求for必须要在这里对i初始化&{array[i]=1;for(j=0;j&i;j++)&array[i]*=2;}do{printf("%d\n",array[k]);}while((k++)&7); //k这样用方便一些&return 0;}
#include&stdio.h&#define SIZE 8int main() //数组值的循环相加&{double first[SIZE],second[SIZE];printf("Please input 8 double numbers:");&scanf("%lf",&first[0]);second[0]=first[0];for(i=1;i&SIZE;i++)&{&scanf("%lf",&first[i]);second[i]=second[i-1]+first[i]; //i-1必须大于0&}i=0;while(i++&SIZE)printf("%lf ",first[i-1]); //i已经自动加1,所以用i-1&printf("\n");&i=0;while(i++&SIZE)printf("%lf ",second[i-1]);&printf("\n");&return 0;}
#include&stdio.h&#define SIZE 255int main() //算是不用&%s&输入字符串的一种方法&{char ch[SIZE];int i=0;printf("Please input some letters:");scanf("%c",&ch[0]); //scanf()函数只是返回输入值的个数while(ch[i++]!='\n') //将其放在while()函数里面会使!='\n'无效&scanf("%c",&ch[i]);&while((--i)&0)printf("%c",ch[i-1]);printf("\n");return 0;}
#include&stdio.h&#define capital 100#define rate_simple 0.1 //多用#define有好处&#define rate_complex 0.05int main() //哪种利息方式更合理&{double Daphne=capital,Deirdre=for(years=1;Deirdre&=Dyears++){Daphne+=capital*rate_Deirdre+=Deirdre*rate_ //复利计算&}printf("That needs %d years, Deirdre owns %.2lf dollars ""and Daphne owns %.2lf dollars\n",years-1,Deirdre,Daphne);&return 0;}
#include&stdio.h&#define capital 100 /*单位为:万美元*/#define rate 0.08&int main() //中大奖之后可以用几年?&{double cash=for(years=1;cash&0;years++){cash=cash*(rate+1)-10;&}printf("That needs %d years!!!\n",years-1);&return 0;}
今天的编程任务算是蛮重的,给我一个教训就是:我还要在编程上下大功夫才行!
11月28号  记:
这两天都没有更新内容啦,真是不好意思,书倒是看了不少,就是编程练习的不多,感觉其实我星期天倒没比平时多多少空闲时间&&
第七章  编程练习
#include&stdio.h&int main() //输出空格和换行符的个数&{&int space,newline,space=newline=others=0;printf("Please input some words:");while((ch=getchar())!='#') //'#'作为结束符&{if(ch==' ')space++;else if(ch=='\n')newline++;elseothers++;&}printf("There are %d ' ',%d '\\n' and %d other signs\n",space,newline,others);return 0;}
#include&stdio.h&int main() //输出字符和其相应的ASCII&{char ch[255];int i=0,j=0;printf("Please input some words:");while((ch[i++]=getchar())!='#') //这样挺好的&;while(j&i-1){while((j%8)!=0){printf("%c,%d\t",ch[j],ch[j]);j++;}&printf("\n"); //这样格式不太好,有待改进&printf("%c,%d\t",ch[j],ch[j]);&j++;}printf("\n");&return 0;}
#include&stdio.h&int main() //奇数偶数在统计&{int temp,even=0,odd=0,sum_even=0,sum_odd=0;printf("Please input some integers(0 to exit):");while(scanf("%d",&temp)&&temp!=0){if(temp%2) //if else 语句的使用&{sum_odd+=odd++;}else&{sum_even+=even++;}}printf("There are %d evens, the sum of the evens is %d\n""There are %d odds, the sum of the odds is %d\n",even,sum_even,odd,sum_odd);return 0;}
#include&stdio.h&int main() //字符替换&{int replace=0;printf("Please input some sentences('#' to exit):");while((ch=getchar())!='#'){if(ch=='.'){putchar('!');replace++; //练习continue的使用&}else if(ch=='!'){putchar('!');putchar('!');replace++;}else&putchar(ch);}printf("There are %d replaces\n",replace);ch=scanf("%c",&ch);return 0;}
#include&stdio.h&int main() //奇数偶数在统计&{int temp,even=0,odd=0,sum_even=0,sum_odd=0;printf("Please input some integers(0 to exit):");while(scanf("%d",&temp)&&temp!=0){switch(temp%2) //switch 的练习使用&{case 0:sum_even+=even++;case 1:&sum_odd+=odd++;default:}}printf("There are %d evens, the sum of the evens is %d\n""There are %d odds, the sum of the odds is %d\n",even,sum_even,odd,sum_odd);return 0;}
#include&stdio.h&int main() //统计&ei&出现的次数&{char ch,last=0,current=0;int num=0;printf("Please input some sentences which include \"ei\"('#' to exit):");while ((ch=getchar())!='#'){last= //注意对前一个的更新&current=if(last=='e'&&current=='i')num++;}printf("&ei&appears %d times in this sentence\n",num);return 0;}
#include&stdio.h&#define SALARY_PER_H 10#define STD_TIME 40#define TIMES 1.5#define SALARY_FIRST 300#define RATE_FIRST 0.15#define SALARY_NEXT 150#define RATE_NEXT 0.2#define RATE_OTHERS 0.25
int main() //工资记录程序&{float salaries=0,taxes=0,net_wages=0;printf("Please input the hours:");scanf("%d",&hours);if(hours&40) //工资的计算&{salaries=STD_TIME*SALARY_PER_H;salaries+=(hours-STD_TIME)*TIMES*SALARY_PER_H;}elsesalaries+=hours*SALARY_PER_H;if(salaries&=SALARY_FIRST) //税金的计算&taxes=salaries*RATE_FIRST;else if(salaries&=(SALARY_FIRST+SALARY_NEXT)){taxes=(SALARY_FIRST)*RATE_FIRST+(salaries-SALARY_FIRST)*RATE_NEXT;}else{taxes=SALARY_FIRST*RATE_FIRST+SALARY_NEXT*RATE_NEXT+(salaries-SALARY_FIRST-SALARY_NEXT)*RATE_OTHERS;&}&net_wages=salaries- //净工资&printf("The salary is %.2f, taxes is %.2f, net salary is %.2f\n",salaries,taxes,net_wages);return 0;}
#include&stdio.h&#define STD_TIME 40#define TIMES 1.5#define SALARY_FIRST 300#define RATE_FIRST 0.15#define SALARY_NEXT 150#define RATE_NEXT 0.2#define RATE_OTHERS 0.25
int main() //工资记录程序&{float salaries=0,taxes=0,net_wages=0,salary_per_h=0.0;printf("*****************************************************************\n");printf("Enter the number corresponding to the desired pay rate or action:\n");printf("1)$8.75/hr 2)$9.33/hr\n""3)$10.00/hr 4)$11.20/hr\n""5)quit\n");printf("*****************************************************************\n");while((chose=getchar())!='5') //比输入数字更一般化&{if(chose=='\n') //&\n&要单独拿出来消除&&if(chose!='1'&&chose!='2'&&chose!='3'&&chose!='4'&&chose!='5'){printf("Please input 1-5,again:\n");}else{&switch(chose){case '1':salary_per_h=8.75;case '2':salary_per_h=9.33;case '3':salary_per_h=10.00;case '4':salary_per_h=11.20;default:}}printf("Please input the hours:");scanf("%d",&hours);if(hours&40) //工资的计算&{salaries=STD_TIME*salary_per_h;salaries+=(hours-STD_TIME)*TIMES*salary_per_h;}elsesalaries+=hours*salary_per_h;if(salaries&=SALARY_FIRST) //税金的计算&taxes=salaries*RATE_FIRST;else if(salaries&=(SALARY_FIRST+SALARY_NEXT)){taxes=(SALARY_FIRST)*RATE_FIRST+(salaries-SALARY_FIRST)*RATE_NEXT;}else{taxes=SALARY_FIRST*RATE_FIRST+SALARY_NEXT*RATE_NEXT+(salaries-SALARY_FIRST-SALARY_NEXT)*RATE_OTHERS;&}net_wages=salaries- //净工资&printf("The salary is %.2f, taxes is %.2f, net salary is %.2f\n",salaries,taxes,net_wages);&salaries=0;taxes=0;net_wages=0;}return 0;}
#include&stdio.h&int main() //素数的判断&{bool prime=int i,small,printf("Please input a positive integer:");&scanf("%d",&number);printf("The primes smaller than %d are:\n",number);for(small=2;small&=small++){for(i=2;i&i++)&{if(small%i==0){&prime= //prime 做为是素数的标记&}&}if(prime)printf("%d ",i);prime=}printf("\n");return 0;}
#include&stdio.h&#define SALARY_SINGLE 17850#define SALARY_HOUSEHOLD 23900#define SALARY_MARRIED 29750#define SALARY_DIVORCED 14875#define RATE_STD 0.15#define RATE_OTHER 0.28
int main() //taxes的计算程序&{float salaries=0.0,salary=0.0,taxes=0.0;printf("*****************************************************************\n");printf("Enter the number corresponding to choose the kind:\n");printf("1)single 2)household\n""3)married 4)divorced\n""5)quit\n");printf("*****************************************************************\n");while((chose=getchar())!='5') //比输入数字更一般化&{if(chose=='\n') //&\n&要单独拿出来消除&&if(chose!='1'&&chose!='2'&&chose!='3'&&chose!='4'&&chose!='5'){printf("Please input 1-5,again:\n");}else{&switch(chose){case '1':salaries=SALARY_SINGLE;case '2':salaries=SALARY_HOUSEHOLD;case '3':salaries=SALARY_MARRIED;case '4':salaries=SALARY_DIVORCED;default:}}printf("Please input your salary:");scanf("%f",&salary);if(salary&salaries) //taxes的计算&{taxes=salaries*RATE_STD;taxes+=(salary-salaries)*RATE_OTHER;}elsetaxes+=salary*RATE_STD;printf("The taxes is %.2f\n",taxes);&salaries=0;taxes=0;}return 0;}
#include&stdio.h&#define THISTLE 1.25#define BEET 0.65 //要大写&#define CARROT 0.89#define DISCOUNT 0.95#define DISCOUNT_FEE 100
int main() //蔬菜买卖的各项费用&{float weight=0.0,thistle_weight=0.0,beet_weight=0.0,carrot_weight=0.0; //各种weight,各种fee&float fees=0.0,fees_thistle=0.0,fees_beet=0.0,fees_carrot=0.0,fees_discount=0.0,total_fees=0.0;printf("*****************************************************************\n");printf("Enter the number corresponding to choose the kind:\n");printf("a)thistle b)beet\n""c)carrot q)quit\n");printf("*****************************************************************\n");while(chose=getchar())&{if(chose=='\n') //依然是对&\n&的特殊处理&else if(chose=='q') //quit前对各项费用进行输出&{if(fees&DISCOUNT_FEE)&{fees_discount=fees*(1-DISCOUNT);fees*=DISCOUNT; //没有运费时的fee&}if(weight&=5.0) //计算添加了运费的fee&total_fees=fees+3.50;&else if(weight&=20)total_fees=fees+10.00;else&total_fees=fees+8+0.1*(weight-20);//进行各项输出&printf("\nThe thistle's price is %.2f;The beet's price is %.2f;""The carrot's price is %.2f\n\n",THISTLE,BEET,CARROT);printf("You have ordered %.2f pounds thistles, %.2f pounds beets,""and %.2f pounds carrots\n\n",thistle_weight,beet_weight,carrot_weight);printf("The fees of thistles are $%.2f;The fees of beets are $%.2f"";The fees of carrots are $%.2f\n\n",fees_thistle,fees_beet,fees_carrot);printf("So this order's fees are:$%.2f,discounts are:$%.2f\n",fees,fees_discount);printf("When it adds transport fees:$%.2f,""the total fees are:$%.2f\n\n",total_fees-fees,total_fees);}&else if(chose!='a'&&chose!='b'&&chose!='c') //对输入不符合要求的字符进行提示&{printf("Please input a or b or c or q,again:\n");}else{&switch(chose) //根据所选的项目进行差别化对待&{case 'a':printf("Please input the thistle's weight that you want:");scanf("%f",&thistle_weight);weight+=thistle_fees_thistle+=thistle_weight*THISTLE;fees+=fees_case 'b':printf("Please input the beet's weight that you want:");scanf("%f",&beet_weight);weight+=beet_fees_beet+=beet_weight*BEET;fees+=fees_case 'c':printf("Please input the carrot's weight that you want:");scanf("%f",&carrot_weight);weight+=carrot_fees_carrot+=carrot_weight*CARROT;fees+=fees_case 'n':default:}printf("Anythings else?('q' for quit):");}}return 0;}
看到最后这几个编程,还是蛮洋洋自得的,虽说说不上很高难度,但是比起前两天强多啦,编译的时候没那么多错误,呵呵,编程能力有提高哈&&
12月2号  记:
这两天有点小不淡定,该给自己心态稳定一下,一定要坚持到底哈&&
第八章  编程练习
#include&stdio.h&int main() //字符个数统计&{int number=0;while((ch=getchar())!=EOF) //以EOF结束则就直接退出了&{number++;putchar(ch); //演示一下行缓冲&}&printf("You have input %d chars\n",number);return 0;}
#include&stdio.h&int main() //字符及其ASCII的输出&{int i=1;printf("Please input some things:");while((ch=getchar())!=EOF){if(ch&=32) //对可打印字符的输出&{putchar(ch);printf(" %3d, ",ch);}else if(ch=='\n'){printf("\\n");printf(" %2d, ",ch);}else if(ch=='\t'){printf("\\t");printf(" %2d, ",ch);}else //对不可打印字符的输出&{putchar('^');putchar(ch+64);printf(" %2d, ",ch);}&if(++i==10) //每行输出10个字符&{putchar('\n');i=1;}&}&return 0;}
#include&stdio.h&#include&ctype.h&int main() //查询大小写字母的个数&{&int capital_letter=0,lower_letter=0;printf("Please input some sentences:");while((ch=getchar())!='$'){if(isupper(ch))capital_letter++; //用ctype中的函数挺方便哈&if(islower(ch))lower_letter++;&}printf("There are %d capital letters and %d lower"" letters in these sentences.\n",capital_letter,lower_letter);return 0;}
#include&stdio.h&int main() //判断句子中的单词数目&{&char ch,current=' ',last=' ';int words=0;printf("Please input some sentences:");while((ch=getchar())!=EOF){current= //这个方法比较傻&&if(last==' '&&(current!=' '||current!='\t'||current!='\n'))words++;last=&}printf("There are %d words in these sentences.\n",words);return 0;}
#include&stdio.h&int main() //二分法快速猜数&{int guess = 50,little=100,big=1;printf("Pick an integer from 1 to 100. I will try to guess ");printf("it.\n Respond with a y if my guess is right and with");printf("\na \"l\" for little or a \"b\" for big.\n");printf("Uh...is your number %d?\n",guess);while(((ch=getchar())!='y')){if(getchar()!='\n')else{switch(ch) //一下的写法可是我自创的哈,鼓励一下&{ //不过 没办法猜测100这个数字&case 'l':big= //为输入big做一个上级的数值记录&guess=(guess+little)/2;&case 'b':little=guess=(guess+big)/2;}&printf("Well, then, is it %d?\n",guess);}}&printf("I knew I could do it!\n");return 0;}
#include&stdio.h&int get_first();int main() //获得第一个非空字符&{ch=get_first();putchar(ch);&return 0;}&int get_first(void){ch=getchar();&while(ch==' '||ch==' '||ch=='\n') //进行一个判断就行&ch=getchar();}
#include&stdio.h&#define STD_TIME 40#define TIMES 1.5#define SALARY_FIRST 300#define RATE_FIRST 0.15#define SALARY_NEXT 150#define RATE_NEXT 0.2#define RATE_OTHERS 0.25
int main() //这个程序在第七章已经实现&{float salaries=0,taxes=0,net_wages=0,salary_per_h=0.0;printf("*****************************************************************\n");printf("Enter the number corresponding to the desired pay rate or action:\n");printf("a)$8.75/hr b)$9.33/hr\n""c)$10.00/hr d)$11.20/hr\n""e)quit\n");printf("*****************************************************************\n");while((chose=getchar())!='e') //比输入数字更一般化&{if(chose=='\n') //&\n&要单独拿出来消除&&if(chose!='a'&&chose!='b'&&chose!='c'&&chose!='d'){printf("Please input a-e,again:\n");}else{&switch(chose){case 'a':salary_per_h=8.75;case 'b':salary_per_h=9.33;case 'c':salary_per_h=10.00;case 'd':salary_per_h=11.20;default:}}printf("Please input the hours:");scanf("%d",&hours);if(hours&40) //工资的计算&{salaries=STD_TIME*salary_per_h;salaries+=(hours-STD_TIME)*TIMES*salary_per_h;}elsesalaries+=hours*salary_per_h;if(salaries&=SALARY_FIRST) //税金的计算&taxes=salaries*RATE_FIRST;else if(salaries&=(SALARY_FIRST+SALARY_NEXT)){taxes=(SALARY_FIRST)*RATE_FIRST+(salaries-SALARY_FIRST)*RATE_NEXT;}else{taxes=SALARY_FIRST*RATE_FIRST+SALARY_NEXT*RATE_NEXT+(salaries-SALARY_FIRST-SALARY_NEXT)*RATE_OTHERS;&}net_wages=salaries- //净工资&printf("The salary is %.2f, taxes is %.2f, net salary is %.2f\n",salaries,taxes,net_wages);&salaries=0;taxes=0;net_wages=0;}return 0;}
#include&stdio.h&#include&ctype.h&int main() //数的加减乘除法&{char ch,ch_arr[100];float first,printf("Enter the opration of your choice:\n" //菜单选项&"a.add \t\t\t s.subtract\n""m.multiply \t\t\t d.divide\nq.quit\n");while((ch=getchar())!='q'){if(getchar()!='\n')if(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'){printf("Please input a 'a' or 's' or 'm' or 'd' or 'q':\n");}printf("Enter first number:");while(scanf("%f",&first)!=1) //判断输入是否合法&{scanf("%s",ch_arr);printf("%s is not a number.\n""Please input a number,such as 2.5,-1.78E8,or 3:",ch_arr);}printf("Enter second number:");again: while(scanf("%f",&second)!=1) //判断输入是否合法&{scanf("%s",ch_arr);printf("%s is not a number.\n""Please input a number,such as 2.5,-1.78E8,or 3:",ch_arr);}switch(ch){case 'a':printf("%f + %f = %f\n",first,second,first+second);case 's':printf("%f - %f = %f\n",first,second,first-second);case 'm':printf("%f * %f = %f\n",first,second,first*second);case 'd':if(second!=0)printf("%f / %f = %f\n",first,second,first/second);else{printf("Enter a number other than 0:");}}if(getchar()!='\n') //消除数字输入后的&\n&&printf("Enter the opration of your choice:\n""a.add \t\t\t s.subtract\n""m.multiply \t\t\t d.divide\nq.quit\n");}printf("Bye.\n");&return 0;}
第九章  编程练习
#include&stdio.h&double min(double,double);int main() //函数调用的联系&{double first,printf("Please input two doubles\n");printf("The first one is :");scanf("%lf",&first);printf("The second one is :");scanf("%lf",&second);printf("The minimum number is:%lf\n",min(first,second));return 0;}double min(double x,double y){return (x&y)?y:x;}
#include&stdio.h&void chline(char,int,int);int main() //指定的列输出指定的字符&{int begin ,printf("Please input the char:\n");ch=getchar();while(getchar()!='\n') //凡是有字符的输入这个看起来就很重要&printf("Please input the line to begin:\n");scanf("%d",&begin);printf("Please input the line to be end:\n");scanf("%d",&end);chline(ch,begin,end);return 0;}void chline(char ch,int i,int j){for(line=0;line&i;line++) //跳过i列&putchar('\n');&for(line=i;line&=j;line++){putchar(ch);putchar('\n');}
#include&stdio.h&void chline(char,int,int);int main() //依然是输出字符&{int num ,printf("Please input the char:\n");ch=getchar();while(getchar()!='\n') //凡是有字符的输入这个看起来就很重要&printf("Please input the char's numbers in each line:\n");scanf("%d",&num);printf("Please input the all lines you want:\n");scanf("%d",&line);chline(ch,num,line);return 0;}void chline(char ch,int i,int j){int num,for(line=0;line&j;line++) //这是和上一道题的唯一区别啦&&&{&for(num=0;num&i;num++)putchar(ch);putchar('\n');}&}
#include&stdio.h&double avg_combine(double,double);int main() //谐均值的计算&{double first ,printf("Please input the first number:\n");scanf("%lf",&first);printf("Please input the second number:\n");scanf("%lf",&second);printf("The avg_combine is %lf\n",avg_combine(first,second));return 0;}double avg_combine(double a,double b){&return 1.0/((1.0/a+1.0/b)/2.0); //这个式子有点长哈&&&}
#include&stdio.h&void large_of(double,double);int main() //?y????滻??С?&{double first ,printf("Please input the first number:\n");scanf("%lf",&first);printf("Please input the second number:\n");scanf("%lf",&second);large_of(first,second);return 0;}void large_of(double x,double y) //?????????????????????????????&{&y=(x&y)?x:y;x=(x&y)?x:y;printf("The first number is %lf now.\n""The second number is %lf now.\n",x,y);}
#include&stdio.h&#include&ctype.h&int letter(char);int main() //判断输入的字母在字母表中的位置&{printf("Please input something:");while((ch=getchar())!=EOF){if(ch=='\n') //这个也是消去&\n&影响的一种方法&if(letter(ch)!=-1)else&printf("%c is not a alphabet\n",ch);}return 0;}int letter (char ch){if(isalpha(ch)){if(isupper(ch)) //ctype.h 用用更健康,哈哈&&&printf("%c is the %3d letter in the alphabet\n",ch,ch-64);elseprintf("%c is the %3d letter in the alphabet\n",ch,ch-96);return 0;&}elsereturn -1;}
#include&stdio.h&double power (double n,int p);int main() //指数函数的实现&{double x,printf("Enter a number and an integer power"" to which the number will be raised.\nEnter q 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;if(p==0||n==0.0) //任何数的0次幂都是1;0的任何次幂也是1;&return 1;else if(p&0) //负指数&{for(i=p;i&0;i++)pow/=n;}else //正指数&{for (i=1;i&=p;i++)pow*=n;}}
#include&stdio.h&double power (double n,int p);int main() //????????????&{double x,printf("Enter a number and an integer power"" to which the number will be raised.\nEnter q 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) //????????????????????????&{if(p&0)pow=n*power(n,p-1);else if(p&0)pow=power(n,p+1)/n;else&pow=1;}
#include&stdio.h&void to_base_n(unsigned long ,int );
int main() //十进制到&任意&进制的转换&{&printf("Enter an integer(q to quit):\n");while(scanf("%ul",&number)==1){printf("Enter the base binary(2-8):\n");scanf("%d",&base);printf("When changed into %d binary, it is: ",base);to_base_n(number,base);putchar('\n');printf("Enter an integer (q to quit):\n");}printf("Done.\n");return 0;}void to_base_n(unsigned long n,int base) //采用递归,超帅&&&{r=n%if(n&=base)to_base_n(n/base,base);putchar('0'+r);}
#include&stdio.h&void Fibonacci(long);
int main() //Fibonacci数列&{printf("How many fibonacci do you want?(2&=n&=46, q to quit)\nn=");while(scanf("%ld",&number)==1){Fibonacci(number);printf("Enter an integer (q to quit):\n");}printf("Done.\n");return 0;}void Fibonacci(long n) //while循环的方式&{long last=1,current=1,temp=1;long i=2;printf("%ld ",last);printf("%ld ",current);&while(i&n){temp=current=last+last=printf("%ld ",current);i++;}printf("\n");}
呵呵,又更新了两章呵,其实我每天工作量还是不小的,就是没自信罢了,时常给自己一点鼓励吧&&
12月3日  记:
今天又要更新一篇啦,哈哈&&只要静下心,其实这些题都是可以写得出来的,关键是静心&&
第十章 编程练习
#include&stdio.h&#define MONTHS 12#define YEARS 5
int main() //用指针实现二位数组的累加&{const float rain[YEARS][MONTHS]={{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}};int year,float subtot,
printf(" YEAR RAINFALL(inches)\n");for(year=0,subtot=0;year&YEARS;year++){for(month=0,subtot=0;month&MONTHS;month++)subtot+=*(*(rain+year)+month); //相当于subtot+=rain[year][month];&printf("%5d %15.1f\n",2000+year,subtot);total+=}printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);printf("MONTHLY AVERAGES:\n\n");printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for(month =0;month&MONTHS;month++){for(year=0,subtot=0;year&YEARS;year++)subtot+=*(*(rain+year)+month); //相当于subtot+=rain[year][month];&printf("%4.1f ",subtot/YEARS);}printf("\n");}
#include&stdio.h&void copy_arr (double[],double[],int); //声明的时候别忘了void&void copy_ptr (double *,double *,int);int main() //数组的copy,数组名和指针两种方式&{double source[5]={1.1,2.2,3.3,4.4,5.5};double target1[5];double target2[5];copy_arr (source,target1,5);copy_ptr (source,target2,5);&return 0;}
void copy_arr(double source[],double target1[],int n){for(i=0;i&n;i++){target1[i]=source[i];printf("%lf ",target1[i]); //把%lf写成了%ld,铸成大错&&晕死&}printf("\n");}void copy_ptr(double * source,double *target2,int n){for(i=0;i&n;i++){*(target2+i)=*(source+i);printf("%lf ",*(target2+i));}printf("\n");}
#include&stdio.h&#define SIZE 8int max(int * ,int ); //数组声明要说明 数据类型&int main() //指针的使用&{int arr[SIZE]={1,22,3,4,55,6,7,15};printf("The max number is:%d\n",max(arr,SIZE));return 0;}int max(int *arr,int n) //等同于int arr[]&{int i,max=*for(i=0;i&n;i++)max=(max&*(arr+i))?max:*(arr+i);}
#include&stdio.h&#define SIZE 8int max(const double * ,int ); //声明时也加const&int main() //依然是指针的使用&{const double arr[SIZE]={1,22,3,4,57,6,7,15};printf("The max number is in the %d position\n",max(arr,SIZE)+1);return 0;}int max(const double *arr,int n) //用一下const,嘻嘻&&&{int index,max_max=*max_index=0;for(index=0;index&n;index++)if(max&*(arr+index)){max=*(arr+index);max_index=}&return max_}
#include&stdio.h&#define SIZE 8int max(int * ,int);&int main() //依然是&&&{int arr[SIZE]={1,22,3,4,55,6,7,15};printf("The max number minus the minimum number is:%d\n",max(arr,SIZE));return 0;}int max(int *arr,int n)&{int i,max,min=max=*for(i=0;i&n;i++){max=(max&*(arr+i))?max:*(arr+i);min=(min&*(arr+i))?min:*(arr+i);}return max-}
#include&stdio.h&#define MONTHS 12#define YEARS 5void copy_ptr (double (*source)[MONTHS],double (*target)[MONTHS],int);
int main() //二维数组的复制&{double copy_rain[YEARS][MONTHS];double rain[YEARS][MONTHS]={{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}};copy_ptr(rain,copy_rain,YEARS*MONTHS);}void copy_ptr(double (*source)[MONTHS],double (*target)[MONTHS],int n){for(i=0;i&n;i++){*(*target+i)=*(*source+i);printf("%.2lf ",*(*target+i));}printf("\n");}
#include&stdio.h&void copy_ptr (double *,double *,int);int main() // 拷贝数组中指定的元素&{double source[7]={1.1,2.2,3.3,4.4,5.5,6.6,7.7};double target[3];copy_ptr (source+2,target,3); //拷贝第三个到第五个元素&return 0;}
void copy_ptr(double * source,double *target,int n){for(i=0;i&n;i++){*(target+i)=*(source+i);printf("%.2lf ",*(target+i));}printf("\n");}
#include&stdio.h& //差点被这个题给吓住,世上无难事,只怕肯登攀&&&#define ROWS 3#define COLS 5void copy_arr(int r,int c,double arr_source[r][c],double arr_target[r][c]);void initialize(int r,int c,double arr[r][c]);void show(int r,int c,double arr[r][c]);int main() //变长数组的使用&{int rows=ROWS;int cols=COLS;double arr_target[rows][cols];double arr_source[rows][cols];initialize(rows,cols,arr_source);copy_arr(rows,cols,arr_source,arr_target);show(rows,cols,arr_source);&show(rows,cols,arr_target);
return 0;}
void initialize(int r,int c,double arr[r][c]){// arr[rows][cols]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};&//变长数组能不能初始化?&int i,j;for(i=0;i&r;i++)for(j=0;j&c;j++)arr[i][j]=i+j+10;
}void copy_arr(int r,int c,double arr_source[r][c],double arr_target[r][c]){int i,j;for(i=0;i&r;i++)for(j=0;j&c;j++)arr_target[i][j]=arr_source[i][j];&}
void show(int r,int c,double arr[r][c]){int i,j;for(i=0;i&r;i++){for(j=0;j&c;j++)printf("%lf ",arr[i][j]); //又是double和long的混淆&printf("\n");&}printf("\n");&}
#include&stdio.h& //这个程序一遍成,有进步,哈哈哈&&&#define INDEX 9void copy_ptr(int *source1,int *source2,int *target,int index);int main() //数组相加&{int source1[INDEX]={1,2,3,4,5,6,7,8,9};int source2[INDEX]={2,3,4,5,6,7,8,9,10};int target[INDEX];copy_ptr(source1,source2,target,INDEX);return 0;}
void copy_ptr(int *source1,int *source2,int *target,int index){for(row=0;row&row++){*(target+row)=*(source1+row)+*(source2+row);printf("%d ",*(target+row));}}
#include&stdio.h&#define COLS 5#define ROWS 3void show(int (*arr) [COLS],int lines); //重点是这个的声明int (*arr) []&void twice(int (*origin) [COLS],int lines); //等价于int arr[][]&int main() //数组值的改变&{int arr[ROWS][COLS]={{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};show(arr,ROWS); //这里面不用写arr[][COLS]&twice(arr,ROWS);show(arr,ROWS);&return 0;}
void show(int arr[][COLS],int lines){int rows,for(rows=0;rows&rows++)for(cols=0;cols&COLS;cols++)printf("%d ",arr[rows][cols]);printf("\n");}
void twice(int (*arr)[COLS],int lines) //数组值的翻倍&{int rows,for(rows=0;rows&rows++)for(cols=0;cols&COLS;cols++)arr[rows][cols]*=2;}
#include&stdio.h& //小打小闹&&&#define MONTHS 12#define YEARS 5void rain(const float (*arr)[MONTHS],int years);
int main() //用指针实现二位数组的累加&{const float arr_rain[YEARS][MONTHS]={{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}};rain(arr_rain,YEARS);}void rain(const float (*arr)[MONTHS],int years){&int year,float subtot,
printf(" YEAR RAINFALL(inches)\n");for(year=0,subtot=0;year&year++){for(month=0,subtot=0;month&MONTHS;month++)subtot+=*(*(arr+year)+month); //相当于subtot+=rain[year][month];&printf("%5d %15.1f\n",2000+year,subtot);total+=}printf("\nThe yearly average is %.1f inches.\n\n",total/years);printf("MONTHLY AVERAGES:\n\n");printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for(month =0;month&MONTHS;month++){for(year=0,subtot=0;year&year++)subtot+=*(*(arr+year)+month); //相当于subtot+=rain[year][month];&printf("%4.1f ",subtot/years);}printf("\n");}
#include&stdio.h& //这是一道综合题 ,充分利用了函数调用&#define ROWS 3#define COLS 5void getnumbers(double arr[][COLS],int rows); //用户输入数组中的值&double show_avg(double arr[][COLS],int rows); //计算每一行的平均值&double show_all_avg(double arr[][COLS],int rows); //计算所有的平均值&double max(double arr[][COLS],int rows); //找最大值
int main() //数组的综合练习&{printf("You need to input 3 groups of numbers,""each group inculdes 5 double numbers!\n");double arr[ROWS][COLS];getnumbers(arr,ROWS);for(r=0;r&ROWS;r++)printf("The %d rows' avg is:%lf\n",r+1,show_avg(arr,r));printf("The array's avg is:%lf\n",show_all_avg(arr,ROWS));printf("The max mumber of the array is:%lf\n",max(arr,ROWS));return 0;}
void getnumbers(double arr[][COLS],int rows){int r,c;for(r=0;r&r++)switch(r){case 0:printf("Please input the first 5 double numbers: ");&for(c=0;c&COLS;c++)scanf("%lf",&arr[r][c]);case 1:printf("Please input the second 5 double numbers:");&for(c=0;c&COLS;c++)scanf("%lf",&arr[r][c]);case 2:printf("Please input the third 5 double numbers: ");&for(c=0;c&COLS;c++)scanf("%lf",&arr[r][c]);default:&}&}
double show_avg(double arr[][COLS],int rows){double avg,sum=0.0;for(c=0;c&COLS;c++)sum+=arr[rows][c];avg=sum/COLS;}
double show_all_avg(double arr[][COLS],int rows){int r,c;double avg,sum=0.0;for(r=0;r&r++){for(c=0;c&COLS;c++)sum+=arr[r][c];}avg=sum/(COLS*rows);}
double max(double arr[][COLS],int rows){int r,c;double max_max_number=arr[0][0];for(r=0;r&r++){for(c=0;c&COLS;c++)max_number=(max_number&arr[r][c])?max_number:arr[r][c];}return max_}
#include&stdio.h& //这是一道综合题 ,充分利用了函数调用
void getnumbers(int rows, int cols,double arr[rows][cols]); //用户输入数组中的值&double show_avg(int rows, int cols,double arr[rows][cols]); //计算每一行的平均值&double show_all_avg(int rows, int cols,double arr[rows][cols]); //计算所有的平均值&double max(int rows, int cols,double arr[rows][cols]); //找最大值
int main() //变长数组的综合练习&{int rows=3,cols=5;printf("You need to input 3 groups of numbers,""each group inculdes 5 double numbers!\n");double arr[rows][cols];getnumbers(rows,cols,arr);for(r=0;r&r++)printf("The %d rows' avg is:%lf\n",r+1,show_avg(r,cols,arr));printf("The array's avg is:%lf\n",show_all_avg(rows,cols,arr));printf("The max mumber of the array is:%lf\n",max(rows,cols,arr));return 0;}
void getnumbers(int rows, int cols,double arr[rows][cols]){int r,c;for(r=0;r&r++)switch(r){case 0:printf("Please input the first 5 double numbers: ");&for(c=0;c&c++)scanf("%lf",&arr[r][c]);case 1:printf("Please input the second 5 double numbers:");&for(c=0;c&c++)scanf("%lf",&arr[r][c]);case 2:printf("Please input the third 5 double numbers: ");&for(c=0;c&c++)scanf("%lf",&arr[r][c]);default:&}&}
double show_avg(int rows, int cols,double arr[rows][cols]){double avg,sum=0.0;for(c=0;c&c++)sum+=arr[rows][c];avg=sum/}
double show_all_avg(int rows, int cols,double arr[rows][cols]){int r,c;double avg,sum=0.0;for(r=0;r&r++){for(c=0;c&c++)sum+=arr[r][c];}avg=sum/(cols*rows);}
double max(int rows, int cols,double arr[rows][cols]){int r,c;double max_max_number=arr[0][0];for(r=0;r&r++){for(c=0;c&c++)max_number=(max_number&arr[r][c])?max_number:arr[r][c];}return max_}
已经写了这么多了,真是不容易啊,呵呵&&快要期末考试了,要忙起来喽&&
阅读(...) 评论()}

我要回帖

更多关于 initialize 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信