首页 > 精品范文库 > 1号文库
c语言课后答案 (范文大全)
编辑:平静如水 识别码:10-1118651 1号文库 发布时间: 2024-08-27 09:51:44 来源:网络

第一篇:c语言课后答案

第7章

数组习题解答

一、在以下每一题的四个选项中,请选择一个正确的答案。【题7.1】 C 【题7.2】 D 【题7.3】 D 【题7.4】 D 【题7.5】 C 【题7.6】 C 【题7.7】 C 【题7.8】 C 【题7.9】 B 【题7.10】 B

二、判断下列各叙述的正确性,若正确在()内标记√,若错误在(【题7.11】 √ 【题7.12】 √ 【题7.13】 × 【题7.14】 × 【题7.15】 √ 【题7.16】 × 【题7.17】 × 【题7.18】 × 【题7.19】 √ 【题7.20】 ×

三、填空。请在下面各叙述的空白处填入合适的内容。【题7.21】 字符数组 【题7.22】 0 【题7.23】 4 【题7.24】 按行的顺序存放 【题7.25】 48 【题7.26】 strcat()【题7.27】 6 【题7.28】 10 【题7.29】 strcmp()

×。– 1 –)内标记 C语言程序设计教程实验指导与习题解答

【题7.30】 #include

四、阅读下面的程序,写出程序运行结果。【题7.31】 abcdefg

abcdef

abcde

abcd

abc

ab

a 【题7.32】 1 1

1

1

1 【题7.33】 a[2]*b[1]=2

a[4]*b[4]=16

a[6]*b[7]=42 【题7.34】 10 1 2 3 4 5 6 7 8 9

五、程序填空。请在下面程序空白处填入合适的语句。【题7.35】 a[j++]=a[i] 【题7.36】 array[i]>array[j]

六、编程。对下面的问题编写程序并上机验证。

【题7.37】 编写程序,用冒泡法对20个整数排序(按升序排序)。

/*lx7_1.c*/ #include “stdio.h” #define N 20 void main(){

int data[N];

int i,j,t;

printf(“请输入 %d 个整数:n”,N);

for(i=0;i

scanf(“%d”,&data[i]);

printf(“n”);

for(i=0;i

for(j=0;j

if(data[i]>data[j])

{

t=data[i];data[i]=data[j];data[j]=t;} – 2 – 第一部分习题解答

printf(“排序后的输出为:n”);

for(i=0;i

printf(“%d ”,data[i]);} 【题7.38】 编写程序,将一个数插入到有序的数列中去,插入后的数列仍然有序。

/*lx7_2.c*/ #include “stdio.h” #define N 5 void main(){

int a[N+1];

int i,j,t,num;

printf(“请输入 %d 个整数:n”,N);

for(i=0;i

scanf(“%d”,&a[i]);

printf(“n”);

for(i=0;i

for(j=i+1;j

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

{ t=a[i];a[i]=a[j];a[j]=t;}

printf(“第一次输出:n”);

for(i=0;i

printf(“%d ”,a[i]);

printf(“n”);

printf(“请输入一个数:”);

scanf(“%d”,&num);

if(num>=a[N-1])

a[N]=num;

else if(num

{

for(i=N-1;i>=0;i--)

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

a[0]=num;

}

else

for(i=N-1;i>=0;i--)

if(a[i]>num)

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

else

{

a[i+1]=num;

break;

– 3 – C语言程序设计教程实验指导与习题解答

}

printf(“第二次输出:n”);

for(i=0;i

printf(“%d ”,a[i]);

printf(“n”);} 【题7.39】 编写程序,在有序的数列中查找某数,若该数在此数列中,则输出它所在的位置,否则输出no found。

/*lx7_3.c*/ #include “stdio.h” #define N 10 void main(){

int a[N];

int num,i;

char ch='n';

printf(“请输入一个数组:n”);

for(i=0;i

scanf(“%d”,&a[i]);

printf(“输出数组:n”);

for(i=0;i

printf(“%d ”,a[i]);

printf(“n”);

printf(“请输入一个数n”);

scanf(“%d”,&num);

printf(“开始查找n”);

for(i=0;i

if(num= =a[i])

{

ch='y';

printf(“%d 在数组的第 %d 位n”,num,i+1);

}

if(ch= ='n')

printf(“no foundn”);} 【题7.40】 若有说明:int a[2][3]={{1,2,3},{4,5,6}};,现要将a的行和列的元素互换后存到另一个二维数组b中,试编程。

/*lx7_4.c*/ #include “stdio.h” void main(){

int a[2][3]={{1,2,3},{4,5,6}},b[3][2];– 4 – 第一部分习题解答

int i,j;

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

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

printf(“%dt”,a[i][j]);

printf(“n”);

}

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

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

b[j][i]=a[i][j];

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

{

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

printf(“%dt”,b[i][j]);

printf(“n”);

} } 【题7.41】 定义一个含有30个整数的数组,按顺序分别赋予从2开始的偶数,然后按顺序每五个数求出一个平均值,放在另一个数组中并输出,试编程。

/*lx7_5.c*/ #include “stdio.h” #define N 30 #define M 5 void main(){

int a[N],sum[N/M];

float s;

int i,j,k=0;

for(i=0;i

{ a[i]=2*(i+1);

printf(“%dt”,a[i]);

}

printf(“n”);

for(i=0;i

{ s=0;

for(j=i;j

s=s+a[j];

sum[k++]=s/M;

}

for(i=0;i

printf(“%dt”,sum[i]);}

– 5 – C语言程序设计教程实验指导与习题解答

【题7.42】 编写程序,在5行7列的二维数组中查找第一次出现的负数。

/*lx7_6.c*/ #include “stdio.h” void main(){

int i,j,a[5][7];

printf(“请输入一个二维数组n”);

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

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

scanf(“%d”,&a[i][j]);

printf(“n”);

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

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

if(a[i][j]<0)

{

printf(“第一次出现的负数被找到!,负数值是%dn”,a[i][j]);

goto end1;

}

end1:

;} 【题7.43】 从键盘上输入60个字符,求相邻字母对(如ab)出现的频率。

/*lx7_7.c*/ #include “stdio.h” #include “string.h” #define N 60 void main(){

int i, len;

char ch[N];

float p,m=0;

printf(“请输入一个字符串:n”);

scanf(“%s”,ch);

len=strlen(ch);

for(i=0;ch[i]!='' && i

if(ch[i]= =ch[i+1]+1 || ch[i]+1= =ch[i+1])

m++;

p=m/len;

printf(“相邻字母对出现的频率是 %fn”,p);} 【题7.44】 编写程序,定义数组int a[4][6], b[4][6], c[4][6],并完成如下操作:(1)从键盘上输入数据给数组a、b。

(2)将数组a与数组b各对应元素作比较,如果相等,则数组c的对应元素为0,若前– 6 – 第一部分习题解答

者大于后者,则数组c的对应元素为1;若前者小于后者,则数组c的对应元素为−1。

(3)输出数组c各元素的值。

/*lx7_8.c*/ #include “stdio.h” void main(){

int i,j;

int a[4][6],b[4][6],c[4][6];

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

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

scanf(“%d”,&a[i][j]);

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

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

scanf(“%d”,&b[i][j]);

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

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

if(a[i][j]==b[i][j])

c[i][j]=0;

else if(a[i][j]>b[i][j])

c[i][j]=1;

else

c[i][j]=-1;

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

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

printf(“%dt”,c[i][j]);

printf(“n”);

} } 【题7.45】 编写程序,从键盘上输入两个字符串a和b,要求不用strcat()函数把串b的前五个字符连接到串a中,如果b的长度小于5,则把b的所有元素都连接到a中。

/*lx7_9.c*/ #include “stdio.h” #include “string.h” #define N 80 void main(){

char a[N],b[N];

int i,j,lena,lenb;

printf(“Please input first string:”);

scanf(“%s”,a);

printf(“Please input second string:”);

– 7 – C语言程序设计教程实验指导与习题解答

scanf(“%s”,b);

lena=strlen(a);

lenb=strlen(b);

if(lenb<=5)

{ for(i=lena,j=0;i

a[i]=b[j++];

a[i]='';

}

else

{ for(i=lena,j=0;j<5;j++)

a[i++]=b[j];

a[i]='';

}

printf(“Output first string:”);

printf(“%sn”,a);} 【题7.46】 编写函数,从一个排好序的整型数组中删去某数。

/*lx7_10.c*/ #define N 10 #include “stdio.h” int dele(int str[ ],int y){

int i, j, p=0;

for(i=0;i

if(str[i]= =y)

{ j=i;p=1;break;}

if(p= =1)

for(;j<=N-1;j++)

str[j]=str[j+1];

return p;} void main(){

int i, j, t, a[N], x;

printf(“Input an array:n”);

for(i=0;i

scanf(“%d”,&a[i]);

for(i=0;i

for(j=i;j

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

{ t=a[i],a[i]=a[j],a[j]=t;} – 8 – 第一部分习题解答

printf(“Input a data:”);

scanf(“%d”,&x);

if(dele(a,x))

for(i=0;i

printf(“%d ”,a[i]);

else

printf(“no deleten”);}

【题7.47】 编写函数,它将无符号整数转换成二进制字符表示。

/*lx7_11.c*/ #include “stdio.h” void dtob(unsigned m){

char str[17];

int i,n;

n=m;

for(i=15;i>=0;i--)

{

if(m%2= =1)

str[i]='1';

else

str[i]='0';

m=m/2;

}

str[16]='';

printf(“n=%dt%sn”,n,str);} void main(){

unsigned num;

printf(“Please input a integer:”);

scanf(“%d”,&num);

dtob(num);} 【题7.48】 编写函数lower()模拟标准函数strlwr(),调用形式为lower(char *st),其作用是将字符串st中的大写字母转换成小写。

/*lx7_12.c*/ #include “stdio.h” #define N 20 void lower(char *st){ int i=0;

while(st[i]!='')

– 9 – C语言程序设计教程实验指导与习题解答

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

st[i]+=32;

i++;

} } void main(){ char s[N];

printf(“Input a string:n”);

scanf(“%s”,s);

lower(s);

printf(“Output: %sn”,s);} 【题7.49】 编写函数replicate()模拟标准函数strset(),调用形式为replicate(char *st,char ch),其作用是将字符串st中的所有字符设置成ch。

/*lx7_13.c*/ #include “stdio.h” #define N 20 void replicate(char *st,char ch){

int i=0;

while(st[i]!='')

st[i++]=ch;} void main(){

char s[N],ch;

printf(“Input a string:”);

scanf(“%s”,s);

ch='a';

replicate(s, ch);

printf(“nOutput: %sn”,s);} 【题7.50】 编写函数reverse()模拟标准函数strrev(),调用形式为reverse(char *st),其作用是颠倒字符串st的顺序,即按与原来相反的顺序排列。

/*lx7_14.c*/ #include “stdio.h” #include “string.h” void reverse(char *st){ int i=0,n;

char t;

n=strlen(st);– 10 – 第一部分习题解答

for(;i

{ t=st[i];

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

st[n-i-1]=t;

} } void main(){ char s[80];

printf(“Input a stringn”);

scanf(“%s”,s);

reverse(s);

printf(“Output: %sn”,s);}

– 11 –

第二篇:c语言课后答案

c语言课后答案

第一章习题答案

一、选择题

1~5:BDCDA 6~10:DABBB 11~12:CC

二、填空题

1、main()

2、函数首部,函数体

3、函数

4、编辑、编译、连接、运行

5、.cpp、.obj、.exe

6、;或 分号

三、编程题

#include

/* 包含标准库的信息 */

void main()

/* 定义名为main 的函数,它不接受参数值 */

{

/* main函数的语句都被括在花括号中 */

printf(“hello, worldn”);

/* main 函数调用库函数printf 以显示字符序列,其中n代表换行符 */

}

第二章习题答案

一、选择题

1~5:CBABB 6~10:CDCDD 11~15:CADBC 16~20:BDAAD

二、填空题

1、整型,实型,字符型,枚举类型2、1 3、9 4、12353514 5、2,1 6、2,2 7、10 20 0

8、a=14 9、2,3,1

10、double

第三章习题答案

一、选择题

1~5:CBBBC 6~10:DDDBB

二、填空题

1、控制语句,表达式语句,复合语句

2、;

3、{} 4、1

5、a

6、c:dec=120,oct=170,hex=78,ASCII=x 7、32767,32767 8、10,A,10 9、3 3

10、(1)123.456001

(2)□□□□□□□ 123.456

(3)123.4560

(4)8765.456700

(5)□□□□□□ 8765.457

(6)8765.4567

(7)8765.4567

三、编程题

1、参考答案:

#include void main()

{

int a,b,c;

long int u,n;

float x,y,z;char c1,c2;

a=3;b=4;c=5;

x=1.2;y=2.4;z=-3.6;

u=51274;n=128765;

c1='a';c2='b';printf(“n”);

printf(“a=%2d b=%2d

c=%2dn”,a,b,c);

printf(“x=%8.6f,y=%8.6f,z=%9.6fn”,x,y,z);

printf(“x+y=%5.2f y+z=%5.2f z+x=%5.2fn”,x+y,y+z,z+x);

printf(“u=%6ld n=%9ldn”,u,n);

printf(“c1='%c' or %d(ASCII)n”,c1,c1);

printf(“c2='%c' or %d(ASCII)n”,c2,c2);

}

2、参考答案:

#include

void main()

{

float a, b, c, t;

printf(“please input a,b,c:n”);

scanf(“%f, %f, %f”, &a, &b, &c);

t=(a+b+c)/3;

printf(“average of %6.2f、%6.2f and

%6.2f }

3、参考答案:

#include main()

{

int R, d;

float s;

printf(”请输入圆的半径“);

scanf(”%d“,&R);

d=2*R;

c=2*3.14159*R;

printf(”d=%d,c=%fn“,d,c);

}

4、参考答案:

#include void main()

{

int h, f, x, y;/* x为鸡的数量,y为兔的数量 */ printf(”请输入鸡兔的总头数h,总脚数 f:“);

scanf(”%d%d“, &h, &f);

x=(4*h-f)/2;

y=(f-2*h)/2;

printf(”笼中有鸡%d 只,有兔%d只。n“, x, y);is %6.2fn”,a,b,c,t);

}

第四章习题答案

一、选择题

CDDDD CDBBC

二、填空题

1、&&,||,!,!,| |

2、(y%2 0)3、1

4、(a+b>c&&a+c>b&&b+c>a)5、2 3 3 6、7

三、编程题

1、参考答案:

#include void main()

{ int x;

scanf(“%d”,&x);

if(x%5 0 && x%7 0)

printf(“yesn”);else

printf(“non”);

}

2、参考答案:

#include void main()

{ int

a, b, c, max;

printf(“请输入三个整数:%%d%%d%%dn”);

scanf(“%d%d%d”, &a, &b, &c);

if(a>=b)

if(a>=c)max=a;

else max=c;

else

if(b>=c)max=b;

else

max=c;

printf(“n 最大数为:%dn”,max);

}

3、参考答案:

#include void main()

{

float

x , y;

printf(“请输入x 的值:”);

scanf(“%f”, &x);

if(x<1)y=1;

else if(x<10)

y=2*x-1;

else

y=3*x-11;

printf(“y = %.2fn”, y);

}

4、参考答案:

#include void main()

{

int year;

float money,rate,total;

/* money:本金 rate:月利率 total:本利合计

*/

printf(“Input money and year =?”);

scanf(“%f%d”, &money, &year);

/* 输入本金和存款年限 */

if(year 1)rate=0.00315;

/* 根据年限确定利率 */

else if(year 2)rate=0.00330;

else if(year 3)rate=0.00345;else if(year 5)rate=0.00375;

else if(year 8)rate=0.00420;

else rate=0.0;

total=money + money * rate * 12 * year;

/* 计算到期的本利合计 */

printf(“ Total = %.2fn”, total);

}

第五章习题答案

一、选择题

1~5: ACCBA 6~10:ACDCB

二、填空题

1、(1)i<=9 或 i<10(2)j%3!=0 2、7

3、(1)ch=ch+

1、(2)printf(“n”)

三、编程题

1、参考答案:

#include void main()

{ int i=2;

long p=1;

do

{ p=p*i;

i=i+2;

} while(i<10);

printf(“2*4*6*8=%ldn”,p);

}

2、参考答案:

#include void main()

{int n=0;

char c;

c=getchar();

while(c!='n')

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

c=getchar();

}

printf(“%dn”,n);

}

3、参考答案:

#include void main()

{int a,max;

scanf(“%d”,&a);max=a;while(a!=0)

{scanf(“%d”,&a);

if(max

}

printf(“%d”,max);

}

4、参考答案: #include void main()

{

int day = 0, buy = 2;

float sum = 0.0, ave;do {

sum += 0.8 * buy;

day++;

buy *= 2;

}

while(buy <= 100);

ave = sum / day;

printf(“%f”, ave);

}

5、参考答案: #include void main()

{ int f1,f2,f5,count=0;

for(f5=0;f5<=20;f5++)

for(f2=0;f2<=(100-f5*5)/2;f2++)

{ f1=100-f5*5-f2*2;

if(f5*5+f2*2+f1 100)

printf(“No.%2d >> 5: %4d 2: %4d 1: %4dn”,++count,f5,f2,f1);

}

printf(“共有%d 种换法”,count);

}

6、参考答案: #include void main()

{ int i,j,n;printf(“nPlease Enter n:”);

scanf(“%d”,&n);

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

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

printf(“ ”);

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

printf(“*”);

printf(“n”);

}

}

7、参考答案: #include void main()

{

int i, j;

float g, sum, ave;

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

{

sum = 0;

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

{

scanf(“%f”, &g);sum += g;}

ave = sum / 5;

printf(“No.%d ave=%5.2fn”, i, ave);

}

}

8、参考答案: #include void main()

{int n,t,number=20;

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

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

{s=s+a/b;t=a;a=a+b;

b=t;}

printf(“s=%9.6fn”,s);

} 第六章习题答案

一、选择题

1~5:CBABD 6~10:CDBCD

二、填空题

1、库函数(或系统函数),自定义函数

2、无参函数,有参函数

3、传值方式

4、实参,形参5、1 3

6、max is 2 7、15 8、1,2,3

三、编程题

1、参考答案:

#include

int is_prime(int a)

{ int i,p=1;

for(i=2;i

if(a%i 0)

{ p=0;break;}

return(p);}

void main()

{int i=11;

if(is_prime(i))

printf(“1”);

else

printf(“0”);

}

2、参考答案:

#include

int gongyue(int num1,int num2)

{int temp,a,b;

if(num1 < num2)

{temp=num1;num1=num2;num2=temp;}

a=num1;

b=num2;

while(b)

{temp=a%b;a=b;b=temp;}

return(a);

}

void main()

{int i=12,j=8;

printf(“%d”,gongyue(i,j));

}

第七章习题答案

一、选择题

1~5:BDDCC 6~10:CDCAB

二、填空题

1、字符、1 2、2

3、c:win98cmd.exe 4、1,2,3,4,5,6,7,8,9,0 5、325678 6、1,0,7,0, 7、4332

8、(1)j=2

三、编程题

1、参考答案:

#include

void main()

{ int i,a[20],s,count;

s=count=0;

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

scanf(“%d”, &a[i]);

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

{ if(a[i]<0)

continue;

s+=a[i];

2)j>=i

(count++;

}

printf(“s=%dt count=%dn”,s,count);

}

2、参考答案:

#include void main()

{ int a[3][4],i,j,max;

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

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

scanf(“%d”,&a[i][j]);

max=a[0][0];

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

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

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

printf(“max=%dn”,max);

}

3、参考答案:

#include void main()

{ int a[11]={3,4,7,9,10,13,14,15,18,20};

int i,j,n;

scanf(“%d”,&n);

i=0;

while(i<10)

{ if(n

{

for(j=10;j>i;j--)

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

a[i]=n;

break;

}

i++;

}

if(i>=10)

a[10]=n;

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

printf(“%4d”,a[i]);

}

第八章习题答案

max=a[i][j];

一、选择题

1?5 A B C C B 6?10 D A C B B 11?15 D A C C C

二、填空题

1.(1)*(2)&

2.(1)*p(2)**a

(3)2 3.10 4.*p>*s

5.*(++p)

三、编程题

1.编程实现从键盘输入一个字符串,将其字符顺序颠倒后重新存放,并输出这个字符串。

#include

#include

void Inverse(char *pStr)?

main()

{

char str[80]?

printf(“Input a string:n”)?

gets(str)?

/*输入字符串*/

Inverse(str)?

/*将存于 str 数组中的字符串逆序存放*/

printf(“The inversed string is:n”)?

puts(str)?

/*输出字符串*/

}

/*函数功能: 实现字符串逆序存放

函数参数: 字符指针变量,所指向的存储单元存放源字符串,逆序后的字符串也存放于此

返回值: 无*/

void Inverse(char *pStr)

{

int len?

char temp?

char *pStart?

/*指针变量 pStart 指向字符串的第一个字符*/

char *pEnd?

/*指针变量 pEnd指向字符串的最后一个字符*/

len = strlen(pStr)?

/*求出字符串长度*/

for(pStart=pStr,pEnd=pStr+len?1? pStart

{

temp = *pStart?

*pStart = *pEnd?

*pEnd = temp?

}

}

2.从键盘任意输入 10 个整数,用函数编程实现计算最大值和最小值,并返回它们所在数组

中的位置。

#include

int FindMax(int num[], int n, int *pMaxPos)?

int FindMin(int num[], int n, int *pMinPos)? main()

{

int num[10], maxValue, maxPos, minValue, minPos, i?

printf(“Input 10 numbers:n ”)?

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

{

scanf(“%d”, &num[i])?

/* 输入 10 个数*/

}

maxValue = FindMax(num, 10, &maxPos)? /* 找最大值及其所在下标位置 */

minValue = FindMin(num, 10, &minPos)? /* 找最小值及其所在下标位置 */

printf(“Max=%d, Position=%d, Min=%d, Position=%dn”,maxValue, maxPos, minValue, minPos)?

}

/*函数功能:求 n个数中的最大值及其所在下标位置

函数入口参数:整型数组 num,存储 n个整数,整型变量 n,表示数组元素个数

函数出口参数:整型指针变量 pMaxPos,指向的地址单元存储最大值在数组中的下标位置

函数返回值: 最大值*/

int FindMax(int num[], int n, int *pMaxPos)

{

int i, max?

max = num[0]?

/*假设 num[0]为最大*/

*pMaxPos = 0?

/*假设最大值在数组中的下标位置为 0 */

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

{

if(num[i] > max)

{

max = num[i]?

*pMaxPos = i?

}

}

return max ? }

/*函数功能: 求 n个数中的最小值及其所在下标位置

函数入口参数: 整型数组 num,存储 n个整数,整型变量 n,表示数组元素个数

函数出口参数: 整型指针变量 pMinPos,指向的地址单元存储最小值在数组中的下标位置

函数返回值: 最小值*/

int FindMin(int num[], int n, int *pMinPos)

{

int i, min?

min = num[0]?

/*假设 num[0]为最小*/

*pMinPos = 0?

/*假设最小值在数组中的下标位置为 0 */

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

{

if(num[i] < min)

{

min = num[i]?

*pMinPos = i?

}

}

return min ?

}

3.将 5 个字符串从小到大排序后输出。

#include

void main(void)

{ int i?

char *pcolor[5]={ “red”, “blue”, “yellow”, “green”, “purple” }?

void fsort(char *color[ ], int n)?

fsort(pcolor, 5)?

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

printf(“%s ”, pcolor[i])?

}

void fsort(char *color[ ], int n)

{

int k, j?

char *temp?

for(k = 1? k < n? k++)

for(j = 0? j < n?k? j++)

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

{

temp = color[j]?

color[j] = color[j+1]?

color[j+1]

temp?

}

}

4.编写一个能对任意m×n阶矩阵进行转置运算的函数 Transpose()。

#include

#define ROW 3

#define COL 4

void Transpose(int(*a)[COL], int(*at)[ROW], int row, int col)?

void InputMatrix(int(*s)[COL], int row, int col)?

void PrintMatrix(int(*s)[ROW], int row, int col)?

main()

{

int s[ROW][COL]?

/*s 代表原矩阵*/

int st[COL][ROW]?

/*st 代表转置后的矩阵*/

printf(“Please enter matrix:n”)?

InputMatrix(s, ROW, COL)? /*输入原矩阵,s 指向矩阵 s的第 0行,是行指针*/

Transpose(s, st, ROW, COL)?/*对矩阵 s 进行转置,结果存放于 st 中*/

printf(“The transposed matrix is:n”)?

PrintMatrix(st, COL, ROW)? /*输出转置矩阵,*st 指向 st 的第 0 行,是行指针*/

}

/* 函数功能:对任意row行 col 列的矩阵转置

函数入口参数:指向一维整型数组的指针变量a,指向单元存放转置前的矩阵元素

整型变量 row,矩阵的行数即二维整型数组的行数

整型变量 col,矩阵的列数即二维整型数组的列数

函数出口参数:指向一维整型数组的指针变量at,指向单元存放转置后的矩阵元素

函数返回值: 无*/

void Transpose(int(*a)[COL], int(*at)[ROW], int row, int col)

{

int i, j?

for(i=0? i

{

for(j=0? j

{

*(*(at+j)+i)= *(*(a+i)+j)?

}

}

}

void InputMatrix(int(*s)[COL], int row, int col)

/*输入矩阵元素*/

{

int i, j?

for(i=0? i

{

for(j=0? j

{

scanf(“%d”, *(s+i)+j)?

/*这里*(s+i)+j 等价于&s[i][j]*/

}

}

} void PrintMatrix(int(*s)[ROW], int row, int col)

/*输入矩阵元素*/

{

int i, j?

for(i=0? i

{

for(j=0? j

{

printf(“%dt”, *(*(s+i)+j))? /*这里*(*(s+i)+j)等价于 s[i][j]*/

}

printf(“ n”)?

}

} 第九章习题答案

一、选择题

1?5 B D D A C

6?10 B C A D C

二、填空题

1.struct DATA d={202_,10,1}?

2.sizeof(struct node)

3.person[i].sex

4.13431

5.(1)struct node*(2)*s(3)p

三、编程题

1.定义一个能正常反映教师情况的结构体 teacher,包含教师姓名、性别、年龄、所在部门和

薪水; 定义一个能存放两人数据的结构体数组 tea,并用如下数据初始化:{{ “Mary “, ‘W’,40,‘Computer’ , 1234 },{“Andy“, ‘M’,55, ‘English’ , 1834}};要求:分别用结构体数组 tea 和指针

p输出各位教师的信息,写出完整定义、初始化、输出过程。

#include

struct teacher

{ char name[8]?

char sex?

int age?

char department[20]?

float salary?

} ?

struct teacher tea[2]= {{“Mary ”, 'W',40, “Computer” , 1234 },{“Andy ”, 'M',55, “English” , 1834}} ?

main()

{ int i?

struct teacher *p?

for(i=0?i<2?i++)

printf(“%s,t%c,t%d,t%s,t%f”,tea[i].name,tea[i].sex,tea[i].age,tea[i].department,tea[i].salary)?

for(p=tea?p

printf(“%s,t%c,t%d,t%s,t%f”, p?>department, p?>salary)?

}

2.定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。

#include

struct

{int year?

int month?

int day?

}date?

main()

{int days?

printf(“Input year,month,day:”)?

scanf(“%d,%D,%d”,&date.year,&date.month,&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+31?

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?

case11: days=date.day+304?

break?

case 12: days date.day+334?

break?

p?>name,p?>sex,p?>age,}

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

days+=1?

printf(“n%d/%d is the %dth day in%d.”,date.month,date.day,days,date.year)?

}

3.构建简单的手机通讯录,手机通讯录包括信息(姓名、年龄、联系电话),要求实现新建、查询功能。假设通信录最多容纳 50 名联系人信息。

#include

#include

/*手机通讯录结构定义*/

struct friends_list{

char name[10]?

/* 姓名 */

int age?

/* 年龄 */

char telephone[13]?

/* 联系电话 */

}?

int Count = 0?

/* 定义全局变量 Count,记录当前联系人总数 */

void new_friend(struct friends_list friends[ ])?

void search_friend(struct friends_list friends[ ], char *name)? int main(void){

int choice?

char name[10]?

struct friends_list friends[50]?

/* 包含 50 个人的通讯录 */

do{

printf(“手机通讯录功能选项:1:新建 2:查询 0:退出n”)?

printf(“请选择功能:”)?

scanf(“%d”, &choice)?

switch(choice){

case 1:

new_friend(friends)?

break?

case 2:

printf(“请输入要查找的联系人名:”)?

scanf(“%s”, name)?

search_friend(friends, name)?

break?

case 0: break?

}

}while(choice!= 0)?

printf(“谢谢使用通讯录功能!n”)?

return 0?

}

/*新建联系人*/

void new_friend(struct friends_list friends[ ])

{

struct friends_list f?

if(Count

50){

printf(“通讯录已满!n”)?

return?

}

printf(“请输入新联系人的姓名:”)?

scanf(“%s”, f.name)?

printf(“请输入新联系人的年龄:”)?

scanf(“%d”, &f.age)?

printf(“请输入新联系人的联系电话:”)?

scanf(“%s”, f.telephone)?

friends[Count] = f?

Count++?

}

/*查询联系人*/

void search_friend(struct friends_list friends[ ], char *name)

{

int i, flag = 0?

if(Count

0){

printf(“通讯录是空的!n”)?

return?

}

for(i = 0? i < Count? i++)

if(strcmp(name,friends[i].name)

0){

/* 找到联系人*/

flag=1?

break?

}

if(flag){

printf(“姓名: %st”, friends[i].name)?

printf(“年龄: %dt”, friends[i].age)?

printf(“电话: %sn”, friends[i].telephone)?

}

else

printf(“无此联系人!”)?

}

4.建立一个教师链表,每个结点包括学号(no),姓名(name[8]),工资(wage),写出动态创建

函数 creat 和输出函数 print。

#include

#include

#define NULL 0

#define LEN sizeof(struct teacher)

struct teacher

{int no?

char name[8]?

float wage?

struct teacher * next?

}?

int n?

struct teacher *creat(void)

{ struct teacher *head?

struct teacher *p1,*p2?

n=0?

p1=p2=(struct teacher *)malloc(LEN)?

scanf(“%d%s%f”,&p1?>no,p1?>name, &p1?>wage)?

head=NULL?

while(p1?>no!=0)

{ n=n+1?

if(n 1)head p1?

else p2?>next p1?

p2=p1?

p1=(struct teacher *)malloc(LEN)?

scanf(“%d%s%f”,&p1?>no,p1?>name, &p1?>wage)?

}

p2?>next=NULL?

return(head)?

}

void print(struct teacher *head)

{ struct teacher *p?

p=head?

if(head!=NULL)

do{

printf(“%dt%st%fn”, p?>no, p?>name, p?>wage)?

p=p?>next?

} while(p!=NULL)?

}

5.在上一题基础上,假如已经按学号升序排列,写出插入一个新教师的结点的函数 insert。

struct teacher insert(struct teacher *head,struct teacher *tea)

{ struct teacher *p0,*p1,*p2?

p1=head?

p0=tea?

if(head=NULL)

{head=p0? p0?>next=NULL?}

else

while((p0?>no>p1?>no)&&(p1?>next!=NULL))

{ p2=p1?

p1=p1?>next?}

if(p0?>no< p1?>no)

{ if(head p1)

head=p0?

else

{ p2?>next p0?

p0?>next p1?

}

else

{ p1?>next p0?p0?>next=NULL?}

n=n+1?

return(head)?

}

第三篇:C语言课后实验答案

#include using namespace std;int main(){ cout<<“Hello World!”< using namespace std;int main(){ int p,q,r;cout<<“please input two intergers:”<>p>>q;if(p>q){

r=p;

p=q;

q=r;} r=p%q;while(r!=0){p=q;q=r;r=p%q;} cout<<“the maximun common pisor is”<

#include using namespace std;int main(){ double a,b,c;cout<<“please input two numbers:”;

} cin>>a>>b;c=a+b;cout<

#include using namespace std;int main(){ char name1[41],name2[41];cout<

cin>>name1;cout<>name2;cout<

#include #include using namespace std;int main(){

} double a,b;double h;double sum;int n;int i;a=0.0;b=1.0;n=1000;h=(b-a)/n;sum=(sin(a)+sin(b))/2;for(i=1;i

#include using namespace std;double grav(double m1,double m2,double distance){ double g,G=6.67E-11;g=G*m1*m2/(distance*distance);return g;} int main(){ double Gse,Gme,Msun,Mearth,Mmoon,Dme;Msun=1.987E30;Mearth=5.975E24;Gse=grav(Msun,Mearth,1.495E11);cout<<“the gravitation between sun and earth is”< #include using namespace std;int main(){ double a,b,c,s,area;cout<<“please input a,b,c=”;cin>>a>>b>>c;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));cout<<“area=”< #include using namespace std;int main(){ double a,b,c,s,delta,p,q;cout<<“please input a,b,c=”;cin>>a>>b>>c;delta=b*b-4*a*c;p=-b/(2*a);q=sqrt(fabs(delta))/(2*a);if(delta>=0)

cout<<“x1=”<

cout<<“x1=”<

cout<#include using namespace std;int main(){ double c,f;cout<<“请输入一个华氏温度:”;cin>>f;c=5.0/9.0*(f-32);cout<<“对应的华氏温度”<

#include using namespace std;int main(){ unsigned int n;char c1,c2,c3,c4;cout<<“请输入一个界于1000与9999之间的数:”;cin>>n;cout<<“反序输出前的数为:”<

#include using namespace std;int main(){ char ch;cout<<“请输入一个字母:”;cin>>ch;if(ch>='A'&&ch<='Z')

ch=ch-'A'+'a';cout<<“将大写转换为小写后,该字母为:”< using namespace std;int main(){ int change;cout<<“请输入要找给顾客的零钱(以分为单位):”;cin>>change;cout<<“找给顾客的5角硬币个数为:”<

#include using namespace std;int main(){ int a[7];

cout<<“please input an array with seven elements:”<

cin>>a[i];int big=a[0];for(int j=0;j<7;j++)

if(a[j]>big)

big=a[j];

cout<<“max=”<

return 0;}

#include using namespace std;int main(){ int M[5][5];int i,j;for(i=0;i<5;i++){ for(j=0;j<5;j++)M[i][j]=0;M[i][i]=1;} for(i=0;i<5;i++){

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

cout<

cout<

#include using namespace std;int mystrlen(char string[]){ int len=0;while(string[len]!='')

len=len+1;return len;} int main(){ char string[100];cout<<“please input a string(within 99 characters):”<>string;cout<<“The length of the string is:”< #include using namespace std;int main(){ string str1(“Alpha”);string str2(“Beta”);string str3(“Omega”);string str4;str4=str1;cout<

} str4=str1+str2;cout<str1)cout<<“str3>str1”<>str5;cout << str5 << endl;return 0;

#include using namespace std;int main(){ const int M=3;const int N=4;double a[M][N]= { 1,2,3,4, 5,6,7,8, 9,10,11,12 };double b[M][N]= {1,4,7,10, 2,5,8,11, 3,6,9,12 };double c[M][N];

} cout<<“矩阵a和矩阵b的和矩阵c为:”<

c[i][j]=a[i][j]+b[i][j];

cout<

#include using namespace std;int main(){ const int M=3;const int N=4;double a[M*N]= { 1,2,3,4, 5,6,7,8, 9,10,11,12 };double b[M*N]= {1,4,7,10, 2,5,8,11, 3,6,9,12 };double c[M*N];cout<<“矩阵a和矩阵b的和矩阵c为:”<

for(int j=0;j

{

c[i*N+j]=a[i*N+j]+b[i*N+j];

cout<

} } cout<

#include using namespace std;int main(){ const int COUNT=16;int list[]= {

503,87,512,61,908,170,897,275,653,426,154,509,612,677,765,703

};for(int i=0;i

for(int j=COUNT-1;j

if(list[j-1]>list[j])

{

int tmp=list[j-1];

list[j-1]=list[j];

list[j]=tmp;

}

cout<<“the result is:”<

for(int k=0;k<16;k++)

cout<

cout<

#include using namespace std;int main(){ const int MAXSIZE=100;int array[MAXSIZE];int n;cout <<“n=”;cin>>n;int sum,sc;int i,j;for(i=0;i

array[i]=0;array[i]=1;for(i=2;i<=n;i++){

sc=0;

for(j=0;j

{

sum=array[j]*i+sc;

sc=sum/10;

array[j]=sum%10;

} } cout<=0;i--)cout<

#include using namespace std;int main(){ char str[]=“this is a sample”;cout<<“The original string is:”<

} { if(str[i]>='a'&&str[i]<='z')

str[i]=str[i]-'a'+'A';i=i+1;} cout<<“after transform:”< using namespace std;void swap(int x,int y){ int tmp;tmp=x;y=tmp;} int main(){ int a=2,b=3;cout<<“before exchange:a=”<

#include using namespace std;void swap(int &x,int &y){ int tmp=x;x=y;y=tmp;} int main(){ int a=2,b=3;cout<<“before exchange:a”<

#include using namespace std;void bubble_up(int list[],int count){ for(int i=0;ii;j=j-1)

if(list[j-1]>list[j])

{

int tmp=list[j-1];

list[j-1]=list[j];

list[j]=tmp;

} } int main(){ int i;int array[16]= {

503,87,521,61,908,170,897,275,653,426,154,509,612,677,765,703 };cout<<“brfore the class:”<

cout<

cout<

} cout<

#include using namespace std;int max(int x,int y);int main(){ cout<<“enter teo integers:”;int a,b;cin>>a>>b;cout<<“the axium number is”<

return 0;} int max(int x,int y){ return x>y?x:y;} #include using namespace std;int func(){ static int count=0;return ++count;} int main(){ for(int i=0;i<10;i++)

cout<

#include using namespace std;void matrix_multi(double a[],double b[],double c[],int l, int m, int n){ int i,j,k;for(i=0;i

for(j=0;j

{

c[i*n+j]=0;

for(k=0;k

c[i*n+j]=c[i*n+j]+a[i*m+k]*b[k*n+j];

} } int main(){ double a[20]= {

1.0,3.0,-2.0,0.0,4.0,-2.0,-1.0,5.0,-7.0,2.0,0.0,8.0,4.0,1.0,-5.0,3.0,-3.0,2.0,-4.0,1.0 };double b[15]= {

4.0,5.0,-1.0,2.0,-2.0,6.0,7.0,8.0,1.0,0.0,3.0,-5.0,9.0,8.0,-6.0 };double c[12];matrix_multi(a,b,c,4,5,3);cout<<“the result is c=”<

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

cout<

cout<

#include using namespace std;int main(){ int n;cout<<“please input n=?”;cin>>n;int *p=new int[n+1];if(p==0||n<=0){

cout<<“error!”<#include using namespace std;int main(){ char str[]=“abcdef”;char *p=&str[5];while(p>=str){

cout<<*p;

p--;} cout< using namespace std;void selectsort(int *list,int count){ for(int i=0;i

int k=i;

for(int j=i+1;j

if(*(list+j)<*(list+k))k=j;

if(k!=i)

{

int tmp=*(list+i);

*(list+i)=*(list+k);

*(list+k)=tmp;

} } } int main(){

int arry[6]={2,7,2,2,3,1};

selectsort(arry,6);

cout<<“the result is:”<

} for(int i=0;i<6;i++)cout<

#include using namespace std;const int N=3;void move(char from ,char to){ cout<<“from”<

move(p1,p3);else {

hanoi(n-1,p1,p3,p2);

move(p1,p3);

hanoi(n-1,p2,p1,p3);} } int main(){ hanoi(N,'A','B','C');

return 0;}

#include using namespace std;int abs(int x){ return x>=0?x:-x;} double abs(double x){ return x>=0?x:-x;} long abs(long x){ return x>=0?x:-x;} int main(){ int x1=1;double x2=-2.5;long x3=3L;cout<<“|x1|=”<

#include using namespace std;int main(int a,char *ar[]){ if(a!=2){

cout<<“Error!!”<

cout<<“Usage:ProgramName”<

return 1;} cout<<“Hello”<

#include using namespace std;inline int max(int x,int y){ return x>y?x:y;} int main(){ cout<<“please enter two integers”;int a,b;cin>>a>>b;cout<<“the maximum is”<

#include using namespace std;const int N=3;void move(char from,char to){ static long no=0;static int pillar[3]={N,0,0};no++;(pillar[from-'A'])--;(pillar[to-'A'])++;cout<<“Step”<move(p1,p3);else {

hanoi(n-1,p1,p3,p2);

move(p1,p3);

hanoi(n-1,p2,p1,p3);} } int main(){ cout<<“Simulation of hanoi tower.”<

#include using namespace std;void swap(int *xp,int*yp){ int tmp;tmp=*xp;*xp=*yp;*yp=tmp;} void sort(int *x,int*y,int *z){ if(*x>*y)swap(x,y);if(*x>*z)swap(x,z);if(*y>*z)swap(y,z);} int main(){ int x1=2,y1=3,z1=1;cout<<“Befor sorting,the list is:”<

#include using namespace std;class Date { public: int day,month,year;};int main(){ Date date1,date2;cin>>date1.day>>date1.month>>date1.year;cout<>date2.day>>date2.month>>date2.year;cout<

#include using namespace std;class Date { public: int day,month,year;};void set_date(Date& d);void show_date(Date d);int main(){ Date date1,date2;set_date(date1);show_date(date1);set_date(date2);show_date(date2);return 0;} void set_date(Date& d){ cin>>d.day>>d.month>>d.year;} void show_date(Date d){ cout< using namespace std;class Date { int day,month,year;public: void init(int,int,int);void print_ymd();void print_mdy();};void Date::init(int yy,int mm,int dd){month=(mm>=1&&mm<=12)?mm:1;year=(yy>=1900&&yy<=2100)?yy:1900;day=(dd>=1&&dd<=13)?dd:1;} void Date::print_ymd(){cout<

#include using namespace std;class Date { int day,month,year;public: void init(int yy,int mm,int dd){ month=(mm>=1&&mm<=12)?mm:1;year=(yy>=1900&&yy<=2100)?yy:1900;day=(dd>=1&&dd<=31)?dd:1;} void print_ymd();};void Date::print_ymd(){cout<

#include #include using namespace std;class Person { char Name[20];int Age;char Sex;public: Person(){ strcpy(Name,“XXX”);

Age=0;

Sex='m';} ~Person(){ cout<<“Now destroying the instance of Person”<

Age=age;

Sex=(sex=='m'?'m':'f');} void Person::ShowMe(){ cout<cout<<“person:t”;

person1.ShowMe();

person1.Register(“Zhang3”,19,'m');

cout<<“person1:t”;

person1.ShowMe();

cout<<“person2:t”;

person2.ShowMe();

person2=person1;

cout<<“person2t”;

person2.ShowMe();

return 0;}

#include using namespace std;class Date { public: int day,month,year;

public: Date();void init(int,int,int);void print_ymd();void print_mdy();};Date::Date(){ year=1900;month=1;day=1;} void Date::init(int yy,int mm,int dd){ month=(mm>=1&&mm<=12)?mm:1;year=(yy>=1900&&yy<=2100)?yy:1900;day=(dd>=1&&dd<=31)?dd:1;} void Date::print_ymd(){ cout<

#include using namespace std;class Date { int day,month,year;

public: Date();Date(int,int,int);void init(int,int,int);void print_ymd();void print_mdy();};Date::Date(){ year=1900;month=1;day=1;} Date::Date(int yy,int mm,int dd){ init(yy,mm,dd);} void Date::init(int yy,int mm,int dd){ month=(mm>=1&&mm<=12)?mm:1;

year=(yy>=1900&&yy<=2100)?yy:1900;

day=(dd>=1&&dd<=31)?dd:1;} void Date::print_ymd(){ cout<

date2.init(202_,4,8);date2.print_ymd();date2.print_mdy();return 0;}

#include using namespace std;class Date { int day,month,year;public: Date():year(1900),month(1),day(1){} Date(int yy,int mm=1,int dd=1);Date(Date& d){ year=d.year;month=d.month;day=d.day;} void print_ymd();};Date::Date(int yy,int mm,int dd):year(yy),month(mm),day(dd){} void Date::print_ymd(){ cout<

return 0;}

#include #include using namespace std;class Person { char Name[20];int Age;char Sex;public: Person(){ strcpy(Name,“XXX”);

Age=0;

Sex='m';} ~Person(){ cout<<“Now destroying the instance of Person”<

Age=age;

Sex=(sex=='m'?'m':'f');} void Person::ShowMe(){ cout<cout<<“person:t”;

person1.ShowMe();

person1.Register(“Zhang3”,19,'m');

cout<<“person1:t”;

person1.ShowMe();

cout<<“person2:t”;

person2.ShowMe();

person2=person1;

cout<<“person2t”;

person2.ShowMe();

return 0;} #include #include using namespace std;class Person { char Name[20];int Age;char Sex;public: Person(){ strcpy(Name,“XXX”);

Age=0;

Sex='m';} ~Person(){ cout<<“Now destroying the instance of Person”<

Age=age;

Sex=(sex=='m'?'m':'f');} void Person::ShowMe(){ cout<cout<<“person:t”;

person1.ShowMe();

person1.Register(“Zhang3”,19,'m');

cout<<“person1:t”;

person1.ShowMe();

cout<<“person2:t”;

person2.ShowMe();

person2=person1;

cout<<“person2t”;

person2.ShowMe();

return 0;}

#include #include using namespace std;int main(){ Person *p1,*p2;p1=new Person;cout<<“person1:t”;p1->ShowMe();p1->Register(“Zhang3,19,'m');cout<<”person1:t“;p1->ShowMe();p2=new Person;cout<<”person2:t“;p2->ShowMe();*p2=*p1;cout<<”person2:t“;p2->ShowMe();delete p1;delete p2;return 0;}

#include using namespace std;class Date { public: int day,month,year;

void init(int,int,int);void print_ymd();

};void Date::init(int yy,int mm,int dd){ year=yy;month=mm;day=dd;} void Date::print_ymd(){cout<init(202_,3,28);p1->print_ymd();int *p2;p2=&date1.year;cout<<*p2<

#include class Test { int x;public: Test(int=0);void print();};Test::Test(int a){x=a;} void Test::print(){ cout<<”

x=“<x=“<x<<”n(*this).x=“<<(*this).x< #include using namespace std;class Person { char Name[20];char Sex;int Age;public: void Register(char *name,int age,char sex){ strcpy(Name,name);

Age=age;

Sex=(sex=='m'?'m':'f');} void ShowMe(){cout<Number=number;

Register(name,age,sex);} void ShowStu(){ cout<

ShowMe();} };int main(){ Student stu;stu.RegisterStu(”计算机51“,85071011,”张弓长“,18,'m');stu.ShowStu();stu.ShowMe();return 0;} #include #include using namespace std;class Person { protected: char Name[20];char Sex;int Age;public: void Register(char *name,int age,char sex){ strcpy(Name,name);

Age=age;

Sex=(sex=='m'?'m':'f');} void ShowMe(){cout<Number=number;

strcpy(Name,name);

Age=age;

Sex=(sex=='m'?'m':'f');} void ShowStu(){ cout<

ShowMe();} };int main(){ Student stu;stu.Register(”计算机51“,85071011,”张弓长“,18,'m');stu.ShowStu();//stu.ShowMe();return 0;}

#include #include using namespace std;class Person { char Name[10];int Age;public: Person(char *name,int age){ strcpy(Name,name);

Age=age;

cout<<”constructor of person“<

:Person(name,age),Monitor(name1,age1){ strcpy(ClassName,classname);

cout<<”constructor of Student“<

第四篇:c语言课后答案

第7章 C++中新增语法功能

一、选择题。在以下每一题的四个选项中,请选择一个正确的答案。

【题7.1】B

【题7.2】D

【题7.3】B

【题7.4】C

【题7.5】B

【题7.6】C

【题7.7】C

【题7.8】A

【题7.9】B

【题7.10】D

二、填空题。请在下面各题的空白处填入合适的内容。

【题7.11】inline

【题7.12】constref

【题7.13】int &6

【题7.14】行尾

【题7.15】引用

【题7.16】返回类型 个数 类型

【题7.17】值调用、地址调用、引用调用。

【题7.18】编辑、编译、链接、运行

【题7.19】cout<

【题7.20】函数的名称

三、程序理解题。请阅读下面的程序,写出程序的运行结果。

【题7.21】n=6, t=10

n=6, t=70

【题7.22】a=150,b=-5

【题7.23】a[0][0]=2

a[0][1]=4

a[1][0]=6

a[1][1]=8

【题7.24】14

xxxxxxx144aa

【题7.25】20

11.04

【题7.26】12.5

四、简答题。简要回答下列个问题。

【题7.27】#include 和 #include “filename.h” 有什么区别?

答:对于#include ,编译器从标准库路径开始搜索 filename.h

对于#include “filename.h”,编译器从用户的工作路径开始搜索 filename.h

【题7.28】数组在做函数实参的时候会转变为什么类型?

答:数组在做实参时会变成指针类型。

【题7.29】说明define和const在语法和含义上有什么不同?

答:

(1)#define是C语法中定义符号变量的方法,符号常量只是用来表达一个值,在编译阶段符号就被值替换了,它没有类型;

(2)const是C++语法中定义常变量的方法,常变量具有变量特性,它具有类型,内存中存在以它命名的存储单元,可以用sizeof测出长度。

【题7.30】什么是常指针,什么是指向常变量的指针?

答:

常指针的含义是该指针所指向的地址不能变,但该地址所指向的内容可以变化,使用常指针可以保证我们的指针不能指向其它的变量,指向常变量的指针是指该指针的变量本身的地址可以变化,可以指向其它的变量,但是它所指的内容不可以被修改。指向长变量的指针定义,【题7.31】class 和 struct 的区别?

答:struct 的成员默认是公有的,而类的成员默认是私有的。

【题7.32】将“引用”作为函数参数有哪些特点?

答:

(1)传递引用给函数与传递指针的效果是一样的。这时,被调函数的形参就成为原来主调函数中的实参变量或对象的一个别名来使用,所以在被调函数中对形参变量的操作就是对其相应的目标对象(在主调函数中)的操作。

(2)使用引用传递函数的参数,在内存中并没有产生实参的副本,它是直接对实参操作;而使用一般变量传递函数的参数,当发生函数调用时,需要给形参分配存储单元,形参变量是实参变量的副本;如果传递的是对象,还将调用拷贝构造函数。因此,当参数传递的数据较大时,用引用比用一般变量传递参数的效率和所占空间都好。

(3)使用指针作为函数的参数虽然也能达到与使用引用的效果,但是,在被调函数中同样要给形参分配存储单元,且需要重复使用“*指针变量名”的形式进行运算,这很容易产生错误且程序的阅读性较差;另一方面,在主调函数的调用点处,必须用变量的地址作为实参。而引用更容易使用,更清晰。

【题7.33】C语言和C++有什么不同?

答:

从机制上:c是面向过程的结构化编程语言;c++是面向对象的编程语言。C++侧重于对象而不是过程,并且C++侧重于类的设计而不是逻辑的设计。

从适用性方面:c适合底层操作、效率高的场合,如操作系统等;c++适合更上层的,复杂的应用程序。

五、编程题。对下面的问题编写成程序并上机验证。

【题7.34】请分别采用传值、传引用和传指针调用的的方式,设计三个函数:cubicByValue(),cubicByRefrence()和cubicByPoint(),用来计算一个数的三次方的值,分析各函数的功能和产生的效果。

#include

int cubicByValue(int a){

return a * a * a;

}

int cubicByRefrence(int & a){

return a * a * a;

}

int cubicByPointer(int * a){

return(*a)*(*a)*(*a);

}

int main(){

int a = 3;

cout << cubicByValue(a)<< endl;

cout << cubicByRefrence(a)<< endl;

cout << cubicByPointer(&a)<< endl;

return 0;

}

【题7.35】请采用函数带默认参数的方法,将矩形的两条边长作为参数,计算不同边长矩形的面积。

#include

using namespace std;

double RectArea(double a = 1, double b = 1){

return a * b;

}

int main(){

double a = 3;

double b = 4;

cout << RectArea()<< endl;

cout << RectArea(a)<< endl;

cout << RectArea(a,b)<< endl;

return 0;

}

运行结果:

2【题7.36】请采用函数重载的方法,将矩形的两条边长作为参数,计算不同数据类型边长矩形的面积。

double RectArea(double a, double b){

return a * b;

}

int RectArea(int a, int b){

return a * b;

}

int main(){

double a = 3.534;

double b = 4.534;

int c = 5;

int d = 6;

cout << RectArea(a, b)<< endl;

cout << RectArea(c, d)<< endl;

return 0;

}

运行结果:

16.0232

【题7.37】定义一个描述复数的结构类型compl,并实现复数的输入和输出。设计两个函数:compl add(compl c1,compl c2)和compl sub(compl c1,compl c2),分别完成复数的加法运算和减法运算。编写主函数验证复数的运算是否正确。

参考答案:

#include

struct complex {

float real;

float com;

};

complex add(complex c1, complex c2){

complex t;

t.real = c1.real + c2.real;

t.com = c1.com + c2.com;

return t;

}

complex sub(complex c1, complex c2){

complex t;

t.real = c1.realc2.com;

return t;

}

complex input(void){

complex c;

cout << “Input a complex number”;

cin >> c.real >> c.com;

return c;

}

void output(complex c){

cout << “Real: ” << c.real << “, complex: ” << c.com << endl;

}

int main(){

complex c1, c2, c3, c4;

c1 = input();

c2 = input();

c3 = add(c1, c2);

c4 = sub(c1, c2);

cout << “c1:”;

output(c1);

cout << “c2:”;

output(c2);

cout << “c1 + c2 =”;

output(c3);

cout << "c1c2 = Real: 5, complex: 3

【题7.38】编写程序,用名为max的函数模板计算两个参数的最大值。分别用一对整型数、浮点数和字符进行测试,验证程序的正确性。

#include

template

T mymax(T x, T y){

return(x > y)?(x):(y);

}

int main(){

//double max(double,double);

int x = 16, y = 18;

long l = 20;

double a = 10.8, b = 12.5;

cout << mymax(a, b)<< endl;

cout << mymax(x, y)<< endl;

cout << mymax(a, l)<< endl;

return 0;

}

输出为:12.5

【题7.39】编写程序,用名为min的函数模板计算三个参数中的最小值。分别用整型数、浮点数和字符进行测试,验证程序的正确性。

#include

template

T mymin(T x, T y, T z){

return(((x < y)?(x):(y))< z)?((x < y)?(x):(y)):(z);

}

int main(){

//double max(double,double);

int x = 16, y = 18, z = 5;

long l = 20;

double a = 10.8, b = 12.5, c = 1.4;

cout << mymin(a, b, c)<< endl;

cout << mymin(x, y, z)<< endl;

cout << mymin(a, l, z)<< endl;

return 0;

}

运行结果:

1.4

第五篇:C语言课后习题答案

C语言课后习题答案-第四版-第一章

5、请参照本章例题,编写一个C语言程序,输出以下信息: **************************** V e r y G o o d!**************************** #include int main(){

printf(“**************************nn”);printf(“

Very Good!nn”);printf(“**************************n”);return 0;}

6、编写一个C语言程序,输入a,b,c三个值,输出其中最大值。#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 int main(){ int a,b,c,max;printf(“please input a,b,c:n”);scanf(“%d%d%d”,&a,&b,&c);max=a>b?a:b;max=max>c?max:c;printf(“The largest number is %dn”,max);return 0;}

第3章

1、假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比。计算公式为

P=(1+r)^n r 为年增长率,n 为年数,p为与现在相比的倍数。#include #include int main(){ float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf(“p=%fn”,p);return 0;}

2、存款利息计算。有1000元,想存5年,可按以下5种办法存:(1)一次存5年期。

(2)先存2年期,到期后将本息在存3年期。(3)先存3年期,到期后将本息在存2年期。

(4)先存1年期,到期后将本息在存1年期,连续存5次。(5)存活期存款。活期利息每一季度结算一次。202_年12月的银行存款利息如下: 1年定期存款利息为4.14%; 2年定期存款利息为4.68%; 3年定期存款利息为5.4%; 5年定期存款利息为5.85%;

活期存款利息为0.27%(活期利息每一季度结算一次。)如果r 为年利率,n 为存款年数,则计算本息和的公式为 1年期本息和:P=1000*(1+r);n年期本息和:P=1000*(1+n*r);存n次1年期的本息和:P=1000*(1+ r)^n;存活期本息和:P=1000*(1+r/4)^4n。

说明:P=1000*(1+r/4)^4n。是一个季度的本息和。#include #include int main(){ float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p=1000;p1=p*((1+r5)*5);

// 一次存5年期

p2=p*(1+2*r2)*(1+3*r3);

// 先存2年期,到期后将本息再存3年期

p3=p*(1+3*r3)*(1+2*r2);

// 先存3年期,到期后将本息再存2年期

p4=p*pow(1+r1,5);

// 存1年期,到期后将本息存再存1年期,连续存5次

p5=p*pow(1+r0/4,4*5);

// 存活期存款。活期利息每一季度结算一次

printf(“p1=%fn”,p1);

// 输出按第1方案得到的本息和

printf(“p2=%fn”,p2);

// 输出按第2方案得到的本息和

printf(“p3=%fn”,p3);

// 输出按第3方案得到的本息和

printf(“p4=%fn”,p4);

// 输出按第4方案得到的本息和

printf(“p5=%fn”,p5);

// 输出按第5方案得到的本息和

return 0;}

3、购房从银行贷了一笔款d,准备每月还款额为p,月利率为r,计算多少月能还清。设d为300000元,p为6000元,r为1%。对求得的月份取小数点后一位,对第2位按四舍五入处理。提示:计算还请月数m 的公式如下: m =[ log p –log(p-d*r)] / log(1+r)可以讲公式该写为

m ={ log [p /(p – d*r)] }/ log(1+r)C的库函数中有求对数的函数log10,是求以10为底的对数,log(p)表示log p。#include #include int main(){ float d=300000,p=6000,r=0.01,m;m=log10(p/(p-d*r))/log10(1+r);printf(“m=%6.2fn”,m);return 0;}

6、请编将“China”,编译成密码,密码规律是:用原有的字母后面第4个字母代替原来的字母。#include int main(){ char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf(“passwor is %c%c%c%c%cn”,c1,c2,c3,c4,c5);return 0;}

7、设圆半径r =1.5,圆柱高h=3,求圆周长、圆面积、圆柱表面积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后2位数字。请编程序。#include int main(){ float h,r,l,s,sq,vq,vz;float pi=3.141526;printf(“请输入圆半径r,圆柱高h∶”);scanf(“%f,%f”,&r,&h);

//要求输入圆半径r和圆柱高h

l=2*pi*r;

//计算圆周长l s=r*r*pi;

//计算圆面积s sq=4*pi*r*r;

//计算圆球表面积sq vq=3.0/4.0*pi*r*r*r;

//计算圆球体积vq vz=pi*r*r*h;

//计算圆柱体积vz printf(“圆周长为:

l=%6.2fn”,l);printf(“圆面积为:

s=%6.2fn”,s);

printf(“圆球表面积为:

sq=%6.2fn”,sq);printf(“圆球体积为:

v=%6.2fn”,vq);printf(“圆柱体积为:

vz=%6.2fn”,vz);return 0;}

第四章

4、有3个正整数a,b,c,有键盘输入,输出其中最大的数。#include int main(){ int a,b,c;printf(“请输入三个整数:”);scanf(“%d,%d,%d”,&a,&b,&c);if(a

if(b

printf(“max=%dn”,c);

else

printf(“max=%dn”,b);

else if(a

printf(“max=%dn”,c);else

} printf(“max=%dn”,a);return 0;

4、有3个正整数a,b,c,有键盘输入,输出其中最大的数。#include int main(){

int a,b,c,temp,max;printf(“请输入三个整数:”);scanf(“%d,%d,%d”,&a,&b,&c);temp=(a>b)?a:b;

/*将a和b中的大者存入temp中*/ max=(temp>c)?temp:c;

/*将a和b中的大者与c比较,取最大者*/ printf(“三个整数的最大数是%dn”,max);return 0;}

5、从键盘输入一个小于1000的正整数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输入数据后先对其检查是否为小于1000的正数。若不是,则要求从新输入。#include #include #define M 1000 int main(){ int i,k;

printf(“请输入一个小于%d的整数i:”,M);scanf(“%d”,&i);while(i>M)

{ printf(“输入的数不符合要求,请重新输入一个小于%d的整数i:”,M);

} k=sqrt(i);printf(“%d的平方根的整数部分是:%dn”,i,k);return 0;} 6、有一个函数: scanf(“%d”,&i);

