当前位置:文档之家› 《c++程序设计》谭浩强_答案 完整版

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

《c++程序设计》谭浩强_答案 完整版
《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<

}

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

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

//计算各位数字

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;

indiv=(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

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

谭浩强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< 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< using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< 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="<

VB程序设计课后习题答案(科学出版社)

同步练习1 二、选择题 01——05 CADAB 06——10 ACDAB 11——15 CBDBB 同步练习2 二、选择题 01——05 ABDCA 06——10 CACBC 11——15 DADAD 16——20 BDBBB 三、填空题 1.可视 2.LEFT、TOP、WIDTH、HEIGHT 3.按字母顺序 4.查看代码 5.工具、编辑器 6.FORM窗体、FONT 7.MULTILINE 8.在运行时设计是无效的 9.工程、工程属性、通用、FORM1.SHOW 10.TABINDEX、0 同步练习3 二、选择题 01——05 BCADB 06——10 ADBBC 11——15 DBCBA 16——20 BAABB 三、填空题 1.整型、长整型、单精度型、双精度型 2.SIN(30*3.14/180)+SQR(X+EXP(3))/ABS(X-Y)-LOG(3*X) 3.164、今天是:3-19 4.FALSE 5.-4、3、-3、3、-4、4 6.CDEF 7.(X MOD 10)*10+X\10 8.(35\20)*20=20 ( 35 \ 20 )* 20 = 20 9.X MOD 3=0 OR X MOD 5=0 10.27.6、8.2、8、1、100、397、TRUE、FALSE 同步练习4 一、选择题 01——05 DBCAD 06——10 CBBAB

11——15 D25BAC 16——20 CBACB 21——25 DAABC 二、填空题 1.正确性、有穷性、可行性、有0个或多个输入、有1个或多个输出2.1 2 3 3.X>=7 4.X

C语言程序设计谭浩强第四版期末复习重点

C语言程序设计谭浩强第四版期末复习重点 Newly compiled on November 23, 2020

第一章 程 序设计和C 语言 .什么是计算机程序 程序.. :一组计算机能识别和执行的指令。只要让计算机执行这个程序,计算机就会自动地、有条不紊地进行工作 计算机的一切操作都是由程序控制的,离开程序,计算机将一事无成。 什么是计算机语言 计算机语言:人和计算机交流信息的、计算机和人都能识别的语言。 计算机语言发展阶段:机器语言(由0和1组成的指令) 符号语言(用英文字母和数字表示指令) 高级语言(接近于人的自然语言和数学语言) 面向过程的语言(非结构化的语言、结构化语言);面向对象的语言 语言的发展及其特点 C 语言是一种用途广泛、功能强大、使用灵活的过程性编程语言,既可用于编写应用软件,又能用于编写系统软件。因此C 语言问世以后得到迅速推广。 C 语言主要特点: 语言简洁、紧凑,使用方便、灵活。(只有37个关键字、9种控制语句;程序书写形式 自由,源程序短) 运算符丰富。(34种运算符;把括号、赋值、强制类型转换等都作为运算符处理;表达 式类型多样化) 数据类型丰富。(包括:整型、浮点型、字符型、数组类型、指针类型、结构体类型、共用体类型;C99又扩充了复数浮点类型、超长整型、布尔类型;指针类型数据,能用来实现各种复杂的数据结构的运算。)

具有结构化的控制语句。(如if…else语句、while语句、do…while语句、switch语句、for语句用函数作为程序的模块单位,便于实现程序的模块化;C语言是完全模块化和结构化的语言) 语法限制不太严格,程序设计自由度大。(对数组下标越界不做检查;对变量的类型使用比较灵活,例如,整型量与字符型数据可以通用;C语言允许程序编写者有较大的自由度,因此放宽了语法检查) 允许直接访问物理地址,能进行位操作,可以直接对硬件进行操作。(C语言具有高级语言的功能和低级语言的许多功能,可用来编写系统软件;这种双重性,使它既是成功的系统描述语言,又是通用的程序设计语言) 用C语言编写的程序可移植性好。(C的编译系统简洁,很容易移植到新系统;在新系统上运行时,可直接编译“标准链接库”中的大部分功能,不需要修改源代码;几乎所有计算机系统都可以使用C语言) 生成目标代码质量高,程序执行效率高。 1.C语言允许用两种注释方式:计算法 3.编写程序 4.对源程序进行编辑、编译和连接 5. 运行程序,分析结 6.编写程序文档 第二章算法——程序的灵魂 一个程序主要包括以下两方面的信息: (1) 对数据的描述。在程序中要指定用到哪些数据以及这些数据的类型和数据的组织形式,这就是数据结构。 (2) 对操作的描述。即要求计算机进行操作的步骤,也就是算法。 数据是操作的对象,操作的目的是对数据进行加工处理,以得到期望的结果。 着名计算机科学家沃思(Nikiklaus Wirth)提出一个公式:算法 + 数据结构 = 程序

C程序设计第四版谭浩强完整版课后习题答案

C程序设计第四版谭浩强完整版课后习题答案集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]

