(完整版)谭浩强c程序设计课后习题答案

(完整版)谭浩强c程序设计课后习题答案


2024年1月13日发(作者:)

谭浩强c++程序设计课后答案

娄警卫

..

第一章

1.5题

#include

using namespace std;

int main()

{

cout<<"This"<<"is";

cout<<"a"<<"C++";

cout<<"program.";

return 0;

1.6题

#include

using namespace std;

int main()

{

int a,b,c;

a=10;

b=23;

c=a+b;

cout<<"a+b=";

cout<

cout<

return 0;

}

1.7七题

#include

using namespace std;

int main()

{

int a,b,c;

int f(int x,int y,int z);

cin>>a>>b>>c;

c=f(a,b,c);

cout<

return 0;

}

int f(int x,int y,int z)

{

int m;

if (x

else m=y;

if (z

return(m);

}

1.8题

#include

using namespace std;

int main()

{

int a,b,c;

cin>>a>>b;

c=a+b;

cout<<"a+b="<

return 0;

}

1.9题

#include

using namespace std;

int main()

{

int a,b,c;

int add(int x,int y);

cin>>a>>b;

c=add(a,b);

cout<<"a+b="<

return 0;

}

int add(int x,int y)

{int z;

z=x+y;

return(z);

}

2 / 75

第二章

2.3题

#include

using namespace std;

int main()

{char c1='a',c2='b',c3='c',c4='101',c5='116';

cout<

cout<<"tb"<

return 0;

}

2.4题

#include

using namespace std;

int main()

{char c1='C',c2='+',c3='+';

cout<<"I say: ""<

cout<<"tt"<<"He says: "C++ is very

interesting!""<< 'n';

return 0;

}

2.7题

#include

using namespace std;

int main()

{int i,j,m,n;

i=8;

j=10;

m=++i+j++;

n=(++i)+(++j)+m;

cout<

return 0;

}

2.8题

#include

using namespace std;

int main()

{char c1='C', c2='h', c3='i', c4='n', c5='a';

c1+=4;

c2+=4;

c3+=4;

c4+=4;

c5+=4;

cout<<"password

is:"<

return 0;

}

第三章

3.2题

#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=3.0/4.0*pi*r*r*r;

vz=pi*r*r*h;

cout<

<

cout<<"l= "<

cout<<"s= "<

cout<<"sq="<

cout<<"vq="<

cout<<"vz="<

return 0;

}

3.3题

#include

using namespace std;

int main ()

{float c,f;

cout<<"请输入一个华氏温度:";

cin>>f;

c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0

cout<<"摄氏温度为:"<

return 0;

};

3 / 75

3.4题

#include

using namespace std;

int main ( )

{char c1,c2;

cout<<"请输入两个字符c1,c2:";

c1=getchar(); //将输入的第一个字符赋给c1

c2=getchar(); //将输入的第二个字符赋给c2

cout<<"用putchar函数输出结果为:";

putchar(c1);

putchar(c2);

cout<

cout<<"用cout语句输出结果为:";

cout<

return 0;

}

3.4题另一解

#include

using namespace std;

int main ( )

{char c1,c2;

cout<<"请输入两个字符c1,c2:";

c1=getchar(); //将输入的第一个字符赋给c1

c2=getchar(); //将输入的第二个字符赋给c2

cout<<"用putchar函数输出结果为:";

putchar(c1);

putchar(44);

putchar(c2);

cout<

cout<<"用cout语句输出结果为:";

cout<

return 0;

}

3.5题

#include

using namespace std;

int main ( )

{char c1,c2;

int i1,i2; //定义为整型

cout<<"请输入两个整数i1,i2:";

cin>>i1>>i2;

c1=i1;

c2=i2;

cout<<"按字符输出结果为:"<

"<

return 0;

}

3.8题

#include

using namespace std;

int main ( )

{ int a=3,b=4,c=5,x,y;

cout<<(a+b>c && b==c)<

cout<<(a||b+c && b-c)<

cout<<(!(a>b) && !c||1)<

cout<<(!(x=a) && (y=b) && 0)<

cout<<(!(a+b)+c-1 && b+c/2)<

return 0;

}

3.9题

include

using namespace std;

int main ( )

{int a,b,c;

cout<<"please enter three integer

numbers:";

cin>>a>>b>>c;

if(a

if(b

cout<<"max="<

else

cout<<"max="<

else if (a

cout<<"max="<

else

cout<<"max="<

cout<

return 0;

}

3.9题另一解

#include

4 / 75

using namespace std;

int main ( )

{int a,b,c,temp,max ;

cout<<"please enter three integer

numbers:";

cin>>a>>b>>c;

temp=(a>b)?a:b; /*

将a和b中的大者存入temp中 */

max=(temp>c)?temp:c; /*

将a和b中的大者与c比较,最大者存入max

*/

cout<<"max="<

return 0;

}

3.10题

#include

using namespace std;

int main ( )

{int x,y;

cout<<"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="<

y=3*x-11="<

}

cout<

return 0;

}

3.11题

#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;

}

3.12题

#include

using namespace std;

int main ()

{long int num;

int

indiv,ten,hundred,thousand,ten_thousand,place;

/*分别代表个位,十位,百位,千位,万位和位数

*/

cout<<"enter an integer(0~99999):";

cin>>num;

if (num>9999)

place=5;

else if (num>999)

place=4;

else if (num>99)

place=3;

else if (num>9)

place=2;

else place=1;

cout<<"place="<

5 / 75

//计算各位数字

ten_thousand=num/10000;

thousand=(int)(num-ten_thousand*10000)/1000;

hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;

ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;

indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);

cout<<"original order:";

switch(place)

{case

5:cout<

dl;

cout<<"reverse order:";

cout<

break;

case

4:cout<

cout<<"reverse order:";

cout<

break;

case

3:cout<

cout<<"reverse order:";

cout<

break;

case 2:cout<

cout<<"reverse order:";

cout<

break;

case 1:cout<

cout<<"reverse order:";

cout<

break;

}

return 0;

}

3.13题

#include

using namespace std;

int main ()

{ long i; //i为利润

float

bonus,bon1,bon2,bon4,bon6,bon10;

bon1=100000*0.1; //利润为10万元时的奖金

bon2=bon1+100000*0.075; //利润为20万元时的奖金

bon4=bon2+100000*0.05; //利润为40万元时的奖金

bon6=bon4+100000*0.03; //利润为60万元时的奖金

bon10=bon6+400000*0.015; //利润为100万元时的奖金

cout<<"enter i:";

cin>>i;

if (i<=100000)

bonus=i*0.1;

//利润在10万元以内按10%提成奖金

else if (i<=200000)

bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金

else if (i<=400000)

bonus=bon2+(i-200000)*0.05;

利润在20万元至40万时的奖金

else if (i<=600000)

bonus=bon4+(i-400000)*0.03;

利润在40万元至60万时的奖金

else if (i<=1000000)

bonus=bon6+(i-600000)*0.015;

利润在60万元至100万时的奖金

else

bonus=bon10+(i-1000000)*0.01;

利润在100万元以上时的奖金

cout<<"bonus="<

// // // // 6 / 75

return 0;

}

3.13题另一解

#include

using namespace std;

int main ()

{long i;

float bonus,bon1,bon2,bon4,bon6,bon10;

int c;

bon1=100000*0.1;

bon2=bon1+100000*0.075;

bon4=bon2+200000*0.05;

bon6=bon4+200000*0.03;

bon10=bon6+400000*0.015;

cout<<"enter i:";

cin>>i;

c=i/100000;

if (c>10) c=10;

switch(c)

{case 0: bonus=i*0.1; break;

case 1: bonus=bon1+(i-100000)*0.075;

break;

case 2:

case3:

bonus=bon2+(i-200000)*0.05;break;

case 4:

case5:

bonus=bon4+(i-400000)*0.03;break;

case 6:

case 7:

case 8:

case 9: bonus=bon6+(i-600000)*0.015;

break;

case 10: bonus=bon10+(i-1000000)*0.01;

}

cout<<"bonus="<

return 0;

}

3.14题

#include

using namespace std;

int main ()

{int t,a,b,c,d;

cout<<"enter four numbers:";

cin>>a>>b>>c>>d;

cout<<"a="<

c="<

if (a>b)

{t=a;a=b;b=t;}

if (a>c)

{t=a; a=c; c=t;}

if (a>d)

{t=a; a=d; d=t;}

if (b>c)

{t=b; b=c; c=t;}

if (b>d)

{t=b; b=d; d=t;}

if (c>d)

{t=c; c=d; d=t;}

cout<<"the sorted sequence:"<

cout<

return 0;

}

3.15题

#include

using namespace std;

int main ()

{int p,r,n,m,temp;

cout<<"please enter two positive integer

numbers n,m:";

cin>>n>>m;

if (n

{temp=n;

n=m;

m=temp; //把大数放在n中, 小数放在m中

}

p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用

while (m!=0) //求n和m的最大公约数

{r=n%m;

n=m;

m=r;

}

cout<<"HCF="<

cout<<"LCD="<

原来两个整数的乘积

return 0;

}

3.16题

#include

using namespace std;

int main ()

{char c;

int letters=0,space=0,digit=0,other=0;

cout<<"enter one line::"<

while((c=getchar())!='n')

{if (c>='a' && c<='z'||c>='A' && c<='Z')

letters++;

else if (c==' ')

space++;

else if (c>='0' && c<='9')

digit++;

else

other++;

}

cout<<"letter:"<

space:"<

other:"<

return 0;

}

3.17题

#include

using namespace std;

int main ()

{int a,n,i=1,sn=0,tn=0;

cout<<"a,n=:";

cin>>a>>n;

while (i<=n)

{tn=tn+a; //赋值后的tn为i个a组成数的值

sn=sn+tn; //赋值后的sn为多项式前i项之和

a=a*10;

++i;

}

cout<<"a+aa+aaa+...="<

return 0;

}

3.18题

#include

using namespace std;

int main ()

{float s=0,t=1;

int n;

for (n=1;n<=20;n++)

{

t=t*n; // 求n!

s=s+t; // 将各项累加

}

cout<<"1!+2!+...+20!="<

return 0;

}

3.19题

#include

using namespace std;

int main ()

{int i,j,k,n;

cout<<"narcissus numbers are:"<

for (n=100;n<1000;n++)

{i=n/100;

j=n/10-i*10;

k=n%10;

if (n == i*i*i + j*j*j + k*k*k)

cout<

}

cout<

return 0;

}

3.20题

#include

using namespace std;

int main()

{const int m=1000; // 定义寻找范围

int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;

int i,a,n,s;

for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数

{n=0; // n用来累计 8 / 75

a的因子的个数

s=a; // s用来存放尚未求出的因子之和,开始时等于a

for (i=1;i

if (a%i==0) // 如果i是a的因子

{n++; // n加1,表示新找到一个因子

s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子

之和

switch(n) // 将找到的因子赋给k1,...,k10

{case 1:

k1=i; break; // 找出的笫1个因子赋给k1

case 2:

k2=i; break; // 找出的笫2个因子赋给k2

case 3:

k3=i; break; // 找出的笫3个因子赋给k3

case 4:

k4=i; break; // 找出的笫4个因子赋给k4

case 5:

k5=i; break; // 找出的笫5个因子赋给k5

case 6:

k6=i; break; // 找出的笫6个因子赋给k6

case 7:

k7=i; break; // 找出的笫7个因子赋给k7

case 8:

k8=i; break; // 找出的笫8个因子赋给k8

case 9:

k9=i; break; // 找出的笫9个因子赋给k9

case 10:

k10=i; break; // 找出的笫10个因子赋给k10

}

}

if (s==0) // s=0表示全部因子都已找到了

{cout<

cout<<"its factors are:";

if (n>1) cout<

n>1表示a至少有2个因子

if (n>2) cout<<","<2表示至少有3个因子,故应再输出一个因子

if (n>3) cout<<","<3表示至少有4个因子,故应再输出一个因子

if (n>4) cout<<","<

以下类似

if (n>5) cout<<","<

if (n>6) cout<<","<

if (n>7) cout<<","<

if (n>8) cout<<","<

if (n>9) cout<<","<

cout<

}

}

return 0;

}

3.20题另一解

#include

using namespace std;

int main()

{int m,s,i;

for (m=2;m<1000;m++)

{s=0;

for (i=1;i

if ((m%i)==0) s=s+i;

if(s==m)

{cout<

cout<<"its factors are:";

for (i=1;i

if (m%i==0) cout<

cout<

}

}

return 0;

}

3.20题另一解

9 / 75

#include

using namespace std;

int main()

{int k[11];

int i,a,n,s;

for (a=2;a<=1000;a++)

{n=0;

s=a;

for (i=1;i

if ((a%i)==0)

{n++;

s=s-i;

k[n]=i; // 将找到的因子赋给k[1]┅k[10]

}

if (s==0)

{cout<

cout<<"its factors are:";

for (i=1;i

cout<

cout<

}

}

return 0;

}

3.21题

#include

using namespace std;

int main()

{int i,t,n=20;

double a=2,b=1,s=0;

for (i=1;i<=n;i++)

{s=s+a/b;

t=a;

a=a+b; // 将前一项分子与分母之和作为下一项的分子

b=t; // 将前一项的分子作为下一项的分母

}

cout<<"sum="<

return 0;

}

3.22题

#include

using namespace std;

int main()

{int day,x1,x2;

day=9;

x2=1;

while(day>0)

{x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍

x2=x1;

day--;

}

cout<<"total="<

return 0;

}

3.23题

#include

#include

using namespace std;

int main()

{float a,x0,x1;

cout<<"enter a positive number:";

cin>>a; // 输入a的值

x0=a/2;

x1=(x0+a/x0)/2;

do

{x0=x1;

x1=(x0+a/x0)/2;

}

while(fabs(x0-x1)>=1e-5);

cout<<"The square root of "<

"<

return 0;

}

3.24题

#include

using namespace std;

int main()

{int i,k;

for (i=0;i<=3;i++) // 输出上面4行*号

10 / 75

{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;

}

3.25题

#include

using namespace std;

int main()

{char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/

for (i='X';i<='Z';i++)

for (j='X';j<='Z';j++)

if (i!=j)

for (k='X';k<='Z';k++)

if (i!=k && j!=k)

if (i!='X' && k!='X' && k!='Z')

cout<<"A--"<

B--"<

return 0;

}

h=hcf(u,v);

cout<<"H.C.F="<

l=lcd(u,v,h);

cout<<"L.C.D="<

return 0;

}

int hcf(int u,int v)

{int t,r;

if (v>u)

{t=u;u=v;v=t;}

while ((r=u%v)!=0)

{u=v;

v=r;}

return(v);

}

int lcd(int u,int v,int h)

{return(u*v/h);

}

4.2题

#include

#include

using namespace std;

float x1,x2,disc,p,q;

int main()

{void greater_than_zero(float,float);

void equal_to_zero(float,float);

void smaller_than_zero(float,float);

float a,b,c;

cout<<"input a,b,c:";

cin>>a>>b>>c;

disc=b*b-4*a*c;

cout<<"root:"<

if (disc>0)

{

greater_than_zero(a,b);

cout<<"x1="<

}

else if (disc==0)

{equal_to_zero(a,b);

cout<<"x1="<

第四章

4.1题

#include

using namespace std;

int main()

{int hcf(int,int);

int lcd(int,int,int);

int u,v,h,l;

cin>>u>>v;

11 / 75

}

else

{smaller_than_zero(a,b);

cout<<"x1="<

cout<<"x2="<

}

return 0;

}

void greater_than_zero(float a,float b) /*

定义一个函数,用来求disc>0时方

程的根 */

{x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

}

void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程

的根 */

{

x1=x2=(-b)/(2*a);

}

void smaller_than_zero(float a,float b) /*

定义一个函数,用来求disc<0时方

程的根 */

{

p=-b/(2*a);

q=sqrt(-disc)/(2*a);

}

4.3题

#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);

}

4.4题

#include

using namespace std;

int main()

{int fac(int);

int a,b,c,sum=0;

cout<<"enter a,b,c:";

cin>>a>>b>>c;

sum=sum+fac(a)+fac(b)+fac(c);

cout<

return 0;

}

int fac(int n)

{int f=1;

for (int i=1;i<=n;i++)

f=f*i;

return f;

}

4.5题

#include

#include

using namespace std;

int main()

{double e(double);

double x,sinh;

cout<<"enter x:";

cin>>x;

sinh=(e(x)+e(-x))/2;

cout<<"sinh("<

12 / 75

return 0;

}

double e(double x)

{return exp(x);}

4.6题

#include

#include

using namespace std;

int main()

{double

solut(double ,double ,double ,double );

double a,b,c,d;

cout<<"input a,b,c,d:";

cin>>a>>b>>c>>d;

cout<<"x="<

return 0;

}

double solut(double a,double b,double

c,double d)

{double x=1,x0,f,f1;

do

{x0=x;

f=((a*x0+b)*x0+c)*x0+d;

f1=(3*a*x0+2*b)*x0+c;

x=x0-f/f1;

}

while(fabs(x-x0)>=1e-5);

return(x);

}

4.7题

#include

#include

using namespace std;

int main()

{void godbaha(int);

int n;

cout<<"input n:";

cin>>n;

godbaha(n);

return 0;

}

void godbaha(int n)

{int prime(int);

int a,b;

for(a=3;a<=n/2;a=a+2)

{if(prime(a))

{b=n-a;

if (prime(b))

cout<

}

}

int prime(int m)

{int i,k=sqrt(m);

for(i=2;i<=k;i++)

if(m%i==0) break;

if (i>k) return 1;

else return 0;

}

4.8题

#include

using namespace std;

int main()

{int x,n;

float p(int,int);

cout<<"input n & x:";

cin>>n>>x;

cout<<"n="<

cout<<"P"<

return 0;

}

float p(int n,int x)

{if (n==0)

return(1);

else if (n==1)

return(x);

else

return(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/ 13 / 75

n);

}

4.9题

#include

using namespace std;

int main()

{void hanoi(int n,char one,char two,char

three);

int m;

cout<<"input the number of diskes:";

cin>>m;

cout<<"The steps of moving "<

disks:"<

hanoi(m,'A','B','C');

return 0;

}

void hanoi(int n,char one,char two,char three)

//将n个盘从one座借助two座,移到three座

{void move(char x,char y);

if(n==1) move(one,three);

else

{hanoi(n-1,one,three,two);

move(one,three);

hanoi(n-1,two,one,three);

}

}

void move(char x,char y)

{cout<"<

4.10题

#include

using namespace std;

int main()

{void convert(int n);

int number;

cout<<"input an integer:";

cin>>number;

cout<<"output:"<

if (number<0)

{cout<<"-";

number=-number;

}

convert(number);

cout<

return 0;

}

void convert(int n)

{int i;

char c;

if ((i=n/10)!=0)

convert(i);

c=n%10+'0';

cout<<" "<

}

4.11题

#include

using namespace std;

int main()

{int f(int);

int n,s;

cout<<"input the number n:";

cin>>n;

s=f(n);

cout<<"The result is "<

return 0;

}

int f(int n)

{;

if (n==1)

return 1;

else

return (n*n+f(n-1));

}

4.12题

#include

#include

using namespace std;

#define S(a,b,c) (a+b+c)/2

#define AREA(a,b,c)

14 / 75

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;

}

4.14题

#include

using namespace std;

//#define LETTER 1

int main()

{char c;

cin>>c;

#if LETTER

if(c>='a' && c<='z')

c=c-32;

#else

if(c>='A' && c<='Z')

c=c+32;

#endif

cout<

return 0;

}

4.15题

#include

using namespace std;

#define CHANGE 1

int main()

{char ch[40];

cout<<"input text:"<

gets(ch);

#if (CHANGE)

{for (int i=0;i<40;i++)

{if (ch[i]!='0')

if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&&

ch[i]<'Z')

ch[i]+=1;

else if (ch[i]=='z'||ch[i]=='Z')

ch[i]-=25;

}

}

#endif

cout<<"output:"<

return 0;

}

4.16题file

#include

using namespace std;

int a;

int main()

{extern int power(int);

int b=3,c,d,m;

cout<<"enter an integer a and its power

m:"<

cin>>a>>m;

c=a*b;

cout<

d=power(m);

cout<

return 0;

}

4.16题file

extern int a;

int power(int n)

{int i,y=1;

for(i=1;i<=n;i++)

y*=a;

return y;

}

第五章

5.1题

#include

#include

using namespace std;

#include

int main()

{int i,j,n,a[101];

15 / 75

for (i=1;i<=100;i++)

a[i]=i;

a[1]=0;

for (i=2;i

for (j=i+1;j<=100;j++)

{if(a[i]!=0 && a[j]!=0)

if (a[j]%a[i]==0)

a[j]=0; }

cout<

for (i=1,n=0;i<=100;i++)

{if (a[i]!=0)

{cout<

n++;}

if(n==10)

{cout<

n=0;}

}

cout<

return 0;

}

5.2题

#include

using namespace std;

//#include

int main()

{int i,j,min,temp,a[11];

cout<<"enter data:"<

for (i=1;i<=10;i++)

{cout<<"a["<

cin>>a[i]; //输入10个数

}

cout<

numbers:"<

for (i=1;i<=10;i++)

cout<

cout<

for (i=1;i<=9;i++) //以下8行是对10个数排序

{min=i;

for (j=i+1;j<=10;j++)

if (a[min]>a[j]) min=j;

temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换

a[i]=a[min];

a[min]=temp;

}

cout<

numbers:"<

for (i=1;i<=10;i++) // 输出已排好序的10个数

cout<

cout<

return 0;

}

5.3题

#include

using namespace std;

int main()

{int a[3][3],sum=0;

int i,j;

cout<<"enter data:"<

for (i=0;i<3;i++)

for (j=0;j<3;j++)

cin>>a[i][j];

for (i=0;i<3;i++)

sum=sum+a[i][i];

cout<<"sum="<

return 0;

}

5.4题

#include

using namespace std;

int main()

{int a[11]={1,4,6,9,13,16,19,28,40,100};

int num,i,j;

cout<<"array a:"<

for (i=0;i<10;i++)

cout<

cout<

cout<<"insert data:";

cin>>num;

if (num>a[9])

a[10]=num;

else

16 / 75

{for (i=0;i<10;i++)

{if (a[i]>num)

{for (j=9;j>=i;j--)

a[j+1]=a[j];

a[i]=num;

break;

}

}

}

cout<<"Now, array a:"<

for (i=0;i<11;i++)

cout<

cout<

return 0;

}

5.5题

#include

using namespace std;

int main()

{ const int n=5;

int a[n],i,temp;

cout<<"enter array a:"<

for (i=0;i

cin>>a[i];

cout<<"array a:"<

for (i=0;i

cout<

for (i=0;i

{ temp=a[i];

a[i]=a[n-i-1];

a[n-i-1]=temp;

}

cout<

for (i=0;i

cout<

cout<

return 0;

}

5.6题

#include

#include

using namespace std;

int main()

{const int n=11;

int i,j,a[n][n];

for (i=1;i

{a[i][i]=1;

a[i][1]=1;

}

for (i=3;i

for (j=2;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j];

for (i=1;i

{for (j=1;j<=i;j++)

cout<

cout<

}

cout<

return 0;

}

5.7题

#include

using namespace std;

int main()

{ const int n=4,m=5; //假设数组为4行5列

int i,j,a[n][m],max,maxj;

bool flag;

for (i=0;i

for (j=0;j

cin>>a[i][j];

for (i=0;i

{max=a[i][0]; maxj=0;

for (j=0;j

if (a[i][j]>max)

{max=a[i][j]; //将本行的最大数存放在max中

maxj=j; //将最大数所在的列号存放在maxj中

}

flag=true; //先假设是鞍点,以flag为真代表

for (int k=0;k

if (max>a[k][maxj]) //将最大数和其同列元素相比

17 / 75

{flag=false; //如果max不是同列最小,表示不是鞍点令flag1为

continue;}

if(flag) //如果flag1为真表示是鞍点

{cout<<"a["<

//输出鞍点的值和所在行列号

break;

}

}

if(!flag) //如果flag为假表示鞍点不存在

cout<<"It does not exist!"<

return 0;

}

5.8题

#include

using namespace std;

int main()

{ const int n=7;

int i,number,top,bott,mid,loca,a[n];

bool flag=true,sign;

char c;

cout<<"enter data:"<

cin>>a[0];

i=1;

while(i

{cin>>a[i];

if (a[i]>=a[i-1])

i++;

else

cout<<"enter this data again:";

}

cout<

for (i=0;i

cout<

cout<

while(flag)

{cout<<"input number to look for:";

cin>>number;

sign=false;

top=0; //top是查找区间的起始位置

bott=n-1; //bott是查找区间的最末位置

if ((numbera[n-1]))

//要查的数不在查找区间内

loca=-1; // 表示找不到

while ((!sign) && (top<=bott))

{mid=(bott+top)/2;

if (number==a[mid])

{loca=mid;

cout<<"Find "<

position is "<

sign=true;

}

else if (number

bott=mid-1;

else

top=mid+1;

}

if(!sign||loca==-1)

cout<

found."<

cout<<"continu or not(Y/N)?";

cin>>c;

if (c=='N'||c=='n')

flag=false;

}

return 0;

}

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);

/* 调用函数一

*/

18 / 75

if(leap(year) month>=3) text[i][j]<='z')

lower++;

/* 调用函数二

*/ else if (text[i][j]>='0' &&

days=days+1; text[i][j]<='9')

cout<<" is the "<

year."<

return 0; space++;

} else

other++;

}

int sum_day(int month,int day) // }

cout<<"upper case:"<

计算日期

{int i; cout<<"lower case:"<

int cout<<"digit :"<

day_tab[12]={31,28,31,30,31,30,31,31,30,31, cout<<"space :"<

30,31}; cout<<"other :"<

for (i=0;i

day+=day_tab[i]; }

return(day);

5.11题

} #include

using namespace std;

int leap(int year) //int main()

{ char a[5]={'*','*','*','*','*'};

判断是否为闰年

{int leap; int i,j,k;

char space=' ';

leap=year%4==0&&year%100!=0||year%400 for (i=0;i<5;i++) // 输出5==0;

return(leap);

{ cout<

每行前先换行

cout<<" "; // 每5.10题 行前面留4个空格

#include for (j=1;j<=i;j++)

using namespace std;

cout<

行再留一个空格

{int i,j,upper,lower,digit,space,other; for (k=0;k<5;k++)

char text[3][80];

cout<

行输出5个*号

for (i=0;i<3;i++) }

{cout<<"please input line "<

gets(text[i]); return 0;

for (j=0;j<80 && text[i][j]!='0';j++) }

{if (text[i][j]>='A'&& text[i][j]<='Z')

5.11题另一解

upper++; #include

else if (text[i][j]>='a' && #include

19 / 75

&&

using namespace std; #include

int main() using namespace std;

{ string stars="*****"; int main()

int i,j; {int j,n;

for (i=0;i<5;i++) // 输出5 char ch[80];

cout<<"input cipher code:";

{ cout<<" "; // 每行 gets(ch);

前面留4个空格

cout<<"cipher code:"<

for (j=1;j<=i;j++) j=0;

cout<<" "; // 每行 while (ch[j]!='0')

再插入i个空格

{ if ((ch[j]>='A') && (ch[j]<='Z'))

cout<

个*号

else if ((ch[j]>='a') && (ch[j]<='z'))

} ch[j]=219-ch[j];

return 0; else

} ch[j]=ch[j];

5.12题

j++;

#include }

using namespace std; n=j;

int main() cout<<"original text:";

{int j,n; for (j=0;j

char ch[80],tran[80]; putchar(ch[j]);

cout<<"input cipher code:"; cout<

gets(ch); return 0;

cout<<"cipher code:"<

j=0;

while (ch[j]!='0')

{ if ((ch[j]>='A') && (ch[j]<='Z'))

5.12另一解

tran[j]=155-ch[j]; #include

else if ((ch[j]>='a') && (ch[j]<='z')) #include

tran[j]=219-ch[j]; using namespace std;

else int main()

tran[j]=ch[j]; {int j;

j++; string ch="I will visit China

} week.",tran;

n=j; tran=ch;

cout<<"original text:"; cout<<"cipher code:"<

for (j=0;j

putchar(tran[j]); while (j<=())

cout<='A') && (ch[j]<='Z'))

return 0; tran[j]=155-ch[j];

} else if ((ch[j]>='a') && (ch[j]<='z'))

tran[j]=219-ch[j];

5.12题另一解

else

20 / 75

next

tran[j]=ch[j];

j++;

}

cout<<"original text:";

cout<

return 0;

}

5.12另一解

#include

#include

using namespace std;

int main()

{int j;

string ch="I will visit China next week.";

cout<<"cipher code:"<

j=0;

while (j<=())

{ if ((ch[j]>='A') && (ch[j]<='Z'))

ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z'))

ch[j]=219-ch[j];

j++;

}

cout<<"original text:";

cout<

return 0;

}

#include

#include

using namespace std;

int main()

{int j;

string ch="I will visit China next week.";

cout<<"cipher code:"<

j=0;

while (j<=())

{ if ((ch[j]>='A') && (ch[j]<='Z'))

ch[j]=155-ch[j];

else if ((ch[j]>='a') && (ch[j]<='z'))

ch[j]=219-ch[j];

j++;

}

cout<<"original text:";

cout<

return 0;

}

5.13题

#include

#include

using namespace std;

int main()

{char s1[80],s2[40];

int i=0,j=0;

cout<<"input string1:";

cin>>s1;

cout<<"input string2:";

cin>>s2;

while (s1[i]!='0')

i++;

while(s2[j]!='0')

s1[i++]=s2[j++];

s1[i]='0';

cout<<"The new string is:"<

return 0;

}

5.13另一解

#include

using namespace std;

int main()

{char s1[80],s2[40];

cout<<"input string1:";

cin>>s1;

cout<<"input string2:";

cin>>s2;

strcat(s1,s2);

cout<<"The new string is:"<

return 0;

}

5.13另一解

#include

#include

21 / 75

using namespace std;

int main()

{ string s1="week",s2="end";

cout<<"s1="<

cout<<"s2="<

s1=s1+s2;

cout<<"The new string is:"<

return 0;

}

5.14题

#include

#include

using namespace std;

int main()

{ const int n=5;

int i,j;

string str[n],temp;

cout<<"please input strings:"<

for(i=0;i

cin>>str[i];

for(i=0;i

for(j=0;j

if(str[j]>str[j+1])

{temp=str[j];str[j]=str[j+1];str[j+1]=temp;}

cout<

for(i=0;i

cout<

return 0;

}

5.15题

#include

#include

using namespace std;

int main()

{ const int n=5;

string str;

for(int i=0;i

{cout<<"please input string:";

cin>>str;

if(str[0]=='A')

cout<

return 0;

}

5.16题

#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.16题另一解

#include

#include

using namespace std;

int main()

{ string a;

int i,n;

char temp;

cout<<"please input a string:";

cin>>a;

n=();

for(i=0;i

{temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}

cout<

return 0;

}

22 / 75

5.17题

#include

#include

using namespace std;

const int n=10;

string name[n];

int num[n],score[n];

int main()

{int i;

void input_data();

input_data();

cout<

for(i=0;i

if(score[i]<60)

cout<

"<

return 0;

}

void input_data()

{int i;

for (i=0;i

{cout<<"input name,number and score of

student "<

cin>>name[i]>>num[i]>>score[i];}

}

cout<

for(i=0;i

if(score[i]<60)

cout<

"<

return 0;

}

void input_data()

{int i;

for (i=0;i

{cout<<"input name,number and score of

student "<

cin>>name[i]>>num[i]>>score[i];}

}

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);

}

6.2题另一解

#include

#include

第六章

6.1题

#include

#include

using namespace std;

const int n=10;

string name[n];

int num[n],score[n];

int main()

{int i;

void input_data();

input_data();

23 / 75

using namespace std;

// 输入10个数的函数

int main() {int i;

{void change(string &,string &); cout<<"input 10 numbers:";

string str1=" ", for (i=0;i<10;i++)

str2=" ", cin>>number[i];

str3=" "; }

char

*p1=&str1[0],*p2=&str2[0],*p3=&str3[0]; void max_min_value(int *number)

cout<<"input three line:"<

// 交换函数

gets(p1); { int *max,*min,*p,temp;

gets(p2); max=min=number;

gets(p3); for (p=number+1;p

if(str1>str2)change(str1,str2); if (*p>*max) max=p;

if(str1>str3)change(str1,str3);

// 将大数地址赋给 max

if(str2>str3)change(str2,str3); else if (*p<*min) min=p;

cout<

// 将小数地址赋给 min

cout<

p; // 将最小数与第一数交换

}

void change(string &st1,string &st2) temp=number[9];number[9]=*max;*max=te

/* 交换两个字符串 */ mp; // 将最小数与第一数交换

{string st; }

st=st1;st1=st2;st2=st;

} void output(int *number)

// 输出函数

{int *p;

6.3题

#include cout<<"now,they are: ";

using namespace std; for (p=number;p

int main() cout<<*p<<" ";

{ void input(int *number); cout<

void max_min_value(int *number); return;

void output(int *number); }

int number[10];

6.4题

input(number); #include

using namespace std;

// 调用输入10个数的函数

max_min_value(number); int main()

{void move(int *array,int n,int m);

// 调用交换函数

output(number); int number[20],n,m,i;

cout<<"how many numbers?";

// 调用输出函数

return 0;

// 询问共有多少个数

} cin>>n;

cout<<"input "<

void input(int *number)

//

要求输入

n

个数

24 / 75

for (i=0;i

比n-1少时(即未退出人数大于1时)执行循

cin>>number[i];

环体

cout<<"how many places do you want {if (*(p+i)!=0) k++;

move?"; // 询问后移多少个位置 if (k==3) // 将退出的人的 cin>>m;

编号置为0

move(number,n,m); {*(p+i)=0;

k=0;

//调用move 函数

cout<<"Now,they are:"<

for (i=0;i

cout<

cout<

if (i==n) i=0; // 报数到尾后,i return 0;

恢复为0

} }

while(*p==0) p++;

void move(int *array,int n,int m) cout<<"The last one is NO."<<*p<

return 0;

//使循环后移一次的函数

{int *p,array_end; }

array_end=*(array+n-1);

6.6题

for (p=array+n-1;p>array;p--) #include

*p=*(p-1); using namespace std;

*array=array_end; int main()

m--; {int length(char *p);

int len;

if (m>0) move(array,n,m); //递归调用,当循 char str[20];

环次数m减至为0时,停止调用

} cout<<"input string:";

cin>>str;

len=length(str);

6.5题

#include cout<<"The length of string is

using namespace std; "<

int main() return 0;

{int i,k,m,n,num[50],*p; }

cout<<"input number of person: n=";

int length(char *p) //求字符串 cin>>n;

长度的函数

p=num; {int n;

for (i=0;i

while (*p!='0')

*(p+i)=i+1; // 以1至n为序 {n++;

给每个人编号

p++;

i=0; // i为每次循环 }

时计数变量

return(n);

k=0; // k为按1,2,3}

报数时的计数变量

m=0; // m为退出人数 6.7题

#include

while (m

using namespace std; p=&s[0];

int main() while (*p!='n')

{void copystr(char *,char *,int); {if (('A'<=*p) && (*p<='Z'))

int m; ++upper;

char str1[20],str2[20]; else if (('a'<=*p) && (*p<='z'))

cout<<"input string:"; ++lower;

gets(str1); else if (*p==' ')

cout<<"which character do you want begin ++space;

to copy?"; else if ((*p<='9') && (*p>='0'))

cin>>m; ++digit;

if (strlen(str1)

cout<<"input error!"<

else p++;

{copystr(str1,str2,m); }

cout<<"result:"<

} case:"<

return 0; cout<<"space:"<

return 0;

void copystr(char *p1,char *p2,int m) }

//字符串部分复制函数*/

{int n;

6.9题

n=0; #include

while (n

{n++; int main()

p1++; {void move(int *);

} int a[3][3],*p,i;

while (*p1!='0') cout<<"input matrix:"<

{*p2=*p1; for (i=0;i<3;i++)

p1++; cin>>a[i][0]>>a[i][1]>>a[i][2];

p2++; p=&a[0][0];

} move(p);

*p2='0'; cout<<"Now,matrix:"<

} for (i=0;i<3;i++)

cout<

6.8题

#include "<

using namespace std; cout<

int main() return 0;

{int }

upper=0,lower=0,digit=0,space=0,other=0,i=0

; void move(int *pointer)

char *p,s[20]; {int i,j,t;

cout<<"input string:"; for (i=0;i<3;i++)

while ((s[i]=getchar())!='n') i++; for (j=i;j<3;j++)

26 / 75

{t=*(pointer+3*i+j);

值与中心元素互换

*(pointer+3*i+j)=*(pointer+3*j+i); *(p+12)=*pmax;

*(pointer+3*j+i)=t; *pmax=temp;

}

}

temp=*p; //将最小值

与左上角元素互换

*p=*pmin;

6.10题

#include *pmin=temp;

using namespace std;

int main() pmin=p+1;

{void change(int *p);

//将a[0][1]的地址赋给pmin,从该位置 int a[5][5],*p,i,j;

开始找最小的元素

cout<<"input matrix:"<

for (i=0;i<5;i++) //找第二最小值 for (i=0;i<5;i++)

的地址赋给

pmin

for (j=0;j<5;j++)

//输入矩阵

for (j=0;j<5;j++) if (((p+5*i+j)!=p) && (*pmin >

cin>>a[i][j]; *(p+5*i+j))) pmin=p+5*i+j;

p=&a[0][0];

temp=*pmin; //将第二最//使p指向0行0列元素 小值与右上角元素互换

change(p); *pmin=*(p+4);

*(p+4)=temp;

//调用函数,实现交换

cout<<"Now,matrix:"<

for (i=0;i<5;i++) // pmin=p+1;

输出已交换的矩阵 for (i=0;i<5;i++) //找第三最小值的 {for (j=0;j<5;j++)

地址赋给pmin

cout<

cout<

} &&(*pmin>*(p+5*i+j))) pmin=p

return 0; +5*i+j;

}

temp=*pmin; // 将第三最小

值与左下角元素互换

void change(int *p) // *pmin=*(p+20);

*(p+20)=temp;

交换函数

{int i,j,temp;

int *pmax,*pmin; pmin=p+1;

pmax=p;

for (i=0;i<5;i++) // 找第四最小值 pmin=p;

的地址赋给pmin */

for (j=0;j<5;j++)

for (i=0;i<5;i++) //找最大值和最小 if (((p+5*i+j)!=p) &&((p+5*i+j)!=(p+4))

值的地址,并赋给 pmax,pmin

for (j=i;j<5;j++) && ((p+5*i+j)!=(p+20)) &&

{if (*pmax<*(p+5*i+j)) pmax=p+5*i+j; (*pmin>*(p+5*i+j)))

if (*pmin>*(p+5*i+j)) pmin=p+5*i+j; pmin=p+5*i+j;

}

temp=*pmin; //将第四最小 temp=*(p+12); //将最大值与右下角元素互换

27 / 75

*pmin=*(p+24);

*(p+24)=temp;

}

6.10题另一解

#include

using namespace std;

int main()

{void change(int *p);

int a[5][5],*p,i,j;

cout<<"input matrix:"<

for

*pmax=temp;

temp=*p; //将最小值与左上角元素互换

*p=*pmin;

*pmin=temp;

pmin=p+1;

//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素

for (i=0;i<5;i++) //找第二最小值(i=0;i<5;i++)

的地址赋给

pmin

for (j=0;j<5;j++)

//输入矩阵

for (j=0;j<5;j++) {if(i==0 && j==0) continue;

cin>>a[i][j]; if (*pmin > *(p+5*i+j)) pmin=p+5*i+j;

p=&a[0][0]; }

//使p指向0行0列元素

change(p);

//调用函数,实现交换

cout<<"Now,matrix:"<

for (i=0;i<5;i++) //输出已交换的矩阵

{for (j=0;j<5;j++)

cout<

cout<

}

return 0;

}

void change(int *p) //交换函数

{int i,j,temp;

int *pmax,*pmin;

pmax=p;

pmin=p;

for (i=0;i<5;i++) //找最大值和最小值的地址,并赋给 pmax,pmin

for (j=i;j<5;j++)

{if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;

if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;

}

temp=*(p+12); //将最大值与中心元素互换

*(p+12)=*pmax;

temp=*pmin; //将第二最小值与右上角元素互换

*pmin=*(p+4);

*(p+4)=temp;

pmin=p+1;

for (i=0;i<5;i++) //找第三最小值的地址赋给pmin

for (j=0;j<5;j++)

{if((i==0 && j==0) ||(i==0 && j==4))

continue;

if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;

}

temp=*pmin; // 将第三最小值与左下角元素互换

*pmin=*(p+20);

*(p+20)=temp;

pmin=p+1;

for (i=0;i<5;i++) // 找第四最小值的地址赋给pmin

for (j=0;j<5;j++)

{if ((i==0 && j==0) ||(i==0 &&

j==4)||(i==4 && j==0)) continue;

if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;

}

temp=*pmin; //将第四最小值与右下角元素互换

28 / 75

*pmin=*(p+24);

*(p+24)=temp;

}

6.11题

#include

using namespace std;

int main()

{void sort(char s[][6]);

int i;

char str[10][6];

cout<<"input 10 strings:"<

for (i=0;i<10;i++)

cin>>str[i];

sort(str);

cout<<"Now,the sequence is:"<

for (i=0;i<10;i++)

cout<

return 0;

}

void sort(char s[][6])

{int i,j;

char *p,temp[10];

p=temp;

for (i=0;i<9;i++)

for (j=0;j<9-i;j++)

if (strcmp(s[j],s[j+1])>0)

{strcpy(p,s[j]);

strcpy(s[j],s[+j+1]);

strcpy(s[j+1],p);

}

}

6.11题另一解

#include

using namespace std;

int main()

{void sort(char (*p)[6]);

int i;

char str[10][6];

char (*p)[6];

cout<<"input 10 strings:"<

for (i=0;i<10;i++)

cin>>str[i];

p=str;

sort(p);

cout<<"Now,the sequence is:"<

for (i=0;i<10;i++)

cout<

return 0;

}

void sort(char (*s)[6])

{int i,j;

char temp[6],*t=temp;

for (i=0;i<9;i++)

for (j=0;j<9-i;j++)

if (strcmp(s[j],s[j+1])>0)

{strcpy(t,s[j]);

strcpy(s[j],s[+j+1]);

strcpy(s[j+1],t);

}

}

6.11题另一解

#include

#include

using namespace std;

int main()

{void sort(string *);

int i;

string str[10],*p=str;

cout<<"input 10 strings:"<

for (i=0;i<10;i++)

cin>>str[i];

sort(p);

cout<<"Now,the sequence is:"<

for (i=0;i<10;i++)

cout<

return 0;

}

void sort(string *s)

{int i,j;

string temp;

for (i=0;i<9;i++)

for (j=0;j<9-i;j++)

if (s[j]>s[j+1])

{temp=s[j];

s[j]=s[+j+1];

29 / 75

s[j+1]=temp;

}

}

6.12题

#include

using namespace std;

int main()

{void sort(char *[]);

int i;

char *p[10],str[10][20];

for (i=0;i<10;i++)

p[i]=str[i]; //将第i个字符串的首地址赋予指针数组p的第i个元素

cout<<"input 10 strings:"<

for (i=0;i<10;i++)

cin>>p[i];

sort(p);

cout<<"Now,the sequence is:"<

for (i=0;i<10;i++)

cout<

return 0;

}

void sort(char *s[])

{int i,j;

char *temp;

for (i=0;i<9;i++)

for (j=0;j<9-i;j++)

if (strcmp(*(s+j),*(s+j+1))>0)

{temp=*(s+j);

*(s+j)=*(s+j+1);

*(s+j+1)=temp;

}

}

6.13题

#include

#include

using namespace std;

int main()

{float integral(float (*p)(float),float a,float

b,int n);

float a1,b1,a2,b2,a3,b3,c,(*p)(float);

float fsin(float); // 对fsin函数作声明

float fcos(float); // 对fcos函数作声明

float fexp(float); // 对fexp函数作声明

int n=20;

cout<<"input a1,b1:"; //输入求sin(x) 定积分的下限和上限

cin>>a1>>b1;

cout<<"input a2,b2:"; // 输入求cos(x) 定积分的下限和上限

cin>>a2>>b2;

cout<<"input a3,b3:"; // 输入求#include

cin>>a3>>b3;

p=fsin;

c=integral(p,a1,b1,n); // 求出sin(x)的定积分

cout<<"The integral of sin(x) is :"<

p=fcos;

c=integral(p,a2,b2,n); // 求出cos(x)的 定积分

cout<<"The integral of cos(x) is :"<

p=fexp;

c=integral(p,a3,b3,n); // 求出

的定积分

cout<<"The integral of exp(x) is :"<

return 0;

}

float integral(float (*p)(float),float a,float b,int

n)

//用矩形法求定积分的通用函数

{int i;

float x,h,s;

h=(b-a)/n;

x=a;

s=0;

for (i=1;i<=n;i++)

{x=x+h;

s=s+(*p)(x)*h;

}

return(s);

}

30 / 75

float fsin(float x) //

计算sin(x) 的函数

{return sin(x);}

float fcos(float x) //

计算cos(x) 的函数

{return cos(x);}

float fexp(float x) //

计算exp(x)的函数

{return exp(x);}

6.13题

#include

#include

using namespace std;

int main()

{float integral(float (*p)(float),float a,float

b,int n);

float a1,b1,a2,b2,a3,b3,c,(*p)(float);

float fsin(float); // 对fsin函数作声明

float fcos(float); // 对fcos函数作声明

float fexp(float); // 对fexp函数作声明

int n=20;

cout<<"input a1,b1:"; //输入求sin(x) 定积分的下限和上限

cin>>a1>>b1;

cout<<"input a2,b2:"; // 输入求cos(x) 定积分的下限和上限

cin>>a2>>b2;

cout<<"input a3,b3:"; // 输入求#include

cin>>a3>>b3;

p=fsin;

c=integral(p,a1,b1,n); // 求出sin(x)的定积分

cout<<"The integral of sin(x) is :"<

p=fcos;

c=integral(p,a2,b2,n); // 求出cos(x)的 定积分

cout<<"The integral of cos(x) is :"<

p=fexp;

c=integral(p,a3,b3,n); // 求出

的定积分

cout<<"The integral of exp(x) is :"<

return 0;

}

float integral(float (*p)(float),float a,float b,int

n)

//用矩形法求定积分的通用函数

{int i;

float x,h,s;

h=(b-a)/n;

x=a;

s=0;

for (i=1;i<=n;i++)

{x=x+h;

s=s+(*p)(x)*h;

}

return(s);

}

float fsin(float x) //

计算sin(x) 的函数

{return sin(x);}

float fcos(float x) //

计算cos(x) 的函数

{return cos(x);}

float fexp(float x) //

计算exp(x)的函数

{return exp(x);}

6.14题

#include

using namespace std;

int main()

{ void sort (char *p,int m);

int i,n;

char *p,num[20];

cout<<"input n:";

cin>>n;

31 / 75

cout<<"please input these

numbers:"<

for (i=0;i

cin>>num[i];

p=&num[0];

sort(p,n);

cout<<"Now,the sequence is:"<

for (i=0;i

cout<

cout<

return 0;

}

void sort (char *p,int m) //

将n个数逆序排列函数

{int i;

char temp, *p1,*p2;

for (i=0;i

{p1=p+i;

p2=p+(m-1-i);

temp=*p1;

*p1=*p2;

*p2=temp;

}

}

6.15题

#include

using namespace std;

int main()

{void avsco(float *,float *);

void avcour1(char (*)[10],float *);

void fali2(char course[5][10],int num[],float

*pscore,float aver[4]);

void good(char course[5][10],int

num[4],float *pscore,float aver[4]);

int i,j,*pnum,num[4];

float score[4][5],aver[4],*pscore,*paver;

char course[5][10],(*pcourse)[10];

cout<<"input course:"<

pcourse=course;

for (i=0;i<5;i++)

cin>>course[i];

cout<<"input NO. and scores:"<

cout<<"NO.";

for (i=0;i<5;i++)

cout<<","<

cout<

pscore=&score[0][0];

pnum=&num[0];

for (i=0;i<4;i++)

{cin>>*(pnum+i);

for (j=0;j<5;j++)

cin>>*(pscore+5*i+j);

}

paver=&aver[0];

cout<

avsco(pscore,paver); //

求出每个学生的平均成绩

avcour1(pcourse,pscore);

// 求出第一门课的平均成绩

cout<

fali2(pcourse,pnum,pscore,paver); //

找出两门课不及格的学生

cout<

good(pcourse,pnum,pscore,paver);

// 找出成绩好的学生

return 0;

}

void avsco(float *pscore,float *paver) // 求每个学生的平均成绩的函数

{int i,j;

float sum,average;

for (i=0;i<4;i++)

{sum=0.0;

for (j=0;j<5;j++)

sum=sum+(*(pscore+5*i+j));

//累计每个学生的各科成绩

average=sum/5;

//计算平均成绩

*(paver+i)=average;

}

}

void avcour1(char (*pcourse)[10],float

*pscore) // 求第一课程的平均成

绩的函数

32 / 75

{int i;

float sum,average1;

sum=0.0;

for (i=0;i<4;i++)

sum=sum+(*(pscore+5*i));

//累计每个学生的得分

average1=sum/4;

//计算平均成绩

cout<<"course 1: "<<*pcourse<<",average

score:"<

}

void fail2(char course[5][10],int num[],float

*pscore,float aver[4])

// 找两门以上课程不及格的学生的函数

{int i,j,k,labe1;

cout<<" ==========Student who failed

in two courses ======= "<

cout<<"NO. ";

for (i=0;i<5;i++)

cout<

cout<<" average"<

for (i=0;i<4;i++)

{labe1=0;

for (j=0;j<5;j++)

if (*(pscore+5*i+j)<60.0) labe1++;

if (labe1>=2)

{cout<

for (k=0;k<5;k++)

cout<<*(pscore+5*i+k)<<" ";

cout<<" "<

}

}

}

void good(char course[5][10],int num[4],float

*pscore,float aver[4])

// 找成绩优秀学生(全部课程成绩在85分以上或平均成绩在90分以上)的函数

{int i,j,k,n;

cout<<" ======Students whose score

is good======"<

cout<<"NO. ";

for (i=0;i<5;i++)

cout<

cout<<" average"<

for (i=0;i<4;i++)

{n=0;

for (j=0;j<5;j++)

if (*(pscore+5*i+j)>85.0) n++;

if ((n==5)||(aver[i]>=90))

{cout<

for (k=0;k<5;k++)

cout<<*(pscore+5*i+k)<<" ";

cout<<" "<

}

}

}

6.16题

#include

using namespace std;

int main()

{char str[50],*pstr;

int i,j,k,m,e10,digit,ndigit,a[10],*pa;

cout<<"input a string:"<

gets(str);

cout<

pstr=&str[0]; //字符指针pstr指向数组str首元素

pa=&a[0]; //指针pa指向a数组首元素

ndigit=0; //ndigit代表有多少个整数

i=0; //i代表字符串中的第几个字符/

j=0; //j代表连续数字的位数

while(*(pstr+i)!='0')

{if((*(pstr+i)>='0') && (*(pstr+i)<='9'))

j++;

else

{if (j>0)

{digit=*(pstr+i-1)-48; //将个数位赋予digit

k=1;

while (k

的其它位的数值累计于digit

{e10=1;

for (m=1;m<=k;m++)

e10=e10*10;

//e10代表该位数所应乘的因子

digit=digit+(*(pstr+i-1-k)-48)*e10;

//将该位数的数值累加于digit

k++;

//位数k自增

}

*pa=digit; //将数值放在数组a中

ndigit++;

pa++; //指针pa指向a数组下一元素

j=0;

}

}

i++;

}

if (j>0) //以数字结尾字符串的最后一个数据

{digit=*(pstr+i-1)-48; //将个数位赋予digit

k=1;

while (k

{e10=1;

for (m=1;m<=k;m++)

e10=e10*10; //e10代表位数所应乘的因子

digit=digit+(*(pstr+i-1-k)-48)*e10; //将该位数的数值累加于digit

k++; /*位数K自增*/

}

*pa=digit; //将数值放到数组a中

ndigit++;

j=0;

}

printf("There are %d numbers in this line.

They are:n",ndigit);

j=0;

pa=&a[0];

for (j=0;j

cout<<*(pa+j)<

cout<

return 0;

}

6.17题

#include

using namespace std;

int main()

{int strcmp(char *p1,char *p2);

int m;

char str1[20],str2[20],*p1,*p2;

cout<<"input two strings:"<

cin>>str1;

cin>>str2;

p1=&str1[0];

p2=&str2[0];

m=strcmp(p1,p2);

cout<<"result:"<

return 0;

}

int strcmp(char *p1,char *p2) //自已定义字符串比较函数

{int i;

i=0;

while(*(p1+i)==*(p2+i))

if (*(p1+i++)=='0') return(0); //全部字符相同时返回结果0

return(*(p1+i)-*(p2+i)); //不相同时返回结果为第一对不相同字符的ASCII码

的差值

}

6.18题

#include

using namespace std;

int main()

{char *month_name[13]={"illegal

month","January","February","March","April",

34 / 75

"May","June","July","August","September","October",

"November","December"};

int n;

cout<<"input month:"<

cin>>n;

if ((n<=12) && (n>=1))

cout<<"It is "<<*(month_name+n)<

else

cout<<"It is wrong"<

return 0;

}

6.19题

#include

using namespace std;

int main()

{void sort(char **p);

const int m=20; //定义字符串的最大长度

int i;

char **p,*pstr[5],str[5][m];

for (i=0;i<5;i++)

pstr[i]=str[i]; /*将第i个字符串的首地址赋予指针数组 pstr 的第i个元素*/

cout<<"input 5 strings:"<

for (i=0;i<5;i++)

cin>>pstr[i];

p=pstr;

sort(p);

cout<<"strings sorted:"<

for (i=0;i<5;i++)

cout<

return 0;

}

void sort(char **p) //冒泡法对5个字符串排序函数

{int i,j;

char *temp;

for (i=0;i<5;i++)

{for (j=i+1;j<5;j++)

{if (strcmp(*(p+i),*(p+j))>0) //比

较后交换字符串地址

{temp=*(p+i);

*(p+i)=*(p+j);

*(p+j)=temp;

}

}

}

}

6.20题

#include

using namespace std;

int main()

{void sort(int **p,int n);

int i,n,data[10],**p,*pstr[10];

cout<<"input n:";

cin>>n;

for (i=0;i

pstr[i]=&data[i]; /*将第i个整数的地址赋予指针数组 pstr 的第i个元素*/

cout<<"input "<

numbers:"<

for (i=0;i

cin>>*pstr[i];

p=pstr;

sort(p,n);

cout<<"Now,the sequence is:"<

for (i=0;i

cout<<*pstr[i]<<" ";

cout<

return 0;

}

void sort(int **p,int n)

{int i,j,*temp;

for (i=0;i

{for (j=i+1;j

{if (**(p+i)>**(p+j)) //比较后交换整数地址

{temp=*(p+i);

*(p+i)=*(p+j);

*(p+j)=temp;

}

7.1题

#include

35 / 75

using namespace std;

struct

{ int year;

int month;

int day;

}date;

int main()

{int days;

cout<<"input year,month,day:";

cin>>>>>>;

switch()

{ case 1: days=; break;

case 2: days=+31; break;

case 3: days=+59; break;

case 4: days=+90; break;

case 5: days=+120; break;

case 6: days=+151; break;

case 7: days=+181; break;

case 8: days=+212; break;

case 9: days=+243; break;

case 10: days=+273; break;

case 11: days=+304; break;

case 12: days=+334; break;

}

if (( %4== 0 && % 100 !=

0

|| % 400 == 0) &&

>=3)

days+=1;

cout<<<<"/"<<<<" is the

"<

<<"th day in "<<<<"."<

return 0;

}

int day;

}date;

int main()

{int i,days;

int

day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

cout<<"input year,month,day:";

cin>>>>>>;

days=0;

for (i=1;i<;i++)

days+=day_tab[i];

days+=;

if ((%4==0 &&

%100!=0 || %400==0) &&

>=3)

days+=1;

cout<<<<"/"<<<<"

is the "<

<<"th day in

"<<<<"."<

return 0;

}

第七章

7.1题另一解

#include

using namespace std;

struct

{int year;

int month;

7.2题

#include

using namespace std;

struct y_m_d

{int year;

int month;

int day;

};

int main()

{y_m_d date;

int days(int,int,int);

/* 对days函数的声明 */

int day_sum;

cout<<"input year,month,day:";

cin>>>>>>;

day_sum=days(,,);

cout<<<<"/"<<<<" is

36 / 75

the "<

<<"th day in "<<<

return 0; }

} print(stu);

return 0;

int days(int year,int month,int day) }

/* 定义days函数 */

{int day_sum,i; void print(student stu[6])

int {int i,j;

day_tab[13]={0,31,28,31,30,31,30,31,31,30,3 cout<<" NO. name score1

1,30,31}; score2 score3"<

day_sum=0; for (i=0;i

for (i=1;i

day_sum+=day_tab[i]; "<

day_sum+=day; for (j=0;j<3;j++)

if ((year%4==0 && year%100!=0 || cout<

year%4==0) && month>=3) ";

day_sum+=1; cout<

return(day_sum); }

} }

7.3题 7.4题

#include #include

#include #include

using namespace std; using namespace std;

const int n=5; const int n=5;

struct student struct student

{ char num[6]; { char num[6];

char name[8]; char name[8];

int score[4]; int score[4];

}stu[n]; }stu[n];

int main() int main()

{void print(student stu[6]); {void input(student stu[]);

int i,j; void print(student stu[]);

for (i=0;i

{cout<<"input scores of student print(stu);

"<

cout<<"NO.: "; }

cin>>stu[i].num;

cout<<"name: "; void input(student stu[])

cin>>stu[i].name; {int i,j;

for (j=0;j<3;j++) for (i=0;i

{cout<<"score "<

cin>>stu[i].score[j]; "<

37 / 75

cout<<"NO.: ";

cin>>stu[i].num;

cout<<"name: ";

cin>>stu[i].name;

for (j=0;j<3;j++)

{cout<<"score "<

cin>>stu[i].score[j];

}

}

}

void print(student stu[])

{int i,j;

cout<<" NO. name score1

score2 score3"<

for (i=0;i

{cout<

"<

for (j=0;j<3;j++)

cout<

";

cout<

}

}

7.5题

#include

#include

using namespace std;

const int n=10;

struct student

{ char num[6];

char name[8];

int score[4];

float avr;

} stu[n];

int main()

{ int i,j,max,maxi,sum;

float average;

for (i=0;i

{cout<<"input scores

"<

cout<<"NO.:";

cin>>stu[i].num;

of student

cout<<"name:";

cin>>stu[i].name;

for (j=0;j<3;j++)

{cout<<"score "<

cin>>stu[i].score[j];

}

cout<

}

average=0;

max=0;

maxi=0;

for (i=0;i

{sum=0;

for (j=0;j<3;j++)

sum+=stu[i].score[j];

stu[i].avr=sum/3.0;

average+=stu[i].avr;

if (sum>max)

{max=sum;

maxi=i;

}

}

average/=n;

cout<<" NO. name

score1 score2 score3

average"<

for (i=0;i

{cout<

"<

for (j=0;j<3;j++)

cout<

";

cout<

}

cout<<"average="<

cout<<"The highest score

is :"<

total:"<

return 0;

}

7.6题

#include

using namespace std;

38 / 75

#define NULL 0

struct student

{long num;

float score;

struct student *next;

};

int main()

{student a,b,c,*head,*p;

=10001; =89.5;

=10003; =90;

数带回一个指向链表头的指针

{student *head;

student *p1,*p2;

int n=0;

p1=p2=new student; //开辟一个新单元,并使p1,p2指向它

cin>>p1->num>>p1->score;

head=NULL;

while(p1->num!=0)

{n=n+1;

if(n==1) head=p1;

=10007; =85; //为结点 else p2->next=p1;

的num和score成员赋值

p2=p1;

head=&a; //将结 p1=new student;

点a的起始地址赋给头指针head

cin>>p1->num>>p1->score;

=&b; //将结}

点b的起始地址赋给a结点的next成员

p2->next=NULL;

=&c; //将结return(head);

点c的起始地址赋给b结点的next成员

}

=NULL; //c结点

的next成员不存放其他结点地址

p=head; //使p指针指向a结点 7.7题

do #include

{cout<num<<" "<score<

#define NULL 0

//输出p指向的结点的数据

p=p->next; struct student

{long num;

//使p指向下一结点

}while(p!=NULL); float score;

student *next;

//输出完c结点后p的值为NULL

return 0; };

} int n;

void print(student *head)

{student *p;

7.6题另一解

#include

cout<<"Now,These "<

#define NULL 0 p=head;

struct student if(head!=NULL)

{long num; do

float score; {cout<num<<" "<score<

student *next; p=p->next;

}; }while(p!=NULL);

//定义n为全局变量,}

本文件模块中各函数均可使用它

student *creat(void) //定义函数。此函7.7题另一解

39 / 75

#include

using namespace std;

#define NULL 0

struct student

{long num;

float score;

student *next;

};

int n;

student *del(student *head,long num)

{student *p1,*p2;

if (head==NULL) //是空表

{cout<<"list null!"<

p1=head; //使p1指向第一个结点

while(num!=p1->num && p1->next!=NULL)

};

int n;

student *del(student *head,long num)

{student *p1,*p2;

if (head==NULL) //是空表

{cout<<"list null!"<

p1=head;

//使p1指向第一个结点

while(num!=p1->num &&

p1->next!=NULL) //p1指向的不是所要找的结点且后面

还有结点

{p2=p1; p1=p1->next;}

//p1后移一个结点

if(num==p1->num)

//找到了

//p1指向的不是所要找的结点且后面还有 {if(p1==head) head=p1->next; //若p1指向结点 的是首结点,把第二个结点地址赋予

{p2=p1; p1=p1->next;} head

//p1后移一个结点 else p2->next=p1->next; //否则将下一 if(num==p1->num)

结点地址赋给前一结点地址

cout<<"delete:"<

//找到了

n=n-1;

{if(p1==head) head=p1->next; //若p1指}

向的是首结点,把第二个结点地址赋予

head

else cout<<"cannot find "<next=p1->next; //否则将下一到该结点

return(head);

结点地址赋给前一结点地址

cout<<"delete:"<

n=n+1;

}

7.9题

#include

else cout<<"cannot find "<

不到该结点

return(head); #define NULL 0

} struct student

{long num;

float score;

7.8题

#include student*next;

using namespace std; };

#define NULL 0 int n;

struct student

{long num; student *insert(student *head,student *stud)

float score; {student *p0,*p1,*p2;

student *next; p1=head; // 40 / 75

使p1指向第一个结点

p0=stud; //指向要插入的结点

if(head==NULL) //原来的链表是空表

{head=p0;p0->next=NULL;} //使p0指向的结点作为头结点

else

{while((p0->num>p1->num) &&

(p1->next!=NULL))

{p2=p1; //使p2指向刚才p1指向的结点

p1=p1->next;} //p1后移一个结点

if(p0->num<=p1->num)

{if(head==p1) head=p0; //插到原来第一个结点之前

else p2->next=p0; //插到p2指向的结点之后

p0->next=p1;}

else

{p1->next=p0; p0->next=NULL;}} //插到最后的结点之后

n=n+1;

//结点数加1

return (head);

}

7.10题

#include

using namespace std;

#define NULL 0

struct student

{long num;

float score;

student *next;

};

int n;

int main()

{ student *creat(void);

student *del(student *,long);

student *insert(student *,student *);

void print(student *);

student *head,stu;

long del_num;

cout<<"input records:"<

head=creat();

//返回头指针

print(head);

//输出全部结点

cout<

cin>>del_num;

//输入要删除的学号

head=del(head,del_num);

//删除后链表的头地址

print(head);

//输出全部结点

cout<

//输入要插入的结点

cin>>>>;

head=insert(head,&stu); //返回地址

print(head);

//输出全部结点

cout<

//输入要插入的结点

cin>>>>;

head=insert(head,&stu); //返回地址

print(head);

return 0;

}

student *creat(void) //建立链表的函数

{student *head;

student *p1,*p2;

n=0;

p1=p2=new student; //开辟一个新单元,并使p1,p2指向它

cin>>p1->num>>p1->score;

head=NULL;

while(p1->num!=0)

{n=n+1;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

41 / 75

p1=new student;

cin>>p1->num>>p1->score;

}

p2->next=NULL;

return(head);

}

student *del(student *head,long num) //删除结的函数

{student *p1,*p2;

if (head==NULL) //是空表

{cout<<"list null!"<

p1=head; //使p1指向第一个结点

while(num!=p1->num && p1->next!=NULL)

{head=p0;p0->next=NULL;} //使p0指向的结点作为头结点

else

{while((p0->num>p1->num) &&

(p1->next!=NULL))

{p2=p1; //使p2指向刚才p1指向的结点

p1=p1->next;} //p1后移一个结点

if(p0->num<=p1->num)

{if(head==p1) head=p0; //插到原来第一个结点之前

else p2->next=p0; //插到p2指向的结点之后*/

p0->next=p1;}

else

//p1指向的不是所要找的结点且后面还有 {p1->next=p0; p0->next=NULL;}} //插到结点 最后的结点之后

{p2=p1; p1=p1->next;}

n=n+1;

//结//p1后移一个结点 点数加1

if(num==p1->num) return (head);

}

//找到了

{if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予 void print(student *head) //输出链head

表的函数

{student *p;

else p2->next=p1->next; //否则将下一结点地址赋给前一结点地址 cout<<"Now,These "<

n=n-1; p=head;

} if(head!=NULL)

do

else cout<<"cannot find "<num<<" "<score<

不到该结点

return(head); p=p->next;

} }while(p!=NULL);

}

student *insert(student *head,student *stud)

//插入结点的函数 7.10题另一解

{student *p0,*p1,*p2; #include

p1=head; //using namespace std;

#define NULL 0

使p1指向第一个结点

p0=stud; //struct student

{long num;

指向要插入的结点

float score;

if(head==NULL) //原 student *next;

来的链表是空表

42 / 75

};

int n;

int main()

{ student *creat(void);

student *del(student *,long);

student *insert(student *,student *);

void print(student *);

student *head,*stu;

long del_num;

cout<<"input records:"<

head=creat();

//返回头指针

print(head);

//输出全部结点

cout<

cin>>del_num;

//输入要删除的学号

while(del_num!=0)

{head=del(head,del_num);

//删除后链表的头地址

print(head);

//输出全部结点

cout<<"input the deleted number:";

cin>>del_num;

}

cout<

//输入要插入的结点

stu=new

//开辟一个新结点

cin>>stu->num>>stu->score;

while(stu->num!=0)

{head=insert(head,stu); //返回地址

print(head);

student *creat(void) //建立链表的函数

{student *head;

student *p1,*p2;

n=0;

p1=p2=new student; //开辟一个新单元,并使p1,p2指向它

cin>>p1->num>>p1->score;

head=NULL;

while(p1->num!=0)

{n=n+1;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=new student;

cin>>p1->num>>p1->score;

}

p2->next=NULL;

return(head);

}

student *del(student *head,long num) //删除结点的函数

{student *p1,*p2;

if (head==NULL) //是空表

{cout<<"list null!"<

p1=head; //student;

使

p1

指向第一个结点

while(num!=p1->num && p1->next!=NULL)

//p1指向的不是所要找的结点且后面还有

结点

{p2=p1; p1=p1->next;}

//p1后移一个结点

if(num==p1->num)

//找到了

{if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予

head

else p2->next=p1->next; //否则将下一结点地址赋给前一结点地址

cout<<"delete:"<

n=n-1;

}

//输出全部结点

cout<

//输入要插入的结点

stu=new student;

cin>>stu->num>>stu->score;

}

return 0;

}

43 / 75

else cout<<"cannot find "<

return(head);

}

student *insert(student *head,student *stud)

//插入结点的函数

{student *p0,*p1,*p2;

p1=head; //使p1指向第一个结点

p0=stud; //指向要插入的结点

if(head==NULL) //原来的链表是空表

{head=p0;p0->next=NULL;} //使p0指向的结点作为头结点

else

{while((p0->num>p1->num) &&

(p1->next!=NULL))

{p2=p1; //使p2指向刚才p1指向的结点

p1=p1->next;} //p1后移一个结点

if(p0->num<=p1->num)

{if(head==p1) head=p0; //插到原来第一个结点之前

else p2->next=p0; //插到p2指向的结点之后

p0->next=p1;}

else

{p1->next=p0; p0->next=NULL;}} //插到最后的结点之后

n=n+1; //结点数加1

return (head);

}

void print(student *head) //输出链表的函数

{student *p;

cout<<"Now,These "<

p=head;

if(head!=NULL)

do

{cout<num<<" "<score<

p=p->next;

}while(p!=NULL);

}

第八章

8.1题

#include

using namespace std;

class Time

{public: //成员改为公用的

int hour;

int minute;

int sec;

};

Time t;

void set_time(void) //在main函数之前定义

{

cin>>;

cin>>;

cin>>;

}

void show_time(void) //在main函数之前定义

{

cout<<<<":"<<<<":"<<<

}

int main()

{set_time();

show_time();

return 0;

}

8.2题

44 / 75

#include

using namespace std;

class Time

void Time::show_time(void)

{cout<

{public:

void set_time(void) Time t;

{cin>>hour; int main()

cin>>minute; { _time();

cin>>sec; _time();

} return 0;

void show_time(void) }

{cout<

8.4题

//xt8-4.h(student.h)

private: int hour; class Student

int minute; { public:

int sec; void display( );

}; void set_value();

private:

Time t; int num;

int main() char name[20];

{ char sex ;

_time(); };

_time();

return 0;

8.4题

} //()

#include

8.3题

using namespace std;

#include #include "xt8-4.h"

using namespace std; int main()

class Time {Student stud;

{public: _value();

void set_time(void); y();

void show_time(void); return 0;

private: }

int hour;

8.4题另一解

int minute;

//(即)

int sec; #include

};

//在此文件中进行函数的定义

#include

void Time::set_time(void) using namespace

{cin>>hour;

//不要漏写此行

cin>>minute; void Student::display( )

cin>>sec; { cout<<"num:"<

} cout<<"name:"<

45 / 75

"xt8-4.h"

std;

cout<<"sex:"<

}

void Student::set_value()

{ cin>>num;

cin>>name;

cin>>sex;

}

8.5题

//xt8-5.h(arraymax.h)

class Array_max

{public:

void set_value();

void max_value();

void show_value();

private:

int array[10];

int max;

};

8.5题

//()

#include

#include "xt8-5.h"

int main()

{Array_max arrmax;

_value();

_value();

_value();

return 0;

}

8.5题

//()

#include

using namespace std;

#include "xt8-5.h"

void Array_max::set_value()

{ int i;

for (i=0;i<10;i++)

cin>>array[i];

}

void Array_max::max_value()

{int i;

max=array[0];

for (i=1;i<10;i++)

if(array[i]>max) max=array[i];

}

void Array_max::show_value()

{cout<<"max="<

}

8.6题

#include

using namespace std;

class Box

{public:

void get_value();

float volume();

void display();

public:

float lengh;

float width;

float height;

};

void Box::get_value()

{ cout<<"please input lengh, width,height:";

cin>>lengh;

cin>>width;

cin>>height;

}

float Box::volume()

{ return(lengh*width*height);}

void Box::display()

{ cout<

int main()

{Box box1,box2,box3;

_value();

cout<<"volmue of bax1 is ";

y();

_value();

cout<<"volmue of bax2 is ";

46 / 75

y();

_value();

cout<<"volmue of bax3 is ";

y();

return 0;

}

8.6题另一解

#include

using namespace std;

class Box

{public:

void get_value();

void volume();

void display();

public:

float lengh;

float width;

float height;

float vol;

};

void Box::get_value()

{ cout<<"please input lengh, width,height:";

cin>>lengh;

cin>>width;

cin>>height;

}

void Box::volume()

{ vol=lengh*width*height;}

void Box::display()

{ cout<

int main()

{Box box1,box2,box3;

_value();

();

cout<<"volmue of bax1 is ";

y();

_value();

();

cout<<"volmue of bax2 is ";

y();

_value();

();

cout<<"volmue of bax3 is ";

y();

return 0;

}

第九章

9.2题

#include

using namespace std;

class Date

{public:

Date(int,int,int);

Date(int,int);

Date(int);

Date();

void display();

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int

y):month(m),day(d),year(y)

{ }

Date::Date(int m,int d):month(m),day(d)

{year=2005;}

Date::Date(int m):month(m)

{day=1;

year=2005;

}

Date::Date()

{month=1;

day=1;

year=2005;

}

47 / 75

void Date::display()

{cout<

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

y();

y();

y();

y();

return 0;

}

9.3题

#include

using namespace std;

class Date

{public:

Date(int=1,int=1,int=2005);

void display();

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int

y):month(m),day(d),year(y)

{ }

void Date::display()

{cout<

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

y();

y();

y();

y();

return 0;

}

9.4题

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void display();

private:

int num;

float score;

};

void Student::display()

{cout<

int main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

Student *p=stud;

for(int i=0;i<=2;p=p+2,i++)

p->display();

return 0;

}

9.5题

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

int num;

48 / 75

float score;

};

void main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

void max(Student* );

Student *p=&stud[0];

max(p);

}

void max(Student *arr)

{float max_score=arr[0].score;

int k=0;

for(int i=1;i<5;i++)

if(arr[i].score>max_score)

{max_score=arr[i].score;k=i;}

cout<

}

9.6题

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void display(){cout<

"<

private:

int num;

float score;

};

int main()

{Student stud(101,78.5);

y();

(101,80.5);

y();

return 0;

}

9.7题

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s)

{num=n;score=s;}

void display() {cout<

"<

//可改为:void display() const

{cout<

private:

int num;

float score;

};

int main()

{const Student stud(101,78.5);

y();

//(101,80.5);

y();

return 0;

}

9.7题另一解

#include

using namespace std;

class Student

{public:

Student(int n,float s):num(n),score(s){}

void change(int n,float s) const

{num=n;score=s;}

void display() const {cout<

"<

private:

mutable int num;

mutable float score;

};

int main()

{const Student stud(101,78.5);

49 / 75


发布者:admin,转转请注明出处:http://www.yc00.com/web/1705081248a1391893.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信