
《C程序设计语言习题解答》(CLVIS LTONDO)电子书网盘下载免费在线阅读
链接:> 密码:2tj7
书名:C程序设计语言习题解答
作者:CLVIS LTONDO
出版社:清华大学出版社
出版年份:1997-11-1
页数:216
内容简介:《大学计算机教育丛书·C程序设计语言习题解答(第2版)(影印版)》由清华大学出版社出版。
c语言程序设计第三版指导答案
附录F 课后题参考答案
习 题 1
11 填空题
1.函数
2.主函数main();主函数main() 3.主函数main() 4.函数首部;函数体 5.{;}
6.顺序结构;选择结构;循环结构 7.c;obj;exe
12 思考题
1.答:结构化程序设计是指,为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。顺序结构,选择结构,循环结构
2.答:算法是对具体问题求解步骤的一种描述。计算机算法的表达工具通常采用以下几种方法:①用自然语言表示算法;②用流程图表示算法;③用伪代码表示算法;④用程序设计语言表示算法。
3.略 4.略 5.略 13 编程题 1.答:
#include "stdioh" main()
{ float a=10, b=20, h=5, s; s=(a+b)h/2;
printf("s=%f " , s ); }
2.答:
#include "stdioh"
main()
{ printf(""); printf(" hello world "); printf(""); }
习 题 2
21 单选题
DBDCA DCABB CA
22 填空题
1.2000000
2.1;0500000
3.9;2 4.6 5.100;d 6.(1)20
(2)0
(3)60 7.(1)10;6;4
(2)6;9;15
(3)3;60;83
8.55或 '7'
9.x=4;y=6;z=3;m=463
23 改错题(略)
习 题 3
31 单选题
BDABC ADCAC BBA
32 填空题
1.3 2.261 3.10
4.2, 1;互换a,b的值 5.66 6.003 7.7
8.50,4,c=3<Enter>
9.i=10,j=20<Enter>
10. (1)65 (2)65,A (3)314,12346
(4)3141600e+000,1234560e+002 (5)8765432100 (6)8765432e+003
11.a=2b=5x=88y=7634c1=65c2=97 12.%d/%d;%d/%d=%2f\n
33 改错题(略) 34 编程题
1.答:
#include "stdioh" main() {
int x,y;
scanf("%d%d",&x,&y); printf("\t\tx\ty\n");
printf("十进制数\t%d\t%d\n",x,y); printf("八进制数\t%o\t%o\n",x,y); printf("十六进制数\t%X\t%x\n",x,y); }
2.答:
#include "stdioh" main() {
char ch;
printf("请输入一个大写英文字母"); scanf("%c",&ch);
printf("大写英文字母是%c\n",ch); printf("它的前导字符是%c\n",ch-1); printf("它的后续字符是%c\n",ch+1); }
3.答:
#include "stdioh" main() {
int x,a,b,c,y;
printf("请输入一个三位整数\n"); scanf("%d",&x); a=x/100;
b=(x-a100)/10; c=x%10;
y=c100+b10+a;
printf("反向输出该整数:%d\n",y); } }
4.答:
#include "stdioh" main()
{ int hour;
double salary, salaryday;
scanf("%d,%lf", &hour, &salaryday );
salary=hoursalaryday- hoursalaryday01;
printf("%82lf\n", salary); }
5.答:
#include "stdioh" main() {
int a,b,c,t;
printf("请输入三个整数\n"); scanf("%d%d%d",&a,&b,&c);
printf("交换前a=%d,b=%d,c=%d\n",a,b,c); t=a;a=c;c=b;b=t;
printf("交换后a=%d,b=%d,c=%d\n",a,b,c); }
习 题 4
41 单选题
BADDD ACBBB BA
42 填空题
1.1
2.(1)a>0 || b>0
(2)x>0 && x<=10 (3)a==15 && b==15 && c==15
(4)p<a || p<b || p<c
3.(1)0 (2)1 (3)1 (4)0 (5)1
4.c=1 5.-4 6.1 7.5, 0, 3 8.5 9.123
10.( cvb= ='y'||cvb= ='Y')&&(work>=3||college=='y'|| college=='Y')&&age<=35
43 改错题(略) 44 编程题
1.答
#include "stdioh"
#include "mathh" main() {
double a,b,c,p,area;
scanf("%lf%lf%lf",&a,&b,&c);
printf("三角形的三边为:%llf,%1lf,%1lf\n",a,b,c); if (a+b>c&&a+c>b&&b+c>a) {p=(a+b+c)/2;
area=sqrt(p(p-a)(p-b)(p-c));
printf("三角形的面积为%2lf\n",area); } else
printf("不能构成三角形\n"); }
2.答:
#include "stdioh" main()
{ int x,y;
scanf("%d,%d",&x,&y); if(xx+yy>1000)
printf("%d\n",(xx+yy)/100); else
printf("%d\n",x+y); }
3.答:
#include "stdioh" #include "mathh" main()
{ double x,y;
scanf("%lf",&x); if(x<-2) y=xx-sin(x); else if (x<=2) y=pow(2,x)+x; else y=sqrt(xx+x+1);
printf("x=%2lf y=%2lf\n",x,y); }
4.答:
#include "stdioh" main( )
{ long ge,shi,qian,wan,x; scanf("%ld",&x); wan=x/10000;
qian=x%10000/1000; shi=x%100/10;
ge=x%10;
if (ge==wan&&shi==qian) /个位等于万位并且十位等于千位/ printf("this number is a huiwen\n"); else
printf("this number is not a huiwen\n");
}
5.答:
#include "stdioh" main()
{ float p,w,s,d,f;
scanf("%f,%,%f",p,s,w); if (s>3000) d=015 else if( s>=2000) d=01; else if(s>=1000) d=008; else if(s>=500) d=005; else if(s>=250) d=002; else d=0 f=pws(1-d); printf("%f",f); }
6.答:
#include "stdioh" main()
{ int year,money; char x;
printf("是否是本公司产品(y/n):"); scanf("%c",&x);
if(x=='y'||x=='Y')
{printf("产品使用的年限:"); scanf("%d",&year);
if(year<=1) money=0;
else if(year<8) money=50; else money=100;
printf("产品保修额是:%d\n",money);
}
else
{ money=200;
printf("不是本公司产品,产品保修额是:%d\n",money); } }
7.答:
#include "stdioh" main()
{ int money,num1,num2;
printf("请输入取款额(≤2000):"); scanf("%d",&money);
if(money>2000) printf("请输入取款额数≤2000!\n"); else if(money%50==0) { num1=money/100; num2=(money-num1100)/50; printf("需支付100元:%d张\n",num1); printf("需支付50元:%d张\n",num2); } else printf("输入钱数必须是50的倍数!\n"); }
习 题 5
51 单选题
CDABA ABDDB DBCB
52 填空题
1.2 0 2.333
3.(1)i<10 或 i<=9 (2)j%3!=0
4.(1)flag(float)k/(k+1) 或10flagk/(k+1) (2)flag=-flag 5.(1)max=x
(2)x!=-1 (3)scanf("%d", &x)
6.(1)x<=9或x<10
(2)y=9-x
53 改错题(略) 54 编程题
1.答:
(1)for循环,其他略
#include "stdioh"
main()
{ int i,s=0;
for(i=1;i<=100;i++) s+=ii;
printf("%d\n",s); }
(2)for循环,其他略
#include "stdioh" main()
{ int i=1,p=1; double s=1; do {
s+=10/p; p=++i;
}while(10/p>1e-6); printf("%lf",s); }
2.答:
#include "stdioh" main()
{ int m,n,t,a,b;
scanf("%d,%d" ,&m,&n) if (m<n)
{ t=m m=n n=t } a=m; b=n; t=m%n while(t)
{ m=n n=t t=m%n;} printf("%d",n); }
3.答:
#include "stdioh" main()
{ int x,y,s=1;
scanf("%d,%d",&x,&y) for( y>0 y--)s=x
printf("%d,%d,%d\n ",s%10,s/10%10,s/100%10); }
4.答:
#include "stdioh" main()
{ int x,y,z;
for( x=1 x<20 x++) for( y=1 y<33 y++) { z=100-x-y
if ((z%3)==0 &&(5x+3y+z/3)==100) printf("x=%d,y=%d,z=%d\n",x,y,z) } }
5.答: (a)
#include "stdioh" main()
{ int j,k
for( j=1 j<=4 j++)
{ for(k=1;k<=4-j;k++)printf(" "); printf("") printf("\n") } }
(b)
#include "stdioh" main()
{ int j,k
for( j=1 j<=4 j++)
{for(k=1;k<=4-j;k++)printf(" "); for(k=1 k<=2j-1 k++) printf("") printf("\n") } }
6.答:
程序分析:利用for循环控制在100~999之间,对每个数分解出个位、十位、百位。
#include <stdioh> main() { int i,j,k,n; printf("water flower'number is:"); for(n=100;n<1000;n++) { i=n/100;/分解出百位/ j=n/10%10;/分解出十位/ k=n%10;/分解出个位/ if(n==iii+jjj+kkk) { printf("%-5d",n); } } printf("\n"); }
7.答:
#include <stdioh> main() { int x; for(x=1000;x>=3;x--) if(x%3==1&&x%5==2&&x%7==3) {
printf("该校的学生人数是:%d人\n",x); break; } }
8.答:
#include <stdioh> main() { int x=12,i=1; while(1)
{ if((x+20+i)==2(x+i)) break; i++; } printf("小明母亲在%d年后比小明的年龄大一倍\n",i); printf("那时小明年龄是:%d岁,小明母亲年龄是:%d岁\n",x+i,x+20+i); }
习 题 6
61 单选题
DBCCB BDC
C语言程序设计教程(第3版)
278
62 填空题
1.c 2.60 3.1000 10 4.16
63 编程题
1.答:
#include "stdioh" #include "mathh"
#define F(a) aa+ sqrt(3aa+2a+1) main()
{ float x, f;
scanf("%f", &x );
f=45/F(exp(x))+F(cos(x))+F(sqrt(x))/F(xx) printf("%f\n", f); }
习 题 7
71 单选题
BCADA CCCDA BCBDB
72 填空题
1.(1)2 3 4 5 (2)10010 (3)QuickC
(4)10000 01000 00100 00010 00001 (5)Language
(6)Language Programming 2.(1)j+=2 (2)a[i]>a[j] 3.(1)i=1 (2)x[i-1]
73 改错题(略) 74 编程题
1.答:
#define N 10
#include "stdioh" main()
{ int a[N]={1,2,3,4,5,6,7,8,9,10,osum=0, qsum=0,j; for(j=0;j<10;j++)
if( j%2) qsum+=a[j];
else osum+=a[j];
printf("osum=%d,qsum=%d\n", osum,qsum); }
2.答:
#define N 10
#include "stdioh" main()
{ int a[N]={10,20,30,40,50,60,70,80,90}, j, k, x; scanf("%d",&x); for(j=0;j<N;j++)
if (x<a[j]) break; if(j==N) a[N-1]=x; else
{for(k=N-1; k>j; k--) a[k]=a[k-1]; a[j]=x;}
for(j=0;j<N;j++)
printf("%d ",a[j]); }
3.答:
#define M 3
#include "stdioh" main()
{ int a[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;; for( j=0;j<M;j++)
for(k=0;k<M;k++) if (a[j][k]!=a[k][j]) { flag=0; break;} if (flag) printf("ok"); else printf("NO"); }
4.答:
#include "stdioh" #include "stringh" main()
{ char c1[10],c2[10],j; gets(c1); gets(c2);
for(j=0; (c1[j]==c2[j]) && c1[j] && c2[j]; j++); if (c1[j]>c2[j]) printf("%d\n",1); if (c1[j]<c2[j]) printf("%d\n",-1); if (c1[j]==c2[j]) printf("%d\n",0); }
5.答:
#include "stdioh" #include "stringh" #define M 3 #define N 80 main()
{ char a[M][N],j,k,n[5]={0}; for(j=0;j<M;j++) gets(a[j]);
for(j=0;j<M;j++)
for(k=0;a[j][k];k++)
if( a[j][k]>='A' && a[j][k]<='Z') n[0]++;
else if (a[j][k]>='a' && a[j][k]<='z') n[1]++; else if (a[j][k]>='0' && a[j][k]<='9') n[2]++; else if (a[j][k]==' ' ) n[3]++; else n[4]++;
for(j=0;j<5;j++) printf("%4d", n[j]); }
习 题 8
81 单选题
DBDAC BACCC
82 填空题
1.(1)2, 1 (2)10#30# (3)FOUR, P (4)60
2.(1)49
(2)2
(3)2
(4)
(5)
83 改错题(略) 84 编程题
1.答:
#include "stdioh"
main()
{ int n1,n2,n3,t; int p1,p2,p3;
printf("please input 3 number:n1,n2,n3:"); scanf("%d,%d,%d",&n1,&n2,&n3); p1=&n1;
p2=&n2; p3=&n3;
if(p1> p2) { t=p1;p1=p2;p2=t;}
if(p1>p3) { t=p1;p1=p3;p3=t;} if(p2>p3) { t=p2;p2=p3;p3=t;}
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3); }
2.答:
#include "stdioh" #define N 3 main()
{ int a[N],p=a; for(;p-a<N; p++) scanf("%d",p); p=a+N-1;
for(;p-a>=0; p--) printf("%d ",p); }
3.答:
#include "stdioh" main()
{ int a[10];
int j,minl=0,maxl=0; for(j=0;j<10;j++)
scanf("%d", a+j); for(j=0;j<10;j++)
{ if(a[maxl]<(a+j)) maxl=j; if(a[minl]>(a+j)) minl=j; }
j=a[0]; a[0]=a[minl];a[minl]=j; j=a[9];a[9]=a[maxl];a[maxl]=j; for(j=0;j<10;j++) printf("%d ", (a+j)); }
4.答:
输入阵列如下: 1 2 3 4 5 6 7 8 9 10 11 12 输出阵列如下:
12 11 10 9 8 7 6 5 4 3 2 1
#define M 3
#define N 4
#include "stdioh" main()
{ int a[M][N]={1,2,3,4,5,6,7,8,9,10,11,12},k,j,p=&a[0][0],t; for(k=0,j=MN-1;k<j;k++,j--)
{ t=(p+k); (p+k)=(p+j); (p+j)=t;} for (k=0 k<M k++) { for(j=0 j<N j++)
printf("%4d ",a[k][j]); printf("\n");
} }
5.答:
#include "stdioh" main() {
int len;
char str[20],p=str;
printf("please input a string:\n"); scanf("%s",str); len=0;
while(p!='\0') {
len++; p++; }
printf("the string has %d characters\n",len); }
6.答:
#include "stringh" #include "stdioh" main() {
char str1[5],ch[5][20],k,t,j,c; void sort(char ); for(k=0;k<5;k++) {str1[k]=ch[k]; gets(str1[k]);} for(k=0;k<7;k++)
{ t=k;
for(j=k+1;j<5;j++) if(strcmp((str1+t),(str1+j))>0) t=j; c=(str1+t);
(str1+t)=(str1+k) (str1+k)=c }
for(k=0;k<5;k++) puts(str1[k]); }
习 题 9
91 单选题
CBBAD DBCCD DCABC BCCBA DCDAB
92 填空题
1.120 2.x 3.3,2,2,3 4.fac /i 5.8,17 6.9 7.10/(ii) 8.
fun-in:30,20,10 fun-end:1015,35,1050 10,20,30 9.012345 10.93636 11.(1)r+b[k] (2)x
12.7 5 3 1 9 13.15
14.(1)x (2)t 15.(1)'\0' (2)n++ 16.024
93 改错题(略) 94 编程题
1.答:
void zhuan( )
{ char ch;
while((ch=getchar())!='\n')
{ if(ch>='a' && ch<='z') ch=ch-32; putchar(ch); } }
2.答:
double expp(int n) { int k, fac=1; double sum=1;
for(k=1; k<=n; k++) { fac=k;
sum+=10/fac }
return(sum); }
3.答:
int xy3( int x, int y)
{ int k, num=1;
for(k=1;k<=y k++) num=x
num=num%1000 return num }
4.答:
int age( int n) { int c;
if(n==1) c=10
else c=age(n-1)+2 return c }
5.答:
#include "stdioh"
main()
{ int a,b,c,d;
void fun(int a,int b,int c, int d); scanf("%d%d",&a,&b); fun(a,b,&c,&d);
printf("%d %d",c,d);
}
void fun(int a,int b,int c, int d) { if (b)
{ c=a/b; d=a%b;} }
6.答:
#include "stdioh"
main(int argc,char argv[]) { int k;
printf("argc=%d\n",argc); for (k=1;k<argc; k++) printf("%s\n",argv[k]); }
习 题 10
101 单选题
CDBBB BBBAD CCBDC
102 填空题
1.所有结构体成员所占存储空间的总和 2.与占用存储空间最大的那个成员相等
附录F 课后题参考答案
285
3.(1)struct satype (2)3 (3)saa (4)9 (5)psa=&sa 4.80 5.struct node 6.0
103 编程题
1.答:
#include "stdioh"
struct student {
long num;
char name[20]; char sex; float score; }; main()
{ struct student s[20], temp; int j,k, man=0, woman=0;
float summan=0,sumwoman=0, aveman, avewoman; for(k=0; k<20; k++)
{ scanf("%ld %s %c%f",&s[k]num,s[k]name,&s[k]sex,&s[k]score); if(s[k]sex=='m')
{ summan+=s[k]score; man++;} else
{ sumwoman+=s[k]score;woman++ } }
aveman=summan/man;
avewoman=sumwoman/woman;
printf("%d\t%f\t%d\t%f\n",man,aveman,woman,avewoman); for(k=0; k<19; k++)
for(j=0;j<20-k;j++)
if(s[j]score<s[j+1]score)
{ temp=s[j];s[j]=s[j+1];s[j+1]=temp;} printf("the sorted numbers:\n"); for(k=0;k<20;k++)
printf("%ld\t%s\t%c\t%51f\n",s[k]num,s[k]name,s[k]sex,s[k]score); }
习 题 11
111 单选题
BADD
112 填空题
1.3d3d330 2.(1)28
(2)20 (3)0 (4)--9
3.(1)251
(2)42
(3)209
(4)–295 (5)848
习 题 12
121 单选题
BCDCA ADA
122 填空题
1.rewind(文件指针) 2."d1dat","rb" 3.stdin
4.文本文件;二进制文件 5.(1)"w"
(2)str[i]--32
(3)"r"
6.fopen 7.Hello 8.(1)"r"
(2)fgetc(fp)
(3)time++
C语言程序设计实验与习题指导课后程序设计答案
P18
(1)
#include<stdioh> int main(void) { intcelsius,fahr; fahr=150; celsius=5fahr/9-532/9; printf("fahr=%d,celsius=%d\n",fahr,celsius); return 0; }
(2)
#include<stdioh> int main(void) { intcelsius,fahr; celsius=26; fahr=9celsius/5+32; printf("celsius=%d,fahr=%d\n",celsius,fahr); return 0; }
(3)
#include<stdioh> int main(void) { intaverage,math,eng,comp; math=87; eng=72; comp=93; average=(math+eng+comp)/3; printf("math=%d,eng=%d,comp=%d,average=%d\n",math,eng,comp,average); return 0; }
(4)
#include<stdioh> int main(void) { intn,a,b,c; n=152
c=n%10;
b=(n/10)%10; a=n/100;
printf("整数%d的个位数字是%d,十位数字是%d,百位数字是%d\n",n,c,b,a); return 0;
}
P27
(1)
#include<stdioh> #include<mathh> int main(void) { intcelsius,fahr; printf("Enter celsius:"); scanf("%d",&celsius); fahr=9celsius/5+32; printf("fahr%d\n",fahr); return 0; }
(2)
#include<stdioh> #include<mathh> int main(void) { intmoney,year; doublerate,interest; printf("Enter money,year,rate:"); scanf("%d%d%lf",&money,&year,&rate); interest=moneypow(1+rate,year)-money; printf("interest=%2f\n",interest); return 0; }
(3)
#include<stdioh> #include<mathh> int main(void) { doublex,y; printf("Enter x:"); scanf("%lf",&x);
if(x<0){ y=pow(x+1,2)+2x+1/x; } else{ y=sqrt(x); }
printf("y=f(%f)=%2f\n",x,y); return 0;
}
(4)
#include<stdioh> int main(void) { intx,y; printf("Enter num1:"); scanf("%d",&x); printf("Enter num2:"); scanf("%d",&y); printf("%d+%d=%d\n",x,y,x+y); printf("%d-%d=%d\n",x,y,x-y); printf("%d%d=%d\n",x,y,xy); printf("%d/%d=%d\n",x,y,x/y); printf("%d%%%d=%d\n",x,y,x%y); return 0; }
10的阶乘
#include<stdioh> int main(void) { inti,n,product; printf("Enter n:"); scanf("%d",&n); product=1; for(i=1;i<=n;i++){ product=producti; } printf("product=%d\n",product); return 0; }
#include <stdioh>
#include <stringh>
char fun1(int num)
{
char s[10];
sprintf(s,"%d\n",num);
printf("%s\n",s);
return s;
}
void fun3(void)
{
char p="c:\\f1txt";
char p1="HELLO WORLD!";
int i=0;
FILE f;
f=fopen(p,"w");
if(f==NULL)
printf("打开失败!");
else
fputs(p1,f);
fclose(f);
}
void fun4(void)
{
FILE f1,f2;
char p1="c:\\f1txt";
char p2="c:\\f2txt";
char q;
f1=fopen(p1,"r");
f2=fopen(p2,"w");
if(f1==NULL||f2==NULL)
printf("打开失败!");
else
{
q=fgetc(f1);
while(q!=EOF)
{
fputc(q,f2);
// putchar(q);
q=fgetc(f1);
}
}
fputc(q,f2);
fclose(f1);
fclose(f2);
}
void fun5(void)
{
FILE f;
int i;
char p="c:\\f12txt";
char a[3]={"welcome","to enter","c world!"};
char q=' ';
f=fopen(p,"w");
for(i=0;i<3;i++)
{ fputs(a[i],f);
fputc(q,f);
}
fclose(f);
f=fopen(p,"r");
while((q=fgetc(f))!=EOF)
putchar(q);
fclose(f);
}
main()
{
char p;
int num;
printf("请输入个整数:\n");
scanf("%d",&num);
p=fun1(num);
//printf("%s\n",p);
fun3();
fun4();
fun5();
}
fun1 fun3 fun4 fun5分别对应你的第1,3,4,5道题的子函数;第二道题我没写出来,题目理解的不是很清楚不好写。其他四个函数我运行了,测试OK了。
C语言程序设计复习:
1、理解以下术语的含义:数组、函数、地址 指针 指针变量 直接访问 间接访问、结构体
2、用起泡法对10个数由小到大排序(P134例题)
5、输入10个学生的成绩,分别用函数实现下列功能:
1)计算所有学生的平均分;
2) 计算学生成绩中最高分;
3) 计算学生成绩中最低分;
4) 统计及格学生人数;
5) 查找成绩为指定成绩(如90)记录,如果没有,请给出提示,如果有,请统计满足条件的记录数。
6、有一个已经排好序的数组,今输入一个数,要求按原来排序的规律将它插入数组中。(P153习题74)
7、编写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字字符间空一个空格。如输入1990,应输出“1 9 9 0”。(要求用函数)(P202习题88)
8、编写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。(要求用指针,不能使用strlen()函数) (P279习题106)
9、编写一个程序,打入月份号,输出该月的英文月名。例如,输入“3”则输出“March”(要求用指针数组)。(P279习题1018)
10、将一个数组中的值按逆序重新存放。例如,输入的数组顺序为8,6,5,4,1,要求改为1,4,5,6,8。(P153习题75)
11、编写一个函数用“起泡法”对输入的10个字符按由小到大顺序排序(要用函数)。(P202习题811)
12、将数组a中n个整数按相反顺序存放(要用函数)。(P237例题107)
13、输入一行文字,找出其中大写字母、小写字母、空格、数字及其他字符各有多少。(要求用指针实现)(P279习题108)
14、编写一个函数,将两个字符串连接(要用自定义函数,不能用strcat函数)。(P202习题86)
15、输入3个字符串,按照由小到大的顺序输出。(要求用指针) (P278习题102)
16、输入10整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。写三个函数:1、输入10个数; 2、进行处理; 3、输出10个数(数据对换要求用指针实现)(P278习题103)
参考答案:
2、用起泡法对10个数由小到大排序
#include <stdioh>
void main()
{ int a[10]; int i,j,t;
printf("input 10 numbers :\n");
for (i=0;i<10;i++)
scanf("%d“,&a[i]);
printf(“\n");
for(j=0;j<9;j++)
for(i=0;i<10-j;i++)
if (a[i]>a[i+1])
printf("the sorted numbers :\n");
for(i=0;i<10;i++)
printf(“%d “,a[i]);
}
3、用递归方法求n阶勒让德多项式的值,递归公式为(要求用函数):(P202习题813)
pn(x)= 1 n=0
x n=1
((2n-1)x-pn-1(x)-(n-1)pn-2(x)/n n>1
3、#include <stdioh>
void main()
{
int x,n;
float p(int,int);
printf(“input n & x:”);
scanf(“%d,%d”,&n,&x);
printf(“n=%d,x=%d\n”,n,x);
printf(“P%d(%d)=%62f\n”,n,x,p(n,x));
}
float p(int n,int x)
{
if(n==0)
return 1;
else if(n==1)
return x;
else
return ((2n-1)xp((n-1),x)-(n-1)p((n-2),x))/n;
}
4、输入3个整数,按由小到大的顺序输出(要求用指针类型)(P228例题)
4、输入3个整数,按由小到大的顺序输出(要求用指针类型)
#include <stdioh>
void main()
{
void sort (inta, intb,intc);
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
sort(&a,&b,&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
}
void sort (int a, int b,int c)
{
int tmp;
if (a>b)
{
tmp=a;
a=b;
b=tmp;
}
if (a>c)
{
tmp=a;
a=c;
c=tmp;
}
if (b>c)
{
tmp=b;
b=c;
c=tmp;
}
}
5、输入10个学生的成绩,分别用函数实现下列功能:
1)计算所有学生的平均分;
6) 计算学生成绩中最高分;
7) 计算学生成绩中最低分;
8) 统计及格学生人数;
9) 查找成绩为指定成绩(如90)记录,如果没有,请给出提示,如果有,请统计满足条件的记录数。
#include <stdioh>
void main()
{
int average(int a[]);
int max(int a[]);
int min(int a[]);
int pass(int a[]);
int search(int a[],int g);
int i,j,g;
int score[10],aver,m1,m2,p,s;
printf("Please input 10 scores:\n");
for(i=0;i<10;i++)
scanf("%d",&score[i]);
printf("\n");
aver=average(score);
m1=max(score);
m2=min(score);
p=pass(score);
printf("平均分为: %d\n",aver);
printf("最高分为: %d\n",m1);
printf("最低分为: %d\n",m2);
printf("及格人数为: %d\n",p);
printf("需要查找吗?\n");
printf("输入1继续查找,输入0退出(1/0):");
scanf("%d",&j);
if(j==1)
{
printf("请输入要查找的分数: \n");
scanf("%d",&g);
s=search(score,g);
if(s==0)
printf("没有满足条件的记录");
else
printf("成绩为%d的学生共有%d名\n",g,s);
}
}
int average(int a[])
{
int i;
int aver,sum=a[0];
for(i=1;i<10;i++)
sum=sum+a[i];
aver=sum/10;
return aver;
}
int max(int a[])
{
int i;
int m=a[0];
for(i=1;i<10;i++)
if(m<a[i])
m=a[i];
return m;
}
int min(int a[])
{
int i;
int m=a[0];
for(i=1;i<10;i++)
if(m>a[i])
m=a[i];
return m;
}
int pass(int a[])
{
int i;
int s=0;
for(i=0;i<10;i++)
if(a[i]>=60)
s++;
return s;
}
int search(int a[],int g)
{
int i;
int s=0;
for(i=0;i<10;i++)
if(a[i]==g)
s++;
return s;
}
6、已有一个已排好次序的数组,要求输入一个数后,按原先排序的规律将它插入数组中。
Void main()
;
int temp1,temp2,number,end,i,j;
printf("初始数组如下:");
for (i=0;i<10;i++)
printf("%5d",a[i]);
printf("\n");
printf("输入插入数据:");
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<1;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("a%6d",a[i]);
}
7、编写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字字符间空一个空格。如输入1990,应输出“1 9 9 0”。
#include <iostream>
void main()
{
void stradd(char str[]);
char str[80];
printf("输入一串数字\n\n");
gets(str);
stradd(str);
printf("\n\n加空格后的字符串\n\n");
puts(str);
}
void stradd(char str[])
{
char a[80];
int i=0,j;
for(j=0;str[j]!='\0';j++)
{
a[i]=str[j];
a[i+1]=' ';
i+=2;
}
a[i]='\0';
for(i=0;a[i]!='\0';i++)
{
str[i]=a[i];
}
str[i]='\0';
}
8、编写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。(要求用指针实现)
#include "stdioh"
int stringlength( char *str )
{int n;
n=0;
While(str!=0)
{n++;
str++;
}
Return(n);
}
int main()
{
char str〔100〕,
int len,
printf("Please input a string: "),
scanf("%s"str),
len = stringlength( str ),
printf("The string’s length is %d"len),
return 0;
}
9、编写一个程序,打入月份号,输出该月的英文月名 n。例如,输入“3”则输出“March”,要求用指针数组处理。
#include <stdioh>
main()
{ char month_name[13]={"illegal month","January","February","March","April",
"May","June","July","August","September","October","November","December"};
int n;
printf("Input month: ");
scanf("%d",&n);
if((n<=12)&&(n>=1))
printf("It is %s\n",(month_name+n));
else
printf("It is wrong\n");
}
10、将一个数组中的值按逆序重新存放。例如,输入的数组顺序为8,6,5,4,1,要求改为1,4,5,6,8。
#include <stdioh>
#define N 5;
void main()
{
int a[N],i,temp;
printf(“enter array a:\n”);
for(i=0;i<N;i++)
scanf(“%d”,&a[i]);
printf(“array a:\n”);
for(i=0;i<N;i++)
printf(“\nNow, array a:\n”);
for(i=0;i<N;i++)
printf(“%4d”,a[i]);
printf(“\n”);
}
11、编写一个函数用“起泡法”对输入的10个字符按由小到大顺序排序(要用函数)。
#include <stdioh>
void main()
{ char str[80];
void sort(char str[]);
printf("输入一个字符串\n\n");
gets(str);
sort(str);
printf("\n\n字符由小到大排序为:\n\n");
puts(str);
}
void sort(char str[])
{ int i,j;
char temp;
for(i=0;i<strlen(str);i++)
{
for(j=0;j<strlen(str)-i-1;j++)
{
if(str[j]>str[j+1])
{
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
}
12、将数组a中n个整数按相反顺序存放(要用函数)。
#include <stdioh>
void inv(int x[ ],int n)/形参x是数组名/
{
int temp,i,j,m=(n-1)/2;
for(i=0;i<=m;i++)
{j=n-1-i;
temp=x[i];x[i]=x[j];x[j]=temp;}
return;
}
void main()
{ int i,a[10]={3,7,9,11,0,6,7,5,4,2};
printf(“转换前的数组为:\n");
for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n");
inv(a,10);
printf(“转换后的数组为:\n");
for(i=0;i<10;i++)
printf("%d,",a[i]);
printf("\n");
}
13、输入一行文字,找出期中大写字母、小写字母、空格、数字及其他字符各有多少?(要求用指针实现)
#include<stdioh>
#include<stringh>
int main()
{
char str[40];//创建字符串数组
int count[5]=;//创建计数器数组并初始化
printf("Please input a string \n");
scanf("%s",str);
char p=str;//指针p指向字符串数组str
int n=strlen(str);//确定输入字符串的长度
for(int i=0;i<n;i++)
{
if((p+i)>='A'&&(p+i)<='Z')
{
count[0]++;//统计大写字母数目
}
else if((p+i)>='a'&&(p+i)<='z')
{
count[1]++;//统计小写字母数目
}
else if((p+i)>='0'&&(p+i)<='9')
{
count[2]++;//统计数字数目
}
else if((p+i)=='')
{
count[3]++;//统计空格数目
}
else
{
count[4]++;//统计其他字符数目
}
}
printf("大写字母 小写字母 数字 空格 其他字符:\n");
for(i=0;i<5;i++)
{
printf("%d\t ",count[i]);//打印各统计数目
}
printf("\n\n");
return 0;
}
14、编写一个函数,将两个字符串连接。
#include < stdioh >
#include<stringh>
void concatenate(char string1[],char string2[],char string[])
{
int i,j;
for(i=0;string1[i]!=’\0’;i++)
string[i]=string1[i];
for(j=0;string2[j]!=’\0’;j++)
string[i+j]=string2[j];
string[i+j]=’\0’;
}
void main()
{
char s1[100],s2[100],s[100];
printf(“input string1:”);
scanf(“%s”,s1);
printf(“input string2:”);
scanf(“%s”,s2);
concatenate(s1,s2,s)
printf(“the new string is %s”,s);
}
15、输入3个字符串,按照由小到大的顺序输出。 (要求用指针实现)
#include <stdioh>
void main()
{
void sort (inta, intb,intc);
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
sort(&a,&b,&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
}
void sort (int a, int b,int c)
{
int tmp;
if (a>b)
{
tmp=a;
a=b;
b=tmp;
}
if (a>c)
{
tmp=a;
a=c;
c=tmp;
}
if (b>c)
{
tmp=b;
b=c;
c=tmp;
}
}
16、输入10整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。写三个函数:1、输入10个数; 2、进行处理; 3、输出10个数(数据对换要求用指针实现)
#include <stdioh>
void input(int number[])
{
Int I;
Printf(“input 10 numbers:”);
For(i=0;i<10;i++)
Scanf(“%d”,&number[i]);
}
void max_min_value(int number[])
{
int max,min,p,temp;
max=min=number;
For(p=number+1;p<number+10;p++)
if(p>max)
max=p;
else if(p<min)
min=p;
temp=number[0];number[0]=min;min=temp;
if(max=number) max=min;
temp=number[9];number[9]=max;max=temp;
}
void output(int number[])
{
int p;
printf(“Now, they are: “);
for(p=number;p<number+10;p++)
Printf(“%d”,p);
printf(“\n”);
}
void main()
{
int number[10];
input(number);
max_min_value(number);
output(number);
}
《C语言程序设计》作业与思考题解答
说明:习题中P119:27 表示《C/C++上机实践及习题选解》中第119页的27题,其它以此类推,书后有解答。非《C/C++上机实践及习题选解》中的习题提供习题参考答案。
习题解答
作业一
1.合法的标识符: a12 A21 sum2 _fun C_program
不合法的标识符:f(x) $a21 21a while tan@sohucom
2
main( )
{ int i, j, s;
i=2; j=3;
s=ij;
printf(“ %d %d=%d”,i,j,s)
}
作业二
1指出下列哪些为C语言正确的整型(int)常量、实型(float)常量、字符型(char)常量?
正确的整型(int)常量:0 -325 128 03456
正确的实型(float)常量: 00 2 12e-3 5e2
正确的字符型(char)常量: ‘b’ ‘104’ ‘108’ ‘ ’ ‘8’
2表达式的结果 (1) 1
(2) 0
(4) 0
(5) 95
(9) ‘A’
(11) 2
(12) 45
3 Y=sin(2x)+(a+b)/(ab)/(1+a/b)+aa/2
4.设m为整数,用逻辑表达式表示:能同时被3与7整除或者被5除余2且小于100的整数。
m%3= =0 && m%7= =0 || m%5= =2 && m<100
作业三
1.华氏温度通过公式C=5/9(f-32)转换成摄氏温度。试编程并上机调试运行:从键盘上输入华氏温度,利用公式计算后输出摄氏温度(要求:保留一位小数;记录调试过程中遇到的问题及解决方法)。
#include
main( )
{ float f,c;
scanf(“%f”,&f);
c=50/9(f-32);
printf(“c=%1f”,c);
}
2
用IF语句:
#include
#include
main( )
{ float x,y;
scanf(“%f”,&x);
if (x<0) y=fabs(x);
else if (x<10) y=x;
else if (x<20) y=3-x/5;
else if (x<40) y=3x-10;
else y=0;
printf(“y=%f’,y);
}
用switch 语句:
#include
#include
main( )
{ float x,y;
scanf(“%f”,&x);
if (x<0) y=fabs(x);
else switch ( int(x/10) )
{ case 0: y=x; break;
case 1: y=3-x/5;break;
case 2,3: y=3x-10; break;
defaut y=0;
}
printf(“y=%f’,y);
}
3 解答见P151;
4.程序如下:
#include
#include
main()
{
int i=0;
char ch;
ch=getchar();
while (ch!=' ')
{ if (ch=='(') i++;
if (ch==')') i--;
if (i<0) break;
ch=getchar();
}
if (i==0) printf("(与)匹配") ;
else if (i<0) printf(" 缺) ") ;
else printf("缺少)");
}
5 程序如下:
#include
main()
{
int i,j,k,m=0;
for (i=1;i<10;i++)
for (j=1;j<20;j++)
{ k=100-i10-j5;
if (k>0) { printf(" i=%-3dj=%-3dk=%-3d",i,j,k);
m++;
if (m%4==0) putchar(' ');
}
}
printf(" m=%d ",m);
}
6 在横线上填写合适语句,完善程序,使之完成如下功能:从键盘上输入一个不多于7位的正整数,统计其中2的个数,并求各位数字中的最小数。
#include
main( )
{int count,min,temp;
long n;
min=9; count=0;
scanf( “%ld” ,&n);
do
{ temp= n % 10 ;
if (temp= =2) count++;
if (min>temp) min=temp;
n=n/10;
}while ( n );
printf(“count=%d,min=%d”,count,min)
}
作业四
1.p128: 46 解答见p156
2 p128: 411 解答见p158
3编写一递归函数计算mn ,并从键盘输入m,n的值(如4,3),求mn 的值(上机调试)。
funm_n(int m,int n)
{
int y;
if (n==1) y=m;
else y=mfunm_n(m,--n);
return y;
}
main()
{int m,n,y;
printf("Input integer(m,n):");
scanf("%d,%d",&m,&n);
y=funm_n(m,n);
printf(" %d^%-d=%-d",m,n,y);
}
4P128: 412 (3); 答案:12345678
5.求3-100中的素数,要求编写一函数来判断某数是否为素数。
main()
{
int prime,i,n=0;
for (i=3;i<=100;i++)
{ prime=prime_num(i);
if (prime) { if (n%10==0) printf(" ");printf("%4d",prime);n++;}
}
}
prime_num(int m)
{ int k=2,r;
do {
r=m % k;
if (r==0) break;
k++;
}while(k<m); p=""> </m);>
if (r!=0) return m;
else return 0;
}
6 结果如下:
main0:x=12,y=2
fun: x=6,y=25
main:x=12,y=2
fun:x=7,y=26
main:x=12,y=2
作业五
1从键盘上输入一串英文字符(不含空格与其他字符),统计每个字符的数目,并输出字母及相应的数目。
#include
#include
main()
{
int i=0,c[52]={0},m=0;
char str[80];
printf("Input a string:");
scanf("%s",str);
while (str[i])
{ if (str[i]>='A' && str[i] <='Z') c[str[i]-'A']++;
if (str[i]>='a' && str[i] <='z') c[str[i]-'a'+26]++;
i++;
}
for (i=0;i<26;i++)
if (c[i]){ if (m%8==0) putchar(' ');m++;
printf("%c:%-d ",i+'A',c[i]);
}
for (i=0;i<26;i++)
if (c[i+26]){if (m%8==0) putchar(' ');m++;
printf("%c:%-d ",i+'a',c[i+26]);
}
}
2 P132: 515 程序见p167
3.从键盘上输入8个数,用选择法按由大到小的顺序排列并输出,要求用指针实现。
#include
#include
main()
{
int data[8],i,p1,p2,temp;
p1=data;
printf("Input 8 numbers:");
for (i=0;i<8;i++)
{ scanf("%d",p1);/ 或 &(p1) / ;
p1++;
}
for (p1=data;p1<data+8-1;p1++) p=""> </data+8-1;p1++)>
for (p2=p1+1;p2<data+8;p2++) p=""> </data+8;p2++)>
if (p1<p2){ temp=p1; p1=p2; p2=temp;}
for (i=0,p1=data;i<8;i++,p1++)
printf("%6d",p1 ) ;
}
4.从键盘上输入一个4 4整数矩阵,以主对角线()为对称轴,将左下角元素中较大元素代替右上角对应元素,并将右上角元素(含对角线元素)输出。
#include
#include
main()
{
int d[4][4],i,j,temp;
printf("Input 16 numbers:");
for (i=0;i<4;i++)
for (j=0;j<4;j++)
scanf("%d",&d[i][j]) ;
for (i=0;i<4;i++)
for (j=0;j<i;j++) p=""> </i;j++)>
if (d[i][j]>d[j][i]) d[j][i]=d[i][j];
for (i=0;i<4;i++)
{ printf(" ");
for (j=0;j<4;j++)
if (j>=i) printf("%6d",d[i][j] ) ;
else printf("%6c",' ') ;
}
}
5.盘上输入两个字符串a,b,下面程序将a,b的对应字符中的较大者存放在数组c的对应位置上,填空完善之。
#include
#include
main( )
{int k=0;
char a[80],b[80],c[80]={‘’},p,q;
p=a; q=b;
gets(a); gets(b);
while (p!=0 || q!=0 )
{ if ( p!=0 ) c[k]=p;
else c[k]=q;
p++; q++; k++;
if ( p !=0 ) strcat(c,p);
else strcat(c,q);
puts(c);
}
作业六
1.P134:65 程序见p178
2.P135:67 程序见p181
3.P134:618 程序见p196
1
long
x=0,t=1;int
i;
for(i=1;i<=20;i++)t=ti,x=x+t;
2
int
s=0,i;
for(i=1;s<1000;i++)s=s+i10+3;
i=i-1;
3
int
f1=1,f2=1,f,i;
printf("%d
%d",f1,f2);
for(i=3;i<=30;i++)
{f=f1+f2;
printf("%d",f);
if(i%5==0)printf("\n");
f1=f2;f2=f;
}
4
char
s[80];int
a=0,b=0,c=0,d=0,i=0;
gets(s);
while(s[i]!=0)
{if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')a++;
//其余省略了
i++;
}
printf("%d%d%d%d",a,b,c,d);
5
int
x,a,b,c,t,i,s;
for(x=100;x<=999;x++)
{s=0;
a=x/100;b=x/10%10;c=x%10;
t=1;for(i=1;i<=a;i++)t=ti,s=s+t;
t=1;for(i=1;i<=b;i++)t=ti,s=s+t;
t=1;for(i=1;i<=c;i++)t=ti,s=s+t;
if(x==s)printf("%d",x);
}
太多了,有的地方省略了,你还是自己填全吧
以上就是关于《C程序设计语言习题解答》pdf下载在线阅读,求百度网盘云资源全部的内容,包括:《C程序设计语言习题解答》pdf下载在线阅读,求百度网盘云资源、求c语言程序设计第三版和c语言程序设计实验与习题指导答案、C语言程序设计题 求高手相助 在线等 回答全追加分等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)