C語言程序設(shè)計答案曹計昌參考_第1頁
C語言程序設(shè)計答案曹計昌參考_第2頁
C語言程序設(shè)計答案曹計昌參考_第3頁
C語言程序設(shè)計答案曹計昌參考_第4頁
C語言程序設(shè)計答案曹計昌參考_第5頁
已閱讀5頁,還剩49頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、文檔供參考,可復(fù)制、編制,期待您的好評與關(guān)注! 第一章習(xí)題1.4原碼:對于一個二進(jìn)制數(shù)X,如果規(guī)定其最高位為符號位,其余各位為該數(shù)的絕對值,并且規(guī)定符號位值為0表示正,為1表示負(fù),采用這種方式的二進(jìn)制編碼稱為該二進(jìn)制數(shù)X的原碼。補(bǔ)碼:正數(shù)的補(bǔ)碼等于正數(shù)的原碼,負(fù)數(shù)的補(bǔ)碼為其原碼除符號位不動外,其余各位變反再加1所得。反碼:對于正數(shù)而言,反碼與原碼相同;對于負(fù)數(shù)而言,反碼符號位的定義與原碼相同,但需要將對應(yīng)原碼的數(shù)值位按位變反。1.5 和:10101010差:000100001.6 和 01073   差 -03371.7 和 0x1AABA差 -0x53201.8(251)

2、10=(11111011)2=(373)8=(FB)161.10在16位機(jī)中,157補(bǔ)= 0000000010011101             -153補(bǔ)= 1111111101100111157-153=157+(-153)= (0000000010011101) 2+(1111111101100111) 2=(0000000000000100) 2=(4) 101.14算法設(shè)計:用變量s存儲累加和,k表示計數(shù)描述為:(1)定義變量s,k。(2)s清零,k賦初值1。(3

3、)判斷k<101?如果是,順序執(zhí)行(4);否則轉(zhuǎn)步驟(5);(4)k加到累加和變量s中,k加1;轉(zhuǎn)步驟(3)。(5)輸出累加和s。(6)結(jié)束。  開始結(jié)束int s=0,k=1;k<101?s=s+k;k=k+1;輸出sNY1.16   第二章習(xí)題2.2(1) x, +, +, y(2)-, 0xabL(3)2.89e+12L(4)”String+” FOO”(5)x, *, *, 2(6)”X?/”(7)a, ?, b(8)x, -, +=, y(9)intx, =, +, 10(10)”String”, “FOO” 2.3不是表

4、識符的如下:4th 首字母為數(shù)字  sizeof關(guān)鍵字 x*y *不是字母、數(shù)字、下劃線temp-2 -不是字母、數(shù)字、下劃線isnt  不是字母、數(shù)字、下劃線enum 關(guān)鍵字 2.4合法常數(shù):.12  0.L               1.E-5     3.F  浮點(diǎn)型常量2L   33333    0377UL

5、 0x9cfU  整型常量“a”  “”    字符串常量45                       a  字符常量 非法常數(shù):必須用轉(zhuǎn)義序列0x1ag 十六進(jìn)制沒有g(shù)E20 沒有尾數(shù)部分18       要用八進(jìn)制數(shù)xa 格式錯誤,可以是xa“34”&

6、#160;   需要轉(zhuǎn)義序列”   需要轉(zhuǎn)義序列 2.5(1)int a, b=5;(2)double h;(3)int x=2.3; 0.3 會被截取。(4)const long y=1; 必須賦初值(5)float a= 2.5*g;  g 沒有定義。(6) int a=b=2; 在 turbo C 中編譯出錯:未定義的符號b在main函數(shù)中。 2.6 (1)4(2)0(3)1(4)6(5)8(6)0(7)3.00(8)1(9)108(10)0 2.7  答案不確定(1)a=b=c 

7、; c未定義(2)正確(3)正確(4)正確(5)a*+-b   表達(dá)式缺值(6)a|bi   運(yùn)算的操作數(shù)必須是整型,而i不是(7)i*j%a  %運(yùn)算的操作數(shù)必須是整型,而a不是(8)正確(9)正確(10)int(a+b)  應(yīng)該改成(int)(a+b) 2.9(1)0(2)-2(3)65535(4)5(5)60(6)113(7)-2(8)-1(9)65532(10)3 2.10unsigned long encrypt(unsigned long x)   unsigned long x0,

8、x1,x2,x3,x4,x5,x6,x7;   x0=(x & 0x0000000F) << 8;   x1=(x & 0x000000F0);   x2=(x & 0x00000F00) << 8;   x3=(x & 0x0000F000);   x4=(x & 0x000F0000) << 8;   x5=(x & 0x00F00000);   x6=(x &

9、 0x0F000000) >> 24;   x7=(x & 0xF0000000);   return(x0|x1|x2|x3|x4|x5|x6|x7); 2.11#include<stdio.h>void main() unsigned long in;unsigned long a,b,c,d;scanf("%ld",&in);/in=1563;a=(in&0xff000000)>>24;b=(in&0x00ff0000)>>16;

10、c=(in&0x0000ff00)>>8;d=in&0x000000ff;printf("%d.%d.%d.%d",a,b,c,d); 2.15(k >>8)& 0xFF00) | (p & 0x00FF)<<8) 2.16max=a>b?a>c?a:c:b>c?b:c;max=a > b ? (a > c) ? a : c):(b > c) ? b : c); 2.17X=y>>n 2.18(c>=0 &

11、& c<=9)? c 0 : c 2.19(a % 3 = 0) && (a % 10 = 5) ? a : 0;第三章習(xí)題3.1 函數(shù)原型是指對函數(shù)的名稱、返回值類型、參數(shù)的數(shù)目和參數(shù)類型的說明。其規(guī)定了調(diào)用該函數(shù)的語法格式,即調(diào)用形式。putchar函數(shù)的原型為:int putchar(int c);puts函數(shù)的原型為: int puts(const char *s);printf函數(shù)的原型為:int printf(const char *format,);getchar函數(shù)的原型為:int getchar_r(void);gets函數(shù)的原型為:c

12、har * gets_r(char *s);scanf函數(shù)的原型為: int scanf(const char *format,);3.2 不同點(diǎn): puts為非格式輸出函數(shù),printf為格式輸出函數(shù);            puts函數(shù)的參數(shù)類型和數(shù)目一定(一個字符串),printf函數(shù)的參數(shù)類型和數(shù)目不固定;            puts函數(shù)輸出后會自動換行,print

13、f函數(shù)沒有這一功能。    相同點(diǎn):二者都向標(biāo)準(zhǔn)設(shè)備輸出;            二者返回值類型都為int。3.3 x1=-1,177777,ffff,65535    x2=-3,177775,fffd,65533    y1=123.456703,   123.457,123.457,123.457  (注意對齊)    y2=123

14、.449997,1.23450e+02,123.45    x1(%4d)=  -13.4 %c;%c;%f;%f;%lu;%d;%d;%d;%f;%Lf3.5 錯誤,運(yùn)行提示為divide error    正確,結(jié)果為b    正確,結(jié)果為         *    正確    正確,但無法正常從結(jié)果中退出    正確

15、60;   正確,結(jié)果為82,63    編譯錯誤,提示 cannot modify a const object    正確    正確3.6 -6.70000    -6    177601    123    -2     03.8#include<stdio.h>void main()  

16、     char c;       c= getchar_r();       if(c>='0'&&c<='9')|(c>='A'&&c<='F')|(c>='a'&&c<='f')      

17、              if(c>='0'&&c<='9')                             

18、0;     printf("%dn",c-'0');                            else if(c>='A'&&c<='F')    

19、                        printf("%dn",c-'A'+10);                     

