C语言程序设计答案曹计昌

上传人:muw****50 文档编号:160679052 上传时间:2022-10-11 格式:DOC 页数:54 大小:109.50KB
收藏 版权申诉 举报 下载
C语言程序设计答案曹计昌_第1页
第1页 / 共54页
C语言程序设计答案曹计昌_第2页
第2页 / 共54页
C语言程序设计答案曹计昌_第3页
第3页 / 共54页
资源描述:

《C语言程序设计答案曹计昌》由会员分享,可在线阅读,更多相关《C语言程序设计答案曹计昌(54页珍藏版)》请在装配图网上搜索。

1、第一章习题1.4原码:对于一个二进制数X,如果规定其最高位为符号位,其余各位为该数的绝对值,并且规定符号位值为0表示正,为1表示负,采用这种方式的二进制编码称为该二进制数X的原码。补码:正数的补码等于正数的原码,负数的补码为其原码除符号位不动外,其余各位变反再加1所得。反码:对于正数而言,反码与原码相同;对于负数而言,反码符号位的定义与原码相同,但需要将对应原码的数值位按位变反。1.5 和:10101010差:000100001.6 和 01073 差 -03371.7 和 0x1AABA差 -0x53201.8(251)10=(11111011)2=(373)8=(FB)161.10在16位

2、机中,157补= 0000000010011101 -153补157-153=157+(-153)= (0000000010011101) 22=(0000000000000100) 2=(4) 101.14算法设计:用变量s存储累加和,k表示计数描述为:(1)定义变量s,k。(2)s清零,k赋初值1。(3)判断k101?如果是,顺序执行(4);否则转步骤(5);(4)k加到累加和变量s中,k加1;转步骤(3)。(5)输出累加和s。(6)结束。开始结束int s=0,k=1;k101?s=s+k;k=k+1;输出sNY1.16 第二章习题2.2(1) x, +, +, y(2)-, 0xabL

3、(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不是表识符的如下:4th 首字母为数字 sizeof关键字x*y *不是字母、数字、下划线temp-2 -不是字母、数字、下划线isnt 不是字母、数字、下划线enum 关键字2.4合法常数:.12 0.L 1.E-5 3.F 浮点型常量2L 33333 0377UL 0x9cfU 整型常量“a” “” 字符串常量45 a 字符常量非法常数:必须用转义序列0x1ag 十六

4、进制没有gE20 没有尾数部分18 要用八进制数xa 格式错误,可以是xa“34” 需要转义序列” 需要转义序列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函数中。2.6(1)4(2)0(3)1(4)6(5)8(6)0(7)3.00(8)1(9)108(10)02.7 答案不确定(1)a=b=c c未定义(2)正确(3)正确(4)正确(5)a*+-b

5、表达式缺值(6)a|bi 运算的操作数必须是整型,而i不是(7)i*j%a %运算的操作数必须是整型,而a不是(8)正确(9)正确(10)int(a+b) 应该改成(int)(a+b)2.9(1)0(2)-2(3)65535(4)5(5)60(6)113(7)-2(8)-1(9)65532(10)32.10unsigned long encrypt(unsigned long x) unsigned long x0,x1,x2,x3,x4,x5,x6,x7; x0=(x & 0x0000000F) 8; x1=(x & 0x000000F0); x2=(x & 0x00000F00) 8; x

6、3=(x & 0x0000F000); x4=(x & 0x000F0000) 24; x7=(x & 0xF0000000); return(x0|x1|x2|x3|x4|x5|x6|x7);2.11#includevoid main()unsigned long in;unsigned long a,b,c,d;scanf(%ld,&in);/in=1563;a=(in&0xff000000)24;b=(in&0x00ff0000)16;c=(in&0x0000ff00)8;d=in&0x000000ff;printf(%d.%d.%d.%d,a,b,c,d);2.15(k 8)& 0xF

7、F00) | (p & 0x00FF)b?ac?a:c:bc?b:c;max=a b ? (a c) ? a : c):(b c) ? b : c);2.17X=yn2.18(c=0 & c=9)? c 0 : c2.19(a % 3 = 0) & (a % 10 = 5) ? a : 0;第三章习题3.1 函数原型是指对函数的名称、返回值类型、参数的数目和参数类型的说明。其规定了调用该函数的语法格式,即调用形式。putchar函数的原型为:int putchar(int c);puts函数的原型为: int puts(const char *s);printf函数的原型为:int print

