谭浩强第二版习题答案,有需要的朋友可以下来参考参考8--10章
第八章编译预处理
8.1
#define SWAP(a,b) t=b;b=a;a=t
main()
{int a,b,t;
scanf("%d,%d",&a,&b);
SWAP(a,b);
printf("a=%d\tb=%d\n",a,b);
}
8.2
#define SURPLUS(a,b) ((a)%(b))
main()
{int a,b;
scanf("%d,%d",&a,&b);
printf("%d",SURPLUS(a,b));
}
8.3
#include"math.h"
#define S(a,b,c) ((a+b+c)/2)
#define AREA(a,b,c) (sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c)))
main()
{float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
if(a+b>c&&b+c>a&&a+c>b)
printf("area=%8.2f\n",AREA(a,b,c));
else
printf("input error");
}
8.4
#define LEAP(y) (y%4==0)&&(y%100!=0)||(y%400==0)
main()
{int year;
scanf("%d",&year);
if(LEAP(year))
printf("yes");
else
printf("no");
}
8.5
printf("value=%format\t",x);
printf("value=%format\t",x);putchar('\n');
printf("value=%format\t",x1);printf("value=%format\t",x2);putchar('\n');
输出结果:
value=5.000000ormat value=5.000000ormat
value=3.000000ormat value=8.000000ormat
8.6
#define PR printf
#define NL "\n"
#define Fs "%f"
#define F "%6.2f"
#define F1 F NL
#define F2 F"\t" F NL
#define F3 F"\t" F "\t" F NL
main()
{float a,b,c;
scanf(Fs,&a);
scanf(Fs,&b);
scanf(Fs,&c);
PR(NL);
PR(F1,a);
PR(F1,b);
PR(F1,c);
PR(NL);
PR(F2,a,b);
PR(F1,c);
PR(NL);
PR(F3,a,b,c);
}
8.7
/*format.h文件*/
#define DIGIT(d) printf("%d\n",d)
#define FLOAT(f) printf("%8.2f\n",f)
#define STRING(s) printf("%s\n",s)
/*用户文件*/
#include"format.h"
main()
{int d,num;
float f;
char s[80];
scanf("%d",&num);
switch(num)
{case 1:scanf("%d",&d);
DIGIT(d);
break;
case 2:scanf("%f",&f);
FLOAT(f);
break;
case 3:scanf("%s",s);
STRING(s);
break;
default:printf("error");
}
}
8.8
main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
printf("max=%d\n",max(a,b,c));
}
max(a,b,c)
int a,b,c;
{int t;
t=x>y?x:y;
return(t>z?t:z);
}
#define MAX(a,b) ((a)>(b)?(a):(b))
main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
printf("max=%d\n",MAX(MAX(a,b),c));
}
8.10
#include"stdio.h"
#define MAX 80
#define CHANGE 1
main()
{char str[MAX];
int i;
scanf("%s",str);
#if(CHANGE)
{for(i=0;i<MAX;i++)
{if(str[i]!='\0')
if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')
str[i]++;
elseif(str[i]=='z'||str[i]=='Z')
str[i]-=25;
}
}
#endif
printf("\n%s\n",str);
}
第九章
指针
9.1
main()
{int n1,n2,n3;
int *p1,*p2,*p3;
scanf("%d,%d,%d",&n1,&n2,&n3);
p1=&n1;
p2=&n2;
p3=&n3;
if(n1>n2)swap(p1,p2);
if(n1>n3)swap(p1,p3);
if(n2>n3)swap(p2,p3);
printf("%d,%d,%d\n",n1,n2,n3);
}
swap(p1,p2)
int *p1,*p2;
{int p;
p=*p1;*p1=*p2;*p2=p;
}
9.2
main()
{char *str1[20],*str2[20],*str3[20];
char swap();
scanf("%s",str1);
scanf("%s",str2);
scanf("%s",str3);
if(strcmp(str1,str2)>0)swap(str1,str2);
if(strcmp(str1,str3)>0)swap(str1,str3);
if(strcmp(str2,str3)>0)swap(str2,str3);
printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap(p1,p2)
char *p1,*p2;
{char *p[20];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
}
9.3
main()
{int number[10];
input(number);
max_min_value(number);
output(number);
}
input(number)
int number[10];
{int i;
for(i=0;i<10;i++)
scanf("%d",&number[i]);
}
max_min_value(number)
int number[10];
{int *max,*min;
int *p,*end;
end=number+10;
max=min=number;
for(p=number+1;p<end;p++)
if(*p>*max)max=p;
else if(*p<*min)min=p;
*p=number[0];
number[0]=*min;
*min=*p;
*p=number[9];
number[9]=*max;
*max=*p;
return;
}
output(number)
int number[10];
{int *p;
for(p=number;p<number+9;p++)
printf("%d,",*p);
printf("%d\n",*p);
}
9.4
main()
{int number[20],n,m,i;
scanf("%d",&n);
scanf("%d",&m);
for(i=0;i<n:i++)
scanf("%d",&number[i]);
move(number,n,m);
for(i=0;i<n;i++)
printf("%8d",number[i]);
}
move(array,n,m)
int array[20],n,m;
{int *p,end;
end=*(array+n-1);
for(p=array+n-1;p>array;p--)
*p=*(p-1);
*array=end;
m--;
if(m>0)move(array,n,m);
}
9.5
#define nmax 50
main()
{int i,k,m,n,num[nmax],*p;
scanf("%d",&n);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
i=k=m=0;
while(m<n-1)
{if(*(p+i)!=0)k++;
if(k==3)
{*(p+i)=0;
k=0;
m++;
}
i++;
if(i==n)i=0;
}
while(*p==0)p++;
printf("%d",*p);
}
9.6
main()
{int len;
char *str[20];
scanf("%s",str);
len=length(str);
printf("\nlen=%d\n",len);
}
length(p)
char *p;
{int n=0;
while(*p!='\0')
{n++;p++;}
return(n);
}
9.7
main()
{int m;
char *str1[20],*str2[20];
scanf("%s",str1);
scanf("%d",&m);
if(strlen(str1)<m)
printf("error");
else
{copystr(str1,str2,m);
printf("%s",str2);
}
}
copystr(p1,p2,m)
char *p1,*p2;
int m;
{int n=0;
while(n<m-1)
{n++;p1++;}
while(*p1!='\0')
{*p2=*p1;
p1++;
p2++;
}
*p2='\0';
}
9.8
#include"stdio.h"
main()
{int cle=0,sle=0,di=0,wsp=0,ot=0,i;
char *p,s[20];
for(i=0;i<20;i++)s[i]=0;
i=0;
while((s[i]=getchar())!='\n')i++;
p=s;
while(*p!='\n')
{if(*p>='a'&&*p<='z')
++sle;
else if(*p>='A'&&*p<='Z')
++cle;
else if(*p==' ')
++wsp;
else if(*p>='0'&&*p<='9')
++di;
else
++ot;
p++;
}
printf("sle=%d,cle=%d,wsp=%d,di=%d,ot=%d\n",sle,cle,wsp,di,ot);
}
9.9
main()
{int a[3][3],*p,i;
for(i=0;i<3;i++)
scanf("%d,%d,%d",a[i][0],a[i][1],a[i][2]);
p=a;
move(p);
for(i=0;i<3;i++)
printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);
}
move(pointer)
int *pointer;
{int i,j,t;
for(i=0;i<2;i++)
for(j=i+1;j<3;j++)
{t=*(pointer+3*i+j);
*(pointer+3*i+j)=*(pointer+3*j+i);
*(pointer+3*j+i)=t;
}
}
9.10
main()
{int a[5][5],*p,i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);
p=a;
change(p);
for(i=0;i<5;i++)
{printf("\n");
for(j=0;j<5;j++)
printf("%8d",a[i][j]);
}
}
change(p)
int *p;
{int i,j,change;
int *pmax,*pmin;
pmax=p;
pmin=p;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{if(*pmax<*(p+5*i+j))pmax=p+5*i+j;
if(*pmin>*(p+5*i+j))pmin=p+5*i+j;
}
change=*(p+12);
*(p+12)=*pmax;
*pmax=change;
change=*p;
*p=*pmin;
*pmin=change;
pmin=p+1;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;
change=*(p+4);
*(p+4)=*pmin;
*pmin=change;
pmin=p+1;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))
pmin=p+5*i+j;
change=*(p+20);
*(p+20)=*pmin;
*pmin=change;
pmin=p+1;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=(p+20))
&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;
change=*(p+24);
*(p+24)=*pmin;
*pmin=change;
}
9.11
main()
{int i;
char *p,str[10][10];
for(i=0;i<10;i++)
scanf("%s",str[i]);
p=str;
sort(p);
for(i=0;i<10;i++)
printf("%s\n",str[i]);
}
sort(p)
char *p;
{int i,j;
char s[10],*smax,*smin;
for(i=0;i<10;i++)
{smax=p+10*i;
for(j=i+1;j<10;j++)
{smin=p+10*j;
if(strcmp(smax,smin)>0)
{strcpy(s,smin);
strcpy(smin,smax);
strcpy(smax,s);
}
}
}
}
9.12
#define MAX 20
main()
{int i;
char *pstr[10],str[10][MAX];
for(i=0;i<10;i++)
pstr[i]=str[i];
for(i=0;i<10;i++)
scanf("%s",pstr[i]);
sort(pstr);
for(i=0;i<10;i++)
printf("%s\n",pstr[i]);
}
sort(pstr)
char *pstr[10];
{int i,j;
char *p;
for(i=0;i<10;i++)
{for(j=i+1;j<10;j++)
{if(strcmp(*(pstr+i),*(pstr+j))>0)
{p=*(pstr+i);
*(pstr+i)=*(pstr+j);
*(pstr+j)=p;
}
}
}
}
9.13
#include"math.h"
main()
{int n=20;
float a,b,a1,b1,a2,b2,c,(*p)(),jiff();
scanf("%f,%f",&a,&b);
scanf("%f,%f",&a1,&b1);
scanf("%f,%f",&a2,&b2);
p=sin;
c=jiff(a,b,n,p);
printf("sin=%f\n",c);
p=cos;
c=jiff(a1,b1,n,p);
printf("cos=%f\n",c);
p=exp;
c=jiff(a2,b2,n,p);
printf("exp=%f\n",c);
}
float jiff(a,b,n,p)
float a,b,(*p)();
int n;
{int i;
float x,f,h,area;
h=(b-a)/n;
x=a;
area=0;
for(i=1;i<=n;i++)
{x=x+h;
area=area+(*p)(x)*h;
}
return(area);
}
9.14
main()
{int i,n,num[20];
char *p;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
p=num;
sort(p,n);
for(i=0;i<n;i++)
printf("%8d",num[i]);
}
sort(p,m)
char *p;
int m;
{int i;
char change,*p1,*p2;
for(i=0;i<m/2;i++)
{p1=p+i;
p2=p+(m-1-i);
change=*p1;
*p1=*p2;
*p2=change;
}
}
9.15
main()
{int i,j,*pnum,num[4];
float score[4][5],aver[4],*psco,*pave;
char course[5][10],*pcou;
pcou=course[0];
for(i=0;i<5;i++)
scanf("%s",pcou+10*i);
printf("number");
for(i=0;i<5;i++)
printf(",%s",pcou+10*i);
printf("\n");
psco=score;
pnum=num;
for(i=0;i<4;i++)
{scanf("%d",pnum+i);
for(j=0;j<5;j++)
scanf(",%f",psco+5*i+j);
}
pave=aver;
printf("\n");
avsco(psco,pave);
avcour1(pcou,psco);
printf("\n");
fali2(pcou,pnum,psco,pave);
printf("\n");
good(pcou,pnum,psco,pave);
}
avsco(psco,pave)
float *psco,*pave;
{int i,j;
float sum,average;
for(i=0;i<4;i++)
{sum=0;
for(j=0;j<5;j+)
sum+=(*(psco+5*i+j));
average=sum/5;
*(pave+i)=average;
}
}
avcour1(pcou,psco)
char *pcou;
float *psco;
{int i;
float sum,average1;
sum=0;
for(i=0;i<4;i++)
sum+=(*(psco+5*i))
average1=sum/4;
printf("%s %5.2f\n",pcou,average1);
}
fali2(pcou,pnum,psco,pave)
char *pcou;
int *pnum;
float *psco,*pave;
{int i,j,k,label;
printf("\nnumber\n");
for(i=0;i<5;i++)
printf("%-8s",pcou+10*i);
printf("\naverage\n");
for(i=0;i<4;i++)
{label=0;
for(j=0;j<5;j++)
if(*(psco+5*i+j)<60.0)label++;
if(label>=2)
{printf("%-8d",*(pnum+i));
for(k=0;k<5;k++)
printf("%-8.2f",*(psco+5*i+k));
printf("%-8.2f",*(pave+i));
}
}
}
good(pcou,pnum,psco,pave)
char *pcou;
int *pnum;
float *psco,*pave;
{int i,j,k,label;
printf("number");
for(i=0;i<5;i++)
printf("%-8s",pcou+10*i);
printf("average");
for(i=0;i<4;i++)
{label=0;
for(j=0;j<5;j++)
if(*(psco+5*i+j)>=85.0)label++;
if((label>=5)||(*(pave+i)>=90))
{printf("%-8d",*(pnum+i));
for(k=0;k<5;k++)
printf("%-8.2f",*(psco+5*i+k));
printf("%-8.2f",*(pave+i));
}
}
}
9.16
#include"stdio.h"
main()
{char str[50],*pstr;
int i,j,k,m,e10,digit,ndigit,a[10],*pa;
gets(str);
pstr=str;
pa=a;
ndigit=0;
i=j=0;
while(*(pstr+i)!='\0')
{if((*(pstr+i)>='0')&&(*(pstr+i)<='9'))
j++;
else
{if(j>0)
{digit=*(pstr+i-1)-48;
k=1;
while(k<j)
{e10=1;
for(m=1;m<=k;m++)
e10=e10*10;
digit+=(*(pstr+i-1-k)-48)*e10;
k++;
}
*pa=digit;
ndigit++;
pa++;
j=0;
}
}
i++;
}
if(j>0)
{digit=*(pstr+i-1)-48;
k=1;
while(k<j)
{e10=1;
for(m=1;m<=k;m++)
e10=e10*10;
digit+=(*(pstr+i-1-k)-48)*e10;
k++;
}
*pa=digit;
ndigit++;
j=0;
}
printf("ndigit=%d\n",ndigit);
j=0;
pa=a;
for(j=0;j<ndigit;j++)
printf("%d",*(pa+j));
}
9.17
main()
{int m;
char str1[20],str2[20],*p1,*p2;
scanf("%s",str1);
scanf("%s",str2);
p1=str1;
p2=str2;
m=strcmp(p1,p2);
printf("%d\n",m);
}
strcmp(p1,p2)
char *p1,*p2;
{int i=0;
while(*(p1+i)==*(p2+i))
if(*(p+i++)=='\0')return(0);
return(*(p1+i)-*(p2+i));
}
9.18
main()
{static char*mname[13]={"illeagl","January","February","March",
"April","May","June","July","August","September","October",
"November","December"};
int n;
scanf("%d",&n);
if((n>=1)&&(n<=12))
printf("%s\n",*(mname+n));
else
printf("error");
}
9.20
main()
{int i;
char **p,*pstr[5],str[5][10];
for(i=0;i<5;i++)
pstr[i]=str[i];
for(i=0;i<5;i++)
scanf("%s",pstr[i]);
p=pstr;
sort(p);
for(i=0;i<5;i++)
printf("%s\n",pstr[i]);
}
sort(p)
char **P;
{int i,j;
char *pchange;
for(i=0;i<5;i++)
{for(j=i+1;j<5;j++)
{if(strcmp(*(p+i),*(p+j))>0)
{pchange=*(p+i);
*(p+i)=*(p+j);
*(p+j)=pchange;
}
}
}
}
9.21
main()
{int i,n,digit[20],**p,*pstr[20];
scanf("%d",&n);
for(i=0;i<n;i++)
pstr[i]=&digit[i];
for(i=0;i<n;i++)
scanf("%d",pstr[i]);
p=pstr;
sort(p,n);
for(i=0;i<n;i++)
printf("%d ",*pstr[i]);
}
sort(p,n)
int **p,n;
{int i,j,*pchange;
for(i=0;i<n;i++)
{for(j=i+1;j<n;j++)
{if(**(p+i)>**(p+j))
{pchange=*(p+i);
*(p+i)=*(p+j);
*(p+j)=pchange;
}
}
}
}
第十章
结构体与共用体
10.1
struct
{int year;
int month;
int day;
}date;
main()
{int days;
scanf("%d,%d,%d",&date.year,&date.month,&date.day);
switch(date.month)
{case 1:days=date.day;break;
case 2:days=date.day+31;break;
case 3:days=date.day+59;break;
case 4:days=date.day+90;break;
case 5:days=date.day+120;break;
case 6:days=date.day+151;break;
case 7:days=date.day+181;break;
case 8:days=date.day+212;break;
case 9:days=date.day+243;break;
case 10:days=date.day+273;break;
case 11:days=date.day+304;break;
case 12:days=date.day+334;break;
}
if((date.year%4==0&&date.year%100!=0||date.year%400==0)
&&date.month>=3)
days+=1;
printf("days=%d\n",days);
}
10.2
struct dt
{int year;
int month;
int day;
}date;
main()
{
scanf("%d,%d,%d",&date.year,&date.month,&date.day);
printf("\n%d\n",days(date.year,date.month,date.day));
}
days(year,month,day)
int year,month,day;
{int daysum=0,i;
static int daytab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}
for(i=1;i<month;i++)
daysum+=daytab[i];
daysum+=day;
if((year%4==0&&year%100!=0||year%400==0)&&month>=3)
daysum+=1;
return(daysum);
}
10.3
10.4
#define N 5
struct student
{char num[6];
char name[8];
int score[4];
}stu[N];
main()
{
input(stu);
print(stu);
}
input(stu)
struct student stu[];
{int i,j;
for(i=0;i<N;i++)
{printf("number");
scanf("%s",stu[i].num);
printf("name");
scanf("%s",stu[i].name);
for(j=0;j<3;j++)
{printf("\nscore\n");
scanf("%d",&stu[i].score[j]);
}
printf("\n");
}
}
print(stu)
struct student stu[];
{int i,j;
printf("\nnumber name score1 score2 score3 \n");
for(i=0;i<N;i++)
{printf("%8s%10s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%7d",stu[i].score[j]);
printf("\n");
}
}
10.5
struct student
{char num[6];
char name[8];
int score[4];
float avr;
}stu[5];
main()
{int i,j,max,maxi,sum;
float average;
for(i=0;i<5;i++)
{printf("number");
scanf("%s",stu[i].num);
printf("name");
scanf("%s",stu[i].name);
for(j=0;j<3;j++)
{printf("\nscore\n");
scanf("%d",&stu[i].score[j]);
}
}
average=0;
max=0;
maxi=0;
for(i=0;i<5;i++)
{sum=0;
for(j=0;j<3;j++)
sum+=stu[i].score[j];
stu[i].avr=sum/3.0;
average+=stu[i].avr;
if(sum>max)
{max=sum;
maxi=i;
}
}
average/=5;
printf("number name score1 score2 score3 average\n");
for(i=0;i<5;i++)
{printf("%8s%10s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%7d",stu[i].score[j]);
printf("%6.2f\n",stu[i].avr);
}
printf("average=%5.2f\n",average);
printf("The best student is %s,sum=%d\n",stu[maxi].name,max);
}