高级语言程序设计课后习题答案宁爱军版.docx

上传人:s****u 文档编号:12759837 上传时间:2020-05-22 格式:DOCX 页数:37 大小:37.74KB
返回 下载 相关 举报
高级语言程序设计课后习题答案宁爱军版.docx_第1页
第1页 / 共37页
高级语言程序设计课后习题答案宁爱军版.docx_第2页
第2页 / 共37页
高级语言程序设计课后习题答案宁爱军版.docx_第3页
第3页 / 共37页
点击查看更多>>
资源描述
西南交通大学通信工程系高级语言程序设计课后习题编程题答案(部分)第六章:循环结构程序设计.#includeint main()int a=1,b=2,n;double sum=0;for(n=0;n=20;n+)sum=sum+(double(b/a);b=a+b;a=b;printf(%lf,sum);return 0;.#includeint main()double s=0,a=1,t;int n=1;dot=1/a;s=s+t;a=a+n;n+;while(t=0.000001);printf(%lf,s);return 0;.#includeint main()double t,sum=1;double i=1;doi=i*i;t=1/i;sum=sum+t;i+;while(t=0.00001);printf(%lf,sum);return 0;.#includevoid main()int n,i,t;for(n=2;n1000;n+)t=0;for(i=1;in;i+)if(n%i=0)t=t+i; if(t=n)printf(%dn,n);.#includevoid main()double sum=0,x=0,n=0,av=0;printf(请输入成绩:);dosum=sum+x;scanf(%lf,&x);n+;while(x!=-99);av=sum/(n-1);printf(平均成绩为%lfn,av);.#includevoid main()int m,n,i,sum=0;printf(请输入整数m和n:);scanf(%d%d,&m,&n);for(i=2;i=m;i+)if(m%i=0&n%i=0)sum+=i;printf(m和n的公约数之和为%dn,sum);.#includevoid main()int man,woman,child;for(man=0;man=9;man+)for(woman=0;woman=0&man+woman+child=36)printf(man:%dnwoman:%dnchild:%dn,man,woman,child);.#includevoid main()int s;for(s=0;s+)if(s%2=1)&(s%3=2)&(s%5=4)&(s%6=5)&(s%7=0)break;printf(阶梯总共有:%d,s);第七章:数组.#include#include#includevoid main()int a100,sum=0;int i,n=0;double av;srand(time(0);for(i=0;i=99;i+)ai=rand()%200;printf(数组为:);for(i=0;i=99;i+)printf(%4d,ai);for(i=0;i=99;i+)if(ai%2=0)sum=sum+ai; n+;av=(double)sum)/n;printf(值为偶数的元素个数为:%dn,n);printf(值为偶数的元素平均值为:%lfn,av);.#includevoid main()int a100;int i,j=0;for(i=1;i=500;i+)if(i%7=0)|(i%11=0)if(!(i%7=0&i%11=0)aj=i;j+;for(i=0;i=j-1;i+)printf(%4d,ai);.#include#include#includevoid main()int a100;int i,x;srand(time(0);for(i=0;i=99;i+)ai=rand()%100;printf(数组为:);for(i=0;i=99;i+)printf(%3d,ai);printf(n);printf(从键盘输入x:);scanf(%d,&x);for(i=0;i=99;i+)if(ai=x)ai=0;printf(删除与x值相等的元素之后的数组为:);for(i=0;i=99;i+)printf(%3d,ai);printf(n);./*定义输入(或赋随机数)10行1列二维数组,按行列方式输出,求其两条对角线的元素之和*/#include#include#includeconst int N=10;void main()int aNN;int i,j,sum=0;srand(time(0);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)aij=rand()%100;printf(生成的数组为:n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)printf(%3d,aij); printf(n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)if(i=j|i=(N-1-j)sum+=aij;printf(两条对角线的元素之和为:sum=%dn,sum);./*定义两个矩阵A和B(数据为1-20的随机数)将A和B相加后放在矩阵A中,将A和B相乘后放在矩阵C中*/#include#include#includeconst int N=4;void main()int aNN,bNN,cNN;int i,j;srand(time(0);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)aij=rand()%21; bij=rand()%21;printf(矩阵A:n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)printf(%3d,aij); printf(n);printf(矩阵B:n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)printf(%3d,bij);printf(n);printf(A和B矩阵相加后A=n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)aij+=bij;printf(%3d,aij);printf(n);printf(C=n);for(i=0;i=N-1;i+)for(j=0;j=N-1;j+)cij=(aij-bij)*bji;printf(%4d,cij);printf(n);./*定义并输入字符串,统计其字符数*/#includevoid main()char str1100;int i,n=0;printf(请输入一串字符:);gets(str1);for(i=0;str1i!=0;i+)n+;printf(%d,n);./*生成两个字符串str1和str2,比较两个字符串的大小*/#includeconst int N=100;void main()char str1N,str2N;int i;printf(请输入两个字符串:);gets(str1);gets(str2);for(i=0;istr2i)printf(字符串str1大于字符串str2);break;if(str1istr2i)printf(字符串str1大于字符串str2);break;.#include#includevoid main()char str100,s;int i,n;printf(请输入字符串:);gets(str); n=strlen(str);printf(原字符串为:);puts(str);for(i=0;i=n/2;i+)s=stri;stri=strn-i-1;strn-1-i=s;printf(反序后字符串为:); puts(str);第八章:函数.#include#define PI 3.14double V(double r)/定义V函数计算圆球体积double v;v=PI*r*r*r*4/3;/注意若把(4/3)放到计算式的头部则运行结果会发生变化,错误return(v);double V2(double r,double h)/定义V2函数计算圆柱体积double v2;v2=PI*r*r*h;return(v2);double output(double v)/输出函数printf(%lfn,v);return 0;void main()double r,h;double v;double v2;scanf_s(%lf,&r);scanf_s(%lf,&h);v=V(r); /函数调用v2=V2(r,h);printf(圆球的体积为:);output(v);printf(圆柱的体积为:);output(v2);.#includedouble f(double x)double y;if(x=0&x=10&x=100)/此处改为else后会一直执行y=x*x,为什么?y=x*x;return(y);void main()double a,b,c,d,s;scanf(%lf,&a);scanf(%lf,&b);scanf(%lf,&c);scanf(%lf,&d);s=(f(a)+f(b)/(f(c)+(f(d);printf(s=%.2lf,s);.#includeint prime(int a)int j;int flag=1;for(j=2;ja;j+)if (a%j=0)flag=0;continue;return flag;void main() int i;for(i=100;i=999;i+) if(prime(i)=1)printf(%4d,i);printf(n);.#include#include#includeint const N=10;void input(int M,int aN)int i,j;srand(time(0);for(i=0;iM;i+)for(j=0;jN;j+)aij=rand()%100;void output(int M,int aN)int i,j;for(i=0;iM;i+)for(j=0;jN;j+)printf(%3d,aij);printf(n);void sum(int M,int aN)int i,j;int sum=0;for(i=0;iM;i+)for(j=0;jN;j+)if(i=j|i=N-1-j)sum=sum+aij;printf(对角线元素之和为:%dn,sum);/printf(n);void avg(int M,int aN)int i,j;int sum=0,n=0,avg;for(i=0;iM;i+)for(j=0;jN;j+)sum=sum+aij;n+;avg=sum/n;printf(数组的平均值为:%dn,avg);/printf(n);void main()int a1010;input(10,a);output(10,a);/printf(n);sum(10,a);/printf(n);avg(10,a);.#includevoid fun(char str)int i;for(i=0;stri!=0;i+)if(stri=a&stri=A&stri=Z)stri=stri+32;void main()char str100;printf(请输入字符串:);gets(str);fun(str);puts(str);./*将s2复制到s1中*/#includevoid str_cpy(char s1,char s2)int i,j;for(i=0;s1i!=0;i+);for(j=0;s2j!=0;j+,i+)s1i=s2j;s1i=0;void main()char s1100,s2100;printf(请输入s1和s2:);gets(s1);gets(s2);printf(s1:);puts(s1);printf(s2:);puts(s2);str_cpy(s1,s2);printf(s1:);puts(s1);.#includeint str_cmp(char s1,char s2)int i,m=0;for(i=0;s1i!=0;i+)m=s1i-s2i;return m;void main()char s1100,s2100;int n;printf(请输入字符串:);gets(s1);gets(s2);n=str_cmp(s1,s2); if(n0)printf(s10)printf(s1s2);elseprintf(s1=s2);.#includevoid Fibonacci(int n)int i;int a=1,b=1,t=0;for(i=3;i=n;i+)t=a+b;a=b;b=t;printf(Fibonacci的第n项为:%dn,t);void main()int n;printf(请输入n:);scanf(%d,&n);Fibonacci(n);第九章:预编译处理.#include#define M(a,b) a/bvoid main()int x,y,m;printf(请输入两个数:);scanf(%d%d,&x,&y);m=M(x,y);printf(m=%d,m);.#includeint min(int a,int b)int min;if(ab)min=b;else min=a;return min;#include#define T 1void main()double a,b;double m;printf(请输入两个数:);scanf(%d%d,&a,&b);#if Tm=a*b;printf(两数之积为m=%dn,m);#elsem=a+b;printf(两数之和为m=%dn,m);#endif第十章:指针.#includevoid main()int *p1,*p2,*p3,*p;int a,b,c;printf(请输入三个整数:);scanf(%d%d%d,&a,&b,&c);p1=&a;p2=&b;p3=&c;if(ab)p=p1;p1=p2;p2=p;if(ac)p=p1;p1=p3;p3=p;if(bc)p=p2;p2=p3;p3=p;printf(%d%d%dn,a,b,c);printf(%d%d%dn,*p1,*p2,*p3);./*输入三个字符串,使用指针的方法按从小到大的顺序输出*/#include#includeint const N=100;void main()char str1N,str2N,str3N;char *p1,*p2,*p3;char *temp;printf(请输入三个字符串:);gets(str1);gets(str2);gets(str3);p1=str1;p2=str2;p3=str3;if(strcmp(str1,str2)0)temp=p1;p1=p2;p2=temp;if(strcmp(str1,str3)0)temp=p1;p1=p3;p3=temp;if(strcmp(str2,str3)0)temp=p2;p2=p3;p3=temp;printf(按从小到大顺序输出三个字符串:);puts(p1);puts(p2);puts(p3);./*输入一行字符串,用指针char *p指向字符串,分别求出其大写字母,小写字母,数字和其他字符的个数*/#includevoid main()char s100;char *p;int m=0,n=0,t=0,k=0;printf(请输入字符串:);gets(s);p=s;while(*p!=0)if(*p=A&*p=a&*p=0&*p=9)t+;else k+;p+;printf(大写字母的个数为m=%dn,m);printf(小写字母的个数为n=%dn,n);printf(数字的个数为t=%dn,t);printf(其他字符的个数为k=%dn,k);./*编写函数fun(char *p1,char*p2,int m),将字符串p1中从第m个字符开始的所有字符复制为字符串2*/#include#includevoid fun(char *p1,char *p2,int m)while(*p2=*(p1+m)!=0)p1+;p2+;void main()char str1100,str2100;char *s1;char *s2;int m;printf(请输入字符串str1:);gets(str1);printf(请输入开始复制的位置:);scanf(%d,&m);s1=str1;s2=str2;fun(s1,s2,m);printf(str2=);puts(str2);.#includeint strcmp(char *s1,char *s2)int n;while(*s1=*s2) s1+; s2+;n=*s1-*s2;return n;void main()char str1100,str2100;gets(str1);gets(str2);printf(%d,strcmp(str1,str2);第十一章:其他数据类型.#includestruct CNint r;int i;struct CN Mulitply(struct CN x,struct CN y)struct CN z;z.r=x.r*y.r-x.i*y.i;z.i=x.r*y.i+x.i*y.r;return z;void Input(struct CN x)printf(请输入一个复数:);scanf(%d%d,&x.r,&x.i);void main()struct CN a;struct CN b;struct CN m;Input(a);Input(b);m=Mulitply(a,b);printf(a*b=%dn,m);./*比较两个同学的生日,输出生日较大的同学的全部信息,包括学号,姓名,性别,生日,(年,月,日)*/#includestruct dateint year;int month;int day;struct student /定义结构体变量int Number;char name20;char sex;struct date birthday;float score;student1,student2;void Input(struct student stu) /结构体变量初始化函数printf(Number:);scanf(%dn,&stu.Number);printf(name:);scanf(n%sn,&stu.name);printf(sex:);scanf(n%c,&stu.sex);printf(birthday:);scanf(n%d%d%d,&stu.birthday.year,&stu.birthday.month,&stu.birthday.day);printf(score:);scanf(n%f,&stu.score);void Output(struct student stu) /输出函数printf(tNumber:%dntName:%sn,stu.Number,stu.name);printf(tSex:%cntBirthday:%d%d%dn,stu.sex,stu.birthday.year,stu.birthday.month,stu.birthday.day);printf(tScore:%f,stu.score);void compare(struct student stu1,struct student stu2) /定义compare函数比较两个同学生日的大小if(stu1.birthday.yearstu2.birthday.year)printf(Student1record is:);Output(stu1);else if(stu1.birthday.yearstu2.birthday.month)printf(Student1record is:);Output(stu1); else if(stu1.birthday.monthstu2.birthday.day) printf(Student1record is:); Output(stu1); else if(stu1.birthday.daystu2.birthday.day) printf(Student2record is:); Output(stu2); else printf(The two students have the same birthday); void main()struct student stu1=0;struct student stu2=0;Input(stu1);Input(stu2);compare(stu1,stu2);./*使用共用体类型,建立一个班级通讯录,其中包括30名学生和5名任课教师,学生信息包括:身份(学生),学号,姓名,性别,电话,教师信息包括:身份(教师)任课科目,姓名,性别,电话。*/#includestructchar name20;char job;char sex;double tel;unionint number;char lesson20;category; /在结构体类型声明中定义共用体变量person35;void main()int i;printf(输入记录:nt姓名t性别t电话t身份t学号或任课科目n);for(i=0;i35;i+)scanf(t%st%ct%lft%ct,&personi.name,&personi.sex,&personi.tel,&personi.job);if(personi.job=s)scanf(%d,&personi.category.number);else if(personi.job=t)scanf(%s,&personi.category.lesson);elseprintf(输入出错);printf(记录是:nt姓名t性别t电话t身份t学号或任课科目n);for(i=0;i35;i+)if(personi.job=s)printf(t%st%ct%lft%ct%dn,&personi.name,&personi.sex,&personi.tel,&personi.job,personi.category.number);elseprintf(t%st%ct%lft%ct%sn,&personi.name,&personi.sex,&personi.tel,&personi.job,personi.category.lesson);./*有10名学生,每名学生的信息包括学号、姓名、3门课程的分数,要求按每名学生3门课程的平均分由高至低输出所有学生的信息。包括名次、学号、姓名、3门课程的分数、3门课程的平均分,并求每门课程的平均分*/#includestruct stuint number;char name20;float math;float English;float PE;double average;double Maverage;double Eaverage;double Paverage;stu3;void main()int i;float Msum=0,Esum=0,Psum=0;printf(输入记录:nt学号t姓名t数学t英语t体育n);for(i=0;i3;i+)scanf(t%dt%st%ft%ft%f,&stui.number,&stui.name,&stui.math,&stui.English,&stui.PE);stui.average=(stui.math+stui.English+stui.PE)/3;Msum=Msum+stui.math;Esum=Esum+stui.English;Psum=Psum+stui.PE;int i,j,k,t=0;struct stu student3=;for(i=0;i3;i+)k=i;for(j=i+1;jstuk.average)k=j;if(k!=i)studentt=stui;stui=stuk;stuk=studentt; t+;printf(记录是:nt学号t姓名t数学t英语t体育t平均分t数学均分t英语均分t体育均分);for(i=0;i3;i+)printf(t%dt%st%ft%ft%ft&lft%lft%lft%lf,stui.number,stui.name,stui.math,stui.English,stui.PE,stui.average,stui.Maverage,stui.Eaverage,stui.Paverage);./*删除一个链表中所有数值相等的点,只保留最前面一个*/#include#include#define NULL 0#define LEN sizeof(struct student)struct studentint num;float score;struct student *next;int n; /n为结点总个数,被定义为全局变量,可被其它函数所用/建立链表的函数如下struct student *creat(void)struct student *head,*p1,*p2;head=NULL; n=0;p1=p2=(struct student *)malloc(LEN); /p1和p2同时指向第一个结点scanf(t%d,%f,&p1-num,&p1-score);while(p1-num!=0)n=n+1;if(n=1)head=p1; /若p1所指为第一个结点,令头指针指向它,即链接到表头elsep2-next=p1;p2=p1;p1=(struct student *)malloc(LEN);scanf(t%d,%f,&p1-num,&p1-score);p2-next=NULL;free(p1);return(head);/删除链表中的结点的函数如下struct student *del(struct student *head)struct student *p1,*p2;if(head=NULL)printf(nThe list is NULL!n);return(head);p1=head;if(p1-next=NULL)printf(nThe list only has one node!n);float score;float nscore;while(p1!=NULL&p1-next!=NULL)score=p1-score;nscore=p1-next-score;if(nscore=score)p2=p1-next;p1-next=p1-next-next;free(p2);n=n-1;p1=p1-next;return(head);/*struct student *del(struct student *head)struct student *p1,*p2;if(head=NULL)printf(nThe list is NULL!n);return(head);p1=head;if(p1-next=NULL)printf(nThe list only has one node!n);float score;float nscore;while(p1!=NULL&p1-next!=NULL)score=p1-score;nscore=p1-next-score;if(nscore=score)*p2=p1-next;*/输出链表void print(struct student *head)struct student *p;printf(共有%d条学生记录,包括:n,n);p=head;while(p!=NULL)printf(t%dt%.1fn,p-num,p-score);p=p-next;void main()struct student *head;printf(请输入记录:n);head=creat();print(head);del(head);print(head);./*将一个链表逆置,即原链头作新链尾,原链尾作新链头*/#include#include#define NULL 0#define LEN sizeof(struct student)struct studentint num;float score;struct student *next;int n; /n为结点总个数,被定义为全局变量,可被其它函数所用/建立链表的函数如下struct student *creat(void)struct student *head,*p1,*p2;head=NULL; n=0;p1=p2=(struct student *)malloc(LEN); /p1和p2同时指向第一个结点scanf(t%d,%f,&p1-num,&p1-score);while(p1-num!=0)n=n+1;if(n=1)head=p1; /若p1所指为第一个结点,令头指针指向它,即链接到表头elsep2-next=p1;p2=p1;p1=(struct student *)malloc(LEN);scanf(%d,%f,&p1-num,&p1-score); /为什么省略%d和%f之间的逗号,输入信息点回车完全没有反应p2-next=NULL;free(p1);return(head);/输出链表void print(struct student *head)struct student *p;printf(共有%d条学生记录,包括:n,n);p=head;while(p!=NULL)printf(t%dt%.1fn,p-num,p-score);p=p-next;/链表逆置struct student *turn(struct student *head)struct student *p1,*p2;p1=head-next;head-next=NULL;while(p1)p2=p1;p1=p1-next;p2-next=head-next;head-next=p2;return(head);void main()struct student *head;printf(请输入记录:n);head=creat();print(head);turn(head);print(head);./*将两个链表连接起来,并按数据值升序排列*/#include#include#define NULL 0#define LEN sizeof(struct student)struct studentint num;float score;struct student *next;int n; /n为结点总个数,被定义为全局变量,可被其它函数所用/建立链表的函数如下struct student *creat(void)struct student *head,*p1,*p2;head=NULL; n=0;p1=p2=(struct student *)malloc(LEN); /p1和p2同时指向第一个结点scanf(t%d,%f,&p1-num,&p1-score);while(p1-num!=0)n=n+1;if(n=1)head=p1; /若p1所指为第一个结点,令头指针指向它,即链接到表头elsep2-next=p1;p2=p1;p1=(struct student *)malloc(LEN);scanf(%d,%f,&p1-num,&p1-score); /为什么省略%d和%f之间的逗号,输入信息点回车完全没有反应p2-next=NULL;free(p1);return(head);/输出链表void print(struct student *head)struct student *p;printf(共有%d条学生记录,包括:n,n);p=head;while(p!=NULL)printf(t%dt%.1fn,p-num,p-score);p=p-next;/比较两个链表中元素num的大小,并不断插入struct student *connect(struct student *p1,struct student *p2)struct student *p,*head;if(p1-numnum)p=p1;head=p;p1=p1-next;elsep=p2;head=p;p2=p2-next;while(p1!=NULL)&(p2!=NULL)if(p1-numnum)p-next=p1;p=p1;p1=p1-next;elsep-next=p2;p=p2;p2=p2-next;if(p1!=NULL)p-next=p1;elsep-next=p2;return(head);/链表排序函数如下(当要连接的两个链表为无序链表时) struct student *sort(struct st
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!