c语言程序设计(科学出版社)课后习题解答
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(2)l,s
3. printf(“x=%.2f,y=%.2f\n”,x,y);
4.
#include<stdio.h>
main()
{
int num1,num2,num3,sum;
float average;
scanf("%d%d%d",&num1,&num2,&num3);
sum=num1+num2+num3;
}
5.
#include <stdio.h>
main( )
{
int score,rank; /* score表示成绩,rank表示级别*/
printf("Please input score:");
scanf("%d",&score);
rank=score/10;
switch(rank)
{
case 10:
6. 1 2 3 4 5 6
7. b[i]
8. bcdefgha
9. p=sum
10.
#include <stdio.h>
#include <string.h>
main()
{
char str[80];
char *p1, *p2;
gets(str);
p1=str;
p2=str+strlen(str)-1;
case 9: printf("成绩等级为:A\n"); break;
case 8: printf("成绩等级为:B\n"); break;
case 7: printf("成绩等级为:C\n"); break;
case 6: printf("成绩等级为:D\n"); break;
default:printf("成绩等级为:E\n"); break;
2. 12
3.合法的有a,b,d
c.改成s.u.rectangle.length=25;
e.改成s.u.circle.radius=5;
f.改成s.u.circle.radius=5;
4. a,b,c,d (说明:变量b有确定的值之后,b++是合法的。)
5.改为
typedef struct product
while (p1<p2 && *p1++ == *p2-- ) ;
puts( p1<p2 ? "不是回文" : "是回文" );
}
第9章
1.
a. #define F(x) (x)*(x)*(x)
b. #define F(x) (x)%4
c. #define F(x,y) (x)*(y)<100?1:0
case 3:printf("Mar \n");break;
case 4:printf("Apr \n");break;
case 5:printf("May \n");break;
case 6:printf("Jun \n");break;
case 7:printf("Jul \n");break;
gets(str);
while (str[i++] != 0) len++;
printf("the length of string is %d.\n",len);
}
第7章
1. 21
2. 136
3. 16
4. (1) func (2) m++
5. 9
6. 1,6,3,2,3
7. 2,5,1,2,3,-2
8.
#include <stdio.h>
int fun(int a,int b,int c);
main( )
{
int a,b,c,max;
printf("Please input three numbers:");
scanf("%d%d%d",&a,&b,&c);
max=fun(a,b,c);
printf("Max is %d.\n",max);
}
int fun(int a,int b,int c)
{
int max;
max= a>b?(a>c?a:c):(b>c?b:c);
return max;
}
9.
#include <stdio.h>
long int sum(int n);
main( )
{
int n;
printf("n=?");
scanf("%d",&n);
printf(“%d-%d=%d”,a,b,c); /*输出结果*/
}
5.
(1)<stdio.h>
(2)x=10;
(3)printf(“s=%d\n”,s);
第2章
1.
(1) c
(2) a
(3) b g
(4) a d e
(5) d
2. a. 5 b. 295 c. 4 d. 29 e. 9
3. a.x=4,y=6
case 8:printf("Agu \n");break;
case 9:printf("Sep \n");break;
case 10:printf("Oct \n");break;
case 11:printf("Nov \n");break;
case 12:printf("Dec \n");break;
{
sum += n % 10;
n /= 10;
}
printf("%d\n", sum);
}
第6章
1.
(1) d
(2) b
(3) d
(4) c
(5) a
2. 11
3. 3
4. 5689
5. 12,6
6. (1) i-- (2) n
7. (1) char k; (2) i<j
8.
#include <stdio.h>
i=0;
while(i<N && target != a[i])
i++;
found = i<N?i:-1;
printf("%d\n",found);
}
10.
#include <stdio.h>
#define N 80
main( )
{
char str[N];
int len=0,i=0;
printf("Please input a string:");
printf("Sum=%ld.\n",sum(n));
}
long int sum(int n)
{
if (n = = 1 ) return 1;
else return sum(n-1)+n;
}
10.
#include <stdio.h>
void fun(n);
main( )
{
int n;
printf("n=?");
scanf("%d",&n);
fun(n);
}
void fun(n)
{
if (n = = 0) return;
else
{
fun(n/2);
printf("%-2d",n%2);
}
}
第8章
1.
(1) b
(2) d
(3) b
(4) c
(5) c
2. 8
3. 123456789
4. 2 3 4 5 6
5. 345
3.
(1) 6
(2) 4
(3) 1
(4) 1
4.
#include <stdio.h>
main( )
{
int x,y;
scanf("%d",&x);
if (x>-5 && x<0)
y=5*x;
if (x == 0)
y=-1;
if (x>0 && x<10)
y=2*x+1;
printf("x=%d,y=%d\n",x,y);
printf("Time is %d:%d:%d\n",time.hour,time.minute,time.second);
}
9.
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
b. x=4,y=3
f.x=3,y=6
4. 16
5.
#include<stdio.h>
main()
{
int a,b,c;
scanf("%d%d",&a,&b);
c=a*b;
printf("%d*%d=%d",a,b,c);
}
第3章
1.
(1) b
(2) b
(3) d
(4) a
(5) b
2.
(1)&a,&b
{
char name[10];
float price;
}PRODUCT;
PRODUCT products[10];
6.
(1) struct employee
(2) printemp(emp)
7. 2 4 3 9 8
8.
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
};
main( )
{
struct time_struct time;
printf("Input time?\n(Example 18:28:38)\n");
scanf("%d:%d:%d",&time.hour,&time.minute,&time.second);
第3章习题解答
第1章
1.C语言程序主要由预处理命令、函数、注释等组成。
2.填空
(1)分号
(2)main
(3)stdio.h
3.
源程序:
#include<stdio.h>
main( )
{
printf(“******************Baidu Nhomakorabea******\n”);
printf(“Hello World!\n”);
hour=total/3600;
minute=total%3600/60;
second=total%3600%60;
printf("%dhours:%dminutes:%dseconds\n",hour,minute,second);
}
第4章
1.
(1) a
(2) b
(3) b
(4) b
(5) b
2. 0
}
9.
#include <stdio.h>
#define N 10
main( )
{
int a[N],i,target,found;
for (i=0;i<N;i++)
scanf("%d",&a[i]);
printf("Please input a number:");
scanf("%d",&target);
average=sum/3.0;
printf("sum=%d,average=%.2f\n",sum,average);
}
5.
#include<stdio.h>
main()
{
int hour,minute,second,total; /*定义变量代表时、分、秒和总秒数*/
scanf("%d",&total);
}
}
第5章
1.
(1) b
(2) a
(3) b
(4) d
(5) d
2. 7,1
3. y=-1
4. m=6
5. 10,14
6. 3
7. 5
8. ABABCDCD
9.
# include <stdio.h>
main ( )
{
int i;
long int sum=0;
for (i=2;i<=200;i+=2)
main( )
{
float a[10];
int i;
float sum=0,average;
for (i=0;i<10;i++)
{
printf("a[%d]=?",i);
scanf("%f",&a[i]);
sum+=a[i];
}
average=sum/10;
printf("average is %.2f\n",average);
printf("x=?");
scanf("%d",&x);
printf("y=?");
scanf("%d",&y);
SWAP(x,y)
printf("x=%d,y=%d.\n",x,y);
}
第10章
1.
struct student{
int sno;
char sname[10];
char sex;
}stu1,stu2;
2.
a. 4
b. 4
c. #define DOUBLE(x) 2*(x)
3. d
4. -20
5. N is undefined
6. 7.5
7. y=6
8.
#include <stdio.h>
#define SWAP(a,b) {int temp;temp=a;a=b;b=temp;}
main()
{
int x,y;
sum+=i;
printf("2+4+6+...+200=%ld",sum);
}
10.
#include <stdio.h>
main()
{
long int n;
int sum = 0;
printf("Please input the nber:");
scanf("%ld",&n);
while(n != 0)
printf(“*************************”);
}
4.
源程序:
#include <stdio.h>
main( )
{
int a, b, c; /*定义变量*/
scanf(“%d”, &a); /*输入第一个整数*/
scanf(“%d”, &b); /*输入第二个整数*/
c=a-b; /*计算差*/
}
}
6.
#include<stdio.h>
void main()
{
int n;
printf("Please input the digit:");
scanf("%d",&n);
switch(n)
{
case 1:printf("Jan \n");break;
case 2:printf("Feb \n");break;
3. printf(“x=%.2f,y=%.2f\n”,x,y);
4.
#include<stdio.h>
main()
{
int num1,num2,num3,sum;
float average;
scanf("%d%d%d",&num1,&num2,&num3);
sum=num1+num2+num3;
}
5.
#include <stdio.h>
main( )
{
int score,rank; /* score表示成绩,rank表示级别*/
printf("Please input score:");
scanf("%d",&score);
rank=score/10;
switch(rank)
{
case 10:
6. 1 2 3 4 5 6
7. b[i]
8. bcdefgha
9. p=sum
10.
#include <stdio.h>
#include <string.h>
main()
{
char str[80];
char *p1, *p2;
gets(str);
p1=str;
p2=str+strlen(str)-1;
case 9: printf("成绩等级为:A\n"); break;
case 8: printf("成绩等级为:B\n"); break;
case 7: printf("成绩等级为:C\n"); break;
case 6: printf("成绩等级为:D\n"); break;
default:printf("成绩等级为:E\n"); break;
2. 12
3.合法的有a,b,d
c.改成s.u.rectangle.length=25;
e.改成s.u.circle.radius=5;
f.改成s.u.circle.radius=5;
4. a,b,c,d (说明:变量b有确定的值之后,b++是合法的。)
5.改为
typedef struct product
while (p1<p2 && *p1++ == *p2-- ) ;
puts( p1<p2 ? "不是回文" : "是回文" );
}
第9章
1.
a. #define F(x) (x)*(x)*(x)
b. #define F(x) (x)%4
c. #define F(x,y) (x)*(y)<100?1:0
case 3:printf("Mar \n");break;
case 4:printf("Apr \n");break;
case 5:printf("May \n");break;
case 6:printf("Jun \n");break;
case 7:printf("Jul \n");break;
gets(str);
while (str[i++] != 0) len++;
printf("the length of string is %d.\n",len);
}
第7章
1. 21
2. 136
3. 16
4. (1) func (2) m++
5. 9
6. 1,6,3,2,3
7. 2,5,1,2,3,-2
8.
#include <stdio.h>
int fun(int a,int b,int c);
main( )
{
int a,b,c,max;
printf("Please input three numbers:");
scanf("%d%d%d",&a,&b,&c);
max=fun(a,b,c);
printf("Max is %d.\n",max);
}
int fun(int a,int b,int c)
{
int max;
max= a>b?(a>c?a:c):(b>c?b:c);
return max;
}
9.
#include <stdio.h>
long int sum(int n);
main( )
{
int n;
printf("n=?");
scanf("%d",&n);
printf(“%d-%d=%d”,a,b,c); /*输出结果*/
}
5.
(1)<stdio.h>
(2)x=10;
(3)printf(“s=%d\n”,s);
第2章
1.
(1) c
(2) a
(3) b g
(4) a d e
(5) d
2. a. 5 b. 295 c. 4 d. 29 e. 9
3. a.x=4,y=6
case 8:printf("Agu \n");break;
case 9:printf("Sep \n");break;
case 10:printf("Oct \n");break;
case 11:printf("Nov \n");break;
case 12:printf("Dec \n");break;
{
sum += n % 10;
n /= 10;
}
printf("%d\n", sum);
}
第6章
1.
(1) d
(2) b
(3) d
(4) c
(5) a
2. 11
3. 3
4. 5689
5. 12,6
6. (1) i-- (2) n
7. (1) char k; (2) i<j
8.
#include <stdio.h>
i=0;
while(i<N && target != a[i])
i++;
found = i<N?i:-1;
printf("%d\n",found);
}
10.
#include <stdio.h>
#define N 80
main( )
{
char str[N];
int len=0,i=0;
printf("Please input a string:");
printf("Sum=%ld.\n",sum(n));
}
long int sum(int n)
{
if (n = = 1 ) return 1;
else return sum(n-1)+n;
}
10.
#include <stdio.h>
void fun(n);
main( )
{
int n;
printf("n=?");
scanf("%d",&n);
fun(n);
}
void fun(n)
{
if (n = = 0) return;
else
{
fun(n/2);
printf("%-2d",n%2);
}
}
第8章
1.
(1) b
(2) d
(3) b
(4) c
(5) c
2. 8
3. 123456789
4. 2 3 4 5 6
5. 345
3.
(1) 6
(2) 4
(3) 1
(4) 1
4.
#include <stdio.h>
main( )
{
int x,y;
scanf("%d",&x);
if (x>-5 && x<0)
y=5*x;
if (x == 0)
y=-1;
if (x>0 && x<10)
y=2*x+1;
printf("x=%d,y=%d\n",x,y);
printf("Time is %d:%d:%d\n",time.hour,time.minute,time.second);
}
9.
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
b. x=4,y=3
f.x=3,y=6
4. 16
5.
#include<stdio.h>
main()
{
int a,b,c;
scanf("%d%d",&a,&b);
c=a*b;
printf("%d*%d=%d",a,b,c);
}
第3章
1.
(1) b
(2) b
(3) d
(4) a
(5) b
2.
(1)&a,&b
{
char name[10];
float price;
}PRODUCT;
PRODUCT products[10];
6.
(1) struct employee
(2) printemp(emp)
7. 2 4 3 9 8
8.
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
};
main( )
{
struct time_struct time;
printf("Input time?\n(Example 18:28:38)\n");
scanf("%d:%d:%d",&time.hour,&time.minute,&time.second);
第3章习题解答
第1章
1.C语言程序主要由预处理命令、函数、注释等组成。
2.填空
(1)分号
(2)main
(3)stdio.h
3.
源程序:
#include<stdio.h>
main( )
{
printf(“******************Baidu Nhomakorabea******\n”);
printf(“Hello World!\n”);
hour=total/3600;
minute=total%3600/60;
second=total%3600%60;
printf("%dhours:%dminutes:%dseconds\n",hour,minute,second);
}
第4章
1.
(1) a
(2) b
(3) b
(4) b
(5) b
2. 0
}
9.
#include <stdio.h>
#define N 10
main( )
{
int a[N],i,target,found;
for (i=0;i<N;i++)
scanf("%d",&a[i]);
printf("Please input a number:");
scanf("%d",&target);
average=sum/3.0;
printf("sum=%d,average=%.2f\n",sum,average);
}
5.
#include<stdio.h>
main()
{
int hour,minute,second,total; /*定义变量代表时、分、秒和总秒数*/
scanf("%d",&total);
}
}
第5章
1.
(1) b
(2) a
(3) b
(4) d
(5) d
2. 7,1
3. y=-1
4. m=6
5. 10,14
6. 3
7. 5
8. ABABCDCD
9.
# include <stdio.h>
main ( )
{
int i;
long int sum=0;
for (i=2;i<=200;i+=2)
main( )
{
float a[10];
int i;
float sum=0,average;
for (i=0;i<10;i++)
{
printf("a[%d]=?",i);
scanf("%f",&a[i]);
sum+=a[i];
}
average=sum/10;
printf("average is %.2f\n",average);
printf("x=?");
scanf("%d",&x);
printf("y=?");
scanf("%d",&y);
SWAP(x,y)
printf("x=%d,y=%d.\n",x,y);
}
第10章
1.
struct student{
int sno;
char sname[10];
char sex;
}stu1,stu2;
2.
a. 4
b. 4
c. #define DOUBLE(x) 2*(x)
3. d
4. -20
5. N is undefined
6. 7.5
7. y=6
8.
#include <stdio.h>
#define SWAP(a,b) {int temp;temp=a;a=b;b=temp;}
main()
{
int x,y;
sum+=i;
printf("2+4+6+...+200=%ld",sum);
}
10.
#include <stdio.h>
main()
{
long int n;
int sum = 0;
printf("Please input the nber:");
scanf("%ld",&n);
while(n != 0)
printf(“*************************”);
}
4.
源程序:
#include <stdio.h>
main( )
{
int a, b, c; /*定义变量*/
scanf(“%d”, &a); /*输入第一个整数*/
scanf(“%d”, &b); /*输入第二个整数*/
c=a-b; /*计算差*/
}
}
6.
#include<stdio.h>
void main()
{
int n;
printf("Please input the digit:");
scanf("%d",&n);
switch(n)
{
case 1:printf("Jan \n");break;
case 2:printf("Feb \n");break;