第一篇:C語(yǔ)言程序設(shè)計(jì)教程答案-楊路明-郭浩志
習(xí)題1
1、算法描述主要是用兩種基本方法:第一是自然語(yǔ)言描述,第二是使用專用工具進(jìn)行算法描述
2、c語(yǔ)言程序的結(jié)構(gòu)如下:
1、c語(yǔ)言程序由函數(shù)組成,每個(gè)程序必須具有一個(gè)main函數(shù)作為程序的主控函數(shù)。
2、“/*”與“*/”之間的內(nèi)容構(gòu)成c語(yǔ)言程序的注釋部分。
3、用預(yù)處理命令#include可以包含有關(guān)文件的信息。
4、大小寫字母在c語(yǔ)言中是有區(qū)別的。
5、除main函數(shù)和標(biāo)準(zhǔn)庫(kù)函數(shù)以外,用戶可以自己編寫函數(shù),程序一般由多個(gè)函數(shù)組成,這些函數(shù)制定實(shí)際所需要做的工作。
例如:
void main()
{
int a,b,c,s;
a=8;b=12;c=6;
s=a+b*c;
printf(“s=%dn”,s);
}
3、c語(yǔ)言的特點(diǎn):
1、c語(yǔ)言具有結(jié)構(gòu)語(yǔ)言的特點(diǎn),程序之間很容易實(shí)現(xiàn)段的共享;
2、c語(yǔ)言的主要結(jié)構(gòu)成分為函數(shù),函數(shù)可以在程序中被定義完成獨(dú)立的任務(wù),獨(dú)立地編譯成代碼,以實(shí)現(xiàn)程序的模塊化。
3、c語(yǔ)言運(yùn)算符豐富,運(yùn)算包含的范圍很廣;
4、c語(yǔ)言數(shù)據(jù)類型豐富。
5、c語(yǔ)言允許直接訪問物理地址,即可直接對(duì)硬件進(jìn)行操作,實(shí)現(xiàn)匯編語(yǔ)言的大部分功能;
6、c語(yǔ)言語(yǔ)法限制不太嚴(yán)格,程序設(shè)計(jì)自由度大,這樣是c語(yǔ)言能夠減少對(duì)程序員的束縛;
7、用c語(yǔ)言編程,生成的目標(biāo)代碼質(zhì)量高,程序執(zhí)行效率高,可移植性好;
4、合法標(biāo)識(shí)符:AB12、leed_
3、EF3_
3、_762、PAS、XYZ43K2
不合法標(biāo)識(shí)符:a*b2、8stu、D.K.Jon、if、ave#xy、#_DT5、C.D
5、F2:將當(dāng)前編輯器中文件存盤.F10:調(diào)用主菜單.F4:程序運(yùn)行到光標(biāo)所在行.Ctrl+F9:當(dāng)前編輯環(huán)境下,進(jìn)行編譯、連接且運(yùn)行程序;
Alt+F5:將窗口切換到DOS下,查看程序運(yùn)行結(jié)果
6、(1): ********************
welcome you
very good
********************
(2): please input three number;5,7,8
max number is:8
7、main8、User screen、Alt+F59、標(biāo)識(shí)符必須是字母或下劃線開頭,大小寫字母含義不同。由數(shù)字、字母和下劃線組成;
關(guān)鍵字是一種語(yǔ)言中規(guī)定具有特定含義的標(biāo)識(shí)符。關(guān)鍵字不能作為變量或函數(shù)名來(lái)使用,用戶只能根據(jù)系統(tǒng)的規(guī)定使用它們。
10、選擇主菜單File項(xiàng)下拉子菜單中Save項(xiàng)或直接按F2鍵存盤。
習(xí)題2
1、符合C語(yǔ)法規(guī)定的常數(shù)為:0x1e、“abn”、1.e5
2、(1):
錯(cuò)誤如下:int x,y=5,z=5,aver;
x=7;
aver =(x+y+x)/3;
結(jié)果如下:AVER=5
(2):錯(cuò)誤如下:char c1='a',c2='b',c3='c';
printf(“a=%db='%c'”end“n”,a,b);
結(jié)果如下:a=3b='A'“end”
aabcc
abc3、4、(1):9,11,9,10
(2):3,1,0,0
(3):11,19,31,1
5、(1):0
(2):0
(3):9.500000
(4):90
(5):10
(6):10
(7):65
(8):4
(9):4.500000
(10):1
(11):0
(12):20
(13):0
6、(5)
7、求x的絕對(duì)值
8、c>(max=a>b?a:b)?c:max;
9、B
10、D
習(xí)題3
1、輸入函數(shù)scanf的參數(shù)錯(cuò)誤,應(yīng)該為:scanf(“%f”,&k);
2、|1234 1234 |
3、ff10 4、1,3,1
5、原字符串
左邊加空格再加字符串本省,字符個(gè)數(shù)總和為5個(gè)
6、scanf(“%d,%d,%c,%c”,&a1,&a2,&c1,&c2);
7、printf(“a+b=%dn”,a+b);
printf(“a-b=%dn”,a-b);
printf(“a*b=%dn”,a*b);
printf(“a/b=%dn”,a/b);
printf(“(float)a/b=%fn”,(float)a/b);
printf(“a%b=%dn”,a%b);
8、void main()
{
float r;
float s,c;
printf(“please input the number:”);
scanf(“%f”,&r);
if(r>=0)
{
s = 3.14*r*r;
c = 2*3.14*r;
printf(“s = %f, c = %fn”,s,c);
}
else printf(“you input number is error!”);
}
9、void main(){
int n;
printf(“please input the number:”);
scanf(“%d”,&n);
if(n>=100 && n <= 999)
printf(“%d%d%d”,n%10,(n/10)%10,n/100);
else printf(“you input number is error!”);}
10、void main(){
int i,j,k;
scanf(“%d,%d,%d”,&i,&j,&k);
((i%2!= 0?1:0)+(j%2!= 0?1:0)+(k%2!= 0?1:0))== 2?printf(“YESn”):printf(“NOn”);}
11、void main(){
char a;
scanf(“%c”,&a);
printf(“%c,%c,%c”,a-1,a,a+1);
printf(“%d,%d,%d”,a-1,a,a+1);}
12、void main(){
float a,b,c,s,Area;
scanf(“%f,%f,%f”,&a,&b,&c);
if(a+b > c || a+c > b || b+c >a)
{
s =(a+b+c)/2;
Area = sqrt(s*(s-a)*(s-b)*(s-c));
printf(“%fn”,Area);
}
else
printf(“you input the number is error!n”);}習(xí)題4 1: 0
2: 20
3:
(x<30&&x>20)||(x<-100)
4: ***a=25,b=14,c=16***
5: 37
6: if(a<=b)
printf(“1”);else
printf(“2”);
7、#include 8、#include while((i*y)<=x){ if(x==(i*y)){temp1=1;break;} temp2=i; i++; } if(temp1) printf(“%d / %d = %d”,x,y,i);else printf(“%d / %d---> shang=%d,yushu=%d”,x,y,temp2,x-y*temp2);getch(); } 9、#include scanf(“%f,%f”,&x,&y);n=(x-2)*(x-2); m=(y-2)*(y-2);if((m+n)<=1) printf(“(%.3f,%.3f)In the yuan”,x,y); else printf(“(%.3f,%.3f)out of the yuan”,x,y); getch();} 10、#include void main() { int temp=0,month,year; printf(“Please input(year,month): ”); scanf(“%d,%d”,&year,&month); if((year%400==0)||(year%4==0&&year%100!=0)) temp=1; if(month==2) { if(temp)printf(“%d year %d month have ”,year,month); else printf(“%d year %d month have 28 ”,year,month); } else if(month%2==0) printf(“%d year %d month have 30 ”,year,month); else printf(“%d year %d month have 31 ”,year,month); getch(); } 11、switch(a/10) { case 5:m=4;break; case 4:m=3;break; case 3:m=2;break;case 2:m=1;break;default:m=5; } 12、方法一: #include y=x-1; else if(x==0) y=x; else if(x>0&&x<8) y=x+1; printf(“%d”,y);getch(); } 方法二: #include else if(x>0&&x<8) y=y=x+1; else y=x-1; printf(“%d”,y); } else printf(“Input error!!”);getch(); } 方法三: #include { int x,y,i; scanf(“%d”,&x); if(x<8&&x>-5) { if(x==0)i=1; else if(x>0&&x<8) i=2; else i=3; } else i=4; switch(i) { case 1:y=x;printf(“%d”,y);break; case 2:y=x+1;printf(“%d”,y);break; case 3:y=x-1;printf(“%d”,y);break; case 4:printf(“Input error!”);break;}getch();}習(xí)題5 1、void main() { int n,value;int i,count=0;float average = 0;long int sum = 0;scanf(“%d”,&n);for(i = 0;i < n;i++){ scanf(“%d”,&value); if(value%2 == 0) { sum+=value; count++; } } average = sum /(float)count;printf(“the average is %fn”,average); } 2、#include “stdio.h” void main() { char ch;int zm = 0, sz = 0;ch = getchar(); while(ch!= '*'){ if((ch >= 'A' && ch <= 'Z')||(ch >= 'a' && ch < 'z')) zm++; if(ch >= '0' && ch <= '9') sz++; ch = getchar();} printf(“zm = %d ; sz = %dn”,zm,sz); } 3、void main() { long i_value;int sum = 0;int temp = 0; scanf(“%ld”,&i_value); if(i_value < 0){printf(“you input the int is error!n”);return;} for(;;) { temp = i_value%10; i_value = i_value/10; sum+=temp; printf(“%d+”,temp); if(i_value == 0)break; } printf(“b=%dn”,sum); } 4、#include “stdio.h” void main() { char ch; ch = getchar(); while(ch!= '.') { if((ch >= 'A' && ch <= 'Z')) { ch = ch + 32; putchar(ch); } else if((ch >= 'a' && ch < 'z')) { ch = ch1; } printf(“Total steps: %dn”, steps); return 0;} 8、main(){ int i,j,k,n;printf(“the narcissus number is:n”);for(n=100;n<1000;n++) {i=n/100; j=n/10-i*10; k=n%10; if(i*i*i+j*j*j+k*k*k==n)printf(“%dn”,n); } } 9、main(){ float i,j,temp,n,sum; int t; i=2;j=1;sum=0; scanf(“%d”,&t); for(n=1;n<=t;n++) {sum=sum+i/j; temp=i;i=i+j;j=temp; } printf(“2/1+3/2+5/3+8/5+13/8+...=%fn”,sum);} 10、void main(){ int nWidth,a,b; scanf(“%d”,&nWidth); for(a=(nWidth%2);a<=nWidth;a+=2) { for(b=0;b { char chOut =' '; int nleft =(nWidth-a)/2; int nright =(nWidth+a)/2; if(b>=nleft&&b { chOut ='*'; } printf(“%c”,chOut); } printf(“n”); } } 11、void main(){ int i=1,j=1; for(i=1;i<=9;i++) { for(j=1;j<=i;j++) printf(“%d*%d=%d”,i,j,i*j); printf(“n”) } } 12、#include “stdio.h” void main(){ char c; //count1是正數(shù)的個(gè)數(shù) //count2是負(fù)數(shù)的個(gè)數(shù) int count1=0,count2=0; //sum1是正數(shù)之和 //sum2是負(fù)數(shù)之和 int sum1=0,sum2=0; int flage = 0; c=getchar(); while(c!= '*') { if(c == '-')flage = 1; if(flage == 0) {sum1+=(c-48);count1++;} else {if(c!= '-'){sum2-=(c-48);flage=0;count2++;}} c = getchar(); } printf(“%d,%dn”,count1,count2); printf(“%f,%fn”,sum1/count1,sum2/count2);} 13、void main(){ int i,j; int s = 0; for(i = 100;i <= 999;i++) { s = 0; for(j=1;j < i-1;j++) { if(i%j == 0)s = s + j; } if(s == i)printf(“%5d”,i); } } 14、#include while(fabs(1.0/k)>= 0.000001){ n++; e = e+ 1.0/k; k = k * n; } printf(“%fn”,e); } 15、#include “math.h” main() {float x0,x1,x2,f0,f1,f2;x1=-10; f1=2*x1*x1*x1-4*x1*x1+3*x1;x2=10; f2=2*x2*x2*x2-4*x2*x2+3*x2;do { x0=(x1+x2)/2; f0=2*x0*x0*x0-4*x0*x0+3*x0; if((f0*f1)<0) {x2=x0;f2=f0;} else {x1=x0;f1=f0;} } while(fabs(f0)>1e-6); printf(“______2*x*x*x-4*x*x+3*x=0______n”);printf(“the root is %fn”,x0);}習(xí)題6 1、#include b=r;} return b;} int multiple(int a,int b){ int d;d=divisor(a,b);return a*b/d;} void main(){ int a,b,c,d;printf(“intput(a,b): ”);scanf(“%d,%d”,&a,&b);c=divisor(a,b);d=multiple(a,b);printf(“ndivisor=%dttmultiple=%d”,c,d); } 2、#include