C程序设计(第四版)(谭浩强)第一章课后习题答案 P006 向屏幕输出文字. #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } P008 求两个数的和. #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum);

return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); }

《VB程序设计》习题答案-林卓然编著

《VB语言程序设计》教材 林卓然编著 《VB程序设计》习题参考答案 第1章习题 一、单选题 1.D 2.B 3.C 4.A 5.C 6. A 7.A 8.B 9.C 10.(1)A (2)B 二、多选题 1.ACDE 2.BCDE 3.CD 4. CE 三、填空题 1.事件驱动 2.解释,编译 3.设计,运行,中断 4. 属性,方法,事件 5. Command2_Click 6. 中央,代码 7.MyForm.frm 8. (1)Cmd1_Click() (2)Print "VB语言程序设计" 编程及上机调试 1. 按要求设置窗体属性 2.在窗体上建立一个标签Label1。程序代码如下: Private Sub Form_Click() Label1.Caption = "你单击了窗体" End Sub Private Sub Form_DblClick() Label1.Caption = "你双击了窗体" End Sub 3.在窗体上建立两个文本框Text1(输入数)、Text2(输出数)和一个命令按钮Command1。两个文本框的Text属性均为空。 Private Sub Command1_Click() Dim x As Single x = Val(Text1.Text) Text2.Text = 15 * x End Sub 第2章习题 一、单选题 1.①D,②D 2.①A,②C 3.A 4.B 5.B 6.B 7.C 8.D 9.B 10.B 11.A 12.A 13.B 14.A 15.B 16.A 17.B 二、多选题 1.BD 2.CD 3.AC 4. AD 三、填空题 1.(1) Sin(50*3.14159/180) (2) (2+x*y)/(2–y*y)

C程序设计第四版谭浩强_习题&例题第7章

第7章函数 例7.1 #include int main() { void print_star(); void print_message(); print_star(); print_message(); print_star(); return 0; } void print_star() { printf("***********************************\n"); } void print_message() { printf("How do you do!\n"); } 例7.2 int max(int x,int y) { int z; z=x>y?x:y; return(z); } #include int main() { int max(int x,int y); int a,b,c; printf("please enter two integer number:"); scanf("%d,%d",&a,&b); c=max(a,b); printf("max is %d\n",c); return 0; }

例7.3 #include int main() { int max(float x,float y); float a,b; int c; printf("please enter two integer number:"); scanf("%f,%f",&a,&b); c=max(a,b); printf("max is %d\n",c); return 0; } int max(float x,float y) { float z; z=x>y?x:y; return(z); } 例7.4 #include int main() { float add(float x,float y); float a,b,c; printf("please enter a and b:"); scanf("%f,%f",&a,&b); c=add(a,b); printf("sum is %f\n",c); return 0; } float add(float x,float y) { float z; z=x+y; return(z); }

《C语言程序设计》_谭浩强版教案

《C 语言程序设计》教案 职称: 助教 ____________ 单 位:湖南理工职业技术学院 学院(教研室):风能工程学院 工业机器人专业 授课教师: 周常欣