8、f(const char *format,);getchar函数的原型为:int getchar_r(void);gets函数的原型为:char * gets_r(char *s);scanf函数的原型为: int scanf(const char *format,);3.2 不同点: puts为非格式输出函数,printf为格式输出函数; puts函数的参数类型和数目一定(一个字符串),printf函数的参数类型和数目不固定; puts函数输出后会自动换行,printf函数没有这一功能。 相同点:二者都向标准设备输出; 二者返回值类型都为int。3.3 x1=-1,177777,ffff,6

9、5535 x2=-3,177775,fffd,65533 y1=123.456703, 123.457,123.457,123.457 (注意对齐) y2=123.449997,1.23450e+02,123.45 x1(%4d)= -13.4 %c;%c;%f;%f;%lu;%d;%d;%d;%f;%Lf3.5 错误,运行提示为divide error 正确,结果为b 正确,结果为 * 正确 正确,但无法正常从结果中退出 正确 正确,结果为82,63 编译错误,提示 cannot modify a const object 正确 正确3.6 -6.70000 -6 177601 123 -2

10、 03.8#includevoid main() char c; c= getchar_r(); if(c=0&c=A&c=a&c=0&c=A&c=F) printf(%dn,c-A+10); else printf(%dn,c-a+10); else putchar(c);3.9#includevoid main() short num,high,low; printf(Please input a short number:n); scanf(%hd,&num); low = 0x00ff & num; high = 0x00ff & (num 8); printf(The high by

11、te is:%cn, high); printf(The low byte is:%cn, low); 3.10#include stdafx.hint main(int argc, char* argv) unsigned short int x; unsigned short int high,low; printf(input a integer:n); scanf(%d,&x); high = (x12)&0x000f; low = (x12)&0xf000; x= x&0x0ff0; x=x|high|low; printf(%dn,x); return 0;3.11#include

12、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#includevoid main() float f,c; scanf(%f,&f); c=(5*(f-32)/9; printf(%.0f(F)=%.2f(C)n,f,c);或者#includevoid main()int f;float c; scanf(%d,&f); c=(5*(f-32)/9; printf(%d

13、(F)=%.2f(C)n,f,c);3.13#include #define PI (3.1415926)int main(int argc, char* argv) double r, h; double s, v; printf(Please input the r and h.); scanf(%lf,%lf, &r, &h); s = 2 * PI * r * h + 2 * PI * r * r; v = PI * r * r * h; printf(s is %lf, v is %lf, s, v); return 0;3.14#include stdafx.hint main(i

14、nt argc, char* argv) char a4 = 编; printf(机内码:%x%xtn,a0&0xff,a1&0xff); printf(区位码:%xtn,a0&0xff8+a1&0xff-0x2020-0x8080); printf(国际码:%xtn,a0&0xff8+a1&0xff-0x8080); return 0;第四章习题4.1#include 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:

15、n);scanf(%f,&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#include int mdays(int y,int m) if (m=2) return (y%4=0 & (y%100=0 | y%400=0)?29:28; el

16、se if (m=4 | m=6 | m=9 | m=11) return 30; else return 31;main() int y,m,d,days; printf(Enter year:); scanf(%d,&y); printf(Enter month:); scanf(%d,&m); printf(Enter day:); scanf(%d,&d); days=d; while(m1)days+=mdays(y,m-1);m-; printf(%dn,days);4.4 if方法:#include stdafx.h#include int main(int argc, char

17、* argv) float x = 0; printf(input the salaryn); scanf(%f,&x); if(x0 & x1000) printf(0n); else if(x2000) printf(%fn,x*0.05); else if(x3000) printf(%fn,x*0.1); else if(x4000) printf(%fn,x*0.15); else if(x5000) printf(%fn,x*0.2); else printf(%fn,x*0.25); return 0;Case方法:#include stdafx.h#include int ma