20、0;      else              printf("%dn",c-'a'+10);              else           

21、;   putchar(c); 3.9#include<stdio.h>void main()       short num,high,low;       printf("Please input a short number:n");   scanf("%hd",&num);   low = 0x00ff & num; &#

22、160; high = 0x00ff & (num >> 8);   printf("The high byte is:%cn", high);   printf("The low byte is:%cn", low);      3.10#include "stdafx.h" int main(int argc, char* argv)       unsigned

23、 short int x;       unsigned short int high,low;       printf("input a integer:n");       scanf("%d",&x);       high = (x>>12)&0x000f;  

24、     low = (x<<12)&0xf000;       x= x&0x0ff0;       x=x|high|low;       printf("%dn",x);       return 0; 3.11 #include<stdi

25、o.h>void main()unsigned short int x,m,n;unsigned short int result;scanf("%hu%hu%hu",&x,&m,&n);result=(x>>(m-n+1)<<(15-n+1);printf("%hun",result);  3.12#include<stdio.h>void main()       float f,c;  

26、;     scanf("%f",&f);       c=(5*(f-32)/9;       printf("%.0f(F)=%.2f(C)n",f,c);或者#include<stdio.h>void main()int f;     float c;      

27、 scanf("%d",&f);       c=(5*(f-32)/9;       printf("%d(F)=%.2f(C)n",f,c); 3.13#include <stdio.h>#define PI (3.1415926)int main(int argc, char* argv)       double r, h; &#

28、160;     double s, v;       printf("Please input the r and h.");       scanf("%lf,%lf", &r, &h);       s = 2 * PI * r * h + 2 * PI * r * r;    

29、;   v = PI * r * r * h;       printf("s is %lf, v is %lf", s, v);       return 0; 3.14#include "stdafx.h" int main(int argc, char* argv)          

30、0;  char a4 = "編"       printf("機(jī)內(nèi)碼:%x%xtn",a0&0xff,a1&0xff);       printf("區(qū)位碼:%xtn",a0&0xff<<8+a1&0xff-0x2020-0x8080);       printf("國際碼:%xtn"

31、;,a0&0xff<<8+a1&0xff-0x8080);       return 0;第四章習(xí)題4.1#include <stdio.h>void main(void)float a,b,c;printf("Please enter the score of A:n");scanf("%f",&a);printf("Please enter the score of B:n");scanf("%f",&a

32、mp;b);printf("Please enter the score of C:n");scanf("%f",&c);if(a-b)*(a-c)<0)  printf("A gets the score %.1f",a);if(b-a)*(b-c)<0)  printf("B gets the score %.1f",b);if(c-a)*(c-b)<0)  printf("C gets the score %.1f",c);4.3#i

33、nclude <stdio.h>int mdays(int y,int m)       if (m=2) return (y%4=0 && (y%100=0 | y%400=0)?29:28;       else if (m=4 | m=6 | m=9 | m=11) return 30;       else return 31; main()  

34、60;    int y,m,d,days;       printf("Enter year:");       scanf("%d",&y);       printf("Enter month:");       scanf("%d",

35、&m);       printf("Enter day:");       scanf("%d",&d);       days=d;       while(m>1)days+=mdays(y,m-1);m-;       print

36、f("%dn",days);4.4  if方法:#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv)       float x = 0;       printf("input the salaryn");       scanf("%f&qu

37、ot;,&x);       if(x<0)              printf("wrongn");       else if(x>0 && x<1000)          &#

38、160;          printf("0n");                     else if(x<2000)             &

39、#160;              printf("%fn",x*0.05);                            else if(x<3000)

40、0;                                  printf("%fn",x*0.1);           

41、60;                       else if(x<4000)                        &#

42、160;                 printf("%fn",x*0.15);                            

43、              else if(x<5000)                                  

44、;               printf("%fn",x*0.2);                              

