- 162.65 KB
- 2022-04-29 14:03:02 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'第1章程序设计和C语言1#include#include1-5#includeintmain()intmain(){floatr5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;{printfp=1000;("**************************nn");r5=0.0585;printf("VeryGood!nn");r3=0.054;printfr2=0.0468;("**************************n");r1=0.0414;return0;r0=0.0072;}p1=p*((1+r5)*5);//一次存5年期p2=p*(1+2*r2)*(1+3*r3);//先存2年1-6#include期,到期后将本息再存3年期intmain()p3=p*(1+3*r3)*(1+2*r2);//先存3年{inta,b,c,max;期,到期后将本息再存2年期printf("pleaseinputa,b,c:n");p4=p*pow(1+r1,5);//存1年scanf("%d,%d,%d",&a,&b,&c);期,到期后将本息存再存1年期,连续存5max=a;次if(maxprintf("p5=%fn",p5);//输出按第5#include方案得到的本息和intmain()return0;{floatp,r,n;}r=0.1;n=10;3-2-2p=pow(1+r,n);#includeprintf("p=%fn",p);#includereturn0;intmain()}{doubler5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;3-2-1r2=0.0468;
r1=0.0414;p4=p*pow(1+r1,5);//存1年r0=0.0072;期,到期后将本息存再存1年期,连续存5次p1=p*((1+r5)*5);//一次存5p5=p*pow(1+r0/4,4*5);//存活期存年期款。活期利息每一季度结算一次p2=p*(1+2*r2)*(1+3*r3);//先存2年printf("p1=%10.2fn",p1);//输出按期,到期后将本息再存3年期第1方案得到的本息和p3=p*(1+3*r3)*(1+2*r2);//先存3年printf("p2=%10.2fn",p2);//输出按期,到期后将本息再存2年期第2方案得到的本息和p4=p*pow(1+r1,5);//存1年printf("p3=%10.2fn",p3);//输出按期,到期后将本息存再存1年期,连续存5第3方案得到的本息和次printf("p4=%10.2fn",p4);//输出按p5=p*pow(1+r0/4,4*5);//存活期存第4方案得到的本息和款。活期利息每一季度结算一次printf("p5=%10.2fn",p5);//输出按printf("p1=%fn",p1);//输出按第1第5方案得到的本息和方案得到的本息和return0;printf("p2=%fn",p2);//输出按第2}方案得到的本息和printf("p3=%fn",p3);//输出按第3方案得到的本息和printf("p4=%fn",p4);//输出按第4方案得到的本息和3-3.printf("p5=%fn",p5);//输出按第5#include方案得到的本息和#includereturn0;intmain()}{floatd=300000,p=6000,r=0.01,m;3-2-3m=log10(p/(p-d*r))/log10(1+r);#includeprintf("m=%6.2fn",m);#includereturn0;intmain()}{floatr5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;3-4r1=0.0414;#includer0=0.0072;intmain(){intc1,c2;p1=p*((1+r5)*5);//一次存5c1=197;年期c2=198;p2=p*(1+2*r2)*(1+3*r3);//先存2年printf("c1=%c,c2=%cn",c1,c2);期,到期后将本息再存3年期printf("c1=%d,c2=%dn",c1,c2);p3=p*(1+3*r3)*(1+2*r2);//先存3年return0;期,到期后将本息再存2年期}
算圆面积ssq=4*pi*r*r;//计算圆球表面积sq3-5vq=3.0/4.0*pi*r*r*r;//计#include算圆球体积vqintmain()vz=pi*r*r*h;//{inta,b;计算圆柱体积vzfloatx,y;printf("圆周长为:l=%6.2fn",l);charc1,c2;printf("圆面积为:s=%6.2fn",s);scanf("a=%db=%d",&a,&b);printf("圆球表面积为:sq=%6.2fn",sq);scanf("%f%e",&x,&y);printf("圆球体积为:v=%6.2fn",vq);scanf("%c%c",&c1,&c2);printf("圆柱体积为:vz=%6.2fn",vz);return0;printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%}cn",a,b,x,y,c1,c2);return0;}3-8-1#includeintmain()3-6{#includeintc1,c2;//整intmain()型定义{charc1="C",c2="h",c3="i",c4="n",c5="a";printf("请输入两个整数c1,c2:");c1=c1+4;scanf("%d,%d",&c1,&c2);c2=c2+4;printf("按字符输出结果:n");c3=c3+4;printf("%c,%cn",c1,c2);c4=c4+4;printf("按ASCII码输出结果为:n");c5=c5+4;printf("%d,%dn",c1,c2);printf("passworreturn0;is%c%c%c%c%cn",c1,c2,c3,c4,c5);}return0;}3-8-2#include3-7intmain()#include{intmain()charc1,c2;{floath,r,l,s,sq,vq,vz;//定义字符型变量floatpi=3.141526;inti1,i2;printf("请输入圆半径r,圆柱高h∶");//定义整型变量scanf("%f,%f",&r,&h);//printf("请输入两个字符c1,c2:");要求输入圆半径r和圆柱高hscanf("%c,%c",&c1,&c2);l=2*pi*r;//i1=c1;计算圆周长l//赋值给整型变量s=r*r*pi;//计i2=c2;
printf("按字符输出结果:n");第4章选择结构程序设计85printf("%c,%cn",i1,i2);习题111printf("按整数输出结果:n");4-4-1printf("%d,%dn",c1,c2);#includereturn0;intmain()}{3-8-3inta,b,c;#includeprintf("请输入三个整数:");intmain()scanf("%d,%d,%d",&a,&b,&c);{if(areturn0;intmain()}{inta,b,c,temp,max;printf("请输入三个整数:");scanf("%d,%d,%d",&a,&b,&c);3-8temp=(a>b)?a:b;#include/*将a和b中的大者存入temp中*/intmain()max=(temp>c)?temp:c;/*{将a和b中的大者与c比较,取最大者*/charc1,c2;printf("三个整数的最大数是%dn",max);printf("请输入两个字符c1,c2:");return0;c1=getchar();}c2=getchar();printf("用putchar语句输出结果为:");putchar(c1);4-5-2putchar(c2);#includeprintf("n");#includeprintf("用printf语句输出结果为:");#defineM1000printf("%c%cn",c1,c2);intmain()return0;{}inti,k;printf("请输入一个小于%d的整数i:",M);
scanf("%d",&i);elseif(x<10)/*1=M){y=2*x-1;{printf("输入的数不符合要求,请重新输printf("x=%d,y=2*x-1=%dn",x,y);入一个小于%d的整数i:",M);}scanf("%d",&i);else/*x>=10*/}{y=3*x-11;k=sqrt(i);printf("x=%d,y=3*x-11=%dn",x,y);printf("%d的平方根的整数部分}是:%dn",i,k);return0;return0;}}4-7-14-5#include#includeintmain()#include{#defineM1000intx,y;intmain()printf("enterx:");{scanf("%d",&x);inti,k;y=-1;printf("请输入一个小于%d的整数i:",M);if(x!=0)scanf("%d",&i);if(x>0)if(i>M)y=1;{printf("输入的数不符合要求,请重新输else入一个小于%d的整数i:",M);y=0;scanf("%d",&i);printf("x=%d,y=%dn",x,y);}return0;k=sqrt(i);}printf("%d的平方根的整数部分是:%dn",i,k);return0;4-7-2}#includeintmain(){4-6.intx,y;#includeprintf("pleaseenterx:");intmain()scanf("%d",&x);{intx,y;y=0;printf("输入x:");if(x>=0)scanf("%d",&x);if(x>0)y=1;if(x<1)/*x<1*/elsey=-1;{y=x;printf("x=%d,y=%dn",x,y);printf("x=%3d,y=x=%dn",x,y);return0;}}
place=5;elseif(num>999)4-8place=4;#includeelseif(num>99)intmain()place=3;{floatscore;elseif(num>9)chargrade;place=2;printf("请输入学生成绩:");elseplace=1;scanf("%f",&score);printf("位数:%dn",place);while(score>100||score<0)printf("每位数字为:");{printf("n输入有误,请重输");ten_thousand=num/10000;scanf("%f",&score);}thousand=(int)(num-ten_thousand*10000)/10switch((int)(score/10))00;{case10:case9:grade="A";break;hundred=(int)(num-ten_thousand*10000-thoucase8:grade="B";break;sand*1000)/100;case7:grade="C";break;case6:grade="D";break;ten=(int)(num-ten_thousand*10000-thousand*case5:1000-hundred*100)/10;case4:case3:indiv=(int)(num-ten_thousand*10000-thousancase2:d*1000-hundred*100-ten*10);case1:switch(place)case0:grade="E";{case}5:printf("%d,%d,%d,%d,%d",ten_thousand,thprintf("成绩是%5.1f,相应的等级ousand,hundred,ten,indiv);是%cn",score,grade);printf("n反序数字为:");return0;}printf("%d%d%d%d%dn",indiv,ten,hundred,thousand,ten_thousand);break;4-9case#include4:printf("%d,%d,%d,%d",thousand,hundred,te#includen,indiv);intmain()printf("n反序数字为:");{intprintf("%d%d%d%dn",indiv,ten,hundred,thounum,indiv,ten,hundred,thousand,ten_thousand,sand);place;//分别代表个位,十位,百位,千位,break;万位和位数caseprintf("请输入一个整数(0-99999):");3:printf("%d,%d,%d",hundred,ten,indiv);scanf("%d",&num);printf("n反序数字为:");if(num>9999)
printf("%d%d%dn",indiv,ten,hundred);break;4-10-2case2:printf("%d,%d",ten,indiv);#includeprintf("n反序数字为:");intmain()printf("%d%dn",indiv,ten);{break;inti;case1:printf("%d",indiv);doubleprintf("n反序数字为:");bonus,bon1,bon2,bon4,bon6,bon10;printf("%dn",indiv);intbranch;break;bon1=100000*0.1;}bon2=bon1+100000*0.075;return0;bon4=bon2+200000*0.05;}bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf("请输入利润i:");4-10-1scanf("%d",&i);#includebranch=i/100000;intmain()if(branch>10)branch=10;{switch(branch)inti;{case0:bonus=i*0.1;break;doublebonus,bon1,bon2,bon4,bon6,bon10;casebon1=100000*0.1;1:bonus=bon1+(i-100000)*0.075;break;bon2=bon1+100000*0.075;case2:bon4=bon2+100000*0.05;case3:bon6=bon4+100000*0.03;bonus=bon2+(i-200000)*0.05;break;bon10=bon6+400000*0.015;case4:printf("请输入利润i:");case5:scanf("%d",&i);bonus=bon4+(i-400000)*0.03;break;if(i<=100000)case6:bonus=i*0.1;case7:elseif(i<=200000)case8:bonus=bon1+(i-100000)*0.075;case9:elseif(i<=400000)bonus=bon6+(i-600000)*0.015;break;bonus=bon2+(i-200000)*0.05;case10:elseif(i<=600000)bonus=bon10+(i-1000000)*0.01;bonus=bon4+(i-400000)*0.03;}elseif(i<=1000000)printf("奖金是%10.2fn",bonus);bonus=bon6+(i-600000)*0.015;return0;else}bonus=bon10+(i-1000000)*0.01;printf("奖金是:%10.2fn",bonus);return0;4-11}#includeintmain()
{intt,a,b,c,d;printf("请输入四个数:");第5章循环结构程序设计114scanf("%d,%d,%d,%d",&a,&b,&c,&d);5-2#includeprintf("a=%d,b=%d,c=%d,d=%dn",a,b,c,d);#include//程if(a>b)序中用到数学函数fabs,应包含头文件{t=a;a=b;b=t;}math.nif(a>c)intmain(){t=a;a=c;c=t;}{if(a>d)intsign=1,count=0;//sign{t=a;a=d;d=t;}用来表示数值的符号,count用来统计循环次if(b>c)数{t=b;b=c;c=t;}doublepi=0.0,n=1.0,term=1.0;//pi开if(b>d)始代表多项式的值,最后代表π的值,n代表{t=b;b=d;d=t;}分母,term代表当前项的值if(c>d)while(fabs(term)>=1e-8)//检{t=c;c=d;d=t;}查当前项term的绝对值是否大于或等于10printf("排序结果如下:n");的(-6)次方printf("%d%d%d%d{n",a,b,c,d);pi=pi+term;//return0;把当前项term累加到pi中}n=n+2;//n+2是下一项的分母sign=-sign;//4-12sign代表符号,下一项的符号与上一项符号#include相反intmain()term=sign/n;//求{出下一项的值terminth=10;count++;//floatcount累加1x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=}-2,x,y,d1,d2,d3,d4;pi=pi*4;//printf("请输入一个点(x,y):");多项式的和pi乘以4,才是π的近似值scanf("%f,%f",&x,&y);printf("pi=%10.8fn",pi);//输出πd1=(x-x4)*(x-x4)+(y-y4)*(y-y4);的近似值/*求该点到各中心点距离*/printf("count=%dn",count);//输出d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);循环次数d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);return0;d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);}if(d1>1&&d2>1&&d3>1&&d4>1)h=0;/*判断该点是否在塔外*/5-3printf("该点高度为%dn",h);#includereturn0;intmain()}{
intp,r,n,m,temp;printf("请输入两个正整数n,m:");scanf("%d,%d,",&n,&m);5-5if(n{intmain()temp=n;{n=m;inta,n,i=1,sn=0,tn=0;m=temp;printf("a,n=:");}scanf("%d,%d",&a,&n);p=n*m;while(i<=n)while(m!=0){{tn=tn+a;/*赋值后的tn为i个a组成数r=n%m;的值*/n=m;sn=sn+tn;/*赋值后的sn为多项式前i项之m=r;和*/}a=a*10;printf("它们的最大公约数为:%dn",n);++i;printf("它们的最小公约数为:%dn",p/n);}return0;printf("a+aa+aaa+...=%dn",sn);}return0;5-4}#includeintmain(){5-6charc;#includeintletters=0,space=0,digit=0,other=0;intmain()printf("请输入一行字符:n");while((c=getchar())!="n"){doubles=0,t=1;{intn;if(c>="a"&&c<="z"||c>="A"&&for(n=1;n<=20;n++)c<="Z"){letters++;t=t*n;elseif(c=="")s=s+t;space++;}elseif(c>="0"&&c<="9")printf("1!+2!+...+20!=%22.15en",s);digit++;return0;else}other++;}5-7printf("字母数:%dn空格数:%dn数字#include数:%dn其它字符intmain()数:%dn",letters,space,digit,other);{return0;intn1=100,n2=50,n3=10;}doublek,s1=0,s2=0,s3=0;
for(k=1;k<=n1;k++)/*计算1到100的和a的因子的个数*/*/s=a;/*s用来存放{s1=s1+k;}尚未求出的因子之和,开始时等于a*/for(k=1;k<=n2;k++)/*计算1到50各数for(i=1;ik1=i;break;/*找出的笫1intmain()个因子赋给k1*/{case2:inti,j,k,n;k2=i;break;/*找出的笫2printf("parcissusnumbersare");个因子赋给k2*/for(n=100;n<1000;n++)case3:{k3=i;break;/*找出的笫3i=n/100;个因子赋给k3*/j=n/10-i*10;case4:k=n%10;k4=i;break;/*找出的笫4if(n==i*i*i+j*j*j+k*k*k)个因子赋给k4*/printf("%d",n);case5:}k5=i;break;/*找出的笫5printf("n");个因子赋给k5*/return0;case6:}k6=i;break;/*找出的笫6个因子赋给k6*/case7:5-9-1k7=i;break;/*找出的笫7#defineM1000/*定义寻找范个因子赋给k7*/围*/case8:#includek8=i;break;/*找出的笫8intmain()个因子赋给k8*/{case9:intk1,k2,k3,k4,k5,k6,k7,k8,k9,k10;k9=i;break;/*找出的笫9个inti,a,n,s;因子赋给k9*/for(a=2;a<=M;a++)/*a是2-1000case10:之间的整数,检查它是否完数*/k10=i;break;/*找出的笫{n=0;/*n用来累计10个因子赋给k10*/
}}5-10if(s==0)#include{intmain()printf("%d,Itsfactorsare",a);{if(n>1)printf("%d,%d",k1,k2);inti,n=20;/*n>1表示a至少有2个因子*/doublea=2,b=1,s=0,t;if(n>2)printf(",%d",k3);for(i=1;i<=n;i++)/*n>2表示至少有3个因子,故应再输出一{个因子*/s=s+a/b;if(n>3)printf(",%d",k4);t=a,/*n>3表示至少有4个因子,故应再输出一a=a+b,个因子*/b=t;if(n>4)printf(",%d",k5);}/*以下类似*/printf("sum=%16.10fn",s);if(n>5)printf(",%d",k6);return0;if(n>6)printf(",%d",k7);}if(n>7)printf(",%d",k8);if(n>8)printf(",%d",k9);if(n>9)printf(",%d",k10);5-11printf("n");#include}intmain()}{return0;doublesn=100,hn=sn/2;}intn;for(n=2;n<=10;n++)5-9-2{#includesn=sn+2*hn;/*第n次落地时共经过的intmain()米数*/{intm,s,i;hn=hn/2;/*第n次反跳高度*/for(m=2;m<1000;m++)}{s=0;printf("第10次落地时共经过%f米for(i=1;i}intmain()}{return0;intday,x1,x2;}day=9;x2=1;
while(day>0)is%5.2fn",x1);{x1=(x2+1)*2;/*第1天的桃子数是return0;第2天桃子数加1后的2倍.*/}x2=x1;day--;5-15}#includeprintf("total=%dn",x1);#includereturn0;intmain()}{floatx0,x1,x2,fx0,fx1,fx2;do{printf("enterx1&x2:");5-13scanf("%f,%f",&x1,&x2);#includefx1=x1*((2*x1-4)*x1+3)-6;#includefx2=x2*((2*x2-4)*x2+3)-6;intmain()}while(fx1*fx2>0);{dofloata,x0,x1;{x0=(x1+x2)/2;printf("enterapositivenumber:");fx0=x0*((2*x0-4)*x0+3)-6;scanf("%f",&a);if((fx0*fx1)<0)x0=a/2;{x2=x0;x1=(x0+a/x0)/2;fx2=fx0;do}{x0=x1;elsex1=(x0+a/x0)/2;{x1=x0;}while(fabs(x0-x1)>=1e-5);fx1=fx0;printf("Thesquarerootof%5.2f}is%8.5fn",a,x1);}while(fabs(fx0)>=1e-5);return0;printf("x=%6.2fn",x0);}return0;}5-14#include5-16#include#includeintmain()intmain(){doublex1,x0,f,f1;{inti,j,k;x1=1.5;for(i=0;i<=3;i++)do{for(j=0;j<=2-i;j++){x0=x1;printf("");f=((2*x0-4)*x0+3)*x0-6;for(k=0;k<=2*i;k++)f1=(6*x0-8)*x0+3;printf("*");x1=x0-f/f1;printf("n");}while(fabs(x1-x0)>=1e-5);}printf("Therootofequationfor(i=0;i<=2;i++)
{for(j=0;j<=i;j++)printf("n");printf("");for(i=2,n=0;i<=100;i++)for(k=0;k<=4-2*i;k++){if(a[i]!=0)printf("*");{printf("%5d",a[i]);printf("n");n++;}}return0;if(n==10)}{printf("n");n=0;5-17}#include}intmain()printf("n");{return0;chari,j,k;/*是a的对手;j是b}的对手;k是c的对手*/for(i="x";i<="z";i++)for(j="x";j<="z";j++)6-2if(i!=j)#includefor(k="x";k<="z";k++)intmain()if(i!=k&&j!=k){inti,j,min,temp,a[11];if(i!="x"&&k!="x"&&k!="z")printf("enterdata:n");for(i=1;i<=10;i++)printf("A--%cnB--%cnC--%cn",i,j,k);{printf("a[%d]=",i);return0;scanf("%d",&a[i]);}}printf("n");printf("Theorginalnumbers:n");for(i=1;i<=10;i++)第6章利用数组处理批量数据142printf("%5d",a[i]);习题168printf("n");6-1for(i=1;i<=9;i++)#include{min=i;#includefor(j=i+1;j<=10;j++)intmain()if(a[min]>a[j])min=j;{inti,j,n,a[101];temp=a[i];for(i=1;i<=100;i++)a[i]=a[min];a[i]=i;a[min]=temp;a[1]=0;}for(i=2;iprintf("%5d",a[i]);intmain()printf("n");{return0;inta[3][3],sum=0;}inti,j;printf("enterdata:n");for(i=0;i<3;i++)6-5for(j=0;j<3;j++)#includescanf("%3d",&a[i][j]);#defineN5for(i=0;i<3;i++)intmain()sum=sum+a[i][i];{inta[N],i,temp;printf("sum=%6dn",sum);printf("enterarraya:n");return0;for(i=0;ifor(i=0;iend)printf("n");a[10]=number;return0;else}{for(i=0;i<10;i++){if(a[i]>number){temp1=a[i];6-6a[i]=number;#includefor(j=i+1;j<11;j++)#defineN10{temp2=a[j];intmain()a[j]=temp1;{inti,j,a[N][N];temp1=temp2;for(i=0;i6-8intmain()#include{inta[15][15],i,j,k,p,n;#defineN4p=1;#defineM5/*数组为while(p==1)4行5列*/{printf("entern(n=1--15):");intmain()scanf("%d",&n);{if((n!=0)&&(n<=15)&&(n%2!=0))inti,j,k,a[N][M],max,maxj,flag;p=0;printf("pleaseinputmatrix:n");}for(i=0;in))for(j=0;jmax)}{max=a[i][j];/*将本行else的最大数存放在max中*/{if(i<1)i=n;maxj=j;/*将最if(j>n)j=1;大数所在的列号存放在maxj中*/}}if(a[i][j]==0)flag=1;/*先假设a[i][j]=k;是鞍点,以flag为1代表*/elsefor(k=0;ka[k][maxj])/*将最大j=j-1;数和其同列元素相比*/a[i][j]=k;{flag=0;/*如果
max不是同列最小,表示不是鞍点令flag1的起始位置为0*/bott=N-1;//bott是查找区间continue;}的最末位置if(flag)/*如果if((numbera[N-1]))flag1为1表示是鞍点*///要查的数不在查找区间内{printf("a[%d][%d]=%dn",i,maxj,max);loca=-1;//表示找不到/*输出鞍点的值和所在行列号*/while((!sign)&&(top<=bott))break;{mid=(bott+top)/2;}if(number==a[mid])}{loca=mid;if(!flag)/*如printf("Hasfound%d,itsposition果flag为0表示鞍点不存在*/is%dn",number,loca+1);printf("Itisnotexist!n");sign=1;return0;}}elseif(numberif(!sign||loca==-1)#defineN15printf("cannotfind%d.n",number);;intmain()printf("continuornot(Y/N)?");{intscanf("%c",&c);i,number,top,bott,mid,loca,a[N],flag=1,sign;if(c=="N"||c=="n")charc;flag=0;printf("enterdata:n");}scanf("%d",&a[0]);return0;i=1;}while(i=a[i-1])6-10i++;#includeelseintmain()printf("enterthisdataagain:n");{inti,j,upp,low,dig,spa,oth;}chartext[3][80];printf("n");upp=low=dig=spa=oth=0;for(i=0;i