《c%2B%2B程序设计》谭浩强_答案_完整版

更新时间:2023-04-10 07:28:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第一章

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<

}

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.3题

#include

using namespace std;

int main()

{char

c1='a',c2='b',c3='c',c4='\101'

,c5='\116';

cout<

cout<<"\t\b"<

<<'\n';

return 0;

}

2.4题

#include

using namespace std;

int main()

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

cout<<"I say:

\""<

cout<<"\t\t"<<"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<

\t'<

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.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<

}

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<

}

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

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

y=x="<

}

else if (x<10)

// 1≤x<10

{y=2*x-1;

cout<<"x="<

y=2*x-1="<

}

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

inp,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="<

//计算各位数字

ten_thousand=num/10000;

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

hundred=(int)(num-ten_tho usand*10000-thousand*10 00)/100;

ten=(int)(num-ten_thousan d*10000-thousand*1000-hu ndred*100)/10;

inp=(int)(num-ten_thousa nd*10000-thousand*1000-h undred*100-ten*10);

cout<<"original order:"; switch(place)

{case

5:cout<

cout<<"reverse order:";

cout<

break;

case

4:cout<

cout<<"reverse order:";

cout<

break;

case

3:cout<

cout<<"reverse

order:";

cout<

<

break;

case

2:cout<

ndl;

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,bon

6,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.0

75; //利润在10万元至

20万时的奖金

else if (i<=400000)

bonus=bon2+(i-200000)*0.0

5; //利润在20万元至

40万时的奖金

else if (i<=600000)

bonus=bon4+(i-400000)*0.0

3; //利润在40万元至

60万时的奖金

else if (i<=1000000)

bonus=bon6+(i-600000)*0.0

15; //利润在60万元至

100万时的奖金

else

bonus=bon10+(i-1000000)*

0.01; //利润在100万元

以上时的奖金

cout<<"bonus="<

ndl;

return 0;

}

3.13题另一解

#include

using namespace std;

int main ()

{long i;

float

bonus,bon1,bon2,bon4,bon

6,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.0

75; break;

case 2:

case 3:

bonus=bon2+(i-200000)*0.0

5;break;

case 4:

case 5:

bonus=bon4+(i-400000)*0.0

3;break;

case 6:

case 7:

case 8:

case 9:

bonus=bon6+(i-600000)*0.0

15; break;

case 10:

bonus=bon10+(i-1000000)*

0.01;

}

cout<<"bonus="<

ndl;

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

b="<

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<

}

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

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:"<

digit:"<

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,k

10;

int i,a,n,s;

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

// a是2~1000之间的整数,

检查它是否为完数

{n=0;

// n用来累计a的因子的个

s=a;

// s用来存放尚未求出的因

子之和,开始时等于a

for (i=1;i

检查i是否为a的因子

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

#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行*号

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

}

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;

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,floa

t);

void

equal_to_zero(float,float);

void

smaller_than_zero(float,floa

t);

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

x2<

}

else if (disc==0)

{equal_to_zero(a,b);

cout<<"x1="<

x2<

}

else

{smaller_than_zero(a,b);

cout<<"x1="<

"i"<

cout<<"x2="<

i"<

}

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<

prime."<

else

cout<

prime."<

return 0;

}

int prime(int n)

