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

这篇博客包含了电大《C++语言程序设计》课程的四次作业及其答案,内容涉及C++基本语法、循环控制、函数调用、字符串处理、链表操作等。作业包括了程序运行结果预测、程序代码分析以及算法实现,旨在巩固和提高学员的C++编程能力。
摘要由CSDN通过智能技术生成

《电大《C++语言程序设计》第1、2、3、4次作业及答案》由会员分享,可在线阅读,更多相关《电大《C++语言程序设计》第1、2、3、4次作业及答案(23页珍藏版)》请在人人文库网上搜索。

1、第一次作业一、写出下列每个程序运行后的输出结果1. #includevoid 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 ,defaultn);printf(%sn,switch end.);2. #includevoid main()int i,s=0;for(i=1;ivoid main()int i,s1=0,s2=0;for(i=0;ivoid main()int n=10,y=1;wh。

2、ile(n-)y+;y+;printf(y=%dn,y);5. #includevoid main()int f,f1,f2,i;f1=f2=1;printf(%d %d ,f1,f2);for(i=3;i#includevoid main()int i,n;for(n=2;ntemp)printf(%d ,n);printf(n);7. #include#includeconst int M=20;void main()int i,c2,c3,c5;c2=c3=c5=0;for(i=1;i#includeconst int M=20;void main()int i,s;for(i=1,s=。

3、0;i5的最小n值。5编写一个主函数,求满足不等式22+42+n2void fun4(char* a,int b)doif(*a=0 & *aconst int M=2,N=3;double Mean(double aMN, int m,int n )double v=0;for(int i=0;iint FF(int a , int n)int mul=1;if(n=1)mul*=a0;else mul=an-1*FF(a,n-1);return mul;/*void main()int a6=1,2,3,4,5,6;printf(%dn,FF(a,6);*/4、答案:#includevoi。

4、d main()double sum=0;int n=1;while(true)if(sum + 1/(double)n 5)break;elsesum += 1/(double)n;n+;printf(%d, %lfn,n,sum);5、答案:#includevoid main()int s=0,i=2;dos+=i*i;if(s+(i+2)*(i+2)=1000)break;else i+=2;while(true);printf(i=%d,s=%d,i,s);6、答案:#includevoid main()int s=0,n;printf(请输入n的值:);scanf(%d,&n);fo。

5、r(int i=1;ivoid 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 #include double SD(int a, int b, cha。

6、r 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.2lfn, SD(。

7、x + y, y, /);3 程序代码:#include void WF(int x, int y) x = x + y;y = x + y;printf(subs: x, y = %d, %dn, x, y);void main() int x = 18, y = 23;printf(main: x, y = %d, %dn, x, y);WF(x, y);x = 2 * x;printf(main: x, y = %d, %dn, x, y);4 程序代码:#include #include void fun(char ss);void main() char s15 = 56789123。

8、4;fun(s);printf(%sn, s);void fun(char ss) int i, n = strlen(ss);for(i = 0; i void InsertSort(int a, int n)int i, j, x;for(i = 1; i = 0; j -) / 为x顺序向前寻找合适的插入位置if(x aj) aj + 1 = aj;else break;aj + 1 = x;void main() int i;int a6 = 20, 15, 32, 47, 36, 28 ;InsertSort(a, 6);for(i = 0; i void main() int a8。

9、 = 3, 5, 7, 9, 11, 13, 15, 17 ;int i, * p = a;for(i = 0; i int LA(int * a, int n) int i, s = 0;for(i = 0; i int LB(int * a, int n) int i, s = 1;for(i = 0; i = k) c +;return c;3 程序代码:#include #include #include const int N = 10;int ff(int x, int y) int z;printf(%d + %d = , x, y);scanf(%d, &z);if(x + y。

10、 = z) return 1; else return 0;void main() int i, a, b, c = 0;srand(time(0); / 初始化随机数序列for(i = 0; i #include void LI(int n) int * a = malloc(n * sizeof(int);int i;for(i = 0; 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 = s) 。

11、m +;return m;参考答案:一、1、答案:运行结果:GG PP BB WW2、答案:运行结果:12.00 160.00 3.503、答案:运行结果:main: x, y = 18, 23subs: x, y = 41, 64main: x, y = 36, 234、答案:运行结果:4321987655、运行结果:47 36 32 28 20 156、答案:运行结果:3 5 7 911 13 15 177、答案:运行结果:50 608、答案:运行结果:b=184二、1、答案:在整型数组a的前n个元素中查找值为x的元素,找到返回1,找不到返回0。2、答案:统计整型数组a的前n个元素中不小于。

12、k的元素个数并返回3、答案:程序随机产生10道20以内整数加法题,请用户回答。并统计得分, 4、答案:调用fun6(m, n, 2)求m和n的最小公倍数5、答案:读入n个整数,然后逆序输出6、答案:返回双精度数数组a的前n个元素中不小于平均值的元素个数。第四次作业一、写出下列每个程序运行后的输出结果1 程序代码:#include struct Worker char name15; / 姓名int age; / 年龄float pay; / 工资;void main() struct Worker x = wanghua, 52, 2350 ;struct Worker y, * p;y = 。

13、x; p = &x;printf(%s %d %6.2fn, y.name, y.age, y.pay);printf(%s %d %6.2fn, p-name, p-age + 1, p-pay + 20);2 程序代码:#include #include struct Worker char name15; / 姓名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 =。

14、 f;x.age +; x.pay *= 2;printf(%s %d %6.2fn, x.name, x.age, x.pay);3 程序代码:#include struct Worker char name15; / 姓名int age; / 年龄float pay; / 工资;int Less(struct Worker r1, struct Worker r2) if(r1.age name);p = f;while(- n) p = p-next = malloc(sizeof(struct StrNode);scanf(%s, p-name);p-next = NULL;retur。

15、n 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 data) p = f;f = f-next;return p;4 * 程序代码:struct IntNode int data; / 结点值域struct IntNode * next; / 结点指针域;int Cou。

16、nt(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,。

17、 &(f-data);p = f;f = malloc(sizeof(struct IntNode);f-next = p;return f-next;6 程序代码:#include #include #include void JA(char * fname)FILE * fout = fopen(fname, w);char a20;printf(输入若干个字符串,每个字符串长度小于20,字符串end作为结束标志n);while(1) scanf(%s, a);if(strcmp(a, end) = 0) break;fputs(a, fout);fputc(n, fout);fclose。

18、(fout);void main()char * p = d:xxkxuxk1.txt;JA(p);参考答案:一、1、答案:运行结果:wanghua 52 2350.00wanghua 53 2370.002、答案:运行结果:liouting 39 986.003、答案:运行结果:def 58 638.00二、1、答案:从标准输入设备读入n-1个人员的信息,依次存到结构数组的a1到an-1元素中2、答案:创建具有n个struct StrNode结点的单向链表并返回其表头结点地址,n为0时返回NULL。3、答案:函数功能:查找单向链表中结点值域最大的结点,并返回其地址。如链表为空则返回NULL4、答案:函数功能:统计并返回单向链表结点个数5、答案:函数功能:从表尾结点开始,逆序创建具有n个struct IntNode结点的单向链表并返回其表头结点地址,n为0时返回NULL6、答案:函数功能:将输入的若干个字符串保存到文本文件d:xxkxuxk1.txt中,每个字符串一行。输入的单个字符串长度必须小于20,输入字符串end结束23。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值