




已阅读5页,还剩31页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
教材练习题参考答案第一章1、#include void main()cout*n;cout 江苏科技大学欢迎您! n;cout*n;2、(1)#include void main()int a=4,b=5,c=6,z;z=a+bc&b=c|+b+a ;cout 表达式的值为: zendl;cout a=a b=b c=cendl;运行结果:表达式的值为: 1 a= 5 b= 6 c=6(2)#include void main()int a=4,b=5,c=6,z;z= c-=a+=b*=10-a ;cout 表达式的值为: zendl;cout a=a b=b c=cendl;运行结果:表达式的值为: -28 a= 34 b= 30 c=-28(3)#include void main()int a=4,b=5,c=6,z;z= a=b+ + -c + b = c ;cout 表达式的值为: zendl;cout a=a b=b c=cendl;运行结果:表达式的值为: 1 a= 1 b= 6 c=5(4)#include void main()int a=4,b=5,c=6,z;z=( b=a+b, a=b-a, b=b-a , a+b+c ) ;cout 表达式的值为: zendl;cout a=a b=b c=cendl;运行结果:表达式的值为: 15 a= 5 b= 4 c=63、#include void main()int a;cout 输入一个数: a;couta 的十位数是: a/10endl;couta 的个位数是: a%10endl;4、#include void main()int a,b,c,v,aver;cout 输入三个数: abc;v=a*a+b*b+c*c;aver=(a+b+c)/3;cout 三个数的平方和是: vendl;cout 三个数的平均值是: averendl;第二章1、#include void main()int a,b,c,t;cout 输入三个数: abc;if(ab)t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(bc)t=b;b=c;c=t;coutatbtcendl;2、#include void main()float x,sum=0.0;int i=1;while(ix;sum=sum+x;i=i+1;cout平均值是: sum/10endl;3、#include void main()double y=0.0,t=1.0;int n;cinn;for(int i=1;i=n;i+) t=t*i; y=y+t;cout n= n y= yendl;4、#include void main() int count=0;for(int n=200;n1000;n+) for(int i=2;in-1) coutit; count+; coutncount=countendl;5、#include void main() int a,n,sum=0,c=0;cout 输入一个数: a; n=a; do sum+=n%10; n/=10; c+; while(n); couta的所有数字之和为:sumendl; couta 是 c 位数 endl;6、#include void main() int a,b,i;for(i=1;i=999;i+) a=i%10; b=i%100; if(i=a*a | i=b*b) coutiendl; 7、#include #include void main() double a,b,n; for(n=10000;n=99999;n+) a=n*10+6; b=6*100000+n; if(fabs(b-4*a)1e-5) coutaendl;break;第三章/1. 一维数组的输入和输出#includevoid main()int a10,k=1;for(int i=0;i10;i+)cout请输入第k+个元素aiai;cout数组a为:n;for(i=0,k=0;i10;i+)k+;coutait;if(k%5=0)coutn;coutn;/2. 通过指针输出一维数组中值为奇数元素#includevoid main()int a10=3,4,5,8,9,11,14,16,19,21,*p=a,k=0;cout数组a中的奇数有:n;for(int i=0;i10;i+)if(pi%2)k+;cout*(p+i)t;if(k%5=0)coutn;coutn;/3. 产生一个随机的二维数组b45,并按矩阵的方式输出#include#includevoid main()int b45;cout产生的数组为:n;for(int i=0;i4;i+)for(int j=0;j5;j+)bij=rand();coutbijt;coutn;coutendl;/4. 二维数组的平方赋值#includevoid main()int a34=3,1,5,2,4,1,7,5,3,6,9,8,b34;cout数组a为:n;for(int i=0;i3;i+)for(int j=0;j4;j+)coutaijt;bij=aij*aij;coutendl;coutendl;cout数组b为:n;for(i=0;i3;i+)for(int j=0;j4;j+)coutbijt;coutendl;coutendl;/5. 小写字母改写为大写字母#includevoid main()char str100;cout=a&stri=z)stri-=a-A;cout转换后的字符串为:n;coutstrn;/6. 通过指针将字符串逆序#include#includevoid main()char str=abc321,*s1=str,*s2,ch;cout原字符串为:ns1endl;s2=str;/Awhile(*s2)s2+;s2-;/Bwhile(s1s2)ch=*s1;*s1=*s2;*s2=ch;s1+;s2-;cout逆序后的字符串为:nstrendl;/*其中A到B行可改为:s2=str+strlen(str)-1;*/第四章习题答案4-1#include void swap(int *p1,int *p2)int t;t=*p1;*p1=*p2;*p2=t;void main()int a,b,c;int *s1,*s2,*s3;coutabc;s1=&a;s2=&b;s3=&c;if(ab)swap(s1,s2);if(ac)swap(s1,s3);if(bc)swap(s2,s3);cout输出排好序的三个数:;coutatbtcendl;4-2#include void trans(int,int);void main()int n,sel;coutn;docoutsel;switch (sel)case 2:trans(n,2);break;case 8:trans(n,8);break;case 16:trans(n,16);break;case 32:trans(n,32);break;case 0:break;default:cout进制输入错误!endl;while(sel!=0);void trans(int n,int base)int c;if(n=0)return;c=n%base;trans(n/base,base);if(c=9)coutct;elsecoutchar(c-10+A)t;4-3#include #include #include void fun(int b,int n)for(int i=0;in-1;i+) /若不是素数,将其值置为0for(int j=i+1;jn;j+)if(bi!=0&bj!=0)if(bj%bi=0)bj=0;void main()int a210=0,n=0,k=5;for(int i=0;i48;i+) /给数组赋初值ai=k,k+=2;fun(a,48);for(i=0;i49;i+)if(ai!=0)coutsetw(6)ai;n+;if(n%5=0)coutendl;coutendl;4-4#include int gcd(int,int);int lcm(int,int);void main()int num1,num2,den1,den2,a,l,g;cinnum1den1num2den2;coutnum1/den1 , num2/den2n;cout求分母的最小公倍数: ;l=lcm(den1,den2);coutln;cout两分数的和为:; a=(l/den1)*num1+(l/den2)*num2;couta/ln;cout求分子分母的最大公约数:;g=gcd(a,l);coutgn;cout输出最简分数:;couta/g/l/g=1;i-)if(!(a%i)|(b%i)break;return (i);int lcm(int a,int b)int i;i=b;while(i%a)i+=b;return(i);4-5#include #include double sin(double x,double eps)double term,sum,y;int n=1; /n=0y=x*x;term=x;sum=0;while(fabs(term)=eps)sum+=term; n+;term=term*y/(2*n-2)*(2*n-1); /(2*n)*(2*n-1)term*=-1; return sum;void main()double x,y;coutx;while(x360)x-=360;y=3.1415926*x/180;cout度数为:x其sin值为:sin(y,1e-6)n;4-6#include #include float solut(float a,float b,float c,float d)float x=1,x0,f,f1;dox0=x;f=(a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c; /对f函数的导数x=x0-f/f1;while(fabs(x-x0)=1e-3);return x;void main()float a,b,c,d;coutabcd;coutax3+bx2+cx+d=0endl;coutx=solut(a,b,c,d)endl;4-7#include #include prime(int n) int i=2; int flag=1; for(;in/2&flag=1;i+) if (n%i=0) flag=0; return(flag); void main() int m,j,k,n=0; for(m=6;m=100;m+=2) for(j=3;j=m/2;j+=2)if (prime(j) k=m-j; if (prime(k) coutsetw(4)m=j+k; if (+n%5=0) coutendl; if(m=j+k)break; coutendl; 4-8#include void f1(int n)int j=n%10;if(n/10)f1(n/10);coutjt;void f2(int n)coutn%10t;if(n/10)f2(n/10);void main()int x;coutx;cout输入的整数为:xn;cout该整数的反序输出为:;f2(x);coutn;cout该整数的正序输出为:;f1(x);coutn;第五章 习题参考解答11 3 12 3 11 3 22 3 21 3 32 3 31 2 32正确程序之一为:#include #define SQR(x) (x)*(x)void main()int counter;for(counter=0;counter5;+counter)coutx=(counter+1) x的平方为SQR(counter+1)n;3#define true 1#define false 0#define ok 2#define error 1#define overflow -24#define IFF(x) if(x%10=0)return true;else retrue false;5#define EXCHANGE(x,y) x=x+y;y=x-y;x=x-y;第六章1.程序如下#include struct student float CPPscore; float eng; ;void main()student s1; float sum;cins1.CPPscores1.eng;sum=s1.CPPscore+s1.eng; coutsum;2.程序如下# include # include struct score char name8 ; int no ; float c, english, maths, average ; ; score input(score st) ; float average( float a, float b, float c ) ; void sort(score st, int n) ; void print (score st, int n) ; void main() score student30 ; for (int i=0 ; i5 ; i+) studenti=input(studenti) ; sort(student, 5) ; print(student, 5) ; score input(score st) ; coutst.no ; coutst.c ; coutst.english ; coutst.maths ; coutendl ; st.average=average(st.c, st.english, st.maths) ; return st ; float average( float a, float b, float c ) return (a+b+c)/3 ; void sort(score st, int n) score temp ; for ( int i=0 ; in-1 ; i+) for ( int j=i ; jn ; j+ ) if (sti.average stj.average )temp=sti ; sti=stj ; stj=temp ; void print (score st, int n) coutNo. 姓名 平均 C+ 英语 数学 endl ; for ( int i=0 ; in ; i+) coutsti.no ; ; couttsti.average ; couttsti.c ; couttsti.english ; couttsti.mathsendl ; 3.程序如下#include struct student long int num;float CPPmid; float CPPend;float ave;void main()student s3;student *p; for(p=s;ps+3;p+) coutp-num; coutp-CPPmid; coutp-CPPend; p-ave=(p-CPPmid+p-CPPend)/2;coutNo. 平均 C+期中 C+期末 endl ;for(p=s;ps+3;p+) coutnum ; couttave; couttCPPmid ; couttCPPend; coutnext;while(p)s+=p-data;p=p-next;return s;5.程序如下#includestruct node int data;node *next; ;node *create( ) node *head ; /头指针node *p , *pend ; int a; couta; head=0 ; while(a!=0) p=new node ; p-data=a ; if(head=0) head=p ; pend=p ; else pend-next=p ; pend=p ; couta; if(head) pend-next=0 ; return head ; node * invert(node *head)node *p,*q;p=head-next;if(p!=NULL)head-next=NULL; do q=p-next;p-next=head; head=p; p=q; while(p!=NULL);return head;void print(node *head ) if(head=0) cout 链表为空!n ; return ; node *p=head ; cout链表上各个结点的值为:n;while(p!=0) cout datanext ; void release(node *head ) if(head=0) coutnext ; delete p; coutn结点空间释放完毕!;void main( )node *head;head=create( );print(head);head=invert(head);coutn逆置后;print(head);release(head);6.程序如下#include struct Node int x; /围成一圈时,人的序号 Node *next;Node * DelNode(Node *head, int m) /依次输出环形链表中凡报到m者的序号Node *p;int count;if(head=NULL)return head;while( head != head-next) /直到链表上只有一个结点为止 count=0;while(count next;p=head-next; /删除p所指向的结点head-next = p-next;head = head-next ; coutxx = 1;head-next = NULL;p=head;for(i=2; inext = new Node; /新结点加入链尾p =p-next;p-x = i;p-next =head; / 构成循环链head = DelNode(head, 5); cout 最后的一个人为: x datadata)newHead=h1;p=h2;elsenewHead=h2;p=h1;node *q= newHeadwhile(q-next)if(q-next-datadata)q=q-next;elsep1=q-next;q-next=p;q=p;p=p1;q-next=p;return head;8.程序如下#include struct Nodechar c; int count; Node * next;Node *search(Node *head, char ch) Node *p;p=head; while(p) if(p-c=ch) p-count+; break; p=p-next;if(p=0) p=new Node; p-c=ch; p-count=1; if(head) p-next=head; else p-next=0; head=p; return head; void print(Node *head) while(head)cout字符:c t出现count次next; void dele(Node *head) Node *p;while(head) p=head; head=head-next; delete p; void main(void) Node *h=0; char s300,*p=s; char c;cout输入一行字符串:; cin.getline(s,300);while( c=*p+ )h = search(h,c);print(h);dele(h);第七章 1#include class Cint m,n;int c;public:C(int x,int y);int f(int n);void fun();void print();C:C(int x,int y)n=m=x,n=y;int C:f(int n)int s=1;for(int i=n;i=1;i-)s*=i;return s;void C:fun()c=f(m)/(f(n)*f(m-n);void C:print()coutm为:m n为:nn;cout组合数为:;coutcn;void main()coutmn;if(mn)C a(m,n);a.fun();a.print();else cout输入数据有误!;2#include class Num int a5; int n;public: Num(int5); void fun(); void show();Num:Num(int b5)for(int i=0;i5;i+)ai=bi;n=0;void Num:fun()for(int i=0;i5;i+)n=n*10+ai;void Num:show()for(int i=0;i5;i+)coutai;coutn;coutnn;void main()int b5;cout请输入5个数字:;for(int i=0;ibi;Num m(b);m.fun();m.show();3#include #include class String char a100,b100; int n;public:String(char c,char d,int m);/构造函数,以参数初始化数据成员; void fun();/功能函数,将b插入a中第n个字符处; void show ();/功能函数,输出数据成员。;String:String(char c,char d,int m)int s=strlen(c),k=strlen(d),i;n=m;for(i=0;i=s;i+)ai=ci;for(i=0;i=n-1;i-)ai+k=ai;for(i=n-1,j=0;in-1+k;i+,j+)ai=bj;void String:show()coutan;coutbn;void main()char c100,d100;cout请输入两个字符串:;cin.getline(c,100);cin.getline(d,100);coutn;String aa(c,d,n);aa.show();aa.fun();aa.show();4#include class An float a0,d;/分别为首项和公差 int n;/项数 float *pa,s;/分别存放数列前n项及前n项的和public:An(float b0,float c,int m);/构造函数:根据给定的数据初始化成员数据; void setdata( ); /求前n项及前n项的和; void show(); /输出所有数据成员; An(); /析构函数。;An:An(float b0,float c,int m)a0=b0;d=c;n=m;pa=new floatn+1;s=0;void An:setdata()pa0=a0;s=pa0;for(int i=1;in;i+)pai=pai-1+d;s+=pai;void An:show()float *p=pa;for(int i=0;in;i+)coutpin;An:An()if(pa)deletepa;void main()float b0,c;int m;coutb0c;coutm;An aa(b0,c,m);aa.setdata();aa.show();5#include #include class String char *str;/存放含数字字符的字符串 int len;/成员字符串的长度public:String(char* ptr); /构造函数,根据给定的字符串初始化成员数据; char getc(int n);/根据给定的序号取成员字符串中的第n个字符; void setc(int n,char c);/根据给定的序号把成员字符串中的第n个字符修改为c void setlen();/求成员字符串的长度 void show();/输出数据成员。 String();String:String(char* ptr)str=new charstrlen(ptr)+1;strcpy(str,ptr);len=strlen(ptr);char String:getc(int n)return strn-1;void String:setc(int n,char c)strn-1=c;void String:setlen()len=strlen(str);void String:show()coutstrn;String:String()if(str)deletestr;void main()cout请输入一个字符串:;char ptr100;cin.getline(ptr,100);String ss(ptr);ss.show();coutn;ss.getc(n);coutc;ss.setc(n,c);ss.show();6#include class Array int (*a)5;/ 表示二维数组 float ave;/ 二维数组的平均值public:Array(int b5,int n); /构造函数,用n行5列的数组初始化成员
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人格心理考试题目及答案
- 2025年外科专科考试试题及答案
- 即兴评述高考试卷及答案
- 专职网格员考试题及答案
- 音乐考试科普知识题及答案
- 恐龙派对课件
- 江苏太仓中考试题及答案
- 急诊临床知识培训内容课件
- 沈阳消防考试题目及答案
- 软技能培训考试题及答案
- 盘磨机-设计计算说明书陈少康
- 比亚迪秦EV新能源汽车空调系统
- 商场全套安全生产管理制度
- 《教育系统重大事故隐患判定指南》知识培训(三)
- 2025年安徽高中学业水平合格性考试化学试卷试题(含答案详解)
- 《低能耗建筑多排孔自保温砌块墙体体系应用技术规程》
- 【五年级】语文上册课课练
- 公路工程质量控制要点及质量通病防治手册
- 防御台风复盘工作情况报告
- 拼音卡片(四线三格)
- 省级临床重点专科建设项目神经内科重点专科建设实施方案
评论
0/150
提交评论