45、0;                  else                                

46、;                        printf("%fn",x*0.25);       return 0; Case方法: #include "stdafx.h"#include <stdio.h>int main(in

47、t argc, char* argv)       float x ;       printf("input the salaryn");       scanf("%f",&x);       int xCase = 0;       xCase

48、= (int)(x/1000.0);       switch(xCase)              case 0:                     printf("0n"); &

49、#160;                   break;       case 1:                     printf("%f

50、n",x*0.05);                     break;       case 2:                   

51、;  printf("%fn",x*0.1);                     break;       case 3:               &

52、#160;     printf("%fn",x*0.15);                     break;       case 4:           &#

53、160;         printf("%fn",x*0.2);                     break;       default:       

54、60;             printf("%fn",x*0.25);              return 0; 4.7#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv)   

55、    char *sa;       char c;       int i = 0,j = 0,k = 0;       do                     c= getchar_r()

56、;              sai+ = c;       while(c != 'r');       for(i=0;sai+1;i+)                 

57、;    for(j = i+1;saj;j+)                                   if( sai=saj && saj =' ')   

58、60;                                             for(k=j;sak;k+)   

59、0;                               sak = sak+1;                  &#

60、160;         j-;                                           &

61、#160;     for(k=0;sak;k+)              printf("%2c",sak);       return 0; 4.10#include <stdio.h>#define EPS 1e-5void main()       int

62、s=1;       float n=1.0,t=1.0,pi=0;       while(1.0/n>=EPS)                     pi=pi+t;         &

63、#160;    n=n+2;              s=s*(-1);              t=s/n;              pi=pi*4;  

64、60;    printf("pi=%10.6fn",pi); 4.11#include<stdio.h>int main()       int a,b,num1,num2,temp;       printf("Input a & b:");       scanf("%d%d",&nu

65、m1,&num2);       if(num1>num2)                     temp=num1; num1=num2; num2=temp;              a=num1; b=n

66、um2;       while(b!=0)                     temp=a%b;              a=b;      

67、0;       b=temp;              printf("The GCD of %d and %d is: %dn",num1,num2,a);       printf("The LCM of them is: %dn",num1*num2/a);4.13#include "stdafx.

68、h"#include <stdio.h>int Primes(int x);/判斷素數(shù)函數(shù)int main(int argc, char* argv)       int i,j;       int num;       for(num = 4;num<=100;num+)          

69、60;          if(num%2 = 0)                                   for(i=1;i<num;i+) 

70、60;                                               for(j=1;j<num;j+) 

71、;                                                  

72、0;           if(num = i+j)                                      &

73、#160;                                      if(Primes(i) && Primes(j)      

74、60;                                                  &#

75、160;                                 printf("%d=%d+%dn",num,i,j);           

76、;                                                  

77、0;                                                   &#

78、160;                                        return 0; int Primes(int x)       int i ;

79、       int n = 0;       for(i = 1;i<=x;i+)                     if(x%i=0)           

80、0;   n+;              if(n=2)              return 1;       else            

81、  return 0;4.17#include<stdio.h>void main(void)       int c,i;       for(i=1,c=32;c<=126;+i,+c)                      

