當前位置:首頁 » 編程語言 » 翁惠玉c語言課後習題
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

翁惠玉c語言課後習題

發布時間: 2022-11-17 06:34:53

A. c語言習題求解 已知指針p的指向如圖所示,則執行*p++;後*p的值是

*p++,首先要知道*和++的優先順序相同,自右向左結合。
則相當於*(p++),但是p++的含義是先用後加。所以先用,就是*p這個操作,後加也就是執行p++。
所以*p++就等價於:{*p;p++}
所以當前*p的值就是20,然後p++(該p是指針不是*p所說的指針指向的值)指針後移指向a[3]位置。
此時,*p為30.

B. C語言程序設計教程答案~有追加懸賞100分!

1 【C語言】《C語言程序設計教程(第二版)》習題答案

說 明
1. 本文所指的《C語言程序設計教程(第二版)》是李鳳霞主編、北京理
工大學出版社出版的,綠皮。

2 第1章 程序設計基礎知識
一、單項選擇題(第23頁)
1-4.CBBC 5-8.DACA

二、填空題(第24頁)
1.判斷條件 2.面向過程編程 3.結構化 4.程序 5.面向對象的程序設計語言 7.有窮性 8.直到型循環 9.演算法 10.可讀性 11.模塊化 12.對問題的分析和模塊的劃分

三、應用題(第24頁)
2.源程序:
main()
{int i,j,k; /* i:公雞數,j:母雞數,k:小雞數的1/3 */
printf("cock hen chick\n");
for(i=1;i<=20;i++)
for(j=1;j<=33;j++)
for(k=1;k<=33;k++)
if (i+j+k*3==100&&i*5+j*3+k==100)
printf(" %d %d %d\n",i,j,k*3);}
執行結果:
cock hen chick
4 18 78
8 11 81
12 4 84
3.現計算斐波那契數列的前20項。
遞推法 源程序:
main()
{long a,b;int i;
a=b=1;
for(i=1;i<=10;i++) /*要計算前30項,把10改為15。*/
{printf("%8ld%8ld",a,b);
a=a+b;b=b+a;}}
遞歸法 源程序:
main()
{int i;
for(i=0;i<=19;i++)
printf("%8d",fib(i));}
fib(int i)
{return(i<=1?1:fib(i-1)+fib(i-2));}
執行結果:
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
4.源程序:
#include "math.h";
main()
{double x,x0,deltax;
x=1.5;
do {x0=pow(x+1,1./3);
deltax=fabs(x0-x);
x=x0;
}while(deltax>1e-12);
printf("%.10f\n",x);}
執行結果:
1.3247179572
5.源程序略。(分子、分母均構成斐波那契數列)
結果是32.66026079864
6.源程序:
main()
{int a,b,c,m;
printf("Please input a,b and c:");
scanf("%d %d %d",&a,&b,&c);
if(a<b){m=a;a=b;b=m;}
if(a<c){m=a;a=c;c=m;}
if(b<c){m=b;b=c;c=m;}
printf("%d %d %d\n",a,b,c);}
執行結果:
Please input a,b and c:123 456 789
789 456 123
7.源程序:
main()
{int a;
scanf("%d",&a);
printf(a%21==0?"Yes":"No");}
執行結果:
42
Yes

3 第2章 C語言概述
一、單項選擇題(第34頁)
1-4.BDCB 5-8.AABC

二、填空題(第35頁)
1.主 2.C編譯系統 3.函數 函數 4.輸入輸出 5.頭 6. .OBJ 7.庫函數 8.文本

三、應用題(第36頁)
5.sizeof是關鍵字,stru、_aoto、file、m_i_n、hello、ABC、SIN90、x1234、until、cos2x、s_3是標識符。
8.源程序:
main()
{int a,b,c;
scanf("%d %d",&a,&b);
c=a;a=b;b=c;
printf("%d %d",a,b);}
執行結果:
12 34
34 12

4 第3章 數據類型與運算規則
一、單項選擇題(第75頁)
1-5.DBACC 6-10.DBDBC 11-15.ADCCC 16-20.CBCCD 21-25.ADDBC 26-27.AB

二、填空題(第77頁)
1.補碼 2.±(10^-308~10^308) 3.int(整數) 4.單目 自右相左 5.函數調用 6.a或b 7.1 8.65,89

三、應用題(第78頁)
1.10 9
2.執行結果:
11
0
0
12
1

5 第4章 順序結構程序設計
一、單項選擇題(第90頁)
1-5.DCDAD 6-10.BACBB

二、填空題(第91頁)
1.一 ;2. 5.169000 3.(1)-2002500 (2)I=-200,j=2500 (3)i=-200
j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.3 8.scanf("%lf%lf%lf",&a,&b,&c); 9. 13 13.000000,13.000000 10.a=a^c;c=c^a;a=a^c;(這種演算法不破壞b的值,也不用定義中間變數。)

三、編程題(第92頁)
1.仿照教材第27頁例2-1。
2.源程序:
main()
{int h,m;
scanf("%d:%d",&h,&m);
printf("%d\n",h*60+m);}
執行結果:
9:23
563
3.源程序:
main()
{int a[]={-10,0,15,34},i;
for(i=0;i<=3;i++)
printf("%d\370C=%g\370F\t",a[i],a[i]*1.8+32);}
執行結果:
-10℃=14°F 0℃=32°F 15℃=59°F 34℃=93.2°F
4.源程序:
main()
{double pi=3.14159265358979,r=5;
printf("r=%lg A=%.10lf S=%.10lf\n",r,2*pi*r,pi*pi*r);}
執行結果:
r=5 A=31.4159265359 S=49.3480220054
5.源程序:
#include "math.h";
main()
{double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
if (a+b>c&&a+c>b&&b+c>a)
{double s=(a+b+c)/2;
printf("SS=%.10lf\n",sqrt(s*(s-a)*(s-b)*(s-c)));}
else printf("Data error!");}
執行結果:
4 5 6
SS=9.9215674165
6.源程序:
main()
{int a=3,b=4,c=5;float d=1.2,e=2.23,f=-43.56;
printf("a=%3d,b=%-4d,c=**%d\nd=%g\ne=%6.2f\nf=%-10.4f**\n",a,b,c,d,e,f);}
7.源程序:
main()
{int a,b,c,m;
scanf("%d %d %d",&a,&b,&c);
m=a;a=b;b=c;c=m;
printf("%d %d %d\n",a,b,c);}
執行結果:
5 6 7
6 7 5
8.源程序:
main()
{int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("average of %d,%d and %d is %.2f\n",a,b,c,(a+b+c)/3.);
執行結果:
6 7 9
average of 6,7 and 9 is 7.33
9.不能。修改後的源程序如下:
main()
{int a,b,c,x,y;
scanf("%d %d %d",&a,&b,&c);
x=a*b;y=x*c;
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("x=%d,y=%d\n",x,y);}

6 第5章 選擇結構程序設計
一、單項選擇題(第113頁)
1-4.DCBB 5-8.DABD

二、填空題(第115頁)
1.非0 0 2.k==0
3.if (abs(x)>4) printf("%d",x);else printf("error!");
4.if((x>=1&&x<=10||x>=200&&x<=210)&&x&1)printf("%d",x);
5.k=1 (原題最後一行漏了個d,如果認為原題正確,則輸出k=%。)
6. 8! Right!11 7.$$$a=0 8.a=2,b=1

三、編程題(第116頁)
1.有錯。正確的程序如下:
main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
printf("min=%d\n",a>b?b>c?c:b:a>c?c:a);}
2.源程序:
main()
{unsigned long a;
scanf("%ld",&a);
for(;a;printf("%d",a%10),a/=10);}
執行結果:
12345
54321
3.(1)源程序:
main()
{int x,y;
scanf("%d",&x);
if (x>-5&&x<0)y=x;
if (x>=0&&x<5)y=x-1;
if (x>=5&&x<10)y=x+1;
printf("%d\n",y);}
(2)源程序:
main()
{int x,y;
scanf("%d",&x);
if(x<10) if(x>-5) if(x>=0) if(x>=5)y=x+1;
else y=x-1; else y=x;
printf("%d\n",y);}
(3)源程序:
main()
{int x,y;
scanf("%d",&x);
if(x<10) if(x>=5)y=x+1;
else if(x>=0)y=x-1;
else if(x>-5)y=x;
printf("%d\n",y);}
(4)源程序:
main()
{int x,y;
scanf("%d",&x);
switch(x/5)
{case -1:if(x!=-5)y=x;break;
case 0:y=x-1;break;
case 1:y=x+1;}
printf("%d\n",y);}
4.本題為了避免考慮每月的天數及閏年等問題,故採用面向對象的程序設計。
現給出Delphi源程序和C++ Builder源程序。
Delphi源程序:
procere TForm1.Button1Click(Sender: TObject);
begin
edit3.Text:=format('%.0f天',[strtodate(edit2.text) -strtodate(edit1.text)]);
end;
procere TForm1.FormCreate(Sender: TObject);
begin
Edit2.Text:=datetostr(now);
button1click(form1)
end;
C++ Builder源程序:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit3->Text=IntToStr(StrToDate(Edit2->Text)-StrToDate(Edit1->Text))+"天";
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit2->Text=DateToStr(Now());
Button1Click(Form1);
}
執行結果:(運行於Windows下) http://img378.photo.163.com/nxgt/41463572/1219713927.jpg