教 学 重 占 八、、 和 难 占 八、、 一、 程序设计和 C 语言 重点:计算机程序、计算机语言、 C 语言编译软件的安装、最简单的 C 语言程序 二、 算法:程序的灵魂 重点:简单的算法举例、算法的特性、用流程图表示算法。 三、 顺序结构程序设计 重点:C 语言的数据类型、C 语句的种类、赋值语句、数据的输入输出及输入输出 中最常用的 控制格式。 四、 选择结构程序设计 重点:关系运算符与逻辑运算符及其组成的具有逻辑值的表达式、 二条分支语句的格 式及基本应用、多分支的选择语句。 五、 循环结构程序设计 重点:C 构成循环的四种方法,尤其是后三种方法、 break 与continue 语句的基本作 用。 难点:while 语句;do-while 语句;for 语句;循环的嵌套; break 与continue 语句。 六、 数组 重点:一维数组、二维数组的定义与引用;字符数组的定义与引用、常用字符串处 理函数及字符处理函数;数组的应用 难点:二维数组的定义与引用;字符数组;数组的应用 七、 函数 重点:函数的定义;函数的参数和函数的值;函数的调用;函数的嵌套调用;函数的 递归调 用;数组作为函数参数;变量作用域;量存储类别; 难点:函数定义、函数调用、函数声明等基本基本概念;函数的嵌套调用与递归调 用;数组作 为函数的参数、变量的存储类别与作用域。 八、 指针 重点和难点:指针与地址的基本概念、指针与变量的关系;指针与数组;指针与字 符串、指针 数组与二级指针;指针的应用 九、 建立自己的数据类型 重点和难点:定义和使用结构体变量、用指针处理链表、共用体类型 十、文件 重点:文件的基本知识、 fopen 、fclose 函数打开与关闭文件、顺序读与数据文件、 随机读写数据文件 难点:用二进制方式向文件读写一组数据。 十一、常见错误分析 重点和难点:文件的基本概念;文件的打开、关闭、常用读写方法。 十二、数组高级应用 重点:不定长数组与二维数组的应用 难点:不定长数组与二维数组的应用 十三、综合应用 重点:数组、if 语句、循环语句、函数知识的综合应用 难点:二维数组、指针 教材、 参 考书 教材:《C 程序设计》(第四版) 谭浩强著 清华大学出版社2010年6月 参考书:《C 程序设计语言》 Kernighan&Ritchie 机械工业出版社 《C 语言程序设计》教案 第1-2课时

C语言程序设计第四版第六章答案_谭浩强

1、用筛选法求100之内的素数。解: #include #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 int main() {int i,j,min,temp,a[11]; printf("enter data:\n"); for (i=1;i<=10;i++)

{printf("a[%d]=",i); scanf("%d",&a[i]); } printf("\n"); printf("The orginal numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); for (i=1;i<=9;i++) {min=i; for (j=i+1;j<=10;j++) if (a[min]>a[j]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; } printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); return 0; } 3、求一个3×3的整型矩阵对角线元素之和。解: #include int main() { int a[3][3],sum=0; int i,j; printf("enter data:\n"); for (i=0;i<3;i++) for (j=0;j<3;j++) scanf("%3d",&a[i][j]); for (i=0;i<3;i++) sum=sum+a[i][i]; printf("sum=%6d\n",sum);

谭浩强--C语言程序设计(第二版)习题答案

C语言程序设计(第2版)课后习题答案 第一章 1.请参照本章例题,编写一个C程序,输出以下信息: ************************** Very good! ************************** 解: #include void main() { printf("**************************"); printf("\n"); printf("Very good!\n"); printf("\n"); printf("**************************"); } 2.编写一个C程序,输入a、b、c三个值,输出其中最大值。 解: #include void main() { int a,b,c,max; printf("请输入三个数a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if(max #include void main() { double P, r=0.1, n=10; P=pow((1+r), n);

printf("%lf\n", P); } 3.请编程序将"China"译成密码,译码规律是用原来字母后面的第4个字母代替原来的字母。例如,字母"A"后面第4个字母是"E","E"代替"A"。因此,"China"应译为"Glmre"。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为'C'、'h'、'i'、'n'、'a',经过运算,使c1、c2、c3、c4、c5分别变为'G'、'l'、'm'、'r'、'e',并输出。 解: #include void main() { char c1='C',c2='h',c3='i',c4='n',c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 第三章 3.用下面的scanf函数输入数据,使a=3, b=7, x=8.5, y=71.82, c1='A', c2='a'。问在键盘上如何输入? 解: #include void main() { int a, b; float x, y; char c1, c2; scanf("a=%d, b=%d", &a, &b); scanf(" x=%f, y=%e", &x, &y); scanf(" c1=%c, c2=%c",&c1, &c2); } a=3, b=7 x=8.5, y=71.82 c1=A, c2=a 5. 设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后2位数字。请编程序。 解: #include void main() { float r,h,C1,Sa,Sb,Va,Vb; scanf("%f,%f",&r,&h);