82、0;           printf("%3d%-5c",c,c);              if(!(i%8)                 printf("n");

83、60;      4.18#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv)       int x;       int i,n,sum;       printf("input 10 numbersn");  

84、     for(i = 0,n = 0,sum = 0;i<10;i+)                     scanf("%d",&x);              if(x >0) 

85、;                                  sum+=x;                &#

86、160;    n+;                            if(n)              printf("numbers = %d,average = %

87、fn",n,1.0*sum/n);       return 0; 第五章習(xí)題5.5Extern和static存儲類型的區(qū)別:Static型外部變量和extern型外部變量的唯一區(qū)別是作用域的限制。靜態(tài)外部變量只能作用于定義它的文件,其他文件中的函數(shù)不能使用。Extern型外部變量的作用域可以擴(kuò)大到整個程序的所有文件。Static和auto存儲類型的區(qū)別:靜態(tài)局部變量和自動變量有根本性的區(qū)別。由于靜態(tài)局部變量在程序執(zhí)行期間不會消失,因此,它的值有連續(xù)性。當(dāng)退出塊時,它的值能保存下來,以便再次進(jìn)入塊時使用,而自動變量

88、的值在退出塊時都丟失了。如果定義時靜態(tài)局部變量有顯示初始化,只在第一次進(jìn)入時執(zhí)行一次賦初值操作,而自動變量每次進(jìn)入時都要執(zhí)行賦初值操作。5.6不能。在C語言中,參數(shù)的傳遞方式是“值傳遞”,即把實參的值拷貝到參數(shù)的存儲區(qū)中。因此,swap()函數(shù)交換的只是實參的本地拷貝,代表swap()實參的變量并沒有被改變。5.7  6,125.10 #include <stdio.h>double sum_fac(int n)   double s=0;   int i;   double fac=1.0; 

89、;  for(i=1;i<=n;i+)        fac*=1.0/i;     s+=fac;      return s;void main(void)   int n;   printf("Please enter the integer n:");   scanf("%d",&n);    p

90、rintf("the sum is %lfn",sum_fac(n);5.17#include <stdio.h>void reverse()       char ch= getchar_r();       if(ch!='n')                 &#

91、160;   reverse();              putchar(ch);       int main()       reverse();       printf("n");    return 0;&

92、#160;第六章習(xí)題6.1(1)正確(2)錯誤,不需要加“;”(3)錯誤,“ident”與“(”之間不能有空格(4)錯誤,宏名不能是關(guān)鍵字“void”(5)錯誤,將x*y改成(x)*(y) 6.4將會導(dǎo)致變量blue的重復(fù)定義 6.5(1)define NO 0(2)#include “common.h”(3)#line 3000(4)#undef TRUE  #define TRUE 1(5)#if TRUE !=0#define FALSE 0#else  #define FALSE 1#endif(6)#ifndef SIZE  

93、;   assert(0);#elseassert(SIZE<10&&SIZE>1);#endif(7)#define SQUARE_VOLUME(x) (x)*(x)*(x) 6.10#include <stdio.h>#define pi 3.1415926#define BALL_VOLUME(r)  (4/3)*pi*(r)*(r)*(r)int main()int r;float v11;for(r=1;r<11;r+)vr=float(BALL_VOLUME(r);printf("r=%

94、2d      v=%.2fn",r,vr);return 0;第七章習(xí)題7.9#include<stdio.h>#include<graphics.h>#define g 10void main()char *buffer;int gdriver=DETECT,gmode,i,size;initgraph(&gdriver,&gmode,"c:tcbgi");setbkcolor(BLUE);setcolor(RED);setlinestyle(0,0,1);setfill

95、style(1,5);circle(200,250,RED);size=imagesize(200,250,200,300);buffer=malloc(size);getimage_r(200,250,200,300,buffer);for(i=0;i<=10;i+)putimage(200,250+g*i*i/2,buffer,COPY_PUT);getch_r();closegraph(); 7.11#include<stdio.h>#define RAND_MAX 32767#define RAND   100int RandomInte

96、ger(int low,int high)     int k;    double d;    d=(double)rand()/(double)RAND_MAX+1);    k=(int)(d*(high-low+1);    return(low+k);void main()long i;int n=0;int szWordRAND;char a="heads"char b="tails"

97、srand(time(0);for(i=0;i<RAND;i+)   szWordi=RandomInteger(0,1);   if(szWordi=1)        printf("n%s",a);     n+;      else       printf("n%s",b);   &

98、#160; n=0;      if(n=3)         printf("nIt took %ld flips to get heads 3 consecutives times",i+1);      break;    7.16 char *MonthName(int month);int MonthDays(int month,int year);int First

99、DayOfMonth(int month,int year);int IsLeapYear(int year);enum weakSUNDAY ,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;  #include “caltools.h”char *MonthName(int month)Switch(month)Case 1:  return(“January”);Case 2:  return(“February”);Case 3:  return(“March”);Case 4:

100、60; return(“April”);Case 5:  return(“May”);Case 6:  return(“June”);Case 7:  return(“July”);Case 8:  return(“August”);Case 9:  return(“September”);Case 10:  return(“October”);Case 11:  return(“November”);Case 12:  return(“December”);Default :  printf(“inpu

101、t error!n”); int MonthDays(int month,int year)Case 1:Case 3:Case 5:Case 7:Case 8:Case 10:Case 12: return(31);Case 4:Case 6:Case 9:Case 11: return(30);Case 1: return (IsLeapYear(year)?29:28); int FirstDayOfMonth(int month,int year)Int i;Long total=0;If(year=2000)For(i=1;i<month;i+)Total+

102、=( MonthDays(i, 2000);Return(total%7+6)%7);)Else if(year>2000)For (i=2000;i<year;i+)Total+=(IsLeapYear(i)?366:365);For(i=1;i<month;i+)Total+=( MonthDays(i, year);Return(total%7+6)%7);)Else if(year=1999)For(i=12;i>month;i-)Total+=( MonthDays(i, 1999);Return(13-total%7)%7);ElseFor (i=1999;

103、i<year;i-)Total+=(IsLeapYear(i)?366:365);For(i=12;i>month;i-)Total+=( MonthDays(i, year);Return(13-total%7)%7); int IsLeapYear(int year)Return ( !(year%4)&&(year%400) | !(year%400) );  #include <stdio.h>#include “caltools.h”Void main()Int month,year;Printf(“please

104、input the month and year:”);Scanf(“%d%d” ,&month,&year);Printf(“the name of the month is %sn”, MonthName( month);Printf(“there are %d days in this month.n”, MonthDays( month,int year);Printf(“the first day of the month in this year is %d”, FirstDayOfMonth( month,year); 第八章習(xí)題8.4#include

105、"stdafx.h"#include "malloc.h"#define N 65535 void DelSpace(char sa);int main(int argc, char* argv)       char saN;       char c;       int i =0;       do&#

106、160;                    c = getchar_r();              if(c = 13)              sa

107、i+ = 'n'              else              sai+ = c;       while(c!='');    DelSpace(sa);    

108、;   int j = 0;       while(1)              if(saj = '')              break;       printf("%c

109、",saj+);              printf("/n");       return 0; void DelSpace(char sa)       char *t1 = (char*)malloc(sizeof(sa);       char *

110、t2 = (char*)malloc(sizeof(sa);       t1 = t2 = sa;        while(*t1)                             

111、60;   *t2+ = *t1+;                 if(*(t2-1)=' ' && *t1=' ')                 t2-;    

112、                  還有一個方法:void DelSpace(char sa,int n)       char* tmpbuf = (char*)malloc(sizeof(sa)+1);       int p1 = 0, p2 = 0;    &#

113、160;  bool bSpace = false;       while(p1 < n)                     if(bSpace && sap1 = ' ')          

114、                         p1+;                           

115、; else                                   if(sap1 = ' ')            

116、                bSpace = true;                     else           

117、0;                bSpace = false;                     tmpbufp2+ = sap1+;         

118、0;                                       strcpy(sa, tmpbuf);       free( tmpbuf); 

119、60; 8.7#include "stdafx.h"#define NUM 100  struct stuInfo       char name10;       int mark;stuNUM; void scoreSort(stuInfo stu,int n);int main(int argc, char* argv)       int n;       printf("input the number of students:n");       scanf("%d",&n);       printf("intput the name and scoren");       for(int i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論