x

(x<1)

y(1<=x<10)

3*x – 11

(x>=10)=

2*x1

(x<0)

Y =

0

(x=0)

(x>0)

写程序,输入x 的值,输出y 相应的值。#include int main(){

int x,y;

printf(“please enter x:”);

scanf(“%d”,&x);

y=0;

if(x>=0)

if(x>0)y=1;

else y=-1;

printf(“x=%d,y=%dn”,x,y);return 0;{ }

8、给出一百分制成绩,要求输出成绩等级‘A’,’B’,’C’,’D’,’E’。90分以上为‘A’,80~89分为’B’,70~79分为’C’,60~69分为’D’,60分以下为’E’。#include int main(){ float score;

char grade;

printf(“请输入学生成绩:”);

scanf(“%f”,&score);

while(score>100||score<0){

}

switch((int)(score/10)){ case 10: case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;printf(“n 输入有误,请重输”);scanf(“%f”,&score);case 6: grade='D';break;case 5: case 4: case 3: case 2: case 1: case 0: grade='E';}

printf(“成绩是 %5.1f,相应的等级是%cn ”,score,grade);return 0;}

9、给出一个不多于5位数的正整数;(1)求出它是几位数;(2)分别输出每一位数字;

(3)按逆顺序输出各位数,例如原有数为123,应输出321.#include #include int main(){ int num,inp,ten,hundred,thousand,ten_thousand,place;//分别代表个位,十位,百位,千位,万位和位数

printf(“请输入一个整数(0-99999):”);scanf(“%d”,&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;printf(“位数:%dn”,place);printf(“每位数字为:”);ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;inp=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);printf(“n反序数字为:”);switch(place){ case 5: printf(“%d%d%d%d%dn”,inp,ten,hundred,thousand,ten_thousand);break;case break;case 3:printf(“%d%d%dn”,inp,ten,hundred);break;case 2: printf(“%d%dn”,inp,ten);break;case 1: printf(“%dn”,inp);break;} return 0;}

11、输入4个整数,要求按由小到大的顺序输出。#include int main(){ int t,a,b,c,d;printf(“请输入四个数:”);scanf(“%d%d%d%d”,&a,&b,&c,&d);printf(“a=%d,b=%d,c=%d,d=%dn”,a,b,c,d);if(a>b)

{ t=a;a=b;b=t;} if(a>c)4: printf(“%d%d%d%dn”,inp,ten,hundred,thousand);

{ 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;} printf(“排序结果如下: n”);printf(“%d %d %d %d n” ,a,b,c,d);return 0;}

12、有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为 1,这4个圆塔高位10cm, 塔以外无建筑物。求该点的建筑物高度(塔外的高度为零)。#include int main(){ int h=10;float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;printf(“请输入一个点(x,y):”);scanf(“%f,%f”,&x,&y);d1=(x-x4)*(x-x4)+(y-y4)*(y-y4);

