分享
分销 收藏 举报 申诉 / 24
播放页_导航下方通栏广告

类型电大《C++语言程序设计》第1、2、3、4次作业及答案.doc

  • 上传人:二***
  • 文档编号:4538446
  • 上传时间:2024-09-27
  • 格式:DOC
  • 页数:24
  • 大小:76.50KB
  • 下载积分:5 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    C++语言程序设计 电大 C++ 语言程序设计 作业 答案
    资源描述:
    整理文档 第一次作业 一、写出下列每个程序运行后的输出结果 1. #include<stdio.h> void main() { int x=5; switch(2*x-3) { case 4:printf("%d ",x); case 7:printf("%d ",2*x+1); case 10:printf("%d ",3*x-1);break; default:printf("%s ","default\n"); } printf("%s\n","switch end."); } 2. #include<stdio.h> void main() { int i,s=0; for(i=1;i<=6;i++) s+=i*i; printf("s=%d\n",s); } 3. #include<stdio.h> void main() { int i,s1=0,s2=0; for(i=0;i<10;i++) if(i%2)s1+=i; else s2+=i; printf("%d %d\n",s1,s2); } . 整理文档 4. #include<stdio.h> void main() { int n=10,y=1; while(n--){y++;y++;} printf("y=%d\n",y); } 5. #include<stdio.h> void main() { int f,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<stdio.h> #include<math.h> void main() { int i,n; for(n=2;n<=20;n++){ int temp=(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<stdio.h> #include<math.h> const int M=20; void main() { int i,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<stdio.h> #include<math.h> const int M=20; void main() { int i,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、答案:11 14 switch end. 2、答案:s=91. 3、答案:25 20. 4、答案:y=21. 5、答案:1 1 2 3 5 8 13 21 34 55 6、答案:2 3 5 7 11 13 17 19 7、答案:10 6 4 8、答案:1 5 7 11 13 37 第二次作业 一、根据下列每个题目要求编写程序 1.编写一个函数,函数头格式为“void fun4(char *a , int b[])”,分别求出由字符指针a所指向的字符串中包含的每种十进制数字出现的次数,把统计结果保存在数组b的相应元素。 2.编写一个函数,函数头格式为“double Mean(double a[M][N] , int m , int n)”,要求返回二维数组a[m][n]中所有元素的平均值,假定在计算过程是采用变量v存放平均值。 . 整理文档 3.编写一个递归函数“int FF(int a[] , int n)”,求出数组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<stdio.h> void fun4(char* a,int b[]){ do{ if(*a>='0' && *a<='9')b[*a-48]++; }while(*a++); } /*void main() { char * a="122333444499888"; int b[10]={0}; fun4(a,b); for(int i=0;i<10;i++) printf("%d ",b[i]); }*/ 2、答案: #include<stdio.h> . 整理文档 const int M=2,N=3; double Mean(double a[M][N], int m,int n ){ double v=0; for(int i=0;i<m;i++) for(int j=0;j<n;j++) v+=a[i][j]; return v/(m*n); } /*void main() { double a[2][3]={1,2,3,4,5,6}; printf("%lf\n",Mean(a,2,3)); }*/ 3、答案: #include<stdio.h> int FF(int a[] , int n){ int mul=1; if(n==1)mul*=a[0]; else mul=a[n-1]*FF(a,n-1); return mul; } /*void main() { int a[6]={1,2,3,4,5,6}; printf("%d\n",FF(a,6)); }*/ 4、答案: . 整理文档 #include<stdio.h> void main() { double sum=0; int n=1; while(true) { if(sum + 1/(double)n > 5)break; else sum += 1/(double)n; n++; } printf("%d, %lf\n",n,sum); } 5、答案: #include<stdio.h> void main() { int s=0,i=2; do { s+=i*i; if(s+(i+2)*(i+2)>=1000)break; else i+=2; }while(true); printf("i=%d,s=%d",i,s); } 6、答案: #include<stdio.h> void main() { . 整理文档 int s=0,n; printf("请输入n的值:"); scanf("%d",&n); for(int i=1;i<=n;i++) s=s*i; printf("n=%d,s=%d",n,s); } 第三次作业 一、写出下列每个程序运行后的输出结果 1. 程序代码: #include <stdio.h> void SB(char ch) { 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; } } void main() { char a1 = 'b', a2 = 'C', a3 = 'f'; SB(a1); SB(a2); SB(a3); SB('A'); printf("\n"); } . 整理文档 2. 程序代码: #include <stdio.h> #include <stdlib.h> double SD(int a, int b, char op) { double x; switch(op) { case '+': x = a + b; break; case '-': x = a - b; break; case '*': x = a * b; break; case '/': if(b) x = (double)a/b; else exit(1); break; default: { printf("运算符错!\n"); exit(1); } } return x; } void main() { int x = 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 <stdio.h> void WF(int x, int y) { x = x + y; y = x + y; printf("subs: x, y = %d, %d\n", x, y); } void main() { int x = 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 <stdio.h> #include <string.h> void fun(char ss[]); void main() { char s[15] = "567891234"; fun(s); printf("%s\n", s); } void fun(char ss[]) { int i, n = strlen(ss); for(i = 0; i < n / 2; i ++) { char c = ss[i]; ss[i] = ss[n - 1 - i]; ss[n - 1 - i] = c; } } 5. 程序代码: #include <stdio.h> void InsertSort(int a[], int n) { int i, j, x; for(i = 1; i < n; i ++) { // 进行n-1次循环 x = a[i]; for(j = i - 1; j >= 0; j --) // 为x顺序向前寻找合适的插入位置 . 整理文档 if(x > a[j]) a[j + 1] = a[j]; else break; a[j + 1] = x; } } void main() { int i; int a[6] = { 20, 15, 32, 47, 36, 28 }; InsertSort(a, 6); for(i = 0; i < 6; i ++) printf("%d ", a[i]); printf("\n"); } 6. 程序代码: #include <stdio.h> void main() { int a[8] = { 3, 5, 7, 9, 11, 13, 15, 17 }; int i, * p = a; for(i = 0; i < 8; i ++) { printf("%5d", * p ++); if((i + 1) % 4 == 0) printf("\n"); } } 7. 程序代码: #include <stdio.h> int LA(int * a, int n) { int i, s = 0; for(i = 0; i < n; i ++) s += a[i]; return s; . 整理文档 } void main() { int a[] = { 5, 10, 15, 20, 25, 30 }; int b = LA(a, 4); int c = LA(a + 2, 3); printf("%d %d\n", b, c); } 8. 程序代码: #include <stdio.h> int LB(int * a, int n) { int i, s = 1; for(i = 0; i < n; i ++) s *= * a ++; return s; } void main() { int a[] = { 1, 2, 3, 4, 2, 4, 5, 2 }; int b = LB(a, 4) + LB(&a[3], 4); printf("b=%d\n", b); } 二、写出下列每个函数的功能 1. 程序代码: int WB(int a[], int n, int x) { int i; for(i = 0; i < n; i ++) if(a[i] == x) return 1; return 0; } 2. 程序代码: . 整理文档 int WC(int a[], int n, int k) { int c = 0, i; for(i = 0; i < n; i ++) if(a[i] >= k) c ++; return c; } 3. 程序代码: #include <stdio.h> #include <stdlib.h> #include <time.h> const int N = 10; int ff(int x, int y) { int z; printf("%d + %d = ", x, y); scanf("%d", &z); if(x + y == z) return 1; else return 0; } void main() { int i, a, b, c = 0; srand(time(0)); // 初始化随机数序列 for(i = 0; i < N; i ++) { a = rand() % 20 + 1; // rand()函数产生0~32767之间的一个随机数 b = rand() % 20 + 1; c += ff(a, b); } printf("得分:%d\n", c * 10); } 4. * 程序代码: int fun6(int m, int n, int b) { . 整理文档 if(m < b && n < b) return m * n; else if(m % b == 0 && n % b == 0) return b * fun6(m / b, n / b, b); else return fun6(m, n, ++ b); } 5. 程序代码: #include <stdio.h> #include <stdlib.h> void LI(int n) { int * a = malloc(n * sizeof(int)); int i; for(i = 0; i < n; i ++) scanf("%d", a + i); for(i = n - 1; i >= 0; i --) printf("%d ", * (a + i)); printf("\n"); free(a); } 6. 程序代码: int LK(double a[], int n) { double s = 0; int i, m = 0; for(i = 0; i < n; i ++) s += a[i]; s /= n; for(i = 0; i < n; i ++) if(a[i] >= s) m ++; return m; } . 整理文档 参考答案: 一、1、答案:运行结果: GG PP BB WW 2、答案:运行结果: 12.00 160.00 3.50 3、答案:运行结果: main: x, y = 18, 23 subs: x, y = 41, 64 main: x, y = 36, 23 4、答案:运行结果: 432198765 5、运行结果: 47 36 32 28 20 15 6、答案:运行结果: 3 5 7 9 11 13 15 17 7、答案:运行结果: 50 60 8、答案:运行结果: b=184 二、1、答案:在整型数组a的前n个元素中查找值为x的元素,找到返回1,找不到返回0。 2、答案:统计整型数组a的前n个元素中不小于k的元素个数并返回 3、答案:程序随机产生10道20以内整数加法题,请用户回答。并统计得分, 4、答案:调用fun6(m, n, 2)求m和n的最小公倍数 5、答案:读入n个整数,然后逆序输出 . 整理文档 6、答案:返回双精度数数组a的前n个元素中不小于平均值的元素个数。 第四次作业 一、写出下列每个程序运行后的输出结果 1. 程序代码: #include <stdio.h> struct Worker { char name[15]; // 姓名 int age; // 年龄 float pay; // 工资 }; void main() { struct Worker x = { "wanghua", 52, 2350 }; struct Worker y, * 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 <stdio.h> #include <string.h> struct Worker { char name[15]; // 姓名 int age; // 年龄 float pay; // 工资 }; void main() { struct Worker x; char * t = "liouting"; . 整理文档 int d = 38; float f = 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 <stdio.h> struct Worker { char name[15]; // 姓名 int age; // 年龄 float pay; // 工资 }; int Less(struct Worker r1, struct Worker r2) { if(r1.age < r2.age) return 1; else return 0; } void main() { struct Worker a[4] = { { "abc", 25, 420 }, { "def", 58, 638 }, { "ghi", 49, 560 }, { "jkl", 36, 375 } }; struct Worker x = a[0]; int i; 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. 程序代码: struct Worker { . 整理文档 char name[15]; // 姓名 int age; // 年龄 float pay; // 工资 }; void QA(struct Worker a[], int n) { int i; for(i = 1; i < n; i ++) scanf("%s %d %f", &a[i].name, &a[i].age, &a[i].pay); } 2. 程序代码: struct StrNode { char name[15]; // 字符串域 struct StrNode * next; // 指针域 }; struct StrNode * QB(int n) { struct StrNode * f, * p; if(n == 0) return NULL; f = malloc(sizeof(struct StrNode)); scanf("%s", f->name); p = f; while(-- n) { p = p->next = malloc(sizeof(struct StrNode)); scanf("%s", p->name); } p->next = NULL; return f; } 3. 程序代码: struct IntNode { . 整理文档 int data; // 结点值域 struct IntNode * next; // 结点指针域 }; struct IntNode * FindMax(struct IntNode * f) { struct IntNode * p = f; if(! f) return NULL; f = f->next; while(f) { if(f->data < p->data) p = f; f = f->next; } return p; } 4. * 程序代码: struct IntNode { int data; // 结点值域 struct IntNode * next; // 结点指针域 }; int Count(struct IntNode * f) { int c = 0; while(f) { c ++; f = f->next; } return c; } . 整理文档 5. 程序代码: struct IntNode { int data; // 结点值域 struct IntNode * next; // 结点指针域 }; struct IntNode * Input(int n) { struct IntNode * f, * p; f = malloc(sizeof(struct IntNode)); if(n == 0) return NULL; f->next = NULL; printf("从键盘输入%d个整数:", n); while(n --) { scanf("%d", &(f->data)); p = f; f = malloc(sizeof(struct IntNode)); f->next = p; } return f->next; } 6. 程序代码: #include <stdio.h> #include <stdlib.h> #include <string.h> void JA(char * fname) { FILE * fout = fopen(fname, "w"); char a[20]; printf("输入若干个字符串,每个字符串长度小于20,字符串end作为结束标志\n"); while(1) { scanf("%s", a); . 整理文档 if(strcmp(a, "end") == 0) break; fputs(a, fout); fputc('\n', fout); } fclose(fout); } void main() { char * p = "d:\\xxk\\xuxk1.txt"; JA(p); } 参考答案: . 整理文档 一、1、答案:运行结果: wanghua 52 2350.00 wanghua 53 2370.00 2、答案:运行结果: liouting 39 986.00 3、答案:运行结果: def 58 638.00 二、1、答案:从标准输入设备读入n-1个人员的信息,依次存到结构数组的a[1]到a[n-1]元素中 2、答案:创建具有n个struct StrNode结点的单向链表并返回其表头结点地址,n为0时返回NULL。 3、答案:函数功能: 查找单向链表中结点值域最大的结点,并返回其地址。如链表为空则返回NULL 4、答案:函数功能: 统计并返回单向链表结点个数 5、答案:函数功能: 从表尾结点开始,逆序创建具有n个struct IntNode结点的单向链表并返回其表头结点地址,n为0时返回NULL 6、答案:函数功能: 将输入的若干个字符串保存到文本文件d:\xxk\xuxk1.txt中,每个字符串一行。输入的单个字符串长度必须小于20,输入字符串end结束 本文档部分内容来源于网络,如有内容侵权请告知删除,感谢您的配合! .
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:电大《C++语言程序设计》第1、2、3、4次作业及答案.doc
    链接地址:https://www.zixin.com.cn/doc/4538446.html
    页脚通栏广告

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork