曹计昌《C程序设计》课后习题答案

  • 格式:doc
  • 大小:62.50 KB
  • 文档页数:14

第一章习题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位机中,[157]补= 11101[-153]补= 1111111101100111157-153=157+(-153)= 11101) 2+(1111111101100111) 2=00100) 2=(4) 101.14算法设计:用变量s存储累加和,k表示计数描述为:(1)定义变量s,k。

(2)s清零,k赋初值1。

(3)判断k<101?如果是,顺序执行(4);否则转步骤(5);(4)k加到累加和变量s中,k加1;转步骤(3)。

(5)输出累加和s。

(6)结束。

开始结束int s=0,k=1;k<101?s=s+k;k=k+1;输出sNY1.16第二章习题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不是表识符的如下:4th 首字母为数字 sizeof关键字x*y *不是字母、数字、下划线isn’t ’不是字母、数字、下划线enum 关键字2.4合法常数:.12 0.L 1.E-5 3.F 浮点型常量2L 33333 0377UL 0x9cfU 整型常量“a”“”字符串常量‘\45’‘\0’‘\a’字符常量非法常数:‘‘’必须用转义序列0x1ag 十六进制没有gE20 没有尾数部分‘\18’要用八进制数‘\0xa’格式错误,可以是’\xa’“3’4””需要转义序列‘”’需要转义序列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(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 表达式缺值(6)a||b^i ^运算的操作数必须是整型,而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(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;x3=(x & 0x0000F000);x4=(x & 0x000F0000) << 8;x5=(x & 0x00F00000);x6=(x & 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);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)& 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>>n2.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 printf(const char *format,…);getchar函数的原型为:int getchar_r(void);scanf函数的原型为: int scanf(const char *format,…);3.2不同点:① puts为非格式输出函数,printf为格式输出函数;② puts函数的参数类型和数目一定(一个字符串),printf函数的参数类型和数目不固定;③ puts函数输出后会自动换行,printf函数没有这一功能。

相同点:①二者都向标准设备输出;②二者返回值类型都为int。

3.3 x1=-1,177777,ffff,65535x2=-3,177775,fffd,65533y1=123.456703, 123.457,123.457,123.457 (注意对齐)y2=123.449997,1.23450e+02,123.45x1(%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-6177601123-2 03.8void main(){char c;c= getchar_r();if((c>='0'&&c<='9')||(c>='A'&&c<='F')||(c>='a'&&c<='f')){if((c>='0'&&c<='9')){printf("%d\n",c-'0');}else if((c>='A'&&c<='F')){printf("%d\n",c-'A'+10);}elseprintf("%d\n",c-'a'+10);}elseputchar(c);}3.9#include<stdio.h>void main(){short num,high,low;printf("Please input a short number:\n");scanf("%hd",&num);high = 0x00ff & (num >> 8);printf("The high byte is:%c\n", high);printf("The low byte is:%c\n", 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",&x);high = (x>>12)&0x000f;low = (x<<12)&0xf000;x= x&0x0ff0;x=x|high|low;printf("%d\n",x);return 0;}3.11#include<stdio.h>void main(){unsigned short int x,m,n;scanf("%hu%hu%hu",&x,&m,&n);result=(x>>(m-n+1))<<(15-n+1);printf("%hu\n",result);}3.12#include<stdio.h>void main(){float f,c;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;scanf("%d",&f);c=(5*(f-32))/9;printf("%d(F)=%.2f(C)\n",f,c); }3.13#include <stdio.h>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.h"int main(int argc, char* argv[]){char a[4] = "编";printf("机内码:%x%x\t\n",a[0]&0xff,a[1]&0xff);printf("区位码:%x\t\n",a[0]&0xff<<8+a[1]&0xff-0x2020-0x8080);printf("国际码:%x\t\n",a[0]&0xff<<8+a[1]&0xff-0x8080);return 0;}第四章习题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",&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 <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(){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(m>1){days+=mdays(y,m-1);m--;}printf("%d\n",days);}4.4 if方法:#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv[]){float x = 0;printf("input the salary\n");scanf("%f",&x);if(x<0)printf("wrong\n");else if(x>0 && x<1000)printf("0\n");else if(x<2000)printf("%f\n",x*0.05);else if(x<3000)printf("%f\n",x*0.1);else if(x<4000)printf("%f\n",x*0.15);else if(x<5000)printf("%f\n",x*0.2);elseprintf("%f\n",x*0.25);return 0;}Case方法:#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv[]){float x ;printf("input the salary\n");scanf("%f",&x);int xCase = 0;xCase = (int)(x/1000.0);switch(xCase){case 0:printf("0\n");break;case 1:printf("%f\n",x*0.05);break;case 2:printf("%f\n",x*0.1);break;case 3:printf("%f\n",x*0.15);break;case 4:printf("%f\n",x*0.2);break;default:printf("%f\n",x*0.25);}return 0;}4.7#include "stdafx.h"#include <stdio.h>int main(int argc, char* argv[]){char *sa;char c;int i = 0,j = 0,k = 0;do{c= getchar_r();sa[i++] = c;}while(c != '\r');for(i=0;sa[i+1];i++){for(j = i+1;sa[j];j++){if( sa[i]==sa[j] && sa[j] ==' '){for(k=j;sa[k];k++)sa[k] = sa[k+1];j--;}}}for(k=0;sa[k];k++)printf("%2c",sa[k]);return 0;}4.8#include<stdio.h>void main(void){char c;printf("Input the chars to be copy:\n");c=getchar();while(c!=EOF){if(c==32){c=getchar();continue;}else{putchar(c);c=getchar();}}}4.10#include <stdio.h>#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.6f\n",pi);}4.11#include<stdio.h>int main(){int a,b,num1,num2,temp;printf("Input a & b:");scanf("%d%d",&num1,&num2);if(num1>num2){temp=num1; num1=num2; num2=temp; }a=num1; b=num2;while(b!=0)。