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
2、) 161.10在 16 位机中,157 补 = 0000000010011101-153补 = 1111111101100111157-153=157+(-153)= (0000000010011101) 2+(1111111101100111) 2=(0000000000000100) 2=(4) 101.14算法设计:用变量 s 存储累加和,k 表示计数描述为:(1)定义变量 s,k。(2)s 清零,k 赋初值 1。(3)判断 k 24;x7=(x return(x0|x1|x2|x3|x4|x5|x6|x7);2.11#includevoid main()unsigned long i
3、n;unsigned long a,b,c,d;scanf(“%ld“,/in=1563;a=(inb=(inc=(ind=inprintf(“%d.%d.%d.%d“,a,b,c,d);2.15(k 8)max=a b ? (a c) ? a : c):(b c) ? b : c);2.17X=yn2.18(c=0 c= getchar_r();if(c=0printf(“Please input a short number:n“);scanf(“%hd“,low = 0x00ff high = 0x00ff printf(“The high byte is:%cn“, high);pri
4、ntf(“The low byte is:%cn“, low);3.10#include “stdafx.h“int main(int argc, char* argv)unsigned short int x;unsigned short int high,low;printf(“input a integer:n“);scanf(“%d“,high = (x12)low = (xvoid main()unsigned short int x,m,n;unsigned short int result;scanf(“%hu%hu%hu“,result=(x(m-n+1)void main()
5、float f,c;scanf(“%f“,c=(5*(f-32)/9;printf(“%.0f(F)=%.2f(C)n“,f,c);或者#includevoid main()int f; float c;scanf(“%d“,c=(5*(f-32)/9;printf(“%d(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“, s
6、= 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.h“int main(int argc, char* argv)char a4 = “编“;printf(“机内码:%x%xtn“,a0printf(“区位码:%xtn“,a0printf(“Please enter the score of A:n“);scanf(“%f“,printf(“Please enter the score of B:n“);sca
7、nf(“%f“,printf(“Please enter the score of C:n“);scanf(“%f“,if(a-b)*(a-c)int mdays(int y,int m)if (m=2) return (y%4=0 else 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“,printf(“Enter month:“);scanf(“%d“,printf(“Enter day:“);scanf(“%d“,days=
8、d;while(m1)days+=mdays(y,m-1);m-;printf(“%dn“,days);4.4 if 方法:#include “stdafx.h“#include int main(int argc, char* argv)float x = 0;printf(“input the salaryn“);scanf(“%f“,if(x0 printf(“input the salaryn“);scanf(“%f“,int xCase = 0;xCase = (int)(x/1000.0);switch(xCase)case 0:printf(“0n“);break;case 1:
9、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;default: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;doc= getchar_r();sai+ = c;while(c !=
10、 r);for(i=0;sai+1;i+)for(j = i+1;saj;j+)if( sai=saj sak;k+)sak = sak+1;j-;for(k=0;sak;k+)printf(“%2c“,sak);return 0;4.8#includevoid main(void)char c;printf(“Input the chars to be copy:n“);c=getchar();while(c!=EOF)if(c=32)c=getchar();continue;elseputchar(c);c=getchar();4.10#include #define EPS 1e-5vo
11、id 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 scanf(“%d%d“,if(num1num2)temp=num1; num1=num2; num2=temp;a=num1; b=num2;while(b!=0)temp=a%b;a=b;b=temp;printf(“The GCD of
12、%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;numvoid main(void)int c,i;for(i=1,c=32;cint main(int argc, char* argv)int x;int i,n,sum;printf(“input 10 nu
13、mbersn“);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 型外部变量的唯一区别是作用域的限制。静态外部变量只能作用于定义它的文件,其他文件中的函数不能使用。Extern 型外部变量的作用域可以扩大到整个程序的所有文件。Static 和 auto 存储类型的区别:静态局部变量和自动变量有根本性的区别。由于静态局部变量在程序执行期间不会
14、消失,因此,它的值有连续性。当退出块时,它的值能保存下来,以便再次进入块时使用,而自动变量的值在退出块时都丢失了。如果定义时静态局部变量有显示初始化,只在第一次进入时执行一次赋初值操作,而自动变量每次进入时都要执行赋初值操作。5.6不能。在 C 语言中,参数的传递方式是“值传递”,即把实参的值拷贝到参数的存储区中。因此, swap()函数交换的只是实参的本地拷贝,代表 swap()实参的变量并没有被改变。5.7 6,125.10 #include double sum_fac(int n)double s=0;int i;double fac=1.0;for(i=1;ivoid reverse
15、()char ch= getchar_r();if(ch!=n)reverse();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 TRU
16、E !=0#define FALSE 0#else#define FALSE 1#endif(6)#ifndef SIZEassert(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;r#include#define g 10void main()char *buffer;int gdrive
17、r=DETECT,gmode,i,size;initgraph(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#define RAND_MAX 32767#define RAND 100int RandomInteger(int low,int high) int k;dou
18、ble 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“;srand(time(0);for(i=0;i2000)For (i=2000;imonth;i-)Total+=( MonthDays(i, 1999);Return(13-total%7)%7);ElseFor (i=1999;imonth;i-)Total+=( MonthDays(i, y
19、ear);Return(13-total%7)%7);int IsLeapYear(int year)Return ( !(year%4)#include #include “caltools.h”Void main()Int month,year;Printf(“please input the month and year:”);Scanf(“%d%d” ,Printf(“the name of the month is %sn”, MonthName( month);Printf(“there are %d days in this month.n”, MonthDays( month,
20、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 =0;doc = getchar_r();if(c = 13)sai+ = n;elsesai+ = c;while(c!=);DelSp
21、ace(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(*(t2-1)= 还有一个方法:void DelSpace(char sa,int n)char* tmpbuf = (char*)malloc(sizeof(sa)+1);int p1 = 0, p2 = 0;bool bSpace = false;while(p1 =0)printf(“name:%s score:%dn“,stuifind.name,stuifind.mark);elseprintf(“Not Find“);return 0;void scoreSort(stuInfo *stu,int n)/n 为学生数for(int i= 0;istumid.mark)i = mid +1;else