新版电大《C++语言程序设计》第1234次作业及答案参考资料.docx
- 文档编号:27635634
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:21
- 大小:20.53KB
新版电大《C++语言程序设计》第1234次作业及答案参考资料.docx
《新版电大《C++语言程序设计》第1234次作业及答案参考资料.docx》由会员分享,可在线阅读,更多相关《新版电大《C++语言程序设计》第1234次作业及答案参考资料.docx(21页珍藏版)》请在冰豆网上搜索。
新版电大《C++语言程序设计》第1234次作业及答案参考资料
2019年新版】电大《C++语言程序设计》第1、2、3、4次作业及
答案参考资料
一、写出下列每个程序运行后的输出结果
1.#include
voidmain()
{
intx=5;
switch(2*x-3)
{
case4:
printf("%d",x);
case7:
printf("%d",2*x+1);
case10:
printf("%d",3*x-1);break;
default:
printf("%s","default\n");
}
printf("%s\n","switchend.");
}
2.#include
voidmain()
{
inti,s=0;
for(i=1;i<=6;i++)
s+=i*i;
printf("s=%d\n",s);
}
3.#include
{
inti,s1=0,s2=0;
for(i=0;i<10;i++)
if(i%2)s1+=i;
elses2+=i;
printf("%d%d\n",s1,s2);
4.#include
voidmain()
{
intn=10,y=1;
while(n--){y++;y++;}printf("y=%d\n",y);
}
5.#include
voidmain()
{
intf,f1,f2,i;
f1=f2=1;
printf("%d%d",f1,f2);
for(i=3;i<=10;i++){f=f1+f2;printf("%d",f);if(i%5==0)printf("\n");f1=f2;
f2=f;
}
printf("\n");
}
6.#include
#include
voidmain()
{
inti,n;
for(n=2;n<=20;n++){
inttemp=(int)sqrt(n);//sqrt(n)求出n的平方根并取整for(i=2;i<=temp;i++)
if(n%i==0)break;
if(i>temp)printf("%d",n);
}
printf("\n");
7.#include
inti,c2,c3,c5;c2=c3=c5=0;
for(i=1;i<=M;i++){if(i%2==0)c2++;if(i%3==0)c3++;if(i%5==0)c5++;
}
printf("%d%d%d\n",c2,c3,c5);}
8.#include
{
inti,s;for(i=1,s=0;i<15;i++){
if(i%2==0||i%3==0)continue;printf("%d",i);
s+=i;
}printf("%d\n",s);
参考答案:
1、答案:
1114switchend.
2、答案:
s=91.
3、答案:
2520.
4、答案:
y=21.
5、答案:
11235
813213455
6、答案:
235711131719
7、答案:
1064
8、答案:
157111337
第二次作业
一、根据下列每个题目要求编写程序
1.编写一个函数,函数头格式为“voidfun4(char*a,intb[])”,分别求出由字符指针a所指向的字符串中包
含的每种十进制数字出现的次数,把统计结果保存在数组b的相应元素。
2.编写一个函数,函数头格式为“doubleMean(doublea[M][N],intm,intn)”,要求返回二维数组a[m][n]
3.编写一个递归函数“intFF(inta[],intn)”,求出数组a中所有元素n个元素之积并返回。
4.编写一个主函数,利用while循环,求出并显示满足不等式1+1/2+1/3+⋯⋯+1/n>5的最小n值。
5.编写一个主函数,求满足不等式22+42+⋯⋯+n2<1000的最大n值,假定分别用i和s为取偶数值和累加值
的变量,并限定使用do循环编程。
6.编写一个主函数,计算并输出n!
的值,其中n值由键盘输入。
参考答案:
1、答案:
#include
voidfun4(char*a,intb[]){
do{
if(*a>='0'&&*a<='9')b[*a-48]++;
}while(*a++);
}
/*voidmain()
{
char*a="122333444499888";
intb[10]={0};
fun4(a,b);
for(inti=0;i<10;i++)
printf("%d",b[i]);
}*/
2、答案:
#include
constintM=2,N=3;
doubleMean(doublea[M][N],intm,intn){doublev=0;
for(inti=0;i for(intj=0;j v+=a[i][j]; returnv/(m*n); } /*voidmain() { doublea[2][3]={1,2,3,4,5,6};printf("%lf\n",Mean(a,2,3)); }*/ 3、答案: #include intFF(inta[],intn){ intmul=1;if(n==1)mul*=a[0]; elsemul=a[n-1]*FF(a,n-1);returnmul; } /*voidmain() { inta[6]={1,2,3,4,5,6};printf("%d\n",FF(a,6)); }*/ 4、答案: #include voidmain() { doublesum=0; intn=1; while(true) { if(sum+1/(double)n>5)break; else sum+=1/(double)n; n++; } printf("%d,%lf\n",n,sum); } 5、答案: #include voidmain() { ints=0,i=2; do { s+=i*i; if(s+(i+2)*(i+2)>=1000)break; elsei+=2; }while(true); printf("i=%d,s=%d",i,s); } 6、答案: #include voidmain() ints=0,n;printf("请输入n的值: ");scanf("%d",&n); for(inti=1;i<=n;i++) s=s*i;printf("n=%d,s=%d",n,s); } 第三次作业 一、写出下列每个程序运行后的输出结果 1.程序代码: #include voidSB(charch){ switch(ch){ case'A': case'a': printf("WW");break; case'B': case'b': printf("GG");break; case'C': case'c': printf("PP");break; default: printf("BB");break; } } voidmain(){ chara1='b',a2='C',a3='f'; SB(a1);SB(a2);SB(a3);SB('A'); printf("\n"); 2.程序代码: #include #include doubleSD(inta,intb,charop){ doublex; switch(op){ case'+': x=a+b;break; case'-': x=a-b;break; case'*': x=a*b;break; case'/': if(b)x=(double)a/b;elseexit (1);break;default: {printf("运算符错! \n");exit (1);}} returnx; } voidmain(){ intx=20,y=8; printf("%3.2lf",SD(x,y,'-')); printf("%3.2lf",SD(x,y,'*'));printf("%3.2lf\n",SD(x+y,y,'/')); } 3.程序代码: #include voidWF(intx,inty){ x=x+y; y=x+y; printf("subs: x,y=%d,%d\n",x,y); } voidmain(){ intx=18,y=23; printf("main: x,y=%d,%d\n",x,y); WF(x,y); x=2*x; printf("main: x,y=%d,%d\n",x,y); } 4.程序代码: #include #include voidfun(charss[]); voidmain(){ chars[15]="567891234"; fun(s); printf("%s\n",s); } voidfun(charss[]){ inti,n=strlen(ss); for(i=0;i charc=ss[i]; ss[i]=ss[n-1-i]; ss[n-1-i]=c; } } 5.程序代码: #include voidInsertSort(inta[],intn) { inti,j,x; for(i=1;i x=a[i]; for(j=i-1;j>=0;j--)//为x顺序向前寻找合适的插入位置 ≡xVa曰)ati-Mτa≡ e_sebreak」 aU+1τ>< ) VOidmain()宀 inf「 infa≡H宀2®IP3N4,3928注 一nsertsortω6)S for(iHo二八6二++)pl∙in⅛≡%d≡a曰)」 Prin⅛□n=x 6∙ #inc一UdeCSfdiO∙hv VOidmain()宀 infasλ35∙7・9・H亠3亠5・17N infi∖pHB for(一Ho二八8二++)宀 Prin⅛≡%5d≡*P++X if((i+•! )%4UH0)Prin⅛□n=)s 7∙"#inc一UdeCsfdiphvinfLA(inf*Binfn)宀infLsH®for(iHO二Cn二++)S+HωΞ-refurns; voidmain(){ inta[]={5,10,15,20,25,30}; intb=LA(a,4); intc=LA(a+2,3); printf("%d%d\n",b,c); } 8.程序代码: #include intLB(int*a,intn){ inti,s=1; for(i=0;i } voidmain(){ inta[]={1,2,3,4,2,4,5,2}; intb=LB(a,4)+LB(&a[3],4);printf("b=%d\n",b); } 二、写出下列每个函数的功能 1.程序代码: intWB(inta[],intn,intx){ inti; for(i=0;i return0; } 2.程序代码: intWC(inta[],intn,intk){intc=0,i; for(i=0;i returnc; } 3.程序代码: #include #include #include constintN=10; intff(intx,inty){ intz; printf("%d+%d=",x,y); scanf("%d",&z); if(x+y==z)return1;elsereturn0; } voidmain(){ inti,a,b,c=0; srand(time(0));//初始化随机数序列 for(i=0;i a=rand()%20+1;//rand()函数产生0~32767之间的一个随机数b=rand()%20+1; c+=ff(a,b); } printf("得分: %d\n",c*10); } 4.*程序代码: intfun6(intm,intn,intb) if(m elseif(m%b==0&&n%b==0)returnb*fun6(m/b,n/b,b);elsereturnfun6(m,n,++b); 5.程序代码: #include #include int*a=malloc(n*sizeof(int)); inti; for(i=0;i free(a); } 6.程序代码: intLK(doublea[],intn){ doubles=0; inti,m=0; for(i=0;i for(i=0;i if(a[i]>=s)m++; returnm; 参考答案: 一、1、答案: 运行结果: GGPPBBWW 2、答案: 运行结果: 12.00160.003.50 3、答案: 运行结果: main: x,y=18,23 subs: x,y=41,64 main: x,y=36,23 4、答案: 运行结果: 432198765 5、运行结果: 473632282015 6、答案: 运行结果: 3579 11131517 7、答案: 运行结果: 5060 8、答案: 运行结果: b=184 二、1、答案: 在整型数组a的前n个元素中查找值为x的元素,找到返回1,找不到返回0。 2、答案: 统计整型数组a的前n个元素中不小于k的元素个数并返回 fun6(m,n,2) 3、答案: 程序随机产生10道20以内整数加法题,请用户回答。 并统计得分,4、答案: 调用 求m和n的最小公倍数 5、答案: 读入n个整数,然后逆序输出 6、答案: 返回双精度数数组a的前n个元素中不小于平均值的元素个数。 第四次作业 一、写出下列每个程序运行后的输出结果 1.程序代码: #include structWorker{ charname[15];//姓名 intage;//年龄 floatpay;//工资 }; voidmain(){ structWorkerx={"wanghua",52,2350}; structWorkery,*p; y=x;p=&x; printf("%s%d%6.2f\n",y.name,y.age,y.pay); printf("%s%d%6.2f\n",p->name,p->age+1,p->pay+20); } 2.程序代码: #include #include structWorker{ charname[15];//姓名intage;//年龄 floatpay;//工资 }; voidmain(){ structWorkerx; char*t="liouting"; intd=38;floatf=493; strcpy(x.name,t); x.age=d;x.pay=f; x.age++;x.pay*=2; printf("%s%d%6.2f\n",x.name,x.age,x.pay); } 3.程序代码: #include }; intLess(structWorkerr1,structWorkerr2){ if(r1.age elsereturn0; } voidmain(){ structWorkera[4]={{"abc",25,420},{"def",58,638}, {"ghi",49,560},{"jkl",36,375}};structWorkerx=a[0]; inti; for(i=1;i<4;i++) if(Less(x,a[i]))x=a[i]; printf("%s%d%6.2f\n",x.name,x.age,x.pay); } 二、写出下列每个函数的功能 1.程序代码: structWorker{ charname[15];//姓名 intage;//年龄 floatpay;//工资 }; voidQA(structWorkera[],intn){ inti; for(i=1;i scanf("%s%d%f",&a[i].name,&a[i].age,&a[i].pay); } 2.程序代码: structStrNode{ charname[15];//字符串域 structStrNode*next;//指针域}; structStrNode*QB(intn){ structStrNode*f,*p; if(n==0)returnNULL; f=malloc(sizeof(structStrNode)); scanf("%s",f->name); p=f; while(--n){ p=p->next=malloc(sizeof(structStrNode));scanf("%s",p->name); } p->next=NULL;returnf; 3.程序代码: structIntNode{ intdata;//结点值域 structIntNode*next;//结点指针域 }; structIntNode*FindMax(structIntNode*f) { structIntNode*p=f; if(! f)returnNULL; f=f->next; while(f){ if(f->data f=f->next; } returnp; } 4.*程序代码: structIntNode{ intdata;//结点值域 structIntNode*next;//结点指针域}; intCount(structIntNode*f) { intc=0; while(f){ c++; f=f->next; } returnc; 5.程序代码: structIntNode{ intdata;//结点值域 structIntNode*next;//结点指针域 }; structIntNode*Input(intn) { structIntNode*f,*p; f=malloc(sizeof(structIntNode)); if(n==0)returnNULL; f->next=NULL; printf("从键盘输入%d个整数: ",n); while(n--){ scanf("%d",&(f->data)); p=f; f=malloc(sizeof(structIntNode));f->next=p; } returnf->next; } 6.程序代码: #include #include #include voidJA(char*fname) { FILE
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+语言程序设计 新版 电大 C+ 语言程序设计 1234 作业 答案 参考资料