{int flag=1,i;

for (i=2;i

flag==1;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("<

nh<

return 0;

}

double e(double x) {return exp(x);}

4.6题

#include

#include

using namespace std;

int main()

{double

solut(double ,double ,doubl e ,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="<

endl;;

cout<<"P"<

x)<

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

d<

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

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<

original numbers:"<

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

cout<

// 输出这10个数

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<

sorted 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,4

0,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

{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:"<

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

//找出第i行中的最大数

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

//将最大数和其同列元素

相比

{flag=false;

//如果max不是同列最小,

表示不是鞍点令flag1为

continue;}

if(flag)

//如果flag1为真表示是鞍

{cout<<"a["<

<"["<

ndl;

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

列号

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

((number

a[n-1])) //要查的数不在

查找区间内

loca=-1; //

表示找不到

while ((!sign) &&

(top<=bott))

{mid=(bott+top)/2;

if

(number==a[mid])

{loca=mid;

cout<<"Find

"<

is "<

sign=true;

}

else if

(number

bott=mid-1;

else

top=mid+1;

}

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

cout<

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); /* 调用函数一

*/

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,3 1,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%10

0!=0||year%400==0;

return(leap);

}

5.10题

#include

using namespace std;

int main()

{int

i,j,upper,lower,digit,space,ot

her;

char text[3][80];

upper=lower=digit=space=o

ther=0;

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

{cout<<"please input

line "<

gets(text[i]);

for (j=0;j<80 &&

text[i][j]!='\0';j++)

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

text[i][j]<='Z')

upper++;

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

&& text[i][j]<='z')

lower++;

else if

(text[i][j]>='0' &&

text[i][j]<='9')

digit++;

else if (text[i][j]=='

')

space++;

else

other++;

}

}

cout<<"upper

case:"<

cout<<"lower

case:"<

cout<<"digit :"<

cout<<"space :"<

e<

cout<<"other :"<

r<

return 0;

}

5.11题

#include

using namespace std;

int main()

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

int i,j,k;

char space=' ';

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

// 输出5行

{ cout<

// 输出每行前先换行

cout<<" ";

// 每行前面留4个空格

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

cout<

// 每行再留一个空格

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

cout<

// 每行输出5个*号

}

cout<

return 0;

}

5.11题另一解

#include

#include

using namespace std;

int main()

{ string stars="*****";

int i,j;

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

// 输出5行

{ cout<<" ";

// 每行前面留4个空格

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

cout<<" ";

// 每行再插入i个空格

cout<

// 输出5个*号

}

return 0;

}

5.12题

#include

using namespace std;

int main()

{int j,n;

char ch[80],tran[80];

cout<<"input cipher

code:";

gets(ch);

cout<<"cipher

code:"<

j=0;

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

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

(ch[j]<='Z'))

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

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

(ch[j]<='z'))

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

else

tran[j]=ch[j];

j++;

}

n=j;

cout<<"original text:";

for (j=0;j

putchar(tran[j]);

cout<

return 0;

}

5.12题另一解

#include

using namespace std;

int main()

{int j,n;

char ch[80];

cout<<"input cipher

code:";

gets(ch);

cout<<"cipher code:"<

j=0;

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

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

else

ch[j]=ch[j];

j++;

}

n=j;

cout<<"original text:";

for (j=0;j

putchar(ch[j]);

cout<

return 0;

}

5.12另一解

#include

#include

using namespace std;

int main()

{int j;

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

cout<<"cipher code:"<

j=0;

while (j<=ch.size())

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

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

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

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

else

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<=ch.size())

{ 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<=ch.size())

{ 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

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];st

r[j+1]=temp;}

cout<

strings:"<

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=a.size();

for(i=0;i

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

cout<

return 0;

}

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<

failed:"<

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]>>sco

re[i];}

}

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

cout<

failed:"<

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]>>sco

re[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<

order is:"<

cout<

ndl<

return 0;

}

void swap(char *p1,char

*p2) /* 交换两

个字符串*/

{char p[20];

strcpy(p,p1);strcpy(p1,p2);st

rcpy(p2,p);

}

6.2题另一解

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

order is:"<

cout<

ndl<

return 0;

}

void change(string &st1,string &st2) /* 交换两个字符串*/ {string st;

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

}

6.3题

#include

using namespace std;

int main()

{ void input(int *number);

void max_min_value(int *number);

void output(int *number);

int number[10];

input(number);

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

max_min_value(number); // 调用交换函数

output(number);

// 调用输出函数

return 0;

}

void input(int *number) // 输入10个数的函数{int i;

cout<<"input 10 numbers:";

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

cin>>number[i];

}

void max_min_value(int *number) // 交换函数