5.源程序:
main()
{unsigned a,b,c;
printf("請輸入三個整數:");
scanf("%d %d %d",&a,&b,&c);
if(a&&b&&c&&a==b&&a==c)printf("構成等邊三角形\n");
else if(a+b>c&&a+c>b&&b+c>a)
if(a==b||a==c||b==c)printf("構成等腰三角形\n");
else printf("構成一般三角形\n");
else printf("不能構成三角形\n");}
執行結果:
請輸入三個整數:5 6 5
構成等腰三角形
6.源程序:
main()
{int x,y;
scanf("%d",&x);
if(x<20)y=1;
else switch(x/60)
{case 0:y=x/10;break;
default:y=6;}
printf("x=%d,y=%d\n",x,y);}
7.源程序:
main()
{unsigned m;float n;
scanf("%d",&m);
if(m<100)n=0;
else if(m>600)n=0.06;
else n=(m/100+0.5)/100;
printf("%d %.2f %.2f\n",m,m*(1-n),m*n);}
執行結果:
450
450 429.75 20.25
8. 2171天(起始日期和終止日期均算在內)
本題可利用第4小題編好的程序進行計算。把起始日期和終止日期分別打入「生日」和「今日」欄內,單擊「實足年齡」按鈕,將所得到的天數再加上1天即可。
9.源程序:
#include "math.h";
main()
{unsigned long i;
scanf("%ld",&i);
printf("%ld %d\n",i%10,(int)log10(i)+1);}
執行結果:
99887
7 5
10.源程序:
main()
{unsigned long i;unsigned j[10],m=0;
scanf("%ld",&i);
for(;i;){j[m++]=(i+2)%10;i/=10;}
for(;m;m--)i=i*10+j[m-1];
printf("%ld\n",i);}
執行結果:
6987
8109
(註:要加密的數值不能是0或以0開頭。如果要以0開頭需用字元串而不能是整數。)

7 第6章 循環結構程序設計
一、單項選擇題(第142頁)
1-4.BCCB 5-8.CBCA

二、填空題(第143頁)
1.原題可能有誤。如無誤,是死循環 2.原題有誤。如果把b=1後面的逗號改為分號,則結果是8。 3.20 4.11 5. 2.400000 6.*#*#*#$ 7.8 5 2 8.①d=1.0 ②++k ③k<=n 9.①x>=0 ②x<amin

三、編程題(第145頁)
1. 源程序:
main()
{int i=1,sum=i;
while(i<101){sum+=i=-i-2;sum+=i=-i+2;}
printf("%d\n",sum);}
執行結果:
51
2.源程序:
main()
{double p=0,n=0,f;int i;
for(i=1;i<=10;i++)
{scanf("%lf",&f);
if (f>0)p+=f; else n+=f;}
printf("%lf %lf %lf\n",p,n,p+n);}
3.源程序:
main()
{unsigned a;
scanf("%ld",&a);
for (;a;printf("%d,",a%10),a/=10);
printf("\b \n");}
執行結果:
23456
6,5,4,3,2
4.源程序:
main()
{unsigned long a,b,c,i;
scanf("%ld%ld",&a,&b);
c=a%1000;
for(i=1;i<b;i++)c=c*a%1000;
if(c<100)printf("0");
if(c<10)printf("0");
printf("%ld\n",c);}
執行結果:
129 57
009
5.略
6.原題提供的計算e的公式有誤(前面漏了一項1)。正確的公式是e= 1 + 1 + 1/2! + 1/3! + … + 1/n! + …
(1)源程序:
main()
{double e=1,f=1;int n;
for(n=1;n<=20;n++){f/=n;e+=f;}
printf("e=%.14lf\n",e);}
執行結果:
e=2.71828182845905
(2)源程序:
main()
{double e=1,f=1;int n;
for(n=1;f>1e-4;n++){f/=n;e+=f;}
printf("e=%.4f\n",e);}
執行結果:
e=2.7183
7.源程序:
main()
{unsigned long a=0,b=1,c=0;int i,d;
scanf("%d",&d);
for (i=1;i<=(d+2)/3;i++)
printf("%10ld%10ld%10ld",a,b,(a+=b+c,b+=c+a,c+=a+b));}
本題還可以用遞歸演算法(效率很低),源程序如下:
unsigned long fun(int i)
{return i<=3?i:fun(i-1)+fun(i-2)+fun(i-3);}
main()
{int i,d; scanf("%d",&d);
for(i=1;i<=d;i++)
printf("%10ld",fun(i));}
執行結果:
15
1 2 3 6 11 20 37 68
125 230 423 778 1431 2632 4841
8.源程序:
main()
{int i;
for(i=1010;i<=9876;i+=2)
if(i/100%11&&i%100%11&&i/10%100%11&&i/1000!=i%10&&i/1000!=i/10%10&&i/100%10!=i%10)printf(" %d",i);}
執行結果:
1024 1026 1028 1032 1034 1036 …… …… 9874 9876
9.源程序:
main()
{int i,j,k;
printf("apple watermelon pear\n");
for(i=1;i<=100;i++)
for(j=1;j<=10;j++)
if((k=100-i-j)*2==400-i*4-j*40)
printf("%4d%7d%9d\n",i,j,k);}
執行結果:
apple watermelon pear
5 5 90
24 4 72
43 3 54
62 2 36
81 1 18
10.源程序:
#include "stdio.h";
#define N 4 /* N為階數,可以改為其他正整數 */
main()
{int m=N*2,i,j;
for(i=1;i<m;printf("\n"),i++)
for(j=1;j<m;
putchar(N-abs(i-N)<=abs(j++-N)?' ':'*'));}
如果把N值改為5,則執行結果如下:
*
***
*****
*******
*********
*******
*****
***
*

作者:寧西貫通 2006-5-7 23:41 回復此發言

--------------------------------------------------------------------------------

8 說明
注意:上面最後一題的輸出結果應該是由星號組成的一個菱形,

9 第7章 數 組
一、單項選擇題(第192頁)
1-4.BBCC 5-8.AABA

二、填空題(第194頁)
1.1
2
4
8
16
32
64
128
256
512
2. ①a[age]++ ②i=18;i<26
3. ①break ②i==8
4. ①a[i]>b[j] ②i<3 ③j<5
5. ①b[j]=a[j][0] ②b[j]<a[j][k] 6.a[k++]=a[j]