/*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);if(d1>1 && d2>1 && d3>1 && d4>1)

h=0;/*判断该点是否在塔外*/ printf(“该点高度为 %dn”,h);return 0;} 第五章

例5.7

用pi/4=1-1/3+1/5-1/7+„„公式求pi近似值,直到发现某一项的绝对值小于10^6为止。

#include #include

// 程序中用到数学函数fabs,应包含头文件math.n int main(){ int sign=1,count=0;

// sign用来表示数值的符号,count用来统计循环次数 double pi=0.0,n=1.0,term=1.0;// pi开始代表多项式的值,最后代表π的值,n

母,term代表当前项的值

while(fabs(term)>=1e-8)

// 检查当前项term的绝对值是否大于或等于10的(-6)次方

{

pi=pi+term;

// 把当前项term累加到pi中 n=n+2;

// n+2是下一项的分母

sign=-sign;

// sign代表符号,下一项的符号与上一项符号相反

} pi=pi*4;

// 多项式的和pi乘以4,才是π的近似值

printf(“pi=%10.8fn”,pi);

// 输出π的近似值

printf(“count=%dn”,count);

// 输出循环次数

return 0;}

3、输入两个正整数m和n,求其最大公约数和最小公倍数。#include int main()term=sign/n;

// 求出下一项的值term count++;