18、in(int argc, char* argv) float x ; printf(input the salaryn); scanf(%f,&x); int xCase = 0; xCase = (int)(x/1000.0); switch(xCase) case 0: printf(0n); break; case 1: printf(%fn,x*0.05); break; case 2: printf(%fn,x*0.1); break; case 3: printf(%fn,x*0.15); break; case 4: printf(%fn,x*0.2); break; defau

19、lt: printf(%fn,x*0.25); return 0;4.7#include stdafx.h#include int main(int argc, char* argv) char *sa; char c; int i = 0,j = 0,k = 0; do c= getchar_r(); sai+ = c; while(c != r); for(i=0;sai+1;i+) for(j = i+1;saj;j+) if( sai=saj & saj = ) for(k=j;sak;k+) sak = sak+1; j-; for(k=0;sak;k+) printf(%2c,sa

20、k); return 0;4.10#include #define EPS 1e-5void main() int s=1; float n=1.0,t=1.0,pi=0; while(1.0/n=EPS) pi=pi+t; n=n+2; s=s*(-1); t=s/n; pi=pi*4; printf(pi=%10.6fn,pi);4.11#includeint main() int a,b,num1,num2,temp; printf(Input a & b:); scanf(%d%d,&num1,&num2); if(num1num2) temp=num1; num1=num2; num

21、2=temp; a=num1; b=num2; while(b!=0) temp=a%b; a=b; 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.h#include int Primes(int x);/判断素数函数int main(int argc, char* argv) int i,j; int num; for(num = 4;num=100;num+) if(num%2 = 0) for

22、(i=1;inum;i+) for(j=1;jnum;j+) if(num = i+j) if(Primes(i) & Primes(j) printf(%d=%d+%dn,num,i,j); return 0;int Primes(int x) int i ; int n = 0; for(i = 1;i=x;i+) if(x%i=0) n+; if(n=2) return 1; else return 0;4.17#includevoid main(void) int c,i; for(i=1,c=32;c=126;+i,+c) printf(%3d%-5c,c,c); if(!(i%8)

23、 printf(n); 4.18#include stdafx.h#include int main(int argc, char* argv) int x; int i,n,sum; printf(input 10 numbersn); for(i = 0,n = 0,sum = 0;i0) sum+=x; n+; if(n) printf(numbers = %d,average = %fn,n,1.0*sum/n); return 0;第五章习题5.5Extern和static存储类型的区别:Static型外部变量和extern型外部变量的唯一区别是作用域的限制。静态外部变量只能作用于定

24、义它的文件,其他文件中的函数不能使用。Extern型外部变量的作用域可以扩大到整个程序的所有文件。Static和auto存储类型的区别:静态局部变量和自动变量有根本性的区别。由于静态局部变量在程序执行期间不会消失,因此,它的值有连续性。当退出块时,它的值能保存下来,以便再次进入块时使用,而自动变量的值在退出块时都丢失了。如果定义时静态局部变量有显示初始化,只在第一次进入时执行一次赋初值操作,而自动变量每次进入时都要执行赋初值操作。5.6不能。在C语言中,参数的传递方式是“值传递”,即把实参的值拷贝到参数的存储区中。因此,swap()函数交换的只是实参的本地拷贝,代表swap()实参的变量并没有

25、被改变。5.7 6,125.10#include double sum_fac(int n) double s=0; int i; double fac=1.0; 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); printf(the sum is %lfn,sum_fac(n);5.17#include void reverse() char ch= getchar_r(); if(ch!=n) rever

26、se(); putchar(ch); int main() reverse(); printf(n); return 0;第六章习题6.1(1)正确(2)错误,不需要加“;”(3)错误,“ident”与“(”之间不能有空格(4)错误,宏名不能是关键字“void”(5)错误,将x*y改成(x)*(y)6.4将会导致变量blue的重复定义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

27、#endif(6)#ifndef SIZE assert(0);#elseassert(SIZE1);#endif(7)#define SQUARE_VOLUME(x) (x)*(x)*(x)6.10#include #define pi 3.1415926#define BALL_VOLUME(r) (4/3)*pi*(r)*(r)*(r)int main()int r;float v11;for(r=1;r11;r+)vr=float(BALL_VOLUME(r);printf(r=%2d v=%.2fn,r,vr);return 0;第七章习题7.9#include#include#de

28、fine g 10void main()char *buffer;int gdriver=DETECT,gmode,i,size;initgraph(&gdriver,&gmode,c:tcbgi);setbkcolor(BLUE);setcolor(RED);setlinestyle(0,0,1);setfillstyle(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

29、(200,250+g*i*i/2,buffer,COPY_PUT);getch_r();closegraph();7.11#include#define RAND_MAX 32767#define RAND 100int RandomInteger(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=tai

30、ls;srand(time(0);for(i=0;iRAND;i+) szWordi=RandomInteger(0,1); if(szWordi=1) printf(n%s,a); n+; else printf(n%s,b); n=0; if(n=3) printf(nIt took %ld flips to get heads 3 consecutives times,i+1); break; 7.16char *MonthName(int month);int MonthDays(int month,int year);int FirstDayOfMonth(int month,int

31、 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: return(“April”);Case 5: return(“May”);Case 6: return(“Ju

32、ne”);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(“input 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

33、: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;i2000)For (i=2000;iyear;i+)Total+=(IsLeapYear(i)?366:365);For(i=1;imonth;i-)Total+=( MonthDays(i, 1999);Return(13-total%7)%7);ElseFor (i=1999;imon

34、th;i-)Total+=( MonthDays(i, year);Return(13-total%7)%7);int IsLeapYear(int year)Return ( !(year%4)&(year%400) | !(year%400) );#include #include “caltools.h”Void main()Int month,year;Printf(“please input the month and year:”);Scanf(“%d%d” ,&month,&year);Printf(“the name of the month is %sn”, MonthNam

35、e( 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);第八章习题8.4#include stdafx.h#include malloc.h#define N 65535void DelSpace(char sa);int main(int argc, char* argv) char saN; char c; int i

36、 =0; do c = getchar_r(); if(c = 13) sai+ = n; else sai+ = c; while(c!=); DelSpace(sa); int j = 0; while(1) if(saj = ) break; printf(%c,saj+); printf(/n); return 0;void DelSpace(char sa) char *t1 = (char*)malloc(sizeof(sa); char *t2 = (char*)malloc(sizeof(sa); t1 = t2 = sa; while(*t1) *t2+ = *t1+; if

37、(*(t2-1)= & *t1= ) t2-; 还有一个方法:void DelSpace(char sa,int n) char* tmpbuf = (char*)malloc(sizeof(sa)+1); int p1 = 0, p2 = 0; bool bSpace = false; while(p1 n) if(bSpace & sap1 = ) p1+; else if(sap1 = ) bSpace = true; else bSpace = false; tmpbufp2+ = sap1+; strcpy(sa, tmpbuf); free( tmpbuf);8.7#include

38、 stdafx.h#define NUM 100struct 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 = 0;in;i+) scanf(%s,&stui.name); scanf(%d,&stui.mark); score

39、Sort(stu,n); int j = 0; while(jn) printf(name:%s score:%dn,stuj.name,stuj.mark); j+; return 0;void scoreSort(stuInfo *stu,int n)/n为学生数 for(int i= 0;in;i+) for(int j =i;jn;j+) if(stui.markstuj.mark) stuInfo temp; temp = stui; stui = stuj; stuj = temp; 8.9/ c84.cpp : Defines the entry point for the co

40、nsole application./#include stdafx.h#define NUM 100struct stuInfo char name10; int mark;stuNUM;void scoreSort(stuInfo stu,int n);int find(stuInfo *stu,int n,int score);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 = 0;i=0) printf(name:%s score:%dn,stuifind.name,stuifind.mark); else printf(Not Find); return 0;void scoreSort(stuInfo *stu,int n)/n为学生数 for(in

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!