《VB程序设计》考试题型练习 答案

《VB 程序设计》考试题型练习 一. 填空 1. VB 中的对象具有 属性 、 事件 、和 方法 三种特性,它们是构成对象的三个基本要素。 2. VB 中的常量分为哪三种: 文字常量 、 符号常量(用户自定义常量) 系统内部常量 。 3. 变量的三要素是指: 变量的名 、 变量的数据类型 、 变量的值 。 4. VB 中日期型数据的表示格式为: #mm/dd/YYYY#或#mm-dd-YYYY# 。 5. 在VB 中,“\”是 整除 运算符;“/”是 浮点除 运算符。两个字符串可以使用连接符 &或+ 串接起来。 6. 2 22z y x z y x ++++的VB 表达式为 (x+y+z )/sqr(x^2+y^2+z^2) 。 7. a ac b b 242-+的VB 表达式为 (b+sqr(b^2-4*a*c))/(2*a) 。 8. 计时器控件的 Interval 属性用于设置Timer 事件发生的时间间隔,为了使计时器控件Timer1每隔0.5秒触发一次Timer 事件,应将该属性设置为 500 ;为了暂时关闭计时器,应把该计时器的 Enable 属性设置为False 。 二. 单选题 1. 如果要使VB 程序运行时不能对文本框中的内容进行编辑,需将文本框的哪个属性设置为True ( D )。 A )MultiLine B )Visible C )TabStop D )Locked 2. 表达式Right("asdf", 2)+Ucase("as" )的值为( A )。 A )dfAS B )asAS C )dfas D )asas 3. 执行如下语句: a =InputBox("Today", "Tomorrow", "Yesterday") 将显示一个输入对话框,在对话框的输入区中的默认值是( C )。 A) Today B) Tomorrow C) Yesterday D) Today Tomorrow Yesterday 4. 设a=3,b=5,则以下表达式值为真的是( B )。 A) a>=b And b>10 B) (a>b) Or (b>0) C) (a<0) Or Not (b>0) D) (-3+5>a) And (b>0)

VB程序设计答案

VB程序设计答案 第一章习题参考答案 一、填空题 1、对象事件驱动 2、编译运行模式解释运行模式 二、简答题 1、简述VB的特点。 答:①可视化的程序设计方法 ②面向对象的程序设计思想 ③事件驱动的编程机制 ④结构化的程序设计语言 ⑤高度的可扩充性 ⑥强大的数据库访问能力 ⑦支持动态数据交换 ⑧支持对象链接与嵌入 2、简述VB的安装过程。 答:①向光驱中放入VB安装光盘; ②计算机将运行自动安装程序,若没有,执行VB目录下的Setup.exe; ③根据提示逐一回答问题,并点击“下一步”; ④点击“完成”; 2、如何启动VB。 答:三种方法: ①单击“开始”按钮,选择“程序”菜单下的“Microsoft Visual Basic 6.0 中文版”菜单项并单击; ②双击桌面上的VB6.0快捷图标; ③在“开始”菜单的运行对话框中输入命令来启动VB; 第二章习题参考答案 一、选择题: 1-10、DCACD BCBBC 11-20、BADCA BBAAB 21-29、AACBB CDDC 二、填空题 1.窗体、控件 2.属性值 3.属性窗口设置、代码窗口设置 4.事件过程 5.对象名、下划线、事件名 6.Form_Click 第三章习题参考答案

一、选择题 1、B 2、C 3、A 4、B 5、A 6、C 7、C 二、填空题 1、11 字符型、字节型、整型、长整型、单精度型、双精度型、货币型、逻辑型、日期型、对象型、变体型。String、Byte、Integer、Long、Single、Double、Currency、Boolean 、Date、Object、Variant 2、双引号(英文)、# 3、ASCII、1、2 4、public、通用、Dim、Static、Private、Static 5、&、+、+ 6、Now()、Time、日期型 三、解答题 1、(3) 2、(1 ) 3、(1)常量(关键字)(2)字符型常量(3)逻辑型常量(4)变量(5)字符串常量(6)变量 (7)字符串常量(8)变量(9)日期型常量(题目应是#11/16/1999#)(10)数值型常量(单精度) 4、31 5、(1)(1+y/x)/(1-y/x) (2)x^3+3*x*y/(2-y) (3)sqr(abs(a*b-c^3)) (4)sqr(s*(s-a)*(s-b)*(s-c)) 6、 7、(1)13 (2)17 (3)#1999-11-12# (4)zxy123ABC 8、(1)19 (2)8.16666666666667 (3)5.25 (4)2 9、(1)-4 (2)2 . 82842712474619 (3)-3 (4)49 (5)”hello”(6)He 7)16 (8)“-459.65”(9)5 10、(4) 11、(2) 12、(4) 第四章习题参考答案 一、选择题: 1—7: DAABBCD 二、填空题 1.500.00% 2.将字号扩大为原来的二倍 3. B 三、编程题(略) 1.文本框、标签和命令按钮的名称分别为text1、label1、command1 Private Sub Command1_Click() Label1.caption=text1.text Command1.caption=text1.text

VB程序的设计教程课后答案

课后习题解答 第一、二章: 1、简述VB6.0的主要特点 答:方便、直观的可视化设计工具;面向对象程序设计方法;事件驱动的编程机制;易学易用的应用程序集成开发环境;结构化程序设计语言;完备的联机帮助功能;强大的多媒体、数据库和网络功能。2、当正常安装VB6.0后,误把Windows子目录删除。当重新安装Windows后,是否需要再次安装VB6.0? 答:要重新安装的。因为安装VB6.0时,有些VB程序被系统自动安装在Windows目录下,所以一旦删除Windows子目录,就必须重新安装VB6.0。 3、Visual Basic 6.0分为学习版、专业版和企业版,如何知道所安装的是哪个版本? 答:在VB6.0的启动界面上,能显示对应的版本。 4、叙述建立一个完整应用程序的过程 答:首先建立用户界面的控件对象(简称控件);然后对控件属性进行设置;再对控件事件过程及编程; 保存应用程序;程序调试和运行。 5、Visual Basic 6.0有多种类型窗口,若要在设计时看到代码窗口,应怎样操作? 答:选择“视图”—>“代码窗口”命令或单击“工程资源管理器”窗口中的“查看代码”按钮。 6、当建立好一个简单的应用程序后,假定该工程仅有一个窗体模块。试问该工程涉及多少个要保存的文件? 若要保存该工程的所有文件,应先保存什么文件?再保存什么文件?若不这样做,系统会出现什么信息? 答:涉及两个文件要保存。先保存窗体文件(.frm),再保存工程文件(.vbp)。若先保存工程文件,系统也会先弹出“文件另存为”对话框,要求先保存窗体文件。 7、保存文件时,若不改变目录名,则系统默认的目录是什么? 答:系统默认的目录是VB98 8、安装Visual Basic 6.0后,帮助系统是否也已安装? 答:没有。VB6.0的帮助系统在两光盘上,即MSDN Library,其中还包含了VC、VFP等其他语言的帮助系统。 9、如何使用Visual Basic 6.0的帮助系统? 答:最方便的方法是选定要帮助的容,然后按F1功能键,这时系统打开MSDN Library查阅器,直接显示与选定容有关的帮助信息。 10、什么是类?什么是对象?什么是事件过程? 答:类是同类对象集合的抽象,它规定了这些对象的公共属性和方法; 对象是类的一个实例,对象包含三个要素:属性、方法和事件。 当在对象上发生了事件后,应用程序就要处理这个事件,而处理的步骤就是事件过程。 11、属性和方法的区别是什么? 答:属性是对象的特征,方法是对象的行为。前者有值,可以被赋值或引用;后者没有值,表示能够执行的操作。 12、当标签边框的大小由Caption属性的值进行扩展或缩小时,应对该控件的什么属性进行何种设置? 答:将该控件的Autosize属性值设置为true。 13、在VB6.0中,命令按钮的显示形式可以有标准的和图形的两种选择,它们通过什么属性来设置?若选 择图形的,则通过什么属性来装入图形?若已在规定的属性里装入了某个图形文件,但该命令按钮还是不能显示该图形,而显示的是Caption属性设置的文字,应怎样修改? 答:命令按钮的显示形式通过将Style属性值设置为Graphical来实现。通过Picture属性来装入图

vb程序设计试题(含答案)

第一套试题 一、选择题(每题2分,共30分) 1.在VB环境中,工程文件的扩展名是( C ) (A).frm (B).bas (C).vbp (D).frx 2.下面4项中不属于面向对象系统三要素的是( A ) (A)变量(B)事件(C)属性(D)方法 3. \、/、Mod、*这4个算述运算符中,优先级别最低的是( C ) (A)\ (B)/ (C)Mod (D)* 4.要使Print方法在Form_Load事件中起作用,要对窗体的( C )属性进行设置(A)BackColor (B)ForeColor (C)AutoRedraw (D)Caption 5.不论何控件,共同具有的属性是( B ) (A)Text (B)Name (C)ForeColor (D)Caption 6.要使Form1窗体的标题栏显示“欢迎使用VB”,以下语句正确的是( D ) (A)Form1.Caption=“欢迎使用VB”(B)Form1.Caption=ˊ欢迎使用VBˊ(C)Form1.Caption=欢迎使用VB (D)Form1.Caption=〞欢迎使用VB〞 7.VB提供了结构化程序设计的3种基本结构,这3种基本结构是( D ) (A)递归结构,选择结构,循环结构(B)选择结构,过程结构,顺序结构 (C)过程结构,输入、输出结构,转向结构(D)选择结构,循环结构,顺序结构 8.给变量赋值a=123+MID(“123456”,3,2),a变量中的值是( D ) (A)“12334”(B)123 (C)12334 (D)157 9.下面If语句统计满足性别为男、职称为副教授以上、年龄小于40岁条件的人数,不正确的语句是( D ) (A)If sex=“男”And age<40 And InStr(duty,“教授”)>0 Then n=n+1 (B)If sex=“男”And age<40 And (duty=“教授”Or duty=“副教授”) Then n=n+1 (C)If sex=“男”And age<40 And Right(duty,2)= “教授” Then n=n+1 (D)If sex=“男”And age<40 And duty=“教授”And duty=“副教授” Then n=n+1 10.若要清除列表框的所有内容,可用来实现的方法是( C )

[整理]VB程序设计复习题及答案.

中南大学现代远程教育课程(专科)考试复习题及答案 《VB程序设计与应用》 一、选择题:在每小题给出的四个选项中,只有一项是符合题目要求的,请把所选项前的字母填在 题后的括号内。 1. Visual Basic程序中分隔一行中各语句的字符是()。[ ] A)‘ B): C)\ D)_ 2. 下列可作为Visual Basic的变量名的是()。[ ] A: Filename B: A(A+B)C: A%D D : Print 3. 要让菜单项无效,应将()属性值设置为False。 [ ] A. Visible B. Enabled C. Moveable D. Checked 4. 以下过程是()事件。 Prviate Sub Form_Load( ) …… End Sub [ ] A: 命令按钮的单击B: 命令按钮的装载 C; 窗体的装载D: 窗体的单击 5. Vb中描述文件扩展名的是()属性。[ ] A: Drive B: Pattern C: FileName D: Path 6. 关于Visual Basic中事件的说法中,不正确的为()。[ ] A)事件是由Visual Basic预先定义好的,能够被对象识别的动作 B)DblClick(双击)是Visual Basic中的一种事件 C)一个对象可以识别一个或多个事件 D)对于一个系统提供的对象可以识别哪些事件,需要程序员自己定义 7. 下列控件中可设置滚动条是()。 [ ] A. 检查框(复选框) B. 框架 C. 文本框 D. 标签框 8. 在Visual Basic中建立一个空窗体,( )不会是其默认名。 [ ] A. Form1 B. Form2 C. Formx D. Form10 9. Const One&=1,则One为()类型的常数。[ ] A)整型 B)字节型

C程序设计第四版谭浩强完整版课后习题答案

