c程序设计实验报告册参考答案
实验一
程序清单:
1.
// First c++ program
#include
using namespace std;
int main()
{
cout<<"Hello world!"< } 2. #include using namespace std ; int main() { cout<<”*”< cout<<”***”< cout<<”*****”< } 3. #include using namespace std; int main() { int i,j,k; cin>>i>>j; k=i*j; cout<<"i*j="< } 实验二 程序清单: 3. #include using namespace std; int main() { int x; cout<<"输入一个整数:"; cin>>x; cout<<"\n 此数的平方数是:"< cout<<" 此数的立方数是:"< cout< return 0; } 4. #include using namespace std; int main() { int n,x; cout<<"请输入一个三位以上的整数:"; cin>>n; x=(n/10)%10; cout<<"该数的十位数字为:"< } 5. #include using namespace std; int main() { int n,a,b,c,d,e; cout<<"请输入一个三位正整数: "; cin>>n; a=n/100; // 得到百位 b=(n/10)%10; // 得到十位 c=n%10; // 得到个位cout< return 0; } 实验三 程序清单 1. 方法一: #include #include using namespace std; int main ( ) { float h,r,l,s,sq,vq,vz; const float pi=3.1415926; cout<<"please enter r,h:"; cin>>r>>h; l=2*pi*r; s=r*r*pi; sq=4*pi*r*r; vq=4.0/3.0*pi*r*r*r; vz=pi*r*r*h; cout< cout<<"l= "< cout<<"s= "< cout<<"sq="< cout<<"vq="< cout<<"vz="< return 0; } 方法二: #include using namespace std; #define PI 3.14 void main() { double xiaoshu(double x); double r=1.5,h=3,c,s,S,v,sv; cout<<"请输入圆半径,输入圆柱的高: "< cin>>r>>h; c=2.0*PI*r; s=2.0*PI*r*r; S=4.0*PI*r*r; v=4.0/3*PI*r*r*r; sv=PI*r*r*h; cout<<"圆的周长为:"< cout<<"圆的面积为:"< cout<<"圆球的表面积为:"< cout<<"圆球的体积为:"< cout<<"圆柱的体积"< } double xiaoshu(double x) { int a; a=x*100; x=a/100.0; return x; } 2. #include using namespace std; void main() { double c,f; cout<<"请输入一个华氏温度"< cin>>f; c=5.0/9*(f-32); c=int(c*100+0.5)/100.0; //取2位小数,并对第3位四舍五入cout<<"华氏温度转化成摄氏温度为:"< } 3. #include using namespace std; int main ( ) { int x,y; cout<<"please enter x:"; cin>>x; if (x<1) { y=x; cout<<"x="< } else if (x<10) // 1≤x<10 { y=2*x-1; cout<<"x="< } else // x≥10 { y=3*x-11; cout<<"x="< } cout< return 0; } 注:三组检验数据:-1,5,12 4. 方法一: #include using namespace std; int main () { float score; char grade; cout<<"please enter score of student:"; cin>>score; while (score>100||score<0) //保证输入一个合理的数据{ cout<<"data error,enter data again."; cin>>score; } switch(int(score/10)) { case 10: case 9: grade='A';break; case 8: grade='B';break; case 7: grade='C';break; case 6: grade='D';break; default:grade='E'; } cout<<"score is "< return 0; } 方法二: #include using namespace std; int main () { float score; char grade; cout<<"请输入一个0到100之间的成绩:"; cin>>score; while (score>100||score<0) { cout<<"data error,enter data again."; cin>>score; } if(score>=90) grade='A'; else if(score>=80) grade='B'; else if(score>=70) grade='C'; else if( score>=60) grade='D'; else grade='E'; cout<<"score is "< return 0; } 5. #include using namespace std; void main() { long i=1,sum=0; for(int j=1;j<=15;j++) { i=i*j; sum=sum+i; } cout<<"1!+2!+...+15!="< } 6. #include using namespace std; void main() { int i,a,b,c; cout<<"100到999以内的\"水仙花数\"为:\n"; for(i=100;i<=999;i++) { a=i/100; //计算百位数字 b=(i-a*100)/10; //计算十位数字 c=i%10; //计算个位数字 if (i==a*a*a+b*b*b+c*c*c) cout< } } 7. #include using namespace std; void main() { int i=0,x; bool key=false; cout<<"请输入一个6位数字的密码: "; cin>>x; for(i=1;i<=3;i++) { if(x==111111) { key=true; break; } cout<<"密码错,请重新输入: "; cin>>x; } if(key) cout<<"欢迎使用财务报表软件!\n"; else cout<<"拒绝使用财务报表软件!\n"; } 8 * *** ***** ******* ***** *** * 方法一: #include using namespace std; int main() { int i,k; for (i=0;i<=3;i++) // 输出上面4行*号 { for (k=0;k<=2*i;k++) cout<<"*"; // 输出*号 cout< for (i=0;i<=2;i++) // 输出下面3行*号 { for (k=0;k<=4-2*i;k++) cout<<"*"; // 输出*号 cout< } return 0; } 方法二: #include #include using namespace std; int main() { int i,k; for (i=-3;i<=3;i++) // 控制行数 { for (k=1;k<=7-2*abs(i);k++) // 控制*号个数 cout<<"*"; cout< return 0; } 9. #include using namespace std; void main() { char x; float total=0,sum,m; int p=0; cout<<"请输入营业员的密码:"; cin>>x; if(x=='1'||x=='2'||x=='3'||x=='4'||x=='5') //对输入的密码进行判断符合进入do //统计销售额 { sum=0; cout<<"请分别输入用户购买商品的价格:"< for(int i=1;i<=10000;i++) { cout<<"第"< cin>>m; //输入一个商品价格 if(m>1000||m==0) //当价格为0或超过1000时结束统计 break; sum=sum+m; //把输入的数(商品价格)进行相加运算} cout< cout<<" 请付款为:"< cout< p++; //对顾客(人数)进行累加 total=total+sum; //把所有的顾客消费进行累加}while(m); else cout<<"请退出收银台,谢谢!"; cout<<"今天商店的营业额为:"< cout<<"今天接待消费客人共:"< } 实验四 程序清单 1.写一个判别素数的函数 #include using namespace std; int main() { int prime(int); /* 函数原型声明*/ int n; cout<<"input an integer:"; cin>>n; if (prime(n)) cout< else cout< return 0; } int prime(int n) { int flag=1,i; for (i=2;i if (n%i==0) flag=0; return(flag); } 2. 编写一个函数divides,带两个整型参数,如果第二个整数能整除第一个整 数(即余数为0),则返回true,否则返回false。 #include using namespace std; void main() { bool divides(int a,int b); /* 函数原型声明*/ int a,b; cout<<"输入两个整数:"; cin>>a>>b; cout< } bool divides(int a,int b) { if(a%b==0) return true; else return false; } 3. 设计一个可以计算阶乘的函数,由用户输入n的值,计算出: 1!+2!+3!+4!+ ……+n! 的值并打印出来 #include using namespace std; void main() { long sum=0; int i,n; cout<<"please input:"< cin>>n; for(i=1;i<=n;i++) sum=sum+fen(i); cout<<"1!+2!+...+"< } long fen(int n) { int j; long f=1; for( j=1;j<=n;j++) f=f*j; return f; } 4. 编写一个函数,取整数值并返回将数字反序的数值,例如输入5432,函数返 回2345。 #include using namespace std; void main() { int rev(int x); int a; cout<<"输入一个整数"; cin>>a; cout< } int rev(int x) { int y=0,a; while(x!=0) { a=x%10; y=y*10+a; x=x/10; } return y; } 5. 完成第4章第12题所要求的程序设计,上机运行并分析其运行结果。 #include #include using namespace std; #define S(a,b,c) (a+b+c)/2 #define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)) int main() { float a,b,c; cout<<"input a,b,c:"; cin>>a>>b>>c; if (a+b>c && a+c>b && b+c>a) cout<<"area="< else cout<<"It is not a triangle!"< return 0; } 6.设计一个重载函数area ,用于求出长方形和圆形的面积,其中长方形的长和宽、圆的半径由用户从键盘输入。 (略) 实验五 程序清单 1. 设计一程序,有一个数组,内放10个整数,找出最小的数和它的下标,然后 把它和数组中最前面的元素对换。 #include using namespace std; void main() { int a[10]; int i,b,min,temp; for( i=0;i<10;i++) { cout<<"请输入第"< cin>>a[i]; } min=a[0],b=0; for(i=0;i<10;i++) if(a[i] { min=a[i]; b=i; } { temp=a[0]; a[0]=min; a[b]=temp; } for( i=0;i<10;i++) cout< cout< } 2.编输入一个n×n的矩阵,求出两条对角线元素值之和。 #include using namespace std; int a[100][100]; void main() { int i,j,n,sum=0,sum1=0,sum2=0; cout<<"请给n*n矩阵输入n的值!!!"< cin>>n; for( i=0;i { cout<<"请给"< for(j=0;j cout<<"请输入第"< cin>>a[i][j]; } cout< } for(i=0;i { for(j=0;j cout< cout< } for(i=0;i sum1=sum1+a[i][i]; for(i=0,j=n-1;(i<=n)&&(j>=0);i++,j--) sum2+=a[i][j]; sum=sum1+sum2; if(n%2!=0) sum=sum-a[n/2][n/2]; cout<<"第一条对角线上数的和为:"< cout<<"\n两条对角线的和为:"< } 3.编写一程序,将两个字符串连接起来,不用strcat函数。 #include using namespace std; void main() { char a[80],b[80]; int i=0,j=0; cout<<"请输入两个字符串:"; cin>>a>>b; while(a[i]!='\0') i++; while(b[j]!='\0') a[i++]=b[j++]; a[i]='\0'; cout< } 4. 完成第5章第9题所要求的程序设计,上机运行并分析其运行结果。 要求:设计两个函数,一个用于判断闰年,一个用于计算日期。 #include using namespace std; int main() { int sum_day(int,int); int leap(int year); int year,month,day,days=0; cout<<"input date(year,month,day):"; cin>>year>>month>>day; cout< days=sum_day(month,day); /* 调用函数一*/ if(leap(year) && month>=3) /* 调用函数二*/ days=days+1; cout<<" is the "< return 0; } int sum_day(int month,int day) //计算日期 { int i; int day_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for (i=0;i day+=day_tab[i]; return(day); } int leap(int year) //判断是否为闰年 { int leap; leap=year%4==0&&year%100!=0||year%400==0; return(leap); } 5. 完成第5章第16题所要求的程序设计,上机运行并分析其运行结果。 要求:(1)用字符数组的方法实现; (2)用string方法。 (1)用字符数组的方法 #include using namespace std; int main() { const n=10; int i; char a[n],temp; cout<<"please input a string:"; for(i=0;i cin>>a[i]; for(i=0;i { temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp; } for(i=0;i cout< cout< return 0; } 5.(2) string方法 #include #include using namespace std; int main() { string a; int i,n; char temp; cout<<"please input a string:"; cin>>a; n=a.size(); for(i=0;i { temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp; } cout< return 0; } 实验六 程序清单 1. 完成第6章第2题所要求的程序设计,上机运行并分析其运行结果。方法一:字符数组 #include #include using namespace std; int main() { void swap(char *,char *); char str1[20],str2[20],str3[20]; cout<<"input three line:"< gets(str1); gets(str2); gets(str3); if(strcmp(str1,str2)>0) swap(str1,str2); if(strcmp(str1,str3)>0) swap(str1,str3); if(strcmp(str2,str3)>0) swap(str2,str3); cout< cout< return 0; } void swap(char *p1,char *p2) /* 交换两个字符串*/ { char p[20]; strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); } 方法二:字符串变量 #include #include using namespace std; int main() {void change(string &,string &); string str1=" ",str2=" ",str3=" "; char *p1=&str1[0],*p2=&str2[0],*p3=&str3[0]; cout<<"input three line:"< gets(p1); gets(p2); gets(p3); if(str1>str2)change(str1,str2); if(str1>str3)change(str1,str3); if(str2>str3)change(str2,str3); cout< cout< return 0; } void change(string &st1,string &st2) /* 交换两个字符串*/ { string st; st=st1;st1=st2;st2=st; } 2. 设计一程序,用引用变量作为形参,实现3个整数由小到大排序。 #include using namespace std; void main() { void swap1(int &x,int &y); int a,b,c; cout<<"输入三个整数: "; cin>>a>>b>>c; if(a>b) swap1(a,b); if(a>c) swap1(a,c); if(b>c) swap1(b,c);