// count累加1 { int p,r,n,m,temp;printf(“请输入两个正整数n,m:”);scanf(“%d,%d,”,&n,&m);if(n

temp=n;

n=m;

m=temp;} p=n*m;while(m!=0){

r=n%m;

n=m;

m=r;} printf(“它们的最大公约数为:%dn”,n);printf(“它们的最小公约数为:%dn”,p/n);return 0;}

4、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。#include int main(){

char c;

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

printf(“请输入一行字符:n”);

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

}

printf(“字母数:%dn空格数:%dn数字数:%dn其它字符数:%dn”,letters,space,digit,other);

return 0;

}

5、求Sn=a+aa+aaa+„„+aa„„a之值,其中a是一个数字,n 表示a 的位数。n 由键盘输入。#include int main(){ int a,n,i=1,sn=0,tn=0;printf(“a,n=:”);scanf(“%d,%d”,&a,&n);while(i<=n){

} printf(“a+aa+aaa+...=%dn”,sn);return 0;}

6、求和:1!+2!+3!+„„+20!。#include int main(){ tn=tn+a;/*赋值后的tn为i个 a组成数的值*/ sn=sn+tn;/*赋值后的sn为多项式前i项之和*/ a=a*10;++i;double s=0,t=1;int n;for(n=1;n<=20;n++){