C程序设计(第四版)(谭浩强)第一章课后习题答案 #include<>代码均调试成功,若有失误大多不是代码问题.自已找找. int main() { printf("Welcome to \n"); return 0; } #include<> int main() { int a,b,sum; a=5; b=4; sum=a+b; printf("The sum is %d .\n",sum); return 0; } P008 调用函数比较两个数的大小. #include<> int main() { int max(int x,int y); int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c); return 0; } int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); }

P015 三个数的大小.(数字0表示课后练习题) #include<> int main() { int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n"); scanf("%d %d %d",&a,&b,&c); d=max(a,b,c); printf("The max is :%d .\n",d); } int max(int x , int y , int z) { int m; if (x>y && x>z) m=x; if (y>x && y>z) m=y; if (z>y && z>x) m=z; return (m); } C程序设计(第四版)(谭浩强)第2章课后 习题答案 算法——程序的灵魂 P017 计算机1-5相乘的积. #include<> int main() { int i,s=1; for(i=1;i<6;i++) { s=s*i; n",s); return 0; } #include<> int main() { int i,s=1; for(i=1;i<12;i++) 可以是i=i+2 { if(i%2!=0) s=s*i; else continue; }

vb程序设计教程课后答案(第三版)

习题一 答案在教材上找。 习题二 1.对象指现实世界中的实体。每个对象有自己的特征(属性)、行为(方法)和发生在该对象上的活动(事件)。 类是对同一种对象的集合与抽象。类包含所创建对象的属性数据,以及对这些数据进行操作的方法。封装和隐藏是类的重要特性,将数据结构和对数据的操作封装在一起,类的内部实现细节对用户来说是透明的。 VB工具箱中的可视图标是VB系统设计好的标准控件类。通过将控件类实例化,得到真正的控件对象。在窗体上画一个控件,就将类转换为对象,创建了一个控件对象。 VB中常用的对象是控件。此外,VB还提供了系统对象。窗体是个特例,它既是类也是对象。当向一个工程添加一个新窗体时,实质就由窗体类创建了一个窗体对象。 对象的三要素:属性、事件和方法。 属性:存放对象的数据(决定对象外观及功能)对象属性的设置方法:在设计阶段,利用属性窗口直接设置属性值;程序运行时,通过语句[对象名.]属性名=属性值。若属性只能在设计阶段设置,在程序运行阶段不可改变,称为只读属性。 事件:发生在对象上的事情。同一事件,对不同的对象,会引发不同的反应。事件过程:应用程序处理事件的步骤。应用程序设计的主要工作就是为对象编写事件过程的代码。 方法:面向对象的程序设计语言,为程序设计人员提供了一种特殊的过程和函数。方法是面向对象的,调用时一定要指明对象。 2.窗体和控件具有的基本属性“ (1) Name属性:名称 所创建的对象名称,在程序中引用。 (2) Caption属性:标题 该属性决定了对象上显示的内容。 (3) Height、Width、Top和Left属性 决定对象的大小和位置。 (4) Enabled 属性:对象是否允许操作 (5) Visible 属性:对象是否可见 (6) Font 属性组:文本的外观 FontName字体、FontSize大小 FontBold粗体、FontItalic斜体 FontStrikethru 删除线 FontUnderline 下划线 (7) ForeColor属性:前景颜色 (8) BackColor属性:背景颜色 (9) MousePointer属性:鼠标指针类型, 设置值范围0~15,由系统指定。若为99,由用户提供的图形文件定义鼠标指针形状。

C语言程序设计课后习题答案第四版谭浩强完整版

C语言程序设计课后习 题答案第四版谭浩强 HUA system office room 【HUA16H-TTMS2A-HUAS8Q8-HUAH1688】

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; }

1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.2简单的算法举例17 2.3算法的特性21 2.4怎样表示一个算法22 2.4.1用自然语言表示算法22 2.4.2用流程图表示算法22 2.4.3三种基本结构和改进的流程图26 2.4.4用N S流程图表示算法28 2.4.5用伪代码表示算法31 2.4.6用计算机语言表示算法32 2.5结构化程序设计方法34 习题36 第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例37 3.2数据的表现形式及其运算39 3.2.1常量和变量39 3.2.2数据类型42

相关主题
文本预览
相关文档 最新文档