面向对象的程序设计语言——C第二版陈志泊 主编 王春玲 孟伟编着 第2到7章课后编程题答案29页
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C++第二单元课后编程题
//第一题
#include <iostream.h>
void main()
float a,b,c;
cout<<"请输入三角形的三条边长:"<<endl;
cout<<"\ta=";
cin>>a;
cout<<"\tb=";
cin>>b;
cout<<"\tc=";
cin>>c;
if(a<=0||b<=0||c<=0||(a+b<=c)||(a+c)<=b||(b+c<=a))
cout<<"此三条边不能组成三角形!"<<endl;
else
{if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
{cout<<"此三角形是直角三角三角形!"<<endl;
if(a>b&&a>c)
cout<<"此直角三角形的面积是:S=" <<b*c/2<<endl;
else if(b>a&&b>c)
cout<<"此直角三角形的面积是:S=" <<a*c/2<<endl;
else cout<<"此直角三角形的面积是:S=" <<a*b/2<<endl;
else cout<<"此三角形不是直角三角形!"<<endl ;
//第二题
#include <iostream.h>
void main()
int length[8],a[8],b[8] ;
int max=0,min=0,m=0,n=0;
cout<<"data type\tmemory used(bytes)(各种数据类型的存储长度是:)";
length[0]=sizeof(short int); //获取短整型长度
cout<<"\nshort int\t"<<length[0];
length[1]=sizeof(int); //获取整型长度
cout<<"\nint \t"<<length[1];
length[2]=sizeof(long int); //获取长整型长度
cout<<"\nlong int \t"<<length[2];
length[3]=sizeof(char); //获取字符型长度
cout<<"\nchar \t" <<length[3];
length[4]=sizeof(float); //获取单浮点型长度(单精度)
cout<<"\nfloat \t"<<length[4];
length[5]=sizeof(double); //获取双浮点型长度(双精度)
cout<<"\ndouble \t"<<length[5];
length[6]=sizeof(long double); // 获取长双浮点型长度(长双精度)cout<<"\nlong double\t"<<length[6];
length[7]=sizeof(bool); //获取布尔型长度
cout<<"\nbool \t"<<length[7]<<endl;
for(int i=0;i<8;i++)
if (length[i]>length[max])//求取长度最大的类型的存取位置
max=i;
if (length[i]<length[min])//求取长度最小的类型的存取位置
min=i;
for(int j=0;j<8;j++)
{ if(length[max]==length[j])
{a[m]=j; m++;}
if(length[min]==length[j])
{b[n]=j; n++;}
m--;
n--;
cout<<"The longest length is from(存储长度最大的数据类型是:) "<<endl; while(m>=0)
{ max= a[m];
switch (max)
case 0:cout<<"short int"<<endl;
break;
case 1:cout<<"int"<<endl;
break;
case 2:cout<<"long int"<<endl;
break;
case 3:cout<<"char"<<endl;
break;
case 4:cout<<"float"<<endl;
break;
case 5:cout<<"double"<<endl;
break;
case 6:cout<<"long double"<<endl;
break;
case 7:cout<<"bool"<<endl;
break;
m--;
cout<<"The shortest length is from(存储长度最小的数据类型是:)"<<endl; while(n>=0)
{min= b[n];
switch (min)
case 0:cout<<"short int."<<endl;
break;
case 1:cout<<"int."<<endl;
break;
case 2:cout<<"long int"<<endl;
break;
case 3:cout<<"char"<<endl;
break;
case 4:cout<<"float"<<endl;
break;
case 5:cout<<"double"<<endl;
break;
case 6:cout<<"long double"<<endl;
break;
case 7:cout<<"bool"<<endl;
break;
n--;
//第三题
#include<iostream>
using namespace std;
void main()
double F,C;
cout<<"请输入一个华氏温度:" <<"\n\tF=(℉)";
cin>>F;
C=(F-32)*5/9;
cout<<"转换成的摄氏温度为:"<<"\n\tC="<<C<<"℃"<<endl; //第四题
#include<iostream>
using namespace std;
void main()
int x,i=0;
int a[100];//数组长度100可以再增大
cout<<"请输入一个十进制正整数:" <<endl;
cin>>x;
while(x>0)//x只能大于0,不能大于等于0(进入死循环)
{ a[i]=x%2;
i++ ;
x=x/2;
i--;//把最后一次加的1减出去,实际没存入数
cout<<"转化成的二进制数为:"<<endl;
while(i>=0)
cout<<a[i];
i--;
cout<<endl;
C++第三单元课后编程题
//第一题
#include<iostream.h>
void main()
int i,k=0,sum=0;
int a[50];
for(i=1;i<=100;i++)
{ if(i%3==0)
{ a[k]=i;
k++; }
k--;
while(k>=0)
{ sum+=a[k];
k--;
cout<<"1到100所有3的倍数的数的和是:sum="<<sum<<endl;
//第二题
#include<iostream.h>
void main()
int i,k,N,sum1=0,sum2=0;
cout<<"输入的整数总个数为:"<<endl;
cin>>N;
cout<<"请输入要输入的整数:"<<endl;
for( i=0;i<N;i++)
{ cin>>k;
if(k%2==0)
sum1+=k;
else sum2+=k;
cout<<"输入的所有整数中的所有偶数之和是:\tsum1="<<sum1<<endl;
cout<<"输入的所有整数中的所有奇数之和是:\tsum2="<<sum2<<endl;
//第三题
#include<iostream.h>
void main()
{int a,b,t;
int r,x;
cout<<"请输入两个正整数:";
cin>>a>>b;
x = a * b;
if (a < b) //替换,保证a为较大的数
t = a;
a = b;
b = t;
while (b != 0)
r = a % b;
a = b;
b = r;
cout<<"最大公约数为:"<<a<<endl;
cout<<"最小公倍数为:"<<x/a<<endl; //最小公倍数即为两个数的乘积除以最大公约数//第四题
#include<iostream.h>
#include<math.h>
void main()
{ int a[10],b[10];
int i,m=0,n=0,c,d,e;//f=0,g;
cout<<"请输入10个正整数:"<<endl;
for(i=0;i<10;i++)
{ cin>>a[i];
m+=a[i]; }
c=m/10;
for(i=0;i<10;i++)
{ b[i]=(a[i]-c)*(a[i]-c);
n+=b[i]; }
// f+=abs(a[i]*a[i]-c*c); }
d=n/10;
// g=f/10;
e=(int)sqrt(d);
cout<<"这10个正整数的平均值为:\t"<<c<<endl;
cout<<"这10个数的方差为:\t"<<d<<endl;
cout<<"这10个数的标准方差为:\t"<<e<<endl;
// cout<<f<<" "<<g<<endl;
//第五题
#include<iostream.h>
void main()
{ char a[4];
int i;
cout<<"请输入4个字母:"<<endl;
for(i=0;i<4;i++)
cin>>a[i];
cout<<"反向输出这4个字母为:"<<endl;
i--; //用while时的语句
while(i>=0) // 用while时的语句
//for(i=3;i>=0; i--)
{ cout<<a[i]<<" ";
i--; }// 用while时的语句(包括大括号,for不需要)cout<<"\n";
//第六题
#include<iostream.h>
void main()
int a[10]={0,1,2,3,4,5,6,7,8,9};
int i,j,k;
cout<<"所有的水仙花数为:"<<endl;
for(i=1;i<10;i++)
{ for(j=0;j<10;j++)
{ for(k=0;k<10;k++)
if( a[i]*100+a[j]*10+a[k]==(a[i]*a[i]*a[i])+ (a[j]*a[j]*a[j])+(a[k]*a[k]*a[k]))
cout<<a[i]<<a[j]<<a[k]<<endl;
//第七题
#include<iostream.h>
void main()
long double i,sum1=1,sum2=0;
for(i=1;i<=50;i++)
{ sum1 *=i;
// cout<<sum1<<endl;
sum2 +=sum1;
cout<<"1!+2!++...+50!="<<sum2<<endl;
//第八题
#include<iostream.h>
#include<math.h>
void main()
{ float a,b,c,d;
float x,x1,x2;
cout<<"请输入一元二次方程的二次项系数,一次项系数,常数项:"<<endl;
cout<<"\ta=";
cin>>a;
cout<<"\tb=";
cin>>b;
cout<<"\tc=";
cin>>c;
d=b*b-4*a*c;
if(a==0)
{ cout<<"方程不是二次方程!"<<endl;
x=((float)-c)/b;
cout<<"方程的解为:\tx="<<x<<endl; }
else{ if(d==0)
{cout<<"方程有两个相等的实根!"<<endl;
x1=x2=((float)-b)/(2*a );
cout<<"方程两个相等的实根是:\tx1=x2="<<x1<<endl; }
else
{ if(d>0)
{ cout<<"方程有两个不等的实根!"<<endl;
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<"方程两个不等的实根分别是:\tx1="<<x1<<"\tx2="<<x2<<endl; }
else
{ cout<<"方程无实根,但有两个不相等的虚根!"<<endl;
cout<<"方程两个不等的虚根分别是:" <<endl;
cout<<"\tx1="<<((float)-b)/(2*a)<<"+"<<sqrt(-d)/(2*a)<<"i"<<endl;
cout<<"\tx2="<<((float)-b)/(2*a)<<"-"<<sqrt(-d)/(2*a)<<"i"<<endl; }
//第九题
#include<iostream.h>
void main()
{ int i,j,k;
for(i=3;i>=0;i--)
{j=i;
while(j>0)
{ cout<<" ";
j--; }
cout<<"@";
k=5-2*i;
while(k>0)
{ cout<<"#";
k--; }
switch (i)
{ case 0:
case 1:
case 2: cout<<"@"<<endl; break;
case 3: cout<<endl; break; }
for(i=1;i<=3;i++)
{j=i;
while(j>0)
{ cout<<" ";
j--; }
cout<<"@";
k=5-2*i;
while(k>0)
{ cout<<"#";
k--; }
switch (i)
{ case 0:
case 1:
case 2: cout<<"@"<<endl; break;
case 3: cout<<endl; break; }
//第十题
#include <iostream.h>
void main()
{ int year,month,day;
cout<<"请输入年月日信息:"<<endl;
cout<<"year=";
cin>>year;
cout<<"month=";
cin>>month;
cout<<"day=";
cin>>day;
while((month<1||month>12||day>31||day<1)||((month==4||month==6||month==9||month==11)&&d ay==31)||(month==2&&day>(28+((year%4==0&&year%100!=0)||(year%400==0)))))
{ cout<<"您输入有误!请重新输入年月日信息:"<<endl;
cout<<"year=";
cin>>year;
cout<<"month=";
cin>>month;
cout<<"day=";
cin>>day;
switch(month)
{ case 1: cout<<"这一天是:"<<year<<"年的第" <<day<<" 天" <<endl; break;
case 2: cout<<"这一天是:"<<year<<"年的第" <<31+day<<" 天" <<endl; break;
case 3: cout<<"这一天是:"<<year<<"年的第" <<31+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 4: cout<<"这一天是:"<<year<<"年的第" <<2*31+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 5: cout<<"这一天是:"<<year<<"年的第" <<2*31+30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 6: cout<<"这一天是:"<<year<<"年的第" <<3*31+30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 7: cout<<"这一天是:"<<year<<"年的第" <<3*31+2*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 8: cout<<"这一天是:"<<year<<"年的第" <<4*31+2*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 9: cout<<"这一天是:"<<year<<"年的第" <<5*31+2*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 10: cout<<"这一天是:"<<year<<"年的第" <<5*31+3*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 11: cout<<"这一天是:"<<year<<"年的第" <<6*31+3*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
case 12: cout<<"这一天是:"<<year<<"年的第" <<6*31+4*30+28+((year%4==0&&year%100!=0)||(year%400==0))+day<<" 天" <<endl; break;
//第十一题
#include<iostream.h>
void main()
{ float x,y;
cout<<"请输入x的值:"<<endl<<"\tx=";
cin>>x;
if( x<0)
{ y=x;
cout<<"\ty="<<y<<endl; }
else if(x>10)
{ y=4*x*x*x-x*x;
cout<<"\ty="<<y<<endl; }
else
{ y=x*x;
cout<<"\ty="<<y<<endl; }
//第十二题
#include<iostream.h>
void main()
{ int i,chicken,rabbit;//n=0;
for(i=0;i<=100;i++)
{ chicken=i/2;
while((100-i)%4==0)
{rabbit=(100-i)/4;
// n++;
cout<<"鸡兔各有:"<<endl;
cout<<"鸡chicken="<<chicken<<"只"<<"\t";
cout<<"兔rabbit="<<rabbit<<"只"<<endl;
break;
//cout<<n<<endl;
C++第四单元课后编程题
//第一题
#include<iostream.h>
#include<math.h>
float LiangDianJvLi(float x0,float y0,float x1,float y1)
{ float x,y,D;
x=x0-x1;
y=y0-y1;
D=sqrt(x*x+y*y);
return D;
void main()
{ float ax,ay,bx,by;
cout<<"请输入a,b两点的坐标(数字间用空格隔开):"<<endl<<"\t";
cin>>ax>>ay>>bx>>by;
cout<<"请确认一下两点坐标:"<<endl<<"\t";
cout<<"a("<<ax<<","<<ay<<")"<<"\t"<<"b("<<bx<<","<<by<<")"<<endl;
cout<<"点a("<<ax<<","<<ay<<")"<<"到"<<"点b("<<bx<<","<<by<<")的距离是:"<<endl;
cout<<"\tD="<<LiangDianJvLi(ax,ay,bx,by)<<endl;
//第二题
#include<iostream.h>
long JieCheng(long n)
{ if(n==0||n==1)//(n==0)
return 1;
return JieCheng(n-1)*n;
void main()
{ long a,b,c;
cout<<"请输入a,b,c的值:"<<endl;
cout<<"\ta=" ;
cin>>a;
cout<<"\tb=";
cin>>b;
cout<<"\tc=";
cin>>c;
cout<<"a!+b!+c!的值是:"<<endl;
cout<<"\ta!+b!+c!="<<(JieCheng(a)+JieCheng(b)+JieCheng(c))<<endl;
//第三题
#include<iostream.h>
#include<math.h>
void HuiWenShuZi(int);
void main()
{ int n;
cout<<"请输入一个整数(整数位数不超过50):"<<endl;
cout<<"\tn=";
cin>>n;
HuiWenShuZi(n);
void HuiWenShuZi(int a)
{ int b,d,i=1,j;
int c[51];
b=a;
d=abs(a);//输入为0是,下面的while不执行,但i仍然是1和输入的是一位数字相同while(d>0)
{ c[i]=d%10;
d=(d-c[i])/10;
i++; }
i--;
if(i%2==0)
{ j=i/2;//当输入是一位数字时,i=1,j=0,不执行while,直接到最后一个else while(j>=1)
{ if(c[j]==c[i/2+1])
i+=2;
else
{ cout<<"\t"<<b<<"不是回文数字!"<<endl; break;
if(j==0)
cout<<"\t"<<b<<"是回文数字!"<<endl;
else
{j=i/2;
while(j>=1)
{ if(c[j]==c[i/2+2])
{ j--;
i+=2;
else
{ cout<<"\t"<<b<<"不是回文数字!"<<endl; break;
if(j==0)
cout<<"\t"<<b<<"是回文数字!"<<endl;
//第四题
#include<iostream.h>
float Average(float,float,float);
float Average(float,float,float,float);
float Average(float,float,float,float,float);
void main()
{ int N;
float a1,a2,a3,b1,b2,b3,b4,c1,c2,c3,c4,c5;
cout<<"请输入您总共选修的课程门数:"<<endl<<"\t";
cin>>N;
while(N!=3&&N!=4&&N!=5)
{ cout<<"您输入有误,请重新输入:"<<endl<<"\t";
cin>>N; }
if(N==3)
{ cout<<"请输入3门选修课的成绩:"<<endl<<"\t";
cin>>a1>>a2>>a3;
cout<<"您3门选修课程的平均分为:"<<Average(a1,a2,a3)<<endl;
else{ if(N==4)
{ cout<<"请输入4门选修课的成绩:"<<endl<<"\t";
cin>>b1>>b2>>b3>>b4;
cout<<"您4门选修课程的平均分为:"<<Average(b1,b2,b3,b4)<<endl;
else if(N==5)
{ cout<<"请输入5门选修课的成绩:"<<endl<<"\t";
cin>>c1>>c2>>c3>>c4>>c5;
cout<<"您5门选修课程的平均分为:"<<Average(c1,c2,c3,c4,c5)<<endl; float Average(float x1,float x2,float x3)
{ float X;
X=(x1+x2+x3)/3;
float Average(float y1,float y2,float y3,float y4)
{ float Y;
Y=(y1+y2+y3+y4)/4;
return Y;
float Average(float z1,float z2,float z3,float z4,float z5) { float Z;
Z=(z1+z2+z3+z4+z5)/5;
return Z;
//第五题
//该程序采用递归的方法把整数转化为字符串
#include <iostream>
using namespace std;
int main(void)
{ int number; //变量
void IntToStr(int n);//函数原型
cout<<"请输入一个整数:"; //数据获取
cin>>number;
cout<<"转换结果是:"<<endl;
cout<<"\t\"" ;
if(number<0) //如果输入的是负数
cout<<"-";//输出负号
number=-number;//转换为正数解决
IntToStr(number);//调用递归函数
cout<<"\""<<endl;
return 0;
/*该函数用递归的方法把整数转换成为字符串*/
void IntToStr(int n)
if((n/10)!=0)//如果N为1位数字,则输出
IntToStr(n/10);//如果N不是1位整数则递归分析cout<<(char)(48+n%10);//输出该位数字
//第六题
#include<iostream.h>
int j=0;
void ZhuanHuan(int n)
{ int i,m;
m=n%10;
// j=(j+m)*10;
cout<<m;
i=(n-m)/10;
if(i>0)
ZhuanHuan(i);
void main()
{int a;
cout<<"请输入一个整数:"<<endl;
cin>>a;
ZhuanHuan(a);
cout<<endl;
//cout<<j/10<<endl;
//第七题
#include<iostream.h>
int Average1(int,int,int);
float Average2(float,float,float);
void main()
{ int a1,a2,a3;
float b1,b2,b3;
int N;
cout<<"您要输入的3个数字是否全为整型或浮点型?"<<endl<<"请按情况选择您要输入的3个数的类型的序号:" <<endl;
cout<<"\t1. 3个数字全为整型!"<<endl<<"\t2. 3个数字全为浮点型!"<<endl<<"\t3. 3个数字整型、浮点型均有!"<<endl;
cin>>N;
while(N!=1&&N!=2&&N!=3)
{cout<<"您选择有误,请重新选择:"<<endl;
cin>>N; }
if(N==1)
{ cout<<"请输入3个数字(数字间用空格键隔开):"<<endl<<"\t";
cin>>a1>>a2>>a3;
cout<<"3个数字的平均值是:\t"<<Average1(a1,a2,a3)<<endl; }
else
{ cout<<"请输入3个数字(数字间用空格键隔开):"<<endl<<"\t";
cin>>b1>>b2>>b3;
cout<<"3个数字的平均值是:\t"<<Average2(b1,b2,b3)<<endl; }
int Average1(int x1,int x2,int x3)
{ int max,min,X;
if(x1>x2)
{ max=x1;
min=x2; }
else
{ max=x2;
min=x1; }
if(max>x3)
{ cout<<"3个数字中的最大值是:\t"<<max<<endl;
{if(x3>min)
cout<<"3个数字中的最小值是:\t"<<min<<endl;
else
cout<<"3个数字中的最小值是:\t"<<x3<<endl; }
else
{ cout<<"3个数字中的最大值是:\t"<<x3<<endl;
cout<<"3个数字中的最小值是:\t"<<min<<endl;
X=(x1+x2+x3)/3;
return X;
float Average2(float y1,float y2,float y3)
{ float max,min,Y;
if(y1>y2)
{ max=y1;
min=y2; }
else
{ max=y2;
min=y1; }
if(max>y3)
{ cout<<"3个数字中的最大值是:\t"<<max<<endl;
{if(y3>min)
cout<<"3个数字中的最小值是:\t"<<min<<endl;
else
cout<<"3个数字中的最小值是:\t"<<y3<<endl; }
else
{ cout<<"3个数字中的最大值是:\t"<<y3<<endl;
cout<<"3个数字中的最小值是:\t"<<min<<endl;
Y=(y1+y2+y3)/3;
return Y;
//第七题(符合书上要求)
#include<iostream.h>
int IMax,IMin,X;
float FMax,FMin,Y;
void Average1(int,int,int);
void Average2(float,float,float);
void main()
{ int a1,a2,a3;
float b1,b2,b3;
int N;
cout<<"您要输入的3个数字是否全为整型或浮点型?"<<endl<<"请按情况选择您要输入的3个数的类型的序号:" <<endl;
cout<<"\t1. 3个数字全为整型!"<<endl<<"\t2. 3个数字全为浮点型!"<<endl<<"\t3. 3个数字整型、浮点型均有!"<<endl;
cin>>N;
while(N!=1&&N!=2&&N!=3)
{cout<<"您选择有误,请重新选择:"<<endl;
cin>>N; }
if(N==1)
{ cout<<"请输入3个数字(数字间用空格键隔开):"<<endl<<"\t";
cin>>a1>>a2>>a3;
Average1(a1,a2,a3);
cout<<"3个数字中的最大值是:\t"<<IMax<<endl;
cout<<"3个数字中的最小值是:\t"<<IMin<<endl;
cout<<"3个数字的平均值是:\t"<<X<<endl; }
else
{ cout<<"请输入3个数字(数字间用空格键隔开):"<<endl<<"\t";
cin>>b1>>b2>>b3;
Average2(b1,b2,b3);
cout<<"3个数字中的最大值是:\t"<<FMax<<endl;
cout<<"3个数字中的最小值是:\t"<<FMin<<endl;
cout<<"3个数字的平均值是:\t"<<Y<<endl; }
void Average1(int x1,int x2,int x3)
{ int max,min;
if(x1>x2)
{ max=x1;
min=x2; }
else
{ max=x2;
min=x1; }
if(max>x3)
{ IMax=max;
{if(x3>min)
IMin=min;
else
IMin=x3; }
else
{ IMax=x3;
IMin=min;
X=(x1+x2+x3)/3;
void Average2(float y1,float y2,float y3)
{ float max,min;
if(y1>y2)
{ max=y1;
min=y2; }
else
{ max=y2;
min=y1; }
if(max>y3)
{ FMax=max;
{if(y3>min)
FMin=min;
else
FMin=y3; }
else
{ FMax=y3;
FMin=min;
Y=(y1+y2+y3)/3;
C++第五单元课后编程题
//第一题
#include<iostream.h>
void main()
{ int a[3][3];
int i,j,temp;
cout<<"请输入二维数组(3*3)个整型元素:"<<endl<<"\t";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"您输入的二维数组(3*3)是:"<<endl;
for(i=0;i<3;i++)
cout<<a[0][i]<<" ";
cout<<endl;
for(i=0;i<3;i++)
cout<<a[1][i]<<" ";
cout<<endl;
for(i=0;i<3;i++)
cout<<a[2][i]<<" ";
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<i;j++) //j<i只是把对角线以下的元素与对角线以上的元素互换;若j<3 则是先把各个元素换了,后又换回来
{ temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp; }
cout<<"转置后的二维数组(3*3)是:"<<endl;
for(i=0;i<3;i++)
cout<<a[0][i]<<" ";
cout<<endl;
for(i=0;i<3;i++)
cout<<a[1][i]<<" ";
cout<<endl;
for(i=0;i<3;i++)
cout<<a[2][i]<<" ";
cout<<endl;
//第二题
#include<iostream.h>
void main()
{ char
*p[12]={"January","February","March","April","May","June","July","August","September","Oct ober","November","December"};
int month;
cout<<"请输入一个数字月份(数字1到数字12任意一个):"<<endl<<"\t";
cin>>month;
while(month<1||month>12)
{ cout<<"请输入一个数字月份(数字1到数字12任意一个):"<<endl<<"\t";
cin>>month; }
cout<<"\t"<<p[month-1]<<endl;
// cout<<"\t"<<*(p+month-1)<<endl;//*(p)和p[0]相等
//第三题
#include<iostream.>
struct student
{ char name[20];
float score1,score2,score3;
float AverageScore;
student stu[5];
void main()
{ int i,k=4;
float MaxAverageScore;
cout<<"请输入5个学生的姓名!"<<endl;
for(i=0;i<5;i++)
{ cout<<"\t"<<"请输入第"<<i+1<<"个学生的姓名:";
cin>>stu[i].name; }
cout<<"请输入5个学生的3科成绩!"<<endl;
for(i=0;i<5;i++)
{ cout<<"\t"<<"请输入第"<<i+1<<"个学生3科成绩:" ;
cin>>stu[i].score1>>stu[i].score2>>stu[i].score3;
stu[i].AverageScore=(stu[i].score1+ stu[i].score2+ stu[i].score3)/3; }
MaxAverageScore=stu[0].AverageScore>=stu[1].AverageScore?stu[0].AverageScore:stu[1].Avera geScore;
MaxAverageScore=MaxAverageScore>=stu[2].AverageScore?MaxAverageScore:stu[2].AverageS core;
MaxAverageScore=MaxAverageScore>=stu[3].AverageScore?MaxAverageScore:stu[3].AverageS core;
MaxAverageScore=MaxAverageScore>=stu[4].AverageScore?MaxAverageScore:stu[4].AverageS core;
while(k>=0)
{ if(MaxAverageScore==stu[k].AverageScore)
{ cout<<"5个学生3科平均成绩最高的是:"<<endl;
cout<<"\t"<<stu[k].name<<endl;
cout<<"\t"<<stu[k].name<<"3科的成绩分别是:"<<endl;
cout<<"\t"<<stu[k].score1<<"\t"<<stu[k].score2<<"\t"<<stu[k].score3<<endl;
cout<<"\t"<<stu[k].name<<"的平均成绩是:"<<"\t"<<MaxAverageScore<<endl;
k--; }
//第六题
#include<iostream.h>
#include <stdlib.h>
struct XiShu
float num;
}a[3],b[3],c[3],d[3];
void main()
{ char equation[2][50],z[10];
int i,j,k,m,n;
cout<<"请输入第一个方程式:"<<endl<<"\t";
cin>>equation[0];
cout<<"请输入第二个方程式"<<endl<<"\t";
cin>>equation[1];
for(i=0;i<2;i++)
{ j=0;
k=0;
while((equation[i][j]>='0'&&equation[i][j]<='9')||equation[i][j]=='.'||equation[i][j]=='-')
{ z[k]=equation[i][j];
k++;
j++;
if(equation[i][j]=='a')
{ z[k]=0;
a[i].num=atof(z);m=j; }
else { a[i].num=0;j=-1;m=j; }
k=0;
j++;
// cout<<a[i].num<<endl;
while((equation[i][j]>='0'&&equation[i][j]<='9')||equation[i][j]=='.'||equation[i][j]=='-'||equation[i] [j]=='+')
{ z[k]=equation[i][j];
k++;
j++;
if(equation[i][j]=='b')
{ z[k]=0;
b[i].num=atof(z); n=j; }
else { b[i].num=0;j=m;n=j; }
k=0;
j++;
// cout<<b[i].num<<endl;
while((equation[i][j]>='0'&&equation[i][j]<='9')||equation[i][j]=='.'||equation[i][j]=='-'||equation[i] [j]=='+')
{ z[k]=equation[i][j];
k++;
j++;
if(equation[i][j]=='c')
{ z[k]=0;
c[i].num=atof(z); }
else { c[i].num=0; j=n; }
k=0;
j=j+2;
// cout<<c[i].num<<endl;
while(equation[i][j]!=0)
{z[k]=equation[i][j];
k++;
j++; }
z[k]=0;
d[i].num=atof(z);
//cout<<d[i].num<<endl;
a[2].num=a[0].num+a[1].num;
b[2].num=b[0].num+b[1].num;
c[2].num=c[0].num+c[1].num;
d[2].num=d[0].num+d[1].num;
//cout<<a[2].num<<endl<<b[2].num<<endl<<c[2].num<<endl<<d[2].num<<endl;
cout<<"两个方程式相加之后为:"<<endl;
if(a[2].num!=0)
cout<<"\t"<<a[2].num<<"a";
if(b[2].num!=0)
if(b[2].num>0)
cout<<"+"<<b[2].num<<"b";
else cout<<b[2].num<<"b";
if(c[2].num!=0)
if(c[2].num>0)
cout<<"+"<<c[2].num<<"c";
else cout<<c[2].num<<"c";
cout<<"="<<d[2].num<<endl;
//第五题
#include<iostream.h>
void FanXiang(char c[]);
char b[50];
void main()
{ char a[50];
cout<<"请输入字符串(字符串长度不能超过49个):"<<endl<<"\t";
cin.getline(a,50);
FanXiang(a);
cout<<"输入的字符串反向后为:"<<endl<<"\t"<<b<<endl;;
void FanXiang(char c[])
{ char *p;
int i=0,k=0;
char s;
do
{ s=c[i];
i++;
} while(s!='\0'); //s=='\0'后,i再加1后执行whilea判断
i--;
p=c;
while(i>=0)
{b[k]=*(p+i-1);
k++;
i--; }
//第六题
#include<iostream.h>
void TongJi(char b[]);
int c[4]={0,0,0,0};
void main()
{ char a[100];
cout<<"请输入要输入的一行文字(文字不能超过99个):"<<endl<<"\t";
cin.getline(a,100);
TongJi(a);
cout<<"输入的一行文字中所有的字母的个数:\t"<<c[0]<<"个"<<endl;
cout<<"输入的一行文字中所有的数字的个数:\t"<<c[1]<<"个"<<endl;
cout<<"输入的一行文字中所有的空格的个数:\t"<<c[2]<<"个"<<endl;
cout<<"输入的一行文字中所有的其他字符的个数:\t"<<c[3]<<"个"<<endl; void TongJi(char b[])
{ int i=0;
while(b[i]!='\0')
{ if((b[i]>=65&&b[i]<=90)||(b[i]>=97&&b[i]<=122))
c[0]+=1;
else if(b[i]>=48&&b[i]<=57)
c[1]+=1;
else if(b[i]==32)
c[2]+=1;
else c[3]+=1;
i++;
C++第六单元课后编程题。