t=t*n;

s=s+t;} printf(“1!+2!+...+20!=%22.15en”,s);return 0;}

7、求1+2+3+„„+100+1+2^2+3^2+4^2++50^2+1+1/2+1/3+1/4+„„+1/10。#include int main(){ int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for(k=1;k<=n1;k++)/*计算1到100的和*/

{s1=s1+k;} for(k=1;k<=n2;k++)/*计算1到50各数的平方和*/

{s2=s2+k*k;} for(k=1;k<=n3;k++)/*计算1到10的各倒数和*/

{s3=s3+1/k;} printf(“sum=%15.6fn”,s1+s2+s3);return 0;}

8、输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如153是一位水仙花数,因为153=13+53+33。#include int main(){ int i,j,k,n;printf(“parcissus numbers are ”);for(n=100;n<1000;n++){

} printf(“n”);return 0;i=n/100;j=n/10-i*10;k=n%10;if(n==i*i*i + j*j*j + k*k*k)printf(“%d ”,n);}

9、编程求1000之内的完数,并按下面格式输出其因子:its factors are 1,2,3 #include 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){

} printf(“%d,its factors are ”,m);for(i=1;i

} return 0;}

10、有一个分数序列:

2/1,3/2,5/3,8/5,13/8,21/13,„„ 求出这个数列前20项之和。#include int main(){ int i,n=20;double a=2,b=1,s=0,t;for(i=1;i<=n;i++){

} printf(“sum=%16.10fn”,s);return 0;}

11、一个球从100米敢赌下落,每次反弹高度为原来的一半,在下落,在反弹。求第10次落地时共经过多少米?第10次反弹多高?

#include s=s+a/b;t=a, a=a+b, b=t;int main(){ double sn=100,hn=sn/2;int n;for(n=2;n<=10;n++){

} printf(“第10次落地时共经过%f米n”,sn);printf(“第10次反弹%f米n”,hn);return 0;}

12、猴子吃桃问题。猴子第1天摘了若干个桃子,当即吃了一半零一个;第2剩下的吃了一半零一个,一次循环。到第十天时想吃就剩下一个桃子。求第一天摘了几个桃子? #include int main(){ int day,x1,x2;day=9;x2=1;sn=sn+2*hn;

/*第n次落地时共经过的米数*/ hn=hn/2;

/*第n次反跳高度*/ while(day>0){ x1=(x2+1)*2;

/*第1天的桃子数是第2天桃子数加1后的2倍.*/

} printf(“total=%dn”,x1);return 0;}

16、输出以下图案:

*

*** ***** ******* ***** *** * #include int main(){ int i,j,k;for(i=0;i<=3;i++)x2=x1;day--;{

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

printf(“ ”);

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

printf(“*”);

printf(“n”);} for(i=0;i<=2;i++){

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

printf(“ ”);

for(k=0;k<=4-2*i;k++)

printf(“*”);

printf(“n”);} return 0;}

17、甲队A,B,C 3 人,乙队 X,Y,Z 3人。A不和X比;X,Z比,请编程找出3 对赛手的名单。#include

int main(){

C不和 char i,j,k;

/*是a的对手;j是b的对手;k是c的对手*/ for(i='x';i<='z';i++)

} 第六章

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

if(i!=j)

for(k='x';k<='z';k++)

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

if(i!='x' && k!='x' && k!='z')

printf(“A--%cnB--%cnC--%cn”,i,j,k);

return 0;

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

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

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

a[j]=0;

}

printf(“n”);

for(i=2,n=0;i<=100;i++){ if(a[i]!=0)

{ printf(“%5d”,a[i]);

n++;

}

if(n==10)

{ printf(“n”);

n=0;

}

}

printf(“n”);

return 0;}

2、用选择法对10个整数排序。#include 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 x 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=%6dn”,sum);

return 0;}

4、有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入数组中。#include int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100};

int temp1,temp2,number,end,i,j;

printf(“array a:n”);

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

printf(“%5d”,a[i]);

printf(“n”);

printf(“insert data:”);

scanf(“%d”,&number);

end=a[9];

if(number>end)

a[10]=number;

else

{ for(i=0;i<10;i++){ if(a[i]>number)

{ temp1=a[i];

a[i]=number;

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

{ temp2=a[j];

a[j]=temp1;

temp1=temp2;

}

break;

}

}

}

printf(“Now array a:n”);

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

printf(“%5d”,a[i]);

printf(“n”);

return 0;}

5、将一个数组中的值按逆顺序重新存放。例如:8,6,5,4,1。要求改为:1,4,5,6,8.。#include #define N 5 int main(){ int a[N],i,temp;

printf(“enter array a:n”);

for(i=0;i

scanf(“%d”,&a[i]);

printf(“array a:n”);

for(i=0;i

printf(“%4d”,a[i]);

for(i=0;i

素的值互换 { temp=a[i];

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

a[N-i-1]=temp;

} printf(“nNow,array a:n”);for(i=0;i

printf(“%4d”,a[i]);

//循环的作用是将对称的元

printf(“n”);

return 0;}

6、输出以下杨辉三角形(要求输出10行)。1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 „„„„„„„„„„„„ #include #define N 10 int main(){ int i,j,a[N][N];

for(i=0;i

{ a[i][i]=1;

a[i][0]=1;

}

for(i=2;i

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

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

for(i=0;i

printf(“%6d”,a[i][j]);

printf(“n”);

}

printf(“n”);

return 0;}

7、输出“魔方阵”。所谓魔方阵就是每行每列和对角线之和相等。例如: 1 6 3 5 7 4 9 2 要求输出1~n^2的自然数构成的魔方阵。#include int main(){ int a[15][15],i,j,k,p,n;

p=1;

while(p==1){ printf(“enter n(n=1--15):”);

scanf(“%d”,&n);

if((n!=0)&&(n<=15)&&(n%2!=0))

p=0;

}

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

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

a[i][j]=0;

j=n/2+1;

a[1][j]=1;

for(k=2;k<=n*n;k++){ i=i-1;

j=j+1;

if((i<1)&&(j>n))

{ i=i+2;

j=j-1;

}

else

{ if(i<1)i=n;

if(j>n)j=1;

}

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

a[i][j]=k;

else

{ i=i+2;

j=j-1;

a[i][j]=k;

}

}

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

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

printf(“%5d”,a[i][j]);

printf(“n”);

}

return 0;}

9、有15个数按由大到小顺序存放在一个数组中,输入一个数,要求折半查找法找出该数是数组中第几个元素值。如果不在数组中则输出“无此数”。#include #define N 15 int main(){

int i,number,top,bott,mid,loca,a[N],flag=1,sign;

char c;

printf(“enter data:n”);

scanf(“%d”,&a[0]);

i=1;

while(i

{ scanf(“%d”,&a[i]);

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

i++;

else

printf(“enter this data again:n”);

}

printf(“n”);

for(i=0;i

printf(“%5d”,a[i]);

printf(“n”);

while(flag){ printf(“input number to look for:”);

scanf(“%d”,&number);

sign=0;

top=0;

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

bott=N-1;

//bott是查找区间的最末位置

if((numbera[N-1]))//要查的数不在查找区间内

loca=-1;

// 表示找不到

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

{mid=(bott+top)/2;

if(number==a[mid])

{ loca=mid;

printf(“Has is %dn”,number,loca+1);

sign=1;

}

else if(number

bott=mid-1;

else

found

%d,its

position

top=mid+1;

}

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

printf(“cannot find %d.n”,number);;

printf(“continu or not(Y/N)?”);

scanf(“ %c”,&c);

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

flag=0;

}

return 0;

}

10、有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符个数。

#include int main(){ int i,j,upp,low,dig,spa,oth;

char text[3][80];

upp=low=dig=spa=oth=0;

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

{ printf(“please input line %d:n”,i+1);

gets(text[i]);

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

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

upp++;

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

low++;

else if(text[i][j]>='0' && text[i][j]<='9')

dig++;

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

spa++;

else

oth++;

}

}

printf(“nupper case: %dn”,upp);

printf(“lower case: %dn”,low);

printf(“digit

: %dn”,dig);

printf(“space

: %dn”,spa);

printf(“other

: %dn”,oth);return 0;}

11、输出以下图案:* * * * *

* * * * * * * * * * * * * * * * * * * * #include int main(){ char a[5]={'*','*','*','*','*'};

int i,j,k;

char space=' ';

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

{ printf(“n”);

printf(“

”);

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

printf(“%c”,space);

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

printf(“%c”,a[k]);

}

printf(“n”);

return 0;}

12、有一行电文,已按下面规律译成密码:A>Z,B>Y,C>X„„a>z,b>y,c>x„„

编程译回原文并输出密码和原文。#include int main(){ int j,n;

char ch[80],tran[80];

printf(“input cipher code:”);

gets(ch);

printf(“ncipher code :%s”,ch);

j=0;

while(ch[j]!='')

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

printf(“noriginal text:”);

for(j=0;j

putchar(tran[j]);

printf(“n”);

return 0;}

12、有一行电文,已按下面规律译成密码:a>z,b>y,c>x„„

编程译回原文并输出密码和原文。#include int main(){ int j,n;

char ch[80];

printf(“input cipher code:n”);

gets(ch);

printf(“ncipher code:%sn”,ch);

j=0;

while(ch[j]!='')

A>Z,B>Y,C>X„„

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

printf(“original text:”);

for(j=0;j

putchar(ch[j]);

printf(“n”);

return 0;}

15、编写一个程序,将S2中的全部字符复制到字符数组S1中。不用strcpy函数。复制时‘’ 也要复制过去。‘’后面的字符不复制。#include int main(){ char s1[80],s2[40];

int i=0,j=0;

printf(“input string1:”);

scanf(“%s”,s1);

printf(“input string2:”);

scanf(“%s”,s2);

while(s1[i]!='')

i++;

while(s2[j]!='')

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

s1[i]='';

printf(“nThe new string is:%sn”,s1);

return 0;}

15、编写一个程序,将S2中的全部字符复制到字符数组S1中。不用strcpy函数。复制时‘’ 也要复制过去。‘’后面的字符不复制。#include #include int main(){ char s1[80],s2[80];

int i;

printf(“input s2:”);

scanf(“%s”,s2);

for(i=0;i<=strlen(s2);i++)

s1[i]=s2[i];

printf(“s1:%sn”,s1);

return 0;} 第七章

1、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。程序有误

#include int main(){ int hcf(int,int);

int lcd(int,int,int);

int u,v,h,l;

scanf(“%d,%d”,&u,&v);

h=hcf(u,v);

printf(“H.C.F=%dn”,h);

l=lcd(u,v,h);

c语言课后答案 (范文大全)
TOP