{ int *max,*min,*p,temp;

max=min=number;

for

(p=number+1;p

if (*p>*max) max=p; // 将大数地址赋给max

else if (*p<*min) min=p; // 将

小数地址赋给min

temp=number[0];number[0]

=*min;*min=temp; // 将

最小数与第一数交换

temp=number[9];number[9]

=*max;*max=temp; // 将

最小数与第一数交换

}

void output(int *number)

// 输出函数

{int *p;

cout<<"now,they are:

";

for

(p=number;p

++)

cout<<*p<<" ";

cout<

return;

}

6.4题

#include

using namespace std;

int main()

{void move(int *array,int

n,int m);

int number[20],n,m,i;

cout<<"how many

numbers?";

// 询问共有多少个数

cin>>n;

cout<<"input "<

numbers:"<

求输入n个数

for (i=0;i

cin>>number[i];

cout<<"how many places

do you want move?"; //

询问后移多少个位置

cin>>m;

move(number,n,m);

//调用move 函数

cout<<"Now,they

are:"<

for (i=0;i

cout<

cout<

return 0;

}

void move(int *array,int

n,int m) //使

循环后移一次的函数

{int *p,array_end;

array_end=*(array+n-1);

for

(p=array+n-1;p>array;p--)

*p=*(p-1);

*array=array_end;

m--;

if (m>0) move(array,n,m);

//递归调用,当循环次数m

减至为0时,停止调用

}

6.5题

#include

using namespace std;

int main()

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

cout<<"input number of

person: n=";

cin>>n;

p=num;

for (i=0;i

*(p+i)=i+1;

// 以1至n为序给每个人

编号

i=0;

// i为每次循环时计数变量

k=0;

// k为按1,2,3报数时的计

数变量

m=0;

// m为退出人数

while (m

// 当退出人数比n-1少时

(即未退出人数大于1时)执

行循

环体

{if (*(p+i)!=0) k++;

if (k==3)

// 将退出的人的编号置为

{*(p+i)=0;

k=0;

m++;

}

i++;

if (i==n) i=0; //

报数到尾后,i恢复为0

}

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

cout<<"The last one is

NO."<<*p<

return 0;

}

6.6题

#include

using namespace std;

int main()

{int length(char *p);

int len;

char str[20];

cout<<"input string:";

cin>>str;

len=length(str);

cout<<"The length of

string is "<

return 0;

}

int length(char *p)

//求字符串长度的函数

{int n;

n=0;

while (*p!='\0')

{n++;

p++;

}

return(n);

}

6.7题

#include

using namespace std;

int main()

{void copystr(char *,char

*,int);

int m;

char str1[20],str2[20];

cout<<"input string:"; gets(str1);

cout<<"which character do you want begin to copy?";

cin>>m;

if (strlen(str1)

cout<<"input

error!"<

else

{copystr(str1,str2,m);

cout<<"result:"<

}

return 0;

}

void copystr(char *p1,char *p2,int m) //字符串部分复制函数*/

{int n;

n=0;

while (n

{n++;

p1++;

}

while (*p1!='\0')

{*p2=*p1;

p1++;

p2++;

}

*p2='\0';

}

6.8题

#include

using namespace std;

int main()

{int

upper=0,lower=0,digit=0,sp ace=0,other=0,i=0;

char *p,s[20];

cout<<"input string:"; while ((s[i]=getchar())!='\n') i++;

p=&s[0];

while (*p!='\n')

{if (('A'<=*p) && (*p<='Z'))

++upper;

else if (('a'<=*p) &&

(*p<='z'))

++lower;

else if (*p==' ')

++space;

else if ((*p<='9') &&

(*p>='0'))

++digit;

else

++other;

p++;

}

cout<<"upper

case:"<

er case:"<

cout<<"space:"<

dl<<"digit:"<

other:"<

return 0;

}

6.9题

#include

using namespace std;

int main()

{void move(int *);

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

cout<<"input

matrix:"<

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

cin>>a[i][0]>>a[i][1]>>a[i][2]

;

p=&a[0][0];

move(p);

cout<<"Now,matrix:"<

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

cout<

"<

"<

cout<

return 0;

}

void move(int *pointer)

{int i,j,t;

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

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

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

*(pointer+3*i+j)=*(pointer+

3*j+i);

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

}

}

6.10题

#include

using namespace std;

int main()

{void change(int *p);

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

cout<<"input

matrix:"<

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

//输入矩阵

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

cin>>a[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;

*pmax=temp;

temp=*p;

//将最小值与左上角元素

互换

*p=*pmin;

*pmin=temp;

pmin=p+1;

//将a[0][1]的地址赋

给pmin,从该位置开始找

最小的元素

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

//找第二最小值的地址赋

给pmin

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

if (((p+5*i+j)!=p)

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

pmin=p+5*i+j;

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(((p+5*i+j)!=(p+4))

&& ((p+5*i+j)!=p)

&&(*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 (((p+5*i+j)!=p) &&((p+5*i+j)!=(p+4)) && ((p+5*i+j)!=(p+20)) &&

(*pmin>*(p+5*i+j)))

pmin=p+5*i+j; temp=*pmin;

//将第四最小值与右下角元素互换

*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 (i=0;i<5;i++) //输入矩阵

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

cin>>a[i][j];

p=&a[0][0];

//使p指向0行0列元素change(p);

//调用函数,实现交换

cout<<"Now,matrix:"<

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

*pmax=temp;

temp=*p;

//将最小值与左上角元素

互换

*p=*pmin;

*pmin=temp;

pmin=p+1;

//将a[0][1]的地址赋

给pmin,从该位置开始找

最小的元素

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

//找第二最小值的地址赋

给pmin

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

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

continue;

if (*pmin >

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

}

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;

//将第四最小值与右下角

元素互换

*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<

}

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

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)(flo

at);

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.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)(flo

at);

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;

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<

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,p

aver); // 找出两门

课不及格的学生

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)

// 求第一课程的平均成

绩的函数

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

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

将含有两位以上数的其它

位的数值累加于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++;

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",

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

l;

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<

}

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

integer 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

using namespace std;

struct

{ int year;

int month;

int day;

}date;

int main()

{int days;

cout<<"input

year,month,day:";

cin>>date.year>>date.mont

h>>date.day;

switch(date.month)

{ case 1: days=date.day;

break;

case 2:

days=date.day+31;

break;

case 3:

days=date.day+59;

break;

case 4:

days=date.day+90;

break;

case 5:

days=date.day+120;

break;

case 6:

days=date.day+151;

break;

case 7:

days=date.day+181;

break;

case 8:

days=date.day+212;

break;

case 9:

days=date.day+243;

break;

case 10:

days=date.day+273; break;

case 11:

days=date.day+304; break;

case 12:

days=date.day+334; break;

}

if ((date.year %4== 0 &&

date.year % 100 != 0

||date.year % 400

== 0) && date.month >=3)

days+=1;

cout<

ate.day<<" is the "<

<<"th day in

"<

return 0;

}

7.1题另一解

#include

using namespace std;

struct

{int year;

int month;

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>>date.year>>date.mont

h>>date.day;

days=0;

for

(i=1;i

days+=day_tab[i];

days+=date.day;

if ((date.year%4==0

&& date.year%100!=0 ||

date.year%400==0) &&

date.month>=3)

days+=1;

cout<

ate.day<<" is the "<

<<"th day in

"<

return 0;

}

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>>date.year>>date.mont h>>date.day;

day_sum=days(date.year,dat e.month,date.day);

cout<

<<"th day in "<

return 0;

}

int days(int year,int month,int day) /* 定义days函数*/

{int day_sum,i;

int

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

for (i=1;i

day_sum+=day_tab[i]; day_sum+=day;

if ((year%4==0 && year%100!=0 || year%4==0) && month>=3)

day_sum+=1;

return(day_sum);

}

7.3题

#include

#include

using namespace std; const int n=5;

struct student { char num[6];

char name[8];

int score[4];

}stu[n];

int main()

{void print(student stu[6]);

int i,j;

for (i=0;i

{cout<<"input scores of

student "<

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

}

cout<

}

print(stu);

return 0;

}

void print(student stu[6])

{int i,j;

cout<<" NO. name

score1 score2

score3"<

for (i=0;i

{cout<

"<

" ";

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

cout<

[j]<<" ";

cout<

}

}

7.4题

#include

#include

using namespace std;

const int n=5;

struct student

{ char num[6];

char name[8];

int score[4];

}stu[n];

int main()

{void input(student stu[]);

void print(student stu[]);

input(stu);

print(stu);

return 0;

}

void input(student stu[])

{int i,j;

for (i=0;i

{cout<<"input scores of

student "<

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<

[j]<<" ";

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 of

student "<

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

}

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;

#define NULL 0

struct student

{long num;

float score;

struct student *next;

};

int main()

{student a,b,c,*head,*p; a.num=10001;

a.score=89.5;

b.num=10003;

b.score=90;

c.num=10007; c.score=85; //为结点的num和score成员赋值

head=&a;

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

a.next=&b;

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

b.next=&c; //将结点c的起始地址赋给

b结点的next成员

c.next=NULL;

//c结点的next成员不存放

其他结点地址

p=head;

//使p指针指向a结点

do

{cout<num<<"

"<score<

指向的结点的数据

p=p->next;

//使p指向下一结点

}while(p!=NULL);

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

NULL

return 0;

}

7.6题另一解

#include

using namespace std;

#define NULL 0

struct student

{long num;

float score;

student *next;

};

//

定义n为全局变量,本文件

模块中各函数均可使用它

student *creat(void) //

定义函数。此函数带回一个

指向链表头的指针

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

else p2->next=p1;

p2=p1;

p1=new student;

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

}

p2->next=NULL;

return(head);

}

7.7题

#include

using namespace std;

#define NULL 0

struct student

{long num;

float score;

student *next;

};

int n;

void print(student *head)

{student *p;

cout<<"Now,These

"<

"<

p=head;

if(head!=NULL)

do

{cout<num<<"

"<score<

p=p->next;

}while(p!=NULL);

}

7.7题另一解

#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!"<

return(head);}

p1=head;

//使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:"<

dl;

n=n+1;

}

else cout<<"cannot find

"<

结点

return(head);

}

7.8题

#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!"<

return(head);}

本文来源:https://www.bwwdw.com/article/ppal.html

Top