三、編程題(第196頁)
1.源程序:
main()
{int a[4][4],i,j,s=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i==j||i+j==3)s+=a[i][j];
printf("%d\n",s);} /* 注:5×5矩陣不能照此計算! */
執行結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
68
2. 源程序:
main()
{int i,a[36];
a[0]=2;
for(i=1;i<=29;i++)a[i]=a[i-1]+2;
for(;i<=35;i++)a[i]=a[(i-30)*5+2];
for(i=0;i<=35;i++)printf("%d\t",a[i]);}
執行結果:
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
42 44 46 48 50 52 54 56 58 60
6 16 26 36 46 56
3. 源程序:
#include "stdlib.h"
#include "time.h"
main()
{int a[30],i,m=0;
randomize();
for(i=0;i<=29;i++)
{a[i]=rand();
if(m<a[i])m=a[i];
printf("%d\t",a[i]);}
for(i=0;i<=29;i++)if(a[i]==m)a[i]=-1;
printf("\n-----------------\n");
for(i=0;i<=29;i++)
if(~a[i])printf("%d\t",a[i]);
printf("\n");}
執行結果:
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
32499 29305 22340 5927 13031 2161 2583 31855 22977 14283
4851 22038 6992 11394 20887 27381 6293 18347 16414 10210
-----------------
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
29305 22340 5927 13031 2161 2583 31855 22977 14283 4851
22038 6992 11394 20887 27381 6293 18347 16414 10210
4.源程序:
main()
{int i,n=0,b[16];
scanf("%d",&i);
for(;i;i>>=1)b[n++]=i&1;
for(;n;)printf("%d",b[--n]);}
執行結果:
9876
10011010010100
本題也可以不用數組。源程序如下:
#include "stdio.h"
main()
{int i,n;
scanf("%d",&i);
for(n=16;n;n--)
{asm ROL i,1
putchar(i&1|48);}
} /* ROL是循環左移的匯編指令 */
5. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 6
main()
{int a[M][N],i,j,t[M];
randomize();
/*生成M行N列隨機數*/
for(i=0;i<M;printf("\n"),t[i++]=0)
for(j=0;j<N;j++)
printf("%4d",a[i][j]=random(50));
/*找出每行的最小數,t[M]是第M行的最小數所在的列數*/
for(i=0;i<M;i++)
for(j=0;j<N;j++)
if(a[i][t[i]]>a[i][j])t[i]=j;
/*比較每個最小數在其所在的列上是否也是最小*/
for(j=0;j<M;j++)
for(i=0;i<M;i++)
{if(i==j)continue;
if(a[j][t[j]]>a[i][t[j]])
{t[j]=-1;break;}
}
printf("-------------------\n");
/*輸出在行和列上均為最小的數*/
for(i=0;i<M;i++)
if(t[i]!=-1)
printf("a[%d,%d]=%d\n",i,t[i],a[i][t[i]]);
}
執行結果:
13 19 13 20 0 1
20 41 6 16 35 30
3 5 37 8 23 15
6 36 24 29 18 1
1 5 28 21 46 34
-------------------
a[0,4]=0
a[1,2]=6
a[3,5]=1
a[4,0]=1
6. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 7
main()
{int a[M][N],i,j,t=0;
randomize();
for(i=0;i<M;i++)
{a[i][N-1]=0;
for(j=0;j<N-1;j++)
{printf("%4d",a[i][j]=random(91)+10);
a[i][N-1]+=a[i][j];}
printf("%4d\n",a[i][N-1]);}
for(i=1;i<M;i++)
if(a[i][N-1]>a[t][N-1])t=i;
if(t)for(j=0;j<N;j++)
{i=a[0][j];a[0][j]=a[t][j];a[t][j]=i;}
printf("-----------------\n");
for(i=0;i<M;printf("\n"),i++)

10 第7章 數 組
for(j=0;j<N;j++)
printf("%4d",a[i][j]);
}
執行結果:
89 17 32 95 35 20 288
39 48 22 27 73 22 231
51 87 39 71 84 46 378
84 94 97 77 27 26 405
69 50 56 89 37 46 347
-----------------
84 94 97 77 27 26 405
39 48 22 27 73 22 231
51 87 39 71 84 46 378
89 17 32 95 35 20 288
69 50 56 89 37 46 347
7. 源程序:
#include "stdlib.h"
#include "time.h"
#define M 5
#define N 6
main()
{int a[M][N],i,j;
struct data{int value,x,y;}max,min;
max.value=0;min.value=100;
randomize();
for(i=0;i<M;printf("\n"),i++)
for(j=0;j<N;j++)
{printf("%4d",a[i][j]=random(100)+1);
if(max.value<a[i][j])
{max.value=a[i][j];max.x=i;max.y=j;}
if(min.value>a[i][j])
{min.value=a[i][j];min.x=i;min.y=j;}
}
printf("-----------------\n");
i=a[0][N-1];a[0][N-1]=max.value;a[max.x][max.y]=i;
i=a[M-1][0];a[M-1][0]=min.value;a[min.x][min.y]=i;
for(i=0;i<M;printf("\n"),i++)
for(j=0;j<N;j++)
printf("%4d",a[i][j]);
}
執行結果:
51 53 74 65 30 40
30 26 50 6 61 27
47 16 54 58 76 19
57 74 44 92 71 48
73 57 60 32 73 67
-----------------
51 53 74 65 30 92
30 26 50 73 61 27
47 16 54 58 76 19
57 74 44 40 71 48
6 57 60 32 73 67
9. 源程序:
main()
{char s[255];int i,j,b=1;
printf("Input a string:");
scanf("%s",s);
i=strlen(s);
for(j=1;j<=i/2;j++)
b=b&&(s[j-1]==s[i-j]);
printf(b?"Yes\n":"No\n");}
執行結果:
Input a string:level
Yes
10. 源程序:
main()
{char s[255],t,max=0,min=0,l,i;
printf("Input a string(length>4):");
gets(s);
l=strlen(s);
for(i=0;i<l;i++)
{if(s[max]<s[i])max=i;if(s[min]>s[i])min=i;}
t=s[1];s[1]=s[max];s[max]=t;if(min==1)min=max;
t=s[l-2];s[l-2]=s[min];s[min]=t;
printf("%s\n",s);}
執行結果:
Input a string(length>4):C++Builder
Cu+Beild+r
11. 源程序:
main()
{char m[13][10]={"****","January","February","March",
"April","May","June","July","August","September",
"October","November","December"};
int i,j,k,a,s,n;
printf("Please input an integer(100..999):");
scanf("%d",&n);
printf("%d:%d+%d+%d=%d, %d%%13=%d, %s\n", n,i,j,k,s,s,a,m[a=((s=(i=n/100)+(j=n/10%10)+(k=n%10))%13)]);}
執行結果:
Please input an integer(100..999):539
539:5+3+9=17, 17%13=4, April

11 第8章 函 數
一、單項選擇題(第241頁)
1-5.BCCAA 6-10.CCDDD 11-15.ACACB

二、填空題(第243頁)
1.看不出原題的意圖。因為要計算1~n的累加和,n應是一個≥1的正整數。可是題目中卻出現了n=0的情況。除非另加規定當n=0時1~n的累加和為0,或者把原題中的計算式改為計算0~n的累加和。據此猜測,原題應填為:①return(0) ②return(n+sum(n-1))
根據題意,如下程序較為合理:
int sum(int n)
{if(n<=0)return(-1); /* -1是出錯標志 */
else if(n==1)return(1);
else return(n+sum(n-1));}
2. ①return(1) ②return(n*facto(n-1))

三、編程題(第244頁)
3.源程序:
main()
{int i,a,b,c;
for(i=100;i<999;i++)
if((a=i/100)*a*a+(b=i/10%10)*b*b+(c=i%10)*c*c==i)
printf("%d\t",i);}
執行結果:
153 370 371 407
8.源程序(非遞歸演算法):
#define P 13 /* P可以改為其他正整數 */
main()
{int a[P],r,c;
for(r=0;r<=P;r++)
{a[r]=1;
for(c=r-1;c>=1;a[c--]+=a[c-1]);
printf("%*d",(P-r)*3+1,a[0]);
for(c=1;c<=r;printf("%6d",a[c++]));
printf("\n");}
}
執行結果:
(應該排列成一個三角形,是貼吧造成現在這個樣子的,不是程序有問題)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
9.源程序(遞歸演算法):
#include "stdio.h"
void printOCT(unsigned long n)
{unsigned long i;
if(i=n>>3)printOCT(i);
putchar((n&7)+48);}
main()
{unsigned long i;
scanf("%ld",&i);
printOCT(i);}
執行結果:
1234567890
11145401322
本題也可以不用遞歸演算法,源程序請參考第7章第三題4。

12 回復:【C語言】《C語言程序設計教程(第二版)》習題答案
但是不同時間印刷的版本課後題不太一樣呢,象我們的是1999年12月第2版,2005年12月第69次印刷的。沒有選擇填空,應用題和樓主不知道有多少相同的,因為看不到原題。這個比較麻煩呢。

作者:210.77.204.* 2006-5-9 18:38 回復此發言

