版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第1章 C+语言概述1填空题(1)函数说明,函数体(2)声明区,主程序区,函数定义区(3)多态性(4)namespace,using(5)std(6)cin,>>(7)/(8)对数据的操作2判断题(1)对(2)错(3)错(4)错(5)错3改错题(1)没有函数体,应改为void main() ;(2)语句没有分号,应改为using namespace myspace;(3)cout和操作符<<共同完成输出的功能,应改为cout<<" Input your name:"(4)应改为#include <iostream.h>4简答题
2、(略)5编程题(略)第2章 基本数据类型、运算符与表达式1 选择题(1)B(2)D(3)B(4)D(5)B2简答题(1)(a)、(c)(e)、(f)、(g)、(h)、(i)(2)(a)、(g)、(i)、(j)(3)(a)5.5(b)0(c)20(d)0,0(e)1(f)1,2(g)3(h)40(i)2(j)3(k)s1>='0'&&s1<='9'(l)N!=03读程序写结果(1)0,15(2)(1、1、1),(1、1、1)(3)(a)a*a+2*a*b+b*b(b)4.0/3.0*3.1415926*R*R*R(c)5.0/9.0*(
3、F-32)(d)b>=a&&b<=c(4) 364143(5)x=14 编程题/(1)编写程序,提示用户输入三角形的三条边长,判断该三角形是否为直角三角形,若是输出结果以及三角形面积。#include <iostream.h>void main() float a,b,c,side1,side2,area; /side1和side2为两条直角边长度 cout<<"please input length of three sides of the triangle: n" / 输出提示信息 cin>>a>&
4、gt;b>>c;/ 输入三角形三边长度if (a*a=b*b+c*c)|(c*c=a*a+b*b)|(b*b=a*a+c*c)/判断是否为直角三角形if (a*a=b*b+c*c)/判断三边中哪两条边为直角边,并存储到side1和side2中side1=b;side2=c;elseif (c*c=a*a+b*b)side1=a;side2=b;elseside1=a;side2=c;area=side1*side2/2;/计算直角三角形的面积cout<<"It is a right-angled triangle and the area is "&
5、lt;<area<<".n"/输出判断结果及直角三角形面积elsecout<<"It is not a right-angled triangle.n"/(2)编写程序,求解各种数据类型的存储长度并显示出来,在其中找出存储长度最大的和最小的两种数据类型并输出。#include <iostream.h>void main() int length7; int max=0,min=0; cout<<"data typetmemory used(bytes)" length0=sizeo
6、f(short int);/获取短整型长度 cout<<"nshort intt"<<length0<<"t" length1=sizeof(int);/获取整型长度 cout<<"ninteger t"<<length1; length2=sizeof(long);/获取长整型长度 cout<<"nlong integert"<<length2; length3=sizeof(char);/获取字符型长度 cout<<
7、"nchar t" <<length3; length4=sizeof(float);/获取单浮点型长度 cout<<"nfloat t"<<length4; length5=sizeof(double);/获取双浮点型长度 cout<<"ndouble t"<<length5; length6=sizeof(bool);/获取布尔型长度 cout<<"nbool t"<<length6<<endl; for(int i
8、=0;i<7;i+) if (lengthi>lengthmax)/求取长度最大的类型的存取位置max=i;if (lengthi<lengthmin)/求取长度最小的类型的存取位置min=i; cout<<"The longest length is from " switch (max) case 0:cout<<"short int."<<endl;break;case 1:cout<<"int."<<endl;break;case 2:cout<
9、;<"long."<<endl;break;case 3:cout<<"char."<<endl;break;case 4:cout<<"float."<<endl;break;case 5:cout<<"double."<<endl;break;case 6:cout<<"bool."<<endl;break; cout<<"The shortest leng
10、th is from " switch (min) case 0:cout<<"short int."<<endl;break;case 1:cout<<"int."<<endl;break;case 2:cout<<"long."<<endl;break;case 3:cout<<"char."<<endl;break;case 4:cout<<"float."<<
11、endl;break;case 5:cout<<"double."<<endl;break;case 6:cout<<"bool."<<endl;break; /(3)编写程序输入一个华氏温度,将其转换为摄氏温度并输出。#include <iostream.h>void main() float C;/变量C为摄氏温度float F; /变量F为华氏温度 cout<<"请输入华氏温度(float类型):n"cin>>F;/输入华氏温度C=(F-32)*
12、5/9;/华氏温度转换为摄氏度cout<<"转换为摄氏温度为 "<<C<<endl;/(4)编写程序输入一个十进制表示的正整数,将其转化为二进制表示并输出结果。#include <iostream.h>void main() int a;/变量C为摄氏温度int b20; /数组存储转换后的二进制数int i;i=0;cout<<"请输入一个十进制的数字:n"cin>>a;/输入十进制数while (a>0)/转换过程bi=a%2;i+;a=(int)(a/2);i-;cout
13、<<"转换的二进制数为: "while (i>=0)/输出二进制结果cout<<bi;i-;cout<<endl;第3章 C+的控制语句1选择题(1)B(2)A(3)A(4)C(5)A2判断题(1)错(2)对(3)对(4)错(5)错3读程序写结果(1) 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5(2) 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 (3)j的值为0;i的值为2;4 编程题/编写程序,计算1到100中所有3的倍数的数的和。#include <i
14、ostream.h>void main() int sum=0;/sum变量为3的倍数的和for(int i=1;i<=100;i+)if (i%3=0)/判断i是否是3的倍数sum+=i;cout<<"1到100中所有3的倍数的数的和为:"<<sum<<endl;/编写程序,用户输入一些整数,该程序分别计算出所有奇数和所有偶数之和,并输出它们。#include <iostream.h>void main() int sum_odd=0;/变量为所有奇数的和int sum_even=0;/变量为所有偶数的和int
15、b;/变量为输入的数字int N;/变量为数字个数cout<<"输入数字的总的个数为"cin>>N;cout<<"请输入数字(数字以空格隔开)"<<endl;for (int i=0;i<N;i+)cin>>b;if (b%2=0)/判断数字是否为偶数sum_even+=b;elsesum_odd+=b;cout<<"所有奇数的和为"<<sum_odd<<endl;cout<<"所有偶数的和为"<
16、<sum_even<<endl;/求解输入两个正整数的最大公约数和最小公倍数。#include <iostream.h>void main() int a,b;/输入的两个正整数int min,max;/最小值和最大值cout<<"请输入两个正整数(数字以空格隔开)"<<endl;cin>>a>>b;if (a>=b)/找出两个数中的最大值和最小值min=b;max=a;elsemin=a;max=b;/最大公约数一定不大于两个数中的最小值while (min>0)if (a%min=
17、0)&&(b%min=0) break;elsemin-;cout<<a<<"和"<<b<<"的最大公约数为"<<min<<endl;/最小公倍数一定不小于两个数中的最大值while (max%a!=0)|(max%b!=0)max+;cout<<a<<"和"<<b<<"的最小公倍数为"<<max<<endl;/求解输入两个正整数的最大公约数和最小公倍数。#
18、include <iostream.h>void main() int a,b;/输入的两个正整数int min,max;/最小值和最大值cout<<"请输入两个正整数(数字以空格隔开)"<<endl;cin>>a>>b;if (a>=b)/找出两个数中的最大值和最小值min=b;max=a;elsemin=a;max=b;/最大公约数一定不大于两个数中的最小值while (min>0)if (a%min=0)&&(b%min=0) break;elsemin-;cout<<
19、a<<"和"<<b<<"的最大公约数为"<<min<<endl;/最小公倍数一定不小于两个数中的最大值while (max%a!=0)|(max%b!=0)max+;cout<<a<<"和"<<b<<"的最小公倍数为"<<max<<endl;/输入4个字母,并反向显示这些字母。#include <iostream.h>void main() char a4;/输入4个字符c
20、out<<"请输入4个字符:"<<endl;for (int i=0;i<4;i+)cin>>ai;cout<<"反向输出4个字符为:"<<endl;for (i=3;i>=0;i-)cout<<ai;cout<<endl;/输出所有的"水仙花数"。/"水仙花数"是指一个3位数,其各位数字的立方和等于该数本身。#include <iostream.h>void main() int N,M;int a3;/存
21、储三位数的个十百位cout<<"水仙花数为:"<<endl;for (N=100;N<=999;N+)M=N;for (int j=0;j<3;j+)/提取数字的个十百位aj=M%10;M/=10;if (a0*a0*a0+a1*a1*a1+a2*a2*a2=N)cout<<N<<" "/求 1!+2!+50!。#include <iostream.h>void main() int sum1=1;/记录i!的结果int sum2=0;/记录i!累加的结果for (int i=1;i
22、<=50;i+)for (int j=1;j<=i;j+)/计算i!sum1*=j;sum2+=sum1;cout<<"1!+2!+50!="<<sum2<<endl; /编写程序求一元二次方程ax*x+bx+c=0的解。#include <iostream.h>#include <math.h>void main() float a,b,c;/方程的系数float s1,s2;/解float temp;cout<<"Input a,b,c:"<<endl;c
23、in>>a>>b>>c;if (a=0)cout<<"不是一元二次方程!"<<endl;elsetemp=b*b-4*a*c;if (temp<0)/无根的情况cout<<"无实根!"<<endl;else if (temp=0)/只有一个实根的情况s1=-b/(2*a);cout<<"方程有一个实根,为"<<s1<<endl;else/两个实根的情况s1=(-b+sqrt(temp)/(2*a);s2=(-b
24、-sqrt(temp)/(2*a);cout<<"方程有两个实根,为"<<s1<<"和"<<s2<<endl;/编写程序,用循环语句打印如下图案。#include <iostream.h>#include <math.h>#include <iomanip.h>void main() for (int i=1;i<=7;i+)cout<<setw(abs(4-i)+1)<<""/显示第一个if (i=1)|(i
25、=7)/第一行和最后一行仅显示一个cout<<endl;continue;for (int m=abs(4-i)+2;m<=6-abs(4-i);m+)/显示中间的*cout<<"*"cout<<""<<endl;/中间几行显示第二个/编写程序,输入年月日信息,并输出这一天为这一年的第几天,注意闰年问题。#include <iostream.h>void main() int year,month,day;int num=0;int length;cout<<"Inp
26、ut year:"cin>>year;cout<<"Input month:"cin>>month;cout<<"Input day:"cin>>day;for (int i=1;i<month;i+)if (i=1)|(i=3)|(i=5)|(i=7)|(i=8)|(i=10)|(i=12)/大月31天length=31;else if (i=4)|(i=6)|(i=9)|(i=11)/小月30天length=30;else if (year%100=0)&&(
27、year%4=0)|(year%100!=0)&&(year%4=0)/闰年判断条件length=29;/闰年的二月29天elselength=28;/非闰年的二月28天num+=length;num+=day;cout<<year<<"年"<<month<<"月"<<day<<"日为这一年的第:"<<num<<"天."<<endl;/编写程序,由用户输入x值,计算函数值并输出y。函数如下所示
28、:#include <iostream.h>void main() int x,y;cout<<"Input X(int):"cin>>x;if (x<0)/x<0的情况y=x;else if (x>10)/x>10的情况y=4*x*x*x-x*x;else/其余的情况y=x*x;cout<<"函数结果y为:"<<y<<endl;/鸡兔同笼问题。若鸡兔共有100只脚,利用循环计算鸡兔各几只。#include <iostream.h>void mai
29、n() int rabbit,chicken;for (rabbit=100/4;rabbit>=0;rabbit-)/兔子最多有25只,最少没有chicken=(100-rabbit*4)/2;cout<<"兔子有"<<rabbit<<"只,鸡有"<<chicken<<"只"<<endl;第4章 函数1 填空题(1)void(2)静态全局变量,static(3)函数重载(4)inline(5)递归函数(6)宏定义命令,文件包含命令,条件编译命令2判断题(1
30、)错(2)错(3)错(4)错(5)错(6)对(7)错(8)错(9)对(10)对3读程序写结果(1)x=7,y=4x=9,y=5(2)34.56b101(3)162228(4)12 15 18 21 24(5)2,1,4,1,3,2,1,4简答题(略)5编程题/编写一个函数,计算直角坐标系中点a(x0,y0)到点b(x1,y1)的距离。#include <iostream.h>#include <math.h>float length(float x0,float y0,float x1,float y1)/求两点距离的子函数return sqrt(x0-x1)*(x0-
31、x1)+(y0-y1)*(y0-y1);void main() float x0,x1,y0,y1;cout<<"Input x and y of a point:"cin>>x0>>y0;cout<<"Input x and y of b point:"cin>>x1>>y1;cout<<"The length from a to b is "<<length(x0,y0,x1,y1)<<endl;/求a!+b!+c!的值,其
32、中求n!要用一个函数实现,通过主函数输入a、b和c的值,并在主函数中输出计算的结果。#include <iostream.h>int factorial(int n)/求两点距离的子函数int sum=1;for (int i=1;i<=n;i+)sum*=i;return sum;void main() int a,b,c;cout<<"Input a,b,c:"cin>>a>>b>>c;cout<<"a!+b!+c!="<<factorial(a)+factor
33、ial(b)+factorial(c)<<endl;/编写一个函数。该函数读入一个整数,并判断这个整数是否为一个回文数字。例如4,44,434,4334,43534都是回文数字。#include <iostream.h>bool palindrome(int n)/判断n是否为回文的子函数int a20;int m=n;int i=0;int temp,num;while (m>0)/将数字的各位反向放置在数组a中ai=m%10;m=m/10;i+;temp=i/2;/需要比较的次数num=-i;/数字的最高位存储在anum中for (int j=0;j<t
34、emp;j+)if (aj!=anum-j) return false;/如果出现不相等的情况立即退出return true;/若比较的各个位置都相等,则为回文void main() int number;cout<<"Input the number:"cin>>number;if (palindrome(number)cout<<number<<" is a palindrome."<<endl;elsecout<<number<<" is not a pa
35、lindrome."<<endl;/编写一个程序,为选修3、4和5门课程的学生计算平均分,其中求平均分要用重载函数实现#include <iostream.h>float average(float a,float b,float c)/求三门课程平均分子函数return (a+b+c)/3;float average(float a,float b,float c,float d)/求四门课程平均分子函数return (a+b+c+d)/4;float average(float a,float b,float c,float d,float e)/求五门课
36、程平均分子函数return (a+b+c+d+e)/5;void main() int number;float s1,s2,s3,s4,s5;cout<<"The total of student's courses is "cin>>number;if (number=3)cout<<"Input three score of student:"cin>>s1>>s2>>s3;cout<<"The average is "<<a
37、verage(s1,s2,s3)<<endl;else if (number=4)cout<<"Input four score of student:"cin>>s1>>s2>>s3>>s4;cout<<"The average is "<<average(s1,s2,s3,s4)<<endl;elsecout<<"Input three score of student:"cin>>s1>>
38、;s2>>s3>>s4>>s5;cout<<"The average is "<<average(s1,s2,s3,s4,s5)<<endl;/用递归方法将一个整数n转换成字符串。#include <iostream>#include <string>using namespace std;string convert(int n);string numstring("");void main() int number;cout<<"In
39、put the number: "cin>>number;cout<<"converted string is "cout<<convert(number)<<endl;string convert(int n)/递归函数if (n/10=0)&&(n=0) return ""elsenumstring=convert(n/10).append(string(1,(n%10+48);/将各位数转换为字符后创建字符串,加到最后return numstring;/编写一个函数。该函数读
40、入一个整数,然后将这个整数上每个位的数字按照相反的顺序输出。/例如输入的整数为12345,输出结果为54321。#include <iostream.h>void reverse(int n)/反向显示整数nint a20;/存储n的各位,最多20位int m=n;int i=0;while (m>0)/提取n的各位ai=m%10;m=m/10;i+;cout<<"reversed number is "for (int j=0;j<=i-1;j+)/反向输出各位cout<<aj;cout<<endl;void m
41、ain() int number;cout<<"Input the number: "cin>>number;reverse(number);/输入三个数字,数字可以为整形或浮点型,分别编写函数来求解三个数字的最大值、最小值和平均值,要求在主函数中完成数字的输入和计算结果的输出。#include <iostream.h>#include <iomanip.h>float fmax,fmin,faverage;int max,min,average;void calculate(float fa,float fb,float f
42、c)/浮点型处理函数fmax=fa>=fb?fa:fb;fmax=fmax>=fc?fmax:fc;fmin=fa<=fb?fa:fb;fmin=fmin<=fc?fmin:fc;faverage=(fa+fb+fc)/3;void calculate(int a,int b,int c)/整型处理函数max=a>=b?a:b;max=max>=c?max:c;min=a<=b?a:b;min=min<=c?min:c;average=(a+b+c)/3;void main() int s1,s2,s3;float f1,f2,f3;int ty
43、pe;cout<<"Input the type: float(1) or int(0):"cin>>type;while (type!=0)&&(type!=1)/输入类型错误时再次输入cout<<"Please input 1 or 0:"cin>>type;cout<<"Input three numbers:"if (type=1)/浮点型数据cin>>f1>>f2>>f3;calculate(f1,f2,f3);c
44、out<<setw(8)<<"max"<<setw(8)<<"min"<<setw(12)<<"average"<<endl;cout<<setw(8)<<fmax<<setw(8)<<fmin<<setw(12)<<faverage<<endl;else/整型数据cin>>s1>>s2>>s3;calculate(s1,s2,s3)
45、;cout<<setw(5)<<"max"<<setw(8)<<"min"<<setw(8)<<"average"<<endl;cout<<setw(5)<<max<<setw(8)<<min<<setw(8)<<average<<endl;第5章 构造数据类型1选择题(1)C(2)D(3)A(4)B(5)C2判断题(1)错(2)对(3)对(4)错(5)错3读程序写结果
46、(1)153(2)422 5 6 8 10(3)65535,21(4)419(5)6904(6)432104 编程题/编写函数,完成指定二维数组(3*3)的转置,即行列对换。#include <iostream.h>#include <iomanip.h>void main()int i,j,temp,row=0,colum=0;int a33;cout<<"输入一个3×3的整型矩阵:"<<endl;for (i=0;i<=2;i+)/从第0行第2行for (j=0;j<=2;j+)/从第0列第2列cin
47、>>aij;/输入矩阵元素for (i=0;i<=2;i+)for (j=0;j<=i;j+)/将aij与aji的值互换temp=aij;aij=aji;aji=temp;for (i=0;i<=2;i+)for (j=0;j<=2;j+)cout<<setw(8)<<aij;/输入矩阵元素cout<<endl;/编写一个程序,要求当输入一个数字月份时,程序输出该月的英文名称。例如输入5时,程序输出"May",要求用指针数组实现。#include <iostream.h>#include &
48、lt;string>void main()/定义指针数组char *month12="January","February","March","April","May","June","July","August","September","October","November","December"int n;cout<<"In
49、put the month(1-12):"cin>>n;while (n>12)|(n<1)cout<<"Input the month(1-12):"cin>>n;cout<<"The month is "<<*(month+n-1)<<endl;/编写一个程序,要求分别输入5个学生的3科成绩,并输出平均成绩最高的学生的姓名及各科成绩。要求用结构体数组实现。#include <iostream.h>#include <string>st
50、ruct studentchar name20;/姓名 float course1;/第一科成绩float course2;/第二科成绩float course3;/第三科成绩float average;/平均成绩;struct student stu5;void main()float temp=-100.0;int j,k=0;char c;for (int i=0;i<5;i+)cout<<"Input NO."<<i+1<<" student's name: "cin>>stui.na
51、me;cout<<"Input NO."<<i+1<<" student's three scores: "cin>>stui.course1>>stui.course2>>stui.course3;/输入三科成绩stui.average=(stui.course1+stui.course2+stui.course3)/3;/求平均成绩if (temp<stui.average)temp=stui.average;/temp存放最高平均分k=i;/k存放该同学的序号co
52、ut<<"The student is "<<<<", three scores are "<<stuk.course1<<", "<<stuk.course2<<" and "<<stuk.course3<<endl;/编写一个程序,实现方程式的相加。#include <iostream.h>#include <string>struct coefficientbo
53、ol pn;int num;a3,b3,c3,d3;void main()char equation250,ch,z8;int m,j; cout<<"Input NO. 1 Equation:"<<endl;cin>>equation0;cout<<"Input NO. 2 Equation:"<<endl;cin>>equation1;for (int i=0; i<2;i+)j=0;ch=equationi0;if (ch='-') ai.pn=false
54、;j+;ch=equationij;elseai.pn=true;if(ch>='0'&&ch<='9') /* 提取a的系数 */ m=0; do zm=ch; j+; m+; ch=equationij; while(ch>='0'&&ch<='9'); zm=0; if (ai.pn) ai.num=atoi(z); else ai.num=-1*atoi(z);j+;ch=equationij;if (ch='-') bi.pn=false;elseb
55、i.pn=true;j+;ch=equationij;if(ch>='0'&&ch<='9') /* 提取b的系数 */ m=0; do zm=ch; j+; m+; ch=equationij; while(ch>='0'&&ch<='9'); zm=0; if (bi.pn) bi.num=atoi(z); else bi.num=-1*atoi(z);j+;ch=equationij;if (ch='-') ci.pn=false;elseci.pn=t
56、rue;j+;ch=equationij;if(ch>='0'&&ch<='9') /* 提取c的系数 */ m=0; do zm=ch; j+; m+; ch=equationij; while(ch>='0'&&ch<='9'); zm=0; if (ci.pn) ci.num=atoi(z); else ci.num=-1*atoi(z);j+;j+;ch=equationij;if (ch='-') di.pn=false;j+;ch=equationij;elsedi.pn=true;if(ch>='0'&&ch<='9') /* 提取=右侧的常数 */ m=0; do zm=ch; j+; m+; ch=e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 资深律师执业技巧与面试策略
- 农业农村内部制度
- 制造型企业内部控制制度
- 同一集团内部转账制度
- 员工内部激励奖惩制度范本
- 品管内部奖励制度
- 商会内部销售制度
- 四大内部等级制度
- 团队内部人员管理制度
- 外汇制度内部考核办法
- +第6课+全球航路的开辟【知识精讲精研】高中历史统编版2019必修中外历史纲要下册
- 《跨境电商数据分析与应用》 课程标准
- 智能健康管理系统
- 国家安全概论-西安交通大学中国大学mooc课后章节答案期末考试题库2023年
- 检验检测机构资质认定评审准则释义
- GB/T 39489-2020全尾砂膏体充填技术规范
- GB/T 14598.301-2010微机型发电机变压器故障录波装置技术要求
- GB 30526-2019烧结墙体材料和泡沫玻璃单位产品能源消耗限额
- GA 139-2009灭火器箱
- 2023年江苏专转本计算机真题及答案
- 部编版小学道德与法治五年级下册第1课《读懂彼此的心》课件
评论
0/150
提交评论