上机测试1、有一分段函数如下,请编写程序,输入x值,输出y值。#includeiostream.h#includemath.hvoidmain(){floatx,y;cinx;if(x0)y=3*x*x-5/x;elseif(x5)y=1/(2+cos(x));elsey=sqrt(sin(x)+1);couty;}测试数据:注意:1.x和y的类型,一般定义为float或double2.算术表达式要写正确3.数学函数:头文件,常用数学函数的名称及用法sin、cos、exp、sqrt、powloglog10fabs4.程序结构以及关系表达式5.可参见“顺序结构程序设计参考答案”5x1xsin5x0xcos210xx5x3y2输入x输出y-214.510.39365460.848872上机测试2、编写程序实现以下功能:计算下列表达式的前n项之和。voidmain(){floatx,sum;floatjs(float,int);intn,i;cinxn;sum=0;for(i=1;i=n;i++)sum+=js(x,i);coutsum;}!i2x1!6x!4x!2xsumi2i642要求使用自定义函数计算,x和n由主函数输入,结果由主函数输出。!i2x1i2i不允许使用cos、sin、pow等数学函数。测试数据输入输出xn31-4.532-1.12533-2.137534-1.9747835-1.9910536-1.9899437-1.98999floatjs(floatx,inti){floatjc,fz,m;intj;m=1;jc=1;fz=1;for(j=1;j=i;j++)m*=-1;for(j=1;j=2*i;j++){jc*=j;fz*=x;}returnm*fz/jc;}注意:按要求的功能编写子函数上机测试3、编写程序实现以下功能:输入两个字符串,在第一个字符串ASCII码值最大的字符后,连接第二个字符串并输出。例如,输入“china”和“ncepu”,输出“chinncepu”。不允许使用strcat、strlen、strcpy、strcmp等字符串处理函数。#includestdio.hvoidmain(){chara[40],b[20];inti,max,j,l;gets(a);gets(b);max=0;for(i=1;a[i]!='\0';i++)if(a[max]a[i])max=i;max++;for(j=0;b[j]!=0;j++){a[max]=b[j];max++;}a[max]=0;puts(a);}注意:算法之间的衔接习题1(读程序写结果)1、#includeiostream.hvoidmain(){inta=1,x,y;x=(a–=2,a*5);y=a&&-1;coutx,y;}ax1-1y-51-5,1习题1(读程序写结果)2、#includeiostream.hvoidmain(){inta=2,b=-1,c=2;if(ab)c=1;if(b0)c=0;elsec+=1;coutc;}0ab2-1c20习题1(读程序写结果)3、#includeiostream.hvoidmain(){intc1=0,c2=0,c3=0,n=212403;while(n0){switch(n%10){case1:c1++;case2:c2++;break;default:c3++;}n=n/10;}coutc1c2c3endl;}133c1c200c301n212403212402212432121211223习题1(读程序写结果)4、#includeiostream.hvoidmain(){inta=1,b=3,i=1;do{a=(b–a)*2+b;b=(a–b)*2+a;i++;if(i%2)break;}while(b100);couta,b;}31,63ab13i1715312633习题1(读程序写结果)5、#includeiostream.hvoidmain(){chars[50]=Iamastudent,c;inti,num=0,w=0;for(i=0;c=s[i];i++)if(c=='')w=0;elseif(w==0){w=1;num++;}coutnum;}4numwic000I111空格02a123mnumwic4空格05a136空格07s148t……14\0习题1(读程序写结果)6、#includeiostream.h#defineS(a,b)a*b–a/bvoidmain(){floata=2;intb=3,c;c=S((a+b),a*b);coutc;}22宏展开(a+b)aa*bb(a+b)*a*b-(a+b)/a*b=(2.0+3)*2.0*3-(2.0+3)/2.0*3=5.0*2.0*3-5.0/2.0*3=30.0-7.5=22.5c=22.5=224AfTeR2#includestdio.hvoidmain(){chara=0,ch;while((ch=getchar())!='\n'){if(a%2!=0&&(ch='a'&&ch='z'))ch=ch-'a'+'A';a++;putchar(ch);}}7、字符'a'的ASCII码值为97#includestdio.hvoidmain(){chara=0,ch;while((ch=getchar())!='\n'){if(a%2!=0&&(ch='a'&&ch='z'))ch=ch-'a'+'A';a++;putchar(ch);}}//运行时输入4after2回车ach0'4'1'a''A'2'f'3't''T'4'e'5'r''R'6'2'7'\n'习题1(读程序写结果)8、#includeiostream.hinta=5;voidfun(intb){inta=10;a+=b;couta;}voidmain(){intc=20;fun(c);a+=c;couta;}3025ac520ab20103025习题1(读程序写结果)9、#includeiostream.hvoidmain(){intaray[3][4]={{3,6},{2,1,0,7},{3,4,2}};inti,j,d=aray[0][0],m=0,n=0;for(i=0;i3;i++)for(j=0;j=i;j++)if(daray[i][j]){d=aray[i][j];m=i;n=j;}coutmn;}21360021073420dm3nij00001012014212习题1(读程序写结果)10、#includeiostream.hvoidmain(){inta[]={2,4,6,8,10},y=1,x,*p=&a[1];for(x=0;x3;x++)y+=*(p+x);couty;}19a[0]a[1]a[2]a[3]a[4]246810pyx1051p+1112p+2193习题1(改错)程序功能:输出1000以内的完数#includeiostream.hvoidmain(){intm,s=0,j;for(m=1;m=1000,m++){for(j=1;j=m;j++)if(m%j)s+=j;if(s=m)coutm\n;}};jmm%j==0==s=0;习题1(填空)1、下面程序的功能是:求数组a中前4个元素之和及最后6个元素之和。#includeiostream.hintfsum(【1】,intn){inti,s;s=0;for(i=0;in;i++)s+=array[i];returns;}voidmain(){inta[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},sumh,sumt;sumh=fsum(a,4);sumt=fsum(【2】,6);coutsumhsumt;}intarray[]a+9或&a[9]习题1(填空)2、下面程序的功能是:求m到n之间素数的个数及其平均值。#includeiostream.hvoidmain(){inti,k,sum=0,m=1,n=20,gs=0;for(k=m;k=n;k++){for(i=2;ik;i++)if(【3】)break;if(【4】){sum+=k;gs++;}}coutgs=gs;coutpj=【5】;}k%i==0i==k(float)sum/gs习题2(读程序写结果)1、#includeiostream.hvoidmain(){inta[3][3]={{1,2,3},{4,5,6},{7,8,9}},b[3]={0},i;for(i=0;i3;i++)b[i]=a[i][2]+a[2][i];for(i=0;i3;i++)coutb[i];}101418123456789b[0]b[1]b[2]000i010114218习题2(读程序写结果)2、#includeiostream.hinta=1,b=2;intg(intx,inty){returnx+y;}voidmain(){inta=3;coutg(a,b)ab;}532ab32xy23习题2(读程序写结果)3、#includeiostream.hvoidmain(){inta[]={5,3,7,2,1,5,3,10},s=0,k;for(k=0;k8;k+=2)s+=*(a+k);couts;}16a[0]a[1]a[2]a[3]a[4]53721aa+2a+4a[5]a[6]a[7]5310sk0052124136a+6168习题2(读程序写结果)4、#includeiostream.h#includestring.hvoidfun(char*str){chartemp;intn,i;n=strlen(str);temp=str[n-1];for(i=n-1;i0;i--)str[i]=str[i-1];str[0]=temp;}voidmain(){chars[]={abcdef};fun(s);couts;}fabcdes[0]s[1]s[2]s[3]s[4]abcdestrs[5]a[6]f\0n6tempfedcbaf5、#includeiostream.hvoidfun(charch){switch(ch){case'A':case'a':coutwell!;case'B':case'b':coutgood!;case'C':case'c':coutpass!;break;default:coutbad!;break;}}voidmain(){chara1='b',a2='C';fun(a1);fun(a2+1);}good!pass!bad!习题2(读程序写结果)6、#includeiostream.hvoidmain(){inta=0,b=1,c=2;if(a0&&++b0)c++;elsec--;coutabc;}011习题2(读程序写结果)7、#includeiostream.hdoublefun(doublex){returnx*x+1;}voidmain(){doublea=0;inti;for(i=0;i30;i+=10)a+=fun((double)i);couta;}503ai0011010220503习题2(读程序写结果)8、#includeiostream.hvoidmain(){charc[20]=12b;intk=0,i;for(i=0;c[i]!='\0';i++)if(c[i]='0'&&c[