--------------------------------------------------------------------------------

13 回復:【C語言】《C語言程序設計教程(第二版)》習題答案
你對照一下主編和出版社,看看對嗎?(見說明的第一條。)
我不是說叫你有問題另發帖子嗎?

14 第9章 指 針
一、單項選擇題(第276頁)
1-5.DCDAC 6-10.CCABC 11-16.AABBB 16-20.DCDBD

二、填空題(第278頁)
1.①int * ②*z
2.*p++
3.①'\0' ②++
4.①q=p+1 ②q<p+10 ③*q>max ④*q<min

三、編程題(第280頁)
7.源程序:
main()
{int i=0;char c[20];
do{scanf("%s",&c);i++;}
while(strcmp(c,"stop"));
printf("%d\n",i);}
執行結果:
This car ran form Nanyang
to Luoyang without a stop
10
9.源程序:
main()
{char s[255],c[255]={0};int i;
gets(s);
for(i=0;s[i];c[s[i++]]++);
for(i=0;i<255;i++)
if(c[i])printf("%c=%d\t",i,c[i]);}
執行結果:
abcedabcdcd
a=2 b=2 c=3 d=3 e=1

C. c語言程序設計課後習題解答與實驗指導

#include<stdio.h>
int max(int x,int y,int z)
{
int t;
t=x>y?x:y;
t=t>z?t:z;
return(t);
}
int min(int x ,int y,int z)
{
int t;
t=x<y?x:y;
t=t<z?t:z;
return(t);
}
int aver(int x ,int y,int z)
{
int t;
t=(x+y+z)/3;
return(t);
}
void main()
{ int x,y,z,a,b,c;
scanf("%d,%d,%d",&x,&y,&z);
a=(*max)(x,y,z);
b=(*min)(x,y,z);
c=(*aver)(x,y,z);
printf("%d,%d,%d\n",a,b,c);
}
建議改成:
#include<stdio.h>
int max(int x,int y,int z)
{
int t;
t=x>y?x:y;
t=t>z?t:z;
return(t);
}
int min(int x ,int y,int z)
{
int t;
t=x<y?x:y;
t=t<z?t:z;
return(t);
}
float aver(int x ,int y,int z)
{
int t;
t=(float)((x+y+z)/3.0);
return(t);
}
void main()
{ int x,y,z,a,b;float c;
scanf("%d,%d,%d",&x,&y,&z);
a=(*max)(x,y,z);
b=(*min)(x,y,z);
c=(*aver)(x,y,z);
printf("max=%d\nmin=%d\naver=%g\n",a,b,c);
}

D. 《C語言程序設計》練習題

一、選擇題
1、C語言規定,在一個源程序中,main()函數的位置(C )。
A) 必須在最開始 B)必須在最後
C) 可以任意 D) 必須在系統調用的庫函數後面

這題你死記就行,你看看這個
任何一個C++程序都必須定義一個main函數,它的返回類型總是int類型。這個函數由操作系統來調用,在main函

數執行完以後,程序也就終止了。 main也可以使用return向操作系統返回一個值,使用操作系統的命令可以檢測

main的返回值。一般約定在main返回0時,表示程序運行過程中沒有出現錯誤,其它非零值表示程序出現異常情況。
若沒有為main指定返回值,這樣返回值是任意的。

2、若x和y都是int 型變數,x=100,y=200,且有以下程序段,printf(「%d」,(x,y));則其輸出結果是( A)。
A) 200 B) 100 C) 100 200 D) 輸出不確定值

(x,y)這是個逗號表達式,表達式的值為最後一個逗號後面表達式的值,因為前面有一個%d,所以輸出 的是y的



3、合法的C語法字元常量是(C )。
A) 「A」 B) 65 C) 『\t』 D) A

字元都是由兩個單引號括起來的,死記, a)為字元串

4、表達式「3!=4」的值是(A )。
A) 1 B) 0 C) T D) F

!=是c語言的邏輯運算符,3!=4為真,多以得1, 3=4為假,值為0

5、 int a=1,b=2,c=3,d=4,執行下列程序後x的值為( C)。
if(a>b) x=1;else if (c>d) x=2;else x=3;
A) 1 B) 2 C) 3 D) 4

如果a>b,1>2為假,不執行x=1,執行else if(c>d),判斷:3>4為假,不執行x=2,執行=3,完

6、以下程序的輸出結果是(D)。
Int x=10,y=3,z;printf(「%d\n」,z=(x%y,x/y));
A)1 B) 0 C) 4 D) 3

逗號表達式,x%y,10%3,商3餘1,模取余數1,x/y,10/3,商3餘1,除法取商3,逗號表達式,表達式的值為最後一

個逗號後面表達式的值

7、為表示x≥y≥z,應使用C語言表達式(A )。
A) (x>=y)&&(y>=z) B) (x>=y) AND (y>=z)
C) (x>=y>=z) D) (x>=y) & (y>=z)

關系運算符,x>=y與y>=z必須是同時成立的,因此要用邏輯運算符&&,&&與的意思

8、合法的數組定義是( a )。
A) int a[ ]=」string」; B) int a[5]={0,1,2,3,4,5};
C) char a=」string」; D) char a[ ]={「01234」};

a不指定數組長度定義,正確
b數組長度為5,就是裡面有5個數組元素,多了一個數組元素
c定義a不是數組而是一個變數,素組應該是a[]
d是錯的,字元串是這樣定義的:char a[ ]={'0','1','2','3','4','\0'};
[4]中數字是數組長度,說明裡面有幾個元素

9、設x和y均為int型變數,則語句「x+=y;y=x-y;x-=y;」的功能是(D)。
A) 把x和y按從小到大排列 B)把x和y按從大到小排列
C)無確定結果 D)交換x和y中的值

x+=y等同於x=x+y,把x加y的值賦給x,x現在存的是x與y的和
y=x-y把x減y的值賦給y,x現在存的是x與y的和 (x+y)-y=x,把x的值給y,y存x的值
x-=y等同於x=x-y,把x減y的值賦給x,(x+y)-y=x,現在的後面的y存的是x的值,做完後x里存的值是y,完成交換

10、若已定義x和y為double類型,則表達式「x=1,y=x+3/2」的值是(C)。
A) 1 B)2 C) 2.0 D) 1.0

逗號表達式,先讓x=1在算y=x+3/2,得2,因為是double類型,所以有小數點

11、下列條件語句中,功能與其他語句不同的是 (D)
A) if(a) printf("%d\n",x); else printf("%d\n",y);
B) if(a==0) printf("%d\n",y); else printf("%d\n",x);
C) if (a!=0) printf("%d\n",x); else printf("%d\n",y);
D) if(a==0) printf("%d\n",x); else printf("%d\n",y);

a 如果a為真輸出x,如果a為假輸出y
b 如果a==0,也就是為假,輸出y,a為真輸出x,同a
c 如果a!=0為真,輸出x,a為假輸出y同上
d 如果a為假輸出x,如果a為真輸出y,與以上的都不同

12、以下能正確定義二維數組的是 (C)
A) int a[][3]; B) int a[][3]=2{2*3};
C) int a[][3]={0}; D) int a[2][3]={{1},{2},{3,4}};

a 沒有給數組賦初值
b {}里不能有*
c 正確,記住第一個[]中可以為空,第二個[]不能為空,死記
d a[2][3] 的意思是有兩個一維數組,每個一維數組里有3個元素,{{1},{2},{3,4}}; 她的意思是三個數組,{}中的每一個{}代表一個數組,里邊的數為數組元素

13、用C語言編寫的代碼程序 (B)
A)可立即執行 B)是一個源程序
C)經過編譯即可執行 D)經過編譯解釋才能執行

死記

E. 求c語言程序設計第三版和c語言程序設計實驗與習題指導答案

c語言程序設計第三版指導答案

附錄F課後題參考答案
習題1
1.1填空題
1.函數
2.主函數main();主函數main()3.主函數main()4.函數首部;函數體5.{;}
6.順序結構;選擇結構;循環結構7..c;.obj;.exe
1.2思考題
1.答:結構化程序設計是指,為使程序具有一個合理的結構以保證程序正確性而規定的一套如何進行程序設計的原則。順序結構,選擇結構,循環結構
2.答:演算法是對具體問題求解步驟的一種描述。計算機演算法的表達工具通常採用以下幾種方法:①用自然語言表示演算法;②用流程圖表示演算法;③用偽代碼表示演算法;④用程序設計語言表示演算法。
3.略4.略5.略1.3編程題1.答:
#include"stdio.h"main()
{floata=10,b=20,h=5,s;s=(a+b)*h/2;
printf("s=%f",s);}
2.答:
#include"stdio.h"
main()
{printf("******************************");printf("*helloworld*");printf("******************************");}
習題2
2.1單選題
DBDCADCABBCA
2.2填空題
1.2.000000

2.1;0.500000
3.9;24.65.100;d6.(1)20
(2)0

(3)607.(1)10;6;4
(2)6;9;15
(3)3;60;83
8.55或'7'
9.x=4;y=6;z=3;m=463
2.3改錯題(略)
習題3
3.1單選題
BDABCADCACBBA
3.2填空題
1.32.2613.10
4.2,1;互換a,b的值5.6.66.0037.7
8.5.0,4,c=3<Enter>
9.i=10,j=20<Enter>
10.(1)65(2)65,A(3)3.14,123.46
(4)3.141600e+000,1.234560e+002(5)8765.432100(6)8.765432e+003
11.a=2b=5x=8.8y=76.34c1=65c2=9712.%d/%d;%d/%d=%.2f
3.3改錯題(略)3.4編程題
1.答:
#include"stdio.h"main(){
intx,y;
scanf("%d%d",&x,&y);printf(" x y ");
printf("十進制數 %d %d ",x,y);printf("八進制數 %o %o ",x,y);printf("十六進制數 %X %x ",x,y);}
2.答:
#include"stdio.h"main(){
charch;
printf("請輸入一個大寫英文字母");scanf("%c",&ch);
printf("大寫英文字母是%c ",ch);printf("它的前導字元是%c ",ch-1);printf("它的後續字元是%c ",ch+1);}
3.答:
#include"stdio.h"main(){
intx,a,b,c,y;
printf("請輸入一個三位整數 ");scanf("%d",&x);a=x/100;
b=(x-a*100)/10;c=x%10;
y=c*100+b*10+a;
printf("反向輸出該整數:%d ",y);}}
4.答:
#include"stdio.h"main()
{inthour;
doublesalary,salaryday;
scanf("%d,%lf",&hour,&salaryday);
salary=hour*salaryday-hour*salaryday*0.1;
printf("%8.2lf ",salary);}
5.答:
#include"stdio.h"main(){
inta,b,c,t;
printf("請輸入三個整數 ");scanf("%d%d%d",&a,&b,&c);
printf("交換前a=%d,b=%d,c=%d ",a,b,c);t=a;a=c;c=b;b=t;
printf("交換後a=%d,b=%d,c=%d ",a,b,c);}
習題4
4.1單選題
BADDDACBBBBA
4.2填空題
1.1
2.(1)a>0||b>0
(2)x>0&&x<=10(3)a==1.5&&b==1.5&&c==1.5
(4)p<a||p<b||p<c
3.(1)0(2)1(3)1(4)0(5)1
4.c=15.-46.17.5,0,38.59.123
10.(cvb=='y'||cvb=='Y')&&(work>=3||college=='y'||college=='Y')&&age<=35
4.3改錯題(略)4.4編程題
1.答
#include"stdio.h"
#include"math.h"main(){
doublea,b,c,p,area;
scanf("%lf%lf%lf",&a,&b,&c);
printf("三角形的三邊為:%.llf,%.1lf,%.1lf ",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 ",area);}else
printf("不能構成三角形 ");}
2.答:
#include"stdio.h"main()
{intx,y;
scanf("%d,%d",&x,&y);if(x*x+y*y>1000)
printf("%d ",(x*x+y*y)/100);else
printf("%d ",x+y);}
3.答:
#include"stdio.h"#include"math.h"main()
{doublex,y;
scanf("%lf",&x);if(x<-2)y=x*x-sin(x);elseif(x<=2)y=pow(2,x)+x;elsey=sqrt(x*x+x+1);
printf("x=%.2lfy=%.2lf ",x,y);}
4.答:
#include"stdio.h"main()
{longge,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("thisnumberisahuiwen ");else
printf("thisnumberisnotahuiwen ");
}
5.答:
#include"stdio.h"main()
{floatp,w,s,d,f;
scanf("%f,%,%f",p,s,w);if(s>3000)d=0.15elseif(s>=2000)d=0.1;elseif(s>=1000)d=0.08;elseif(s>=500)d=0.05;elseif(s>=250)d=0.02;elsed=0f=p*w*s*(1-d);printf("%f",f);}
6.答:
#include"stdio.h"main()
{intyear,money;charx;
printf("是否是本公司產品(y/n):");scanf("%c",&x);
if(x=='y'||x=='Y')
{printf("產品使用的年限:");scanf("%d",&year);
if(year<=1)money=0;
elseif(year<8)money=50;elsemoney=100;

printf("產品保修額是:%d ",money);
}
else
{money=200;
printf("不是本公司產品,產品保修額是:%d ",money);}}
7.答:
#include"stdio.h"main()
{intmoney,num1,num2;
printf("請輸入取款額(≤2000):");scanf("%d",&money);
if(money>2000)printf("請輸入取款額數≤2000! ");elseif(money%50==0){num1=money/100;num2=(money-num1*100)/50;printf("需支付100元:%d張 ",num1);printf("需支付50元:%d張 ",num2);}elseprintf("輸入錢數必須是50的倍數! ");}
習題5
5.1單選題
CDABAABDDBDBCB
5.2填空題
1.202.333
3.(1)i<10或i<=9(2)j%3!=0
4.(1)flag*(float)k/(k+1)或1.0*flag*k/(k+1)(2)flag=-flag5.(1)max=x


(2)x!=-1(3)scanf("%d",&x)
6.(1)x<=9或x<10
(2)y=9-x
5.3改錯題(略)5.4編程題
1.答:
(1)for循環,其他略
#include"stdio.h"
main()
{inti,s=0;
for(i=1;i<=100;i++)s+=i*i;
printf("%d ",s);}
(2)for循環,其他略
#include"stdio.h"main()
{inti=1,p=1;doubles=1;do{
s+=1.0/p;p*=++i;
}while(1.0/p>1e-6);printf("%lf",s);}
2.答:
#include"stdio.h"main()
{intm,n,t,a,b;
scanf("%d,%d",&m,&n)if(m<n)
{t=mm=nn=t}a=m;b=n;t=m%nwhile(t)
{m=nn=tt=m%n;}printf("%d",n);}
3.答:
#include"stdio.h"main()
{intx,y,s=1;
scanf("%d,%d",&x,&y)for(y>0y--)s*=x
printf("%d,%d,%d ",s%10,s/10%10,s/100%10);}
4.答:
#include"stdio.h"main()
{intx,y,z;
for(x=1x<20x++)for(y=1y<33y++){z=100-x-y
if((z%3)==0&&(5*x+3*y+z/3)==100)printf("x=%d,y=%d,z=%d ",x,y,z)}}
5.答:(a)
#include"stdio.h"main()
{intj,k
for(j=1j<=4j++)
{for(k=1;k<=4-j;k++)printf("");printf("****")printf(" ")}}
(b)
#include"stdio.h"main()
{intj,k
for(j=1j<=4j++)
{for(k=1;k<=4-j;k++)printf("");for(k=1k<=2*j-1k++)printf("*")printf(" ")}}
6.答:
程序分析:利用for循環控制在100~999之間,對每個數分解出個位、十位、百位。
#include<stdio.h>main(){inti,j,k,n;printf("waterflower'numberis:");for(n=100;n<1000;n++){i=n/100;/*分解出百位*/j=n/10%10;/*分解出十位*/k=n%10;/*分解出個位*/if(n==i*i*i+j*j*j+k*k*k){printf("%-5d",n);}}printf(" ");}
7.答:
#include<stdio.h>main(){intx;for(x=1000;x>=3;x--)if(x%3==1&&x%5==2&&x%7==3){
printf("該校的學生人數是:%d人 ",x);break;}}
8.答:
#include<stdio.h>main(){intx=12,i=1;while(1)
{if((x+20+i)==2*(x+i))break;i++;}printf("小明母親在%d年後比小明的年齡大一倍 ",i);printf("那時小明年齡是:%d歲,小明母親年齡是:%d歲 ",x+i,x+20+i);}
習題6
6.1單選題
DBCCBBDC
C語言程序設計教程(第3版)
278
6.2填空題
1.c2.603.1000104.16
6.3編程題
1.答:
#include"stdio.h"#include"math.h"
#defineF(a)a*a+sqrt(3*a*a+2*a+1)main()
{floatx,f;
scanf("%f",&x);
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x)printf("%f ",f);}
習題7
7.1單選題
BCADACCCDABCBDB
7.2填空題
1.(1)2345(2)10010(3)QuickC

(4)1000001000001000001000001(5)Language
(6)LanguageProgramming2.(1)j+=2(2)a[i]>a[j]3.(1)i=1(2)x[i-1]
7.3改錯題(略)7.4編程題
1.答:
#defineN10
#include"stdio.h"main()
{inta[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];
elseosum+=a[j];
printf("osum=%d,qsum=%d ",osum,qsum);}
2.答:
#defineN10
#include"stdio.h"main()
{inta[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.答:
#defineM3
#include"stdio.h"main()
{inta[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");elseprintf("NO");}
4.答:
#include"stdio.h"#include"string.h"main()
{charc1[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 ",1);if(c1[j]<c2[j])printf("%d ",-1);if(c1[j]==c2[j])printf("%d ",0);}
5.答:
#include"stdio.h"#include"string.h"#defineM3#defineN80main()
{chara[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]++;
elseif(a[j][k]>='a'&&a[j][k]<='z')n[1]++;elseif(a[j][k]>='0'&&a[j][k]<='9')n[2]++;elseif(a[j][k]=='')n[3]++;elsen[4]++;
for(j=0;j<5;j++)printf("%4d",n[j]);}
習題8
8.1單選題
DBDACBACCC
8.2填空題
1.(1)2,1(2)10#30#(3)FOUR,P(4)60
2.(1)49
(2)2
(3)2

(4)

(5)
8.3改錯題(略)8.4編程題
1.答:
#include"stdio.h"
main()
{intn1,n2,n3,t;int*p1,*p2,*p3;
printf("pleaseinput3number: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("thesortednumbersare:%d,%d,%d ",n1,n2,n3);}
2.答:
#include"stdio.h"#defineN3main()
{inta[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"stdio.h"main()
{inta[10];
intj,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.答:
輸入陣列如下:123456789101112輸出陣列如下:
121110987654321
#defineM3
#defineN4
#include"stdio.h"main()
{inta[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=M*N-1;k<j;k++,j--)
{t=*(p+k);*(p+k)=*(p+j);*(p+j)=t;}for(k=0k<Mk++){for(j=0j<Nj++)
printf("%4d",a[k][j]);printf(" ");
}}
5.答:
#include"stdio.h"main(){
intlen;
charstr[20],*p=str;
printf("pleaseinputastring: ");scanf("%s",str);len=0;
while(*p!=''){
len++;p++;}
printf("thestringhas%dcharacters. ",len);}
6.答:
#include"string.h"#include"stdio.h"main(){
char*str1[5],ch[5][20],k,t,j,*c;voidsort(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
9.1單選題
CBBADDBCCDDCABCBCCBADCDAB
9.2填空題
1.1202.x3.3,2,2,34.fac/i5.8,176.97.1.0/(i*i)8.
fun-in:30,20,10fun-end:1015,35,105010,20,309.01234510.9363611.(1)r+b[k](2)*x
12.7531913.15
14.(1)*x(2)t15.(1)''(2)n++16.024
9.3改錯題(略)9.4編程題
1.答:
voidzhuan()
{charch;
while((ch=getchar())!=' ')
{if(ch>='a'&&ch<='z')ch=ch-32;putchar(ch);}}
2.答:
doubleexpp(intn){intk,fac=1;doublesum=1;
for(k=1;k<=n;k++){fac*=k;
sum+=1.0/fac}
return(sum);}
3.答:
intxy3(intx,inty)
{intk,num=1;
for(k=1;k<=yk++)num*=x
num=num%1000returnnum}
4.答:
intage(intn){intc;
if(n==1)c=10
elsec=age(n-1)+2returnc}
5.答:
#include"stdio.h"
main()
{inta,b,c,d;
voidfun(inta,intb,int*c,int*d);scanf("%d%d",&a,&b);fun(a,b,&c,&d);
printf("%d%d",c,d);
}
voidfun(inta,intb,int*c,int*d){if(b)
{*c=a/b;*d=a%b;}}
6.答:
#include"stdio.h"
main(intargc,char*argv[]){intk;
printf("argc=%d ",argc);for(k=1;k<argc;k++)printf("%s ",argv[k]);}
習題10
10.1單選題
CDBBBBBBADCCBDC
10.2填空題
1.所有結構體成員所佔存儲空間的總和2.與佔用存儲空間最大的那個成員相等
附錄F課後題參考答案

285
3.(1)structsatype(2)3(3)sa.a(4)9(5)psa=&sa4.805.structnode6.0
10.3編程題
1.答:
#include"stdio.h"
structstudent{
longnum;
charname[20];charsex;floatscore;};main()
{structstudents[20],temp;intj,k,man=0,woman=0;
floatsumman=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 %f %d %f ",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("thesortednumbers: ");for(k=0;k<20;k++)
printf("%ld %s %c %5.1f ",s[k].num,s[k].name,s[k].sex,s[k].score);}
習題11
11.1單選題
BADD
11.2填空題
1.3d3d3302.(1)28
(2)20(3)0(4)--9
3.(1)251
(2)42
(3)209
(4)–295(5)848
習題12
12.1單選題
BCDCAADA
12.2填空題
1.rewind(文件指針)2."d1.dat","rb"3.stdin
4.文本文件;二進制文件5.(1)"w"
(2)str[i]--32

(3)"r"
6.fopen7.Hello8.(1)"r"

(2)fgetc(fp)

(3)time++


C語言程序設計實驗與習題指導課後程序設計答案
P18
(1)
#include<stdio.h>intmain(void){intcelsius,fahr;fahr=150;celsius=5*fahr/9-5*32/9;printf("fahr=%d,celsius=%d ",fahr,celsius);return0;}
(2)
#include<stdio.h>intmain(void){intcelsius,fahr;celsius=26;fahr=9*celsius/5+32;printf("celsius=%d,fahr=%d ",celsius,fahr);return0;}
(3)
#include<stdio.h>intmain(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 ",math,eng,comp,average);return0;}
(4)
#include<stdio.h>intmain(void){intn,a,b,c;n=152
c=n%10;
b=(n/10)%10;a=n/100;
printf("整數%d的個位數字是%d,十位數字是%d,百位數字是%d ",n,c,b,a);return0;
}
P27
(1)
#include<stdio.h>#include<math.h>intmain(void){intcelsius,fahr;printf("Entercelsius:");scanf("%d",&celsius);fahr=9*celsius/5+32;printf("fahr%d ",fahr);return0;}
(2)
#include<stdio.h>#include<math.h>intmain(void){intmoney,year;doublerate,interest;printf("Entermoney,year,rate:");scanf("%d%d%lf",&money,&year,&rate);interest=money*pow(1+rate,year)-money;printf("interest=%.2f ",interest);return0;}
(3)
#include<stdio.h>#include<math.h>intmain(void){doublex,y;printf("Enterx:");scanf("%lf",&x);
if(x<0){y=pow(x+1,2)+2*x+1/x;}else{y=sqrt(x);}
printf("y=f(%f)=%.2f ",x,y);return0;
}
(4)
#include<stdio.h>intmain(void){intx,y;printf("Enternum1:");scanf("%d",&x);printf("Enternum2:");scanf("%d",&y);printf("%d+%d=%d ",x,y,x+y);printf("%d-%d=%d ",x,y,x-y);printf("%d*%d=%d ",x,y,x*y);printf("%d/%d=%d ",x,y,x/y);printf("%d%%%d=%d ",x,y,x%y);return0;}
10的階乘
#include<stdio.h>intmain(void){inti,n,proct;printf("Entern:");scanf("%d",&n);proct=1;for(i=1;i<=n;i++){proct=proct*i;}printf("proct=%d ",proct);return0;}

F. C語言問題 設計C語言程序來計算表達式E=2/1+3/2+5/3+8/5.........的值..

記為:E=M1+M2+M3+......
M1=A1/B1,A1=2,B1=1;
Mn=An/Bn,An=B(n-1),Bn=A(n-1)+B(n-1),說明n為下標,表示第n項.第n項的分母為第n-1的分子,其分子為第n-1的分子與分母之和.

#include "stdio.h"
main()
{
int i,n,a=2,b=1;
float sum=0.0;
scanf("%d",&n); //輸入要計算到多少項
for(i=1;i<n;i++)
{
sum+=a/b;
t=b;//置第i項的分子分母
b=a;
a==t;
}
printf("the sum is %f\n",s);
}

G. C語言小題~~!!

#include <stdio.h>

int main()
{
char a[] = "china";
int i;
for(i = 0 ; i < 5; i++)
a[i] += 4;
printf("%s\n",a);
return 0;
}

H. c語言程序設計習題三

1、r!=0 r=m%n

2、s=s+f(i)
3、i<n a[i] max

I. 求<c語言程序設計>的答案

(第一題)#include <stdio.h>
void main()
{
int gy(int m, int n);
int x,y,max, min;

printf("請輸入兩個大於0的正整數,以空格或者回車間隔:\n");
scanf("%d%d",&x,&y);
while(x<1 || y<1)
{
printf("輸入數據不正確,請重新輸入。\n");
printf("請輸入兩個大於0的正整數,以空格或者回車間隔:\n");
scanf("%d%d",&x,&y);
}
max=gy(x,y);
min=x*y/max;
printf("%d與%d的最大公約數是:%d,最小公倍數是:%d\n",x,y,max,min);
}
int gy(int m, int n)
{
int max, t;
while(m%n != 0) // while(m%n)
{
t=n; n=m%n; m=t;
}
max=n;
return max;
}
第二題#include <stdio.h>
#include <math.h>
void main()
{
void root(double a, double b, double c);
double a, b, c;
printf("請輸入一元二次方程的系數,用空格或者回車間隔:\n");
scanf("%lf%lf%lf",&a,&b,&c);
root(a,b,c);
}
void root(double a, double b, double c)
{
double disc, x1, x2, real , imag;
disc = b*b-4*a*c;
if(disc>0)
{
x1 = (-b+sqrt(disc))/(2*a);
x2 = (-b-sqrt(disc))/(2*a);
printf("方程有兩個實根,分別是:%f 與 %f\n", x1, x2);
}
else if(disc==0)
{
x1 = (-b)/(2*a);
printf("方程有一個實根,它是:%f\n ", x1);
}
else
{
real=(-b)/(2*a);
imag=sqrt(-disc)/(2*a);
printf("方程有兩個虛根,分別是:%f+%fi, %f-%fi\n", real,imag,real,imag);
}
}
測試1:請輸入一元二次方程的系數,用空格或者回車間隔:1 2 1
方程有一個實根,它是:-1.000000
測試2:請輸入一元二次方程的系數,用空格或者回車間隔:1 6 5
方程有兩個實根,分別是:-1.000000 與 -5.000000
測試3:請輸入一元二次方程的系數,用空格或者回車間隔:1 1 1
方程有兩個虛根,分別是:-0.500000+0.866025i, -0.500000-0.866025i
第三題#include <stdio.h>
#include <math.h>
void main()
{
int isprime(int n);
int n;
scanf("%d",&n);
while(n<2)
{
printf("Please reinput(n>=2):");
scanf("%d",&n);
}
if(isprime(n))
printf("%d是素數。\n",n);
else
printf("%d不是素數。\n",n);
}
int isprime(int n)
{
int i,k=sqrt(n);
for(i=2;i<=k;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
第四題#include <stdio.h>
#include <math.h>
void main()
{
void printA(int a[3][3]);
void reverse(int a[3][3]); //轉置函數的聲明
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf("原二維數組:\n");
printA(a);
reverse(a); //函數轉置
printf("轉置後的數組:\n");
printA(a);
}
void printA(int a[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%6d",a[i][j]);
}
printf("\n");
}
}
void reverse(int a[3][3]) //函數轉置的定義
{
int i,j,t;
for(i=0;i<3;i++)
{
for(j=0;j<i;j++)
{
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
}
第五題#include <stdio.h>
#include <string.h>
void main()
{
char str[30];
void fanxu(char *p);
printf("請輸入一個字元串:");
gets(str);
printf("進行反序......\n");
fanxu(str);
printf("反序後的字元串:");
puts(str);
}
void fanxu(char *p)
{
int len=strlen(p);
char *q,c;
q=p+len-1; //字元串最後一個字元位置
for(;p<q;p++,q--)
{
c=*p; *p=*q; *q=c;
}

}
第六題#include <stdio.h>
#include <string.h>
void main()
{
char str1[60],str2[30];
void str_(char *p, char *q);
printf("請輸入第一個字元串:");
gets(str1);
printf("請輸入第二個字元串:");
gets(str2);
str_(str1, str2);
printf("連接後的字元串:");
puts(str1);
}
void str_(char *p, char *q)
{
for(;*p!='\0';p++);
for(;*q!='\0';p++,q++)
{
*p=*q;
}
*p='\0';
}
(0808)#include <stdio.h>
#include <string.h>
void main()
{
char str[80];
void insert(char str[]); //插入空格的函數
printf("請輸入一個字元串:");
gets(str);
insert(str); //插入空格
puts(str); //輸出字元串
}
void insert(char str[])
{
int len, i;
len=strlen(str);
for(i=len; i>0; i--) // 設置空格
{
str[2*i]=str[i];
str[2*i-1]=' ';
}
}
(0809)#include <stdio.h>
int letter; //字母個數
int digit; //數字個數
int space; //空格個數
int others; //其它字母個數
void main()
{
void count(char str[]); //統計個數的函數的聲明
char s[81];
printf("請輸入一個字元串:");
gets(s);
letter=0; digit=0;
space=0; others=0;
count(s);
printf("字元串中共有 %d 個字母,%d 個數字,%d 個空格,%d個其它字母。\n",letter,digit,space,others);
}
void count(char str[]) //統計個數的函數的定義
{
int i;
char c;
for(i=0; str[i]!='\0';i++)
{
c=str[i];
if(c>='a' && c<='z' || c>='A' && c<='Z')
{ letter++; }
else if(c>='0' && c<='9')
{ digit++; }
else if(c==' ')
{ space++; }
else
{ others++; }
}
}
(0810)#include <stdio.h>
#include <string.h>
void main()
{
int i;
char line[81];
int alphabetic(char c); //判斷一個字元是空格還是其它字母
int longest(char str[]);//尋找最長單詞的起始位置
printf("請輸入一行字元串:\n");
gets(line);
printf("最長的字元串是:");
for(i=longest(line); alphabetic(line[i]); i++)
{ printf("%c",line[i]);}
printf("\n");
}
int alphabetic(char c) //如果為空格返回0,其它字母返回1
{
if(c!=' ')
return 1;
else
return 0;
}
int longest(char str[])
{
int len=0; // 記錄每一個單詞的長度
int length=0; // 記錄最長單詞的長度
int flag=1; // 其值為0時表示當前位置處於字元串中,為1時表示當前位置為空格
int place=0; // 記錄最長字元串(單詞)的起始位置
int point; // 每個字元串的起始位置
for(int i=0; i<=strlen(str); i++)
{
if(alphabetic(str[i])) //如果當前位置為非空格
{
if(flag) //如果前一字元為空格
{
point = i; // 設置當前單詞的起始位置
flag = 0; // flag設為0,表示處於單詞中
}
else //如果前一字元為非空格
{ len++; } // 單詞的長度加1
}
else //當前位置為空格
{
flag = 1; //flag設為1,表示當前位置為空格
if(len >= length) //如果最近單詞的長度大於最長長度
{
length = len;
place = point; //設置最長單詞的起始位置
len = 0; //len歸0,重新開始計算單詞的長度
}
}
}
return place;
}
(0811)#include <stdio.h>
#include <string.h>
void main()
{
void inputNum(int a[], int n);
void outputNum(int a[], int n);
void bubble(int a[], int n);
int a[11], n;
printf("請輸入你要排序的數的個數:");
scanf("%d",&n);
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
printf("從小到大排序為:\n");
outputNum(a,n);
}
void inputNum(int a[], int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
}
void outputNum(int a[], int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("%6d",a[i]);
}
printf("\n");
}
void bubble(int a[], int n)
{
int i,j,t;
for(i=1;i<=n-1;i++)
{
for(j=1; j<=n-i; j++)
{
if(a[j]>a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(0813)#include <stdio.h>
void main()
{
double lrd(int, double);
double x;
int n;
printf("請輸入n階勒讓德多項式的階數:\n");
scanf("%d",&n);
printf("請輸入x的值:\n");
scanf("%lf",&x);
printf("參數為%f 的 %d 階勒讓德多項式的值為 %f.\n",x,n,lrd(n,x));
}
double lrd(int n, double x)
{
if(n==0)
return 1;
else if(n==1)
return x;
else
return ((2*n-1)*x - lrd(n-1,x) -(n-1)*lrd(n-2,x))/n;
}
/*
測試1:請輸入n階勒讓德多項式的階數:0請輸入x的值:99.99參數為99.990000 的 0 階勒讓德多項式的值為 1.000000.
測試2:請輸入n階勒讓德多項式的階數:1請輸入x的值:99.99
參數為99.990000 的 1 階勒讓德多項式的值為 99.990000.
測試3:請輸入n階勒讓德多項式的階數:2
請輸入x的值:99.99 參數為99.990000 的 2 階勒讓德多項式的值為 99.490000.
測試4:請輸入n階勒讓德多項式的階數:10
請輸入x的值:1.1
參數為1.100000 的 10 階勒讓德多項式的值為 0.888677.
(0817)#include <stdio.h>
void main()
{
int num;
void convert(int num); // 轉換為字元串的函數的聲明
printf("請輸入一個整數:\n");
scanf("%d",&num);
printf("轉換成字元串:");
if(num<0)
{
putchar('-');
num = -num;
}
convert(num);
printf("\n");
}
void convert(int n)
{
int i;
if((i = (n/10)) != 0)
{
convert(i);
}
putchar(n%10 + '0');
}
(0818)#include <stdio.h>
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
int year,month,day;
int days;
int count(int year,int month, int day);
int leap(int y);
printf("請輸入年份:");
scanf("%d",&year);
while(year<=0)
{
printf("年份不能為負,請重新輸入:");
scanf("%d",&year);;
}
if(leap(year)) //如果為閏年,2月份的天數為29
{ d[2]=29; }
printf("輸入月份:");
scanf("%d",&month);
while(month<1 || month>12)
{
printf("月份在1月和12月之間,你的輸入有誤,請重新輸入:");
scanf("%d",&month);
}
printf("輸入日數:");
scanf("%d",&day);;
while(day<1 || day >d[month])
{
printf("日數應在1和%d之間,請重新輸入:",d[month]);
scanf("%d",&day);
}
days=count(year,month,day);
printf("%d年%d月%d日是該年的第%d天\n",year,month,day,days);
}
int leap(int y)
{
if(y%4==0 && y%100!=0 || y%400==0)
return 1;
else
return 0;
}
int count(int year,int month, int day)
{
int i,days=0;
for(i=1; i<month; i++)
{ days += d[i]; }
days+=day;
return days;
}
(08050)#include <stdio.h>
#include <string.h>
void main()
{
char str[30];
void fanxu(char str[]);
printf("請輸入一個字元串:");
gets(str);
printf("進行反序......\n");
fanxu(str);
printf("反序後的字元串:");
puts(str);
}
void fanxu(char str[])
{
int len=strlen(str);
int i,j;
char c;
i=0; j=len-1;//i,j分別存儲第一個字元和最後一個字元的下標
for(;i<j; i++,j--)
{
c=str[i]; str[i]=str[j]; str[j]=c;
}
}
(08051)#include <stdio.h>
#include <string.h>
void main()
{
char str[30];
void fanxu(char s[]);
printf("請輸入一個字元串:");
gets(str);
printf("進行反序......\n");
fanxu(str);
printf("反序後的字元串:");
puts(str);
}
void fanxu(char s[])
{
int len=strlen(s);
int i;
char c;
for(i=0;i<=len/2;i++)
{
c=s[i];
s[i]=s[len-i-1];
s[len-i-1]=c;
}
}
(08061)#include <stdio.h>
#include <string.h>
void main()
{
char str1[60],str2[30];
void str_(char s1[], char s2[]);
printf("請輸入第一個字元串:");
gets(str1);
printf("請輸入第二個字元串:");
gets(str2);
str_(str1, str2);
printf("連接後的字元串:");
puts(str1);
}
void str_(char s1[], char s2[])
{
int i,j;
for(i=0;s1[i];i++); // s1[i]!='\0'
for(j=0;s1[i]=s2[j];i++,j++);
// (s1[i]=s2[j])!='\0'
}
(08110)#include <stdio.h>
#include <string.h>
void main()
{
void inputNum(char a[], char n);
void outputNum(char a[], char n);
void bubble(char a[], char n);
char a[11], n;
printf("請輸入你要排序的數的個數:");
scanf("%d",&n);
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
printf("從小到大排序為:\n");
outputNum(a,n);
}
void inputNum(char a[], char n)
{
char i;
printf("請連續輸入%d個字元:",n);
fflush(stdin); //一般在輸入字元或者字元串之前要清空輸入緩沖區
for(i=1;i<=n;i++)
{
scanf("%c",&a[i]);
}
}
void outputNum(char a[], char n)
{
char i;
for(i=1;i<=n;i++)
{
printf("%2c",a[i]);
}
printf("\n");
}
void bubble(char a[], char n)
{
char i,j,t;
for(i=1;i<=n-1;i++)
{
for(j=1; j<=n-i; j++)
{
if(a[j]>a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(08112)#include <iostream.h>
#include <iomanip.h>
void main()
{
void inputNum(int a[], int n);
void outputNum(int a[], int n);
void bubble(int a[], int n);
int a[11], n;
cout<<"請輸入你要排序的數的個數:";
cin>>n;
while(n<1 || n>10)
{
cout<<"請重新輸入,(1<=n<=10):";
cin>>n;
}
inputNum(a,n);
outputNum(a,n);
bubble(a,n);
cout<<"從小到大排序為:\n";
outputNum(a,n);
}
void inputNum(int a[], int n)
{
int i;
for(i=1;i<=n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
}
void outputNum(int a[], int n)
{
int i;
for(i=1;i<=n;i++)
{
cout<<setw(6)<<a[i];
}
cout<<endl;
}
void bubble(int a[], int n)
{
int i,j,t;
for(i=1;i<=n-1;i++)
{
for(j=1; j<=n-i; j++)
{
if(a[j]>a[j+1])
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
}
}
(08181)#include <iostream.h>
#include <iomanip.h>

int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
int year,month,day;
int days;
int count(int year,int month, int day);
int leap(int y);
cout<<"請輸入年份:";
cin>>year;
while(year<=0)
{
cout<<"年份不能為負,請重新輸入:";
cin>>year;
}
if(leap(year)) //如果為閏年,2月份的天數為29
{ d[2]=29; }
cout<<"輸入月份:";
cin>>month;
while(month<1 || month>12)
{
cout<<"月份在1月和12月之間,你的輸入有誤,請重新輸入:";
cin>>month;
}
cout<<"輸入日數:";
cin>>day;
while(day<1 || day >d[month])
{
cout<<"日數應在1和"<<d[month]<<"之間,請重新輸入:"<<endl;
cin>>day;
}
days=count(year,month,day);
cout<<year<<"年"<<month<<"月"<<day<<"日是該年的第"<<days<<"天\n";
}
int leap(int y)
{
if(y%4==0 && y%100!=0 || y%400==0)
return 1;
else
return 0;
}
int count(int year,int month, int day)
{
int i,days=0;

for(i=1; i<month; i++)
{ days += d[i]; }
days+=day;
return days;
}

J. 求C語言程序設計習題集與上機指導-第三版 (及答案)......

大學學習資料免費下載網 有

在 其他公共課程 版塊

標題:
譚浩強《C程序設計題解與上機指導》(電子書+紙質書)
譚浩強編《C語言程序設計》(電子書word(txt)、pdf+紙質書)
譚浩強C語言程序設計習題參考解答
和配套 C程序設計題解與上機指導 答案

都有的 還有其他課程答案 (下載不用積分)