您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页电大《C++语言程序设计》第1、2、3、4次作业及答案

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

来源:化拓教育网
精品文档

第一次作业

一、写出以下每个程序运行后的输出结果 1. #include void main() {

int x=5; switch(2*x-3) {

}

printf(\"%s\\n\}

case 4:printf(\"%d \case 7:printf(\"%d \case 10:printf(\"%d \default:printf(\"%s \

2. #include void main() { }

3. #include void main() { }

4. #include void main() {

int n=10,y=1; while(n--){y++;y++;} int i,s1=0,s2=0; for(i=0;i<10;i++)

if(i%2)s1+=i; else s2+=i;

printf(\"%d %d\\n\int i,s=0; for(i=1;i<=6;i++)

s+=i*i;

printf(\"s=%d\\n\

.

精品文档

}

printf(\"y=%d\\n\

5. #include void main() { }

6. #include #include void main() { }

7. #include #include const int M=20; void main() {

int i,c2,c3,c5; c2=c3=c5=0; for(i=1;i<=M;i++){ int i,n;

for(n=2;n<=20;n++){ }

printf(\"\\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 \

int f,f1,f2,i; f1=f2=1;

printf(\"%d %d \for(i=3;i<=10;i++){ }

printf(\"\\n\");

f=f1+f2; printf(\"%d \if(i%5==0)printf(\"\\n\"); f1=f2; f2=f;

.

精品文档

}

}

if(i%2==0)c2++; if(i%3==0)c3++; if(i%5==0)c5++;

printf(\"%d %d %d\\n\

8. #include #include const int M=20; void main() { } 参:

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

int i,s;

for(i=1,s=0;i<15;i++){ }

printf(\"%d\\n\

if(i%2==0 || i%3==0)continue; printf(\"%d \s+=i;

第二次作业

一、根据以下每个题目要求编写程序

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 void fun4(char* a,int b[]){ }

/*void main() { }*/ 2、答案: #include const int M=2,N=3;

double Mean(double a[M][N], int m,int n ){ }

/*void main() {

.

do{

if(*a>='0' && *a<='9')b[*a-48]++;

}while(*a++);

int b[10]={0}; fun4(a,b);

for(int i=0;i<10;i++)

printf(\"%d \

double v=0; for(int i=0;ifor(int j=0;jv+=a[i][j];

return v/(m*n);

double a[2][3]={1,2,3,4,5,6};

精品文档

}*/

printf(\"%lf\\n\

3、答案: #include int FF(int a[] , int n){ }

/*void main() { }*/ 4、答案: #include void main() { }

5、答案: #include void main()

.

int mul=1; if(n==1)mul*=a[0]; else mul=a[n-1]*FF(a,n-1); return mul;

int a[6]={1,2,3,4,5,6}; printf(\"%d\\n\

double sum=0; int n=1; while(true) { }

printf(\"%d, %lf\\n\

if(sum + 1/(double)n > 5)break; else

sum += 1/(double)n;

n++;

{ 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\

}

6、答案: #include void main() { int s=0,n;

printf(\"请输入n的值:\"); scanf(\"%d\ for(int i=1;i<=n;i++)

s=s*i; printf(\"n=%d,s=%d\

}

一、写出以下每个程序运行后的输出结果 1. 程序代码: #include 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() { }

2. 程序代码: #include #include

double SD(int a, int b, char op) { }

void main() { }

3. 程序代码: #include void WF(int x, int y) {

.

char a1 = 'b', a2 = 'C', a3 = 'f'; SB(a1); SB(a2); SB(a3); SB('A'); printf(\"\\n\");

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;

int x = 20, y = 8;

printf(\"%3.2lf \printf(\"%3.2lf \printf(\"%3.2lf\\n\

精品文档

}

x = x + y; y = x + y;

printf(\"subs: x, y = %d, %d\\n\

void main() { }

4. 程序代码: #include #include void fun(char ss[]); void main() { }

void fun(char ss[]) { }

5. 程序代码: #include

void InsertSort(int a[], int n) {

.

int x = 18, y = 23;

printf(\"main: x, y = %d, %d\\n\WF(x, y); x = 2 * x;

printf(\"main: x, y = %d, %d\\n\

char s[15] = \"5671234\"; fun(s);

printf(\"%s\\n\

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;

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() { }

6. 程序代码: #include void main() { }

7. 程序代码: #include int LA(int * a, int n) { }

.

int i;

int a[6] = { 20, 15, 32, 47, 36, 28 }; InsertSort(a, 6);

for(i = 0; i < 6; i ++) printf(\"%d \printf(\"\\n\");

int a[8] = { 3, 5, 7, 9, 11, 13, 15, 17 }; int i, * p = a; for(i = 0; i < 8; i ++) { }

printf(\"%5d\if((i + 1) % 4 == 0) printf(\"\\n\");

int i, s = 0;

for(i = 0; i < n; i ++)

s += a[i];

return s;

精品文档

void main() { }

8. 程序代码: #include int LB(int * a, int n) { }

void main() { }

二、写出以下每个函数的功能 1. 程序代码:

int WB(int a[], int n, int x) { }

2. 程序代码:

int WC(int a[], int n, int k) { }

.

int a[] = { 5, 10, 15, 20, 25, 30 }; int b = LA(a, 4); int c = LA(a + 2, 3); printf(\"%d %d\\n\

int i, s = 1;

for(i = 0; i < n; i ++) s *= * a ++; return s;

int a[] = { 1, 2, 3, 4, 2, 4, 5, 2 }; int b = LB(a, 4) + LB(&a[3], 4); printf(\"b=%d\\n\

int i;

for(i = 0; i < n; i ++)

if(a[i] == x) return 1;

return 0;

int c = 0, i; for(i = 0; i < n; i ++)

if(a[i] >= k) c ++;

return c;

精品文档

3. 程序代码: #include #include #include const int N = 10; int ff(int x, int y) { }

void main() { }

4. * 程序代码: int fun6(int m, int n, int b) { }

5. 程序代码: #include #include void LI(int n) {

.

int z;

printf(\"%d + %d = \scanf(\"%d\

if(x + y == z) return 1; else return 0;

int i, a, b, c = 0;

srand(time(0)); // 初始化随机数序列 for(i = 0; i < N; i ++) { }

printf(\"得分:%d\\n\

a = rand() % 20 + 1; // rand()函数产生0~32767之间的一个随机数 b = rand() % 20 + 1; c += ff(a, 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);

int * a = malloc(n * sizeof(int));

精品文档

}

int i;

for(i = 0; i < n; i ++) scanf(\"%d\for(i = n - 1; i >= 0; i --) printf(\"%d \printf(\"\\n\"); free(a);

6. 程序代码: int LK(double a[], int n) { } 参:

一、1、答案:运行结果:

GG PP BB WW 2、答案:运行结果: 3、答案:运行结果:

main: x, y = 18, 23 subs: x, y = 41, 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

.

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;

精品文档

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 struct Worker { };

void main() { }

2. 程序代码: #include #include struct Worker { };

char name[15]; // 姓名 int age; // 年龄 float pay; // 工资

struct Worker x = { \"wanghua\struct Worker y, * p; y = x; p = &x;

printf(\"%s %d %6.2f\\n\

printf(\"%s %d %6.2f\\n\char name[15]; // 姓名 int age; // 年龄 float pay; // 工资

.

精品文档

void main() { }

3. 程序代码: #include struct Worker { };

int Less(struct Worker r1, struct Worker r2) { }

void main() { }

二、写出以下每个函数的功能 1. 程序代码: struct Worker {

.

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\

char name[15]; // 姓名 int age; // 年龄 float pay; // 工资

if(r1.age < r2.age) return 1; else return 0;

struct Worker a[4] = { { \"abc\

{ \"ghi\

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\

char name[15]; // 姓名

精品文档

};

int age; // 年龄 float pay; // 工资

void QA(struct Worker a[], int n) { }

2. 程序代码: struct StrNode { };

struct StrNode * QB(int n) { }

3. 程序代码: struct IntNode { };

struct IntNode * FindMax(struct IntNode * f) {

.

int i;

for(i = 1; i < n; i ++)

scanf(\"%s %d %f\

char name[15]; // 字符串域 struct StrNode * next; // 指针域

struct StrNode * f, * p; if(n == 0) return NULL;

f = malloc(sizeof(struct StrNode)); scanf(\"%s\p = f; while(-- n) { }

p->next = NULL; return f;

p = p->next = malloc(sizeof(struct StrNode)); scanf(\"%s\

int data; // 结点值域

struct IntNode * next; // 结点指针域

精品文档

}

struct IntNode * p = f; if(! f) return NULL; f = f->next; while(f) { } return p;

if(f->data < p->data) p = f; f = f->next;

4. * 程序代码: struct IntNode { };

int Count(struct IntNode * f) { }

5. 程序代码: struct IntNode { };

struct IntNode * Input(int n) {

.

int data; // 结点值域

struct IntNode * next; // 结点指针域

int c = 0; while(f) { } return c;

c ++; f = f->next;

int data; // 结点值域

struct IntNode * next; // 结点指针域

struct IntNode * f, * p;

f = malloc(sizeof(struct IntNode)); if(n == 0) return NULL;

精品文档

}

f->next = NULL;

printf(\"从键盘输入%d个整数:\while(n --) { }

return f->next;

scanf(\"%d\p = f;

f = malloc(sizeof(struct IntNode)); f->next = p;

6. 程序代码: #include #include #include void JA(char * fname) { }

void main() { }

参:

.

FILE * fout = fopen(fname, \"w\"); char a[20];

printf(\"输入假设干个字符串,每个字符串长度小于20,字符串end作为结束标志\\n\"); while(1) { }

fclose(fout);

scanf(\"%s\

if(strcmp(a, \"end\") == 0) break; fputs(a, fout); fputc('\\n', fout);

char * p = \"d:\\\\xxk\\\\xuxk1.txt\"; JA(p);

精品文档

一、1、答案:运行结果: 2、答案:运行结果: 3、答案:运行结果:

二、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、答案:函数功能:

将输入的假设干个字符串保存到文本文件中,每个字符串一行。输入的单个字符串长度必须小于20,输入字符串end结束

.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务