看到了有人在网上发布了我希望得到的完美答案,我不由自主的转载了;
在此相与大家正在学习C的或将要学习C的朋友一份检验自己的答案。
:这是老版的答案,基本和现在的版本相同,章节标号不同,因为以前没有现在的第二章的内容
第一章
1.6
main()
{int a,b,c,max;
printf("input three numbers:\n");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(max<b)max=b;
if(max<c)max=c;
printf("max=%d",max);
}
第二章
2.3
(1)(10)10=(12)8=(a)16
(2)(32)10=(40)8=(20)16
(3)(75)10=(113)8=(4b)16
(4)(-617)10=(176627)8=(fd97)16
(5)(-111)10=(177621)8=(ff91)16
(6)(2483)10=(4663)8=(963)16
(7)(-28654)10=(110022)8=(9012)16
(8)(21003)10=(51013)8=(520b)16
2.6
aabb (8)cc (8)abc
(7)AN
2.7
main()
{char c1='C',c2='h',c3='i',c4='n',c5='a';
c1+=4, c2+=4, c3+=4, c4+=4, c5+=4;
printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);
}
2.8
main()
{int c1,c2;
c1=97;c2=98;
printf("%c %c",c1,c2);
}
2.9
(1)=2.5
(2)=3.5
2.10
9,11,9,10
2.12
(1)24 (2)10 (3)60 (4)0 (5)0 (6)0
第三章
3.4
main()
{int a,b,c;
long int u,n;
float x,y,z;
char c1,c2;
a=3;b=4;c=5;
x=1.2;y=2.4;z=-3.6;
u=51274;n=128765;
c1='a';c2='b';
printf("\n");
printf("a=%2d b=%2d c=%2d\n",a,b,c);
printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);
printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);
printf("u=%6ld n=%9ld\n",u,n);
printf("c1='%c'or %d(ASCII)\n",c1,c1);
printf("c2='%c'or %d(ASCII)\n",c2,c2);
}
3.5
57
5 7
67.856400,-789.123962
67.856400,-789.123962
67.86 -789.12,67.856400,-789.123962,67.856400,-789.123962
6.785640e+001,-7.89e+002
A,65,101,41
1234567,4553207,d687
65535,177777,ffff,-1
COMPUTER, COM
3.6
a=3 b=7/
x=8.5 y=71.82/
c1=A c2=a/
3.7
10 20Aa1.5 -3.75 +1.4,67.8/
(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车
3.8
main()
{float pi,h,r,l,s,sq,sv,sz;
pi=3.1415926;
printf("input r,h\n");
scanf("%f,%f",&r,&h);
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
sv=4.0/3.0*pi*r*r*r;
sz=pi*r*r*h;
printf("l=%6.2f\n",l);
printf("s=%6.2f\n",s);
printf("sq=%6.2f\n",sq);
printf("vq=%6.2f\n",sv);
printf("vz=%6.2f\n",sz);
}
3.9
main()
{float c,f;
scanf("%f",&f);
c=(5.0/9.0)*(f-32);
printf("c=%5.2f\n",c);
}
3.10
#include"stdio.h"
main()
{char c1,c2;
scanf("%c,%c",&c1,&c2);
putchar(c1);
putchar(c2);
printf("\n");
printf("%c%c\n",c1,c2);
}
第四章
4.3
(1)0 (2)1 (3)1 (4)0 (5)1
4.4
main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
if(a<b)
if(b<c)
printf("max=%d\n",c);
else
printf("max=%d\n",b);
else if(a<c)
printf("max=%d\n",c);
else
printf("max=%d\n",a);
}
main()
{int a,b,c,temp,max;
scanf("%d,%d,%d",&a,&b,&c);
temp=(a>b)?a:b;
max=(c>temp)?c:temp;
printf("max=%d",max);
}
4.5
main()
{int x,y;
scanf("%d",&x);
if(x<1)y=x;
else if(x<10)y=2*x-1;
else y=3*x-11;
printf("y=%d",y);
}
4.6
main()
{int score,temp,logic;
char grade;
logic=1;
while(logic)
{scanf("%d",&score);
if(score>=0&&score<=100)logic=0;
}
if(score==100)
temp=9;
else
temp=(score-score%10)/10;
switch(temp)
{case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:grade='E';
}
printf"score=%d,grade=%c",score,grade);
}
4.7
main()
{long int num;
int indiv,ten,hundred,thousand,ten_thousand,place;
scanf("%ld",&num);
if(num>9999) place=5;
else if(num>999) place=4;
else if(num>99) place=3;
else if(num>9) place=2;
else place=1;
printf("place=%d\n",place);
ten_thousand=num/10000;
thousand=(num-ten_thousand*10000)/1000;
hundred=(num-ten_thousand*10000-thousand*1000)/100;
ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;
switch(place)
{case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);
printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);
break;
case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);
printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d\n",hundred,ten,indiv);
printf("%d,%d,%d\n",indiv,ten,hundred);
break;
case 2:printf("%d,%d\n",ten,indiv);
printf("%d,%d\n",indiv,ten);
break;
case 1:printf("%d\n",indiv);
printf("%d\n",indiv);
}
}
4.8
main()
{long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
scanf("%ld",&i);
if(i<=1e5)bonus=i*0.1;
else if(i<=2e5)bonus=bon1+(i-100000)*0.075;
else if(i<=4e5)bonus=bon2+(i-200000)*0.05;
else if(i<=6e5)bonus=bon4+(i-400000)*0.03;
else if(i<=1e6)bonus=bon6+(i-600000)*0.015;
else bonus=bon10+(i-1000000)*0.01;
printf("bonus=%10.2f",bonus);
}
main()
{long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
scanf("%ld",&i);
branch=i/100000;
if(branch>10)branch=10;
switch(branch)
{case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3:bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5:bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7
case 8:
case 9:bonus=bon6+(i-600000)*0.015;break;
case 10:bonus=bon10+(i-1000000)*0.01;
}
printf("bonus=%10.2f",bonus);
}
4.9
main()
{int t,a,b,c,d;
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
if(a>b){t=a;a=b;b=t;}
if(a>c){t=a;a=c;c=t;}
if(a>d){t=a;a=d;d=t;}
if(b>c){t=b;b=c;c=t;}
if(b>d){t=b;b=d;d=t;}
if(c>d){t=c;c=d;d=t;}
printf("%d %d %d %d\n",a,b,c,d);
}
4.10
main()
{int h=10;
float x,y,x0=2,y0=2,d1,d2,d3,d4;
scanf("%f,%f",&x,&y);
d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);
d2=(x-x0)*(x-x0)+(y+y0)*(y+y0);
d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);
d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);
if(d1>1&&d2>1&&d3>1&&d4>1)h=0;
printf("h=%d",h);
}
第五章 循环控制
5.1
main()
{int a,b,num1,num2,temp;
scanf("%d,%d",&num1,&num2);
if(num1<num2){temp=num1;num1=num2;num2=temp;}
a=num1;b=num2;
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
5.2
#include"stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,other=0;
while((c=getchar())!='\n')
{if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++;
else if(c==' ')space++;
else if(c>='0'&&c<='9')digit++;
else other++;
}
printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other);
}
5.3
main()
{int a,n,count=1,sn=0,tn=0;
scanf("%d,%d",&a,&n);
while(count<=n)
{tn+=a;
sn+=tn;
a*=10;
++count;
}
printf("a+aa+aaa+...=%d\n",sn);
}
5.4
main()
{float n,s=0,t=1;
for(n=1;n<=20;n++)
{t*=n;
s+=t;
}
printf("s=%e\n",s);
}
5.5
main()
{int N1=100,N2=50,N3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k<=N1;k++)s1+=k;
for(k=1;k<=N2;k++)s2+=k*k;
for(k=1;k<=N3;k++)s3+=1/k;
printf("s=%8.2f\n",s1+s2+s3);
}
5.6
main()
{int i,j,k,n;
for(n=100;n<1000;n++)
{i=n/100;
j=n/10-i*10;
k=n%10;
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("n=%d\n",n);
}
}
5.7
#define M 1000
main()
{int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int i,j,n,s;
for(j=2;j<=M;j++)
{n=0;
s=j;
for(i=1;i<j;i++)
{if((j%i)==0)
{n++;
s=s-i;
switch(n)
{case 1:k0=i;break;
case 2:k1=i;break;
case 3:k2=i;break;
case 4:k3=i;break;
case 5:k4=i;break;
case 6:k5=i;break;
case 7:k6=i;break;
case 8:k7=i;break;
case 9:k8=i;break;
case 10:k9=i;break;
}
}
}
if(s==0)
{printf("j=%d\n",j);
if(n>1)printf("%d,%d",k0,k1);
if(n>2)printf(",%d",k2);
if(n>3)printf(",%d",k3);
if(n>4)printf(",%d",k4);
if(n>5)printf(",%d",k5);
if(n>6)printf(",%d",k6);
if(n>7)printf(",%d",k7);
if(n>8)printf(",%d",k8);
if(n>9)printf(",%d\n",k9);
}
}
}
main()
{static int k[10];
int i,j,n,s;
for(j=2;j<=1000;j++)
{n=-1;
s=j;
for(i=1;i<j;i++)
{if((j%i)==0)
{n++;
s=s-i;
k[n]=i;
}
}
if(s==0)
{printf("j=%d\n",j);
for(i=0;i<n;i++)
printf("%d,",k[i]);
printf("%d\n",k[n]);
}
}
}
5.8
main()
{int n,t,number=20;
float a=2;b=1;s=0;
for(n=1;n<=number;n++)
{s=s+a/b;
t=a,a=a+b,b=t;
}
printf("s=%9.6f\n",s);
}
5.9
main()
{float sn=100.0,hn=sn/2;
int n;
for(n=2;n<=10;n++)
{sn=sn+2*hn;
hn=hn/2;
}
printf("sn=%f\n",sn);
printf("hn=%f\n",hn);
}
5.10
main()
{int day,x1,x2;
day=9;
x2=1;
while(day>0)
{x1=(x2+1)*2;
x2=x1;
day--;
}
printf("x1=%d\n",x1);
}
5.11
#include"math.h"
main()
{float a,xn0,xn1;
scanf("%f",&a);
xn0=a/2;
xn1=(xn0+a/xn0)/2;
do
{xn0=xn1;
xn1=(xn0+a/xn0)/2;
}
while(fabs(xn0-xn1)>=1e-5);
printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1);
}
5.12
#include"math.h"
main()
{float x,x0,f,f1;
x=1.5;
do
{x0=x;
f=((2*x0-4)*x0+3)*x0-6;
f1=(6*x0-8)*x0+3;
x=x0-f/f1;
}
while(fabs(x-x0)>=1e-5);
printf("x=%6.2f\n",x);
}
5.13
#include"math.h"
main()
{float x0,x1,x2,fx0,fx1,fx2;
do
{scanf("%f,%f",&x1,&x2);
fx1=x1*((2*x1-4)*x1+3)-6;
fx2=x2*((2*x2-4)*x2+3)-6;
}
while(fx1*fx2>0);
do
{x0=(x1+x2)/2;
fx0=x0*((2*x0-4)*x0+3)-6;
if((fx0*fx1)<0)
{x2=x0;
fx2=fx0;
}
else
{x1=x0;
fx1=fx0;
}
}
while(fabs(fx0)>=1e-5);
printf("x0=%6.2f\n",x0);
}
5.14
main()
{int i,j,k;
for(i=0;i<=3;i++)
{for(j=0;j<=2-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=2;i++)
{for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<=4-2*i;k++)
printf("*");
printf("\n");
}
}
5.15
main()
{char i,j,k;
for(i='x';i<='z';i++)
for(j='x';j<='z';j++)
{if(i!=j)
for(k='x';k<='z';k++)
{if(i!=k&&j!=k)
{if(i!='x'&&k!='x'&&k!='z')
printf("\na--%c\tb--%c\tc--%c\n",i,j,k);
}
}
}
}
第六章 数组
6.1
#include <math.h>
#define N 101
main()
{ int i,j,line,a[N];
for (i=2;i<N;i++) a[i]=i;
for (i=2;i<sqrt(N);i++)
for (j=i+1;j<N;j++)
{if(a[i]!=0 && a[j]!=0)
if (a[j]%a[i]==0)
a[j]=0; }
printf("\n");
for (i=2,line=0;i<N;i++)
{ if(a[i]!=0)
{ printf("%5d",a[i]);
line++; }
if(line==10)
{ printf("\n");
line=0; }
}
}
6.2
#define N 10
main()
{int i,j,min,temp,a[N];
for(i=0;i<N;i++)
scanf("%d",&a[i]);
for(i=0;i<N-1;i++)
{min=i;
for(j=i+1;j<N;j++)
if(a[min]>a[j])min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(i=0;i<N;i++)
printf("%5d",a[i]);
}
6.3
main()
{float a[3][3],sum;
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{scanf("%f",&sum);
a[i][j]=sum;
}
for(i=0;i<3;i++)
sum=sum+a[i][i];
printf("sum=%f",sum);
}
6.4
main()
{int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number, end,i,j;
scanf("%d",&number);
end=a[9];
if(number>end) a[10]=number;
else
{for(i=0;i<10;i++)
{if(a[i]>number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("%6d",a[i]);
}
6.5
#define N 5
main()
{int a[N]={8,6,5,4,1},i,temp;
for(i=0;i<N/2;i++)
{temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
for(i=0;i<N;i++)
printf("%4d",a[i]);
}
6.6
#define N 11
main()
{int i,j,a[N][N];
for(i=1;i<N;i++)
{a[i][i]=1;
a[i][1]=1;
}
for(i=3;i<N;i++)
for(j=2;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=1;i<N;i++)
{for(j=1;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n");
}
}
6.7
main()
{int a[16][16],i,j,k,p,m,n;
p=1;
while(p==1)
{scanf("%d",&n);
if((n!=0)&&(n<=15)&&(n%2!=0))p=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=0;
j=n/2+1;
a[1][j]=1;
for(k=2;k<=n*n;k++)
{i=i-1;
j=j+1;
if((i<1)&&(j>n))
{i=i+2;
j=j-1;
}
else
{if(i<1)i=n;
if(j>n)j=1;
}
if(a[i][j]==0)a[i][j]=k;
else
{i=i+2;
j=j-1;
a[i][j]=k;
}
}
for(i=1;i<=n;i++)
{for(j=1;j<=n;j++)
printf("%3d",a[i][j]);
printf("\n");
}
}
6.8
#define N 10
#define M 10
main()
{int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;
scanf("%d,%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
flag2=0;
for(i=0;i<n;i++)
{max=a[i][0];
for(j=0;j<m;j++)
if(max<a[i][j])
{max=a[i][j];
maxj=j;
}
for(k=0,flag1=1;k<n&&flag1;k++)
if(max>a[k][maxj])flag1=0;
if(flag1)
{ printf("\na[%d][%d]=%d\n",i,maxj,max);
flag2=1;
}
}
if(!flag2) printf("NOT");
}
6.9
#include<stdio.h>
#define N 15
main()
{int i,j,number,top,bott,min,loca,a[N],flag;
char c;
for(i=0;i<=N;i++)
scanf("%d",&a[i]);
flag=1;
while(flag)
{scanf("%d",&number);
loca=0;
top=0;
bott=N-1;
if((number<a[0])||(number>a[N-1]))
loca=-1;
while((loca==0)&&(top<=bott))
{min=(bott+top)/2;
if(number==a[min])
{loca=min;
printf("number=%d,loca=%d\n",number,loca+1);
}
else if(number<a[min])
bott=min-1;
else
top=min+1;
}
if(loca==0||loca==-1)
printf("%d not in table\n",number);
printf("continue Y/N or y/n\n");
c=getchar();
if(c=='N'||c=='n')flag=0;
}
}
6.10
main()
{int i,j,uppn,lown,dign,span,othn;
char text[3][80];
uppn=lown=dign=span=othn=0;
for(i=0;i<3;i++)
{gets(text[i]);
for(j=0;j<80&&text[i][j]!='\0';j++)
{if(text[i][j]>='A'&&text[i][j]<='Z')
uppn++;
else if(text[i][j]>='a'&&text[i][j]<='z')
lown++;
else if(text[i][j]>='0'&&text[i][j]<='9')
dign++;
else if(text[i][j]==' ')
span++;
else
othn++;
}
}
for(i=0;i<3;i++)
printf("%s\n",text[i]);
printf("uppn=%d\n",uppn);
printf("lown=%d\n",lown);
printf("dign=%d\n",dign);
printf("span=%d\n",span);
printf("othn=%d\n",othn);
}
6.11
main()
{static char a[5]={'*','*','*','*','*'};
int i,j,k;
char space=' ';
for(i=0;i<=5;i++)
{printf("\n");
for(j=1;j<=3*i;j++)
printf("%1c",space);
for(k=0;k<=5;k++)
printf("%3c",a[k]);
}
}
6.12
#include<stdio.h>
main()
{int i,n;
char ch[80],tran[80];
gets(ch);
i=0;
while(ch[i]!='\0')
{if((ch[i]>='A')&&(ch[i]<='Z'))
tran[i]=26+64-ch[i]+1+64;
else if((ch[i]>='a')&&(ch[i]<='z'))
tran[i]=26+96-ch[i]+1+96;
else
tran[i]=ch[i];
i++;
}
n=i;
for(i=0;i<n;i++)
putchar(tran[i]);
}
6.13
main()
{char s1[80],s2[40];
int i=0,j=0;
scanf("%s",s1);
scanf("%s",s2);
while(s1[i]!='\0')i++;
while(s2[j]!='\0')s1[i++]=s2[j++];
s1[i]='\0';
printf("s=%s\n",s1);
}
6.14
#include<stdio.h>
main()
{int i,resu;
char s1[100],s2[100];
gets(s1);
gets(s2);
i=0;
while((s1[i]==s2[i])&&(s1[i]!='\0'))i++;
if(s1[i]=='\0'&&s2[i]=='\0')resu=0;
else
resu=s1[i]-s2[i];
printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu);
}
6.15
#include"stdio.h"
main()
{char from[80],to[80];;
int i;
scanf("%s",from);
for(i=0;i<=strlen(from);i++)
to[i]=from[i];
printf("%s\n",to);
}
第七章
7.1
hcf(u,v)
int u,v;
{int a,b,t,r;
if(u>v){t=u;u=v;v=t;}
a=u;b=v;
while((r=b%a)!=0)
{b=a;a=r;}
return(a);
}
lcd(u,v,h)
int u,v,h;
{return(u*v/h);}
main()
{int u,v,h,l;
scanf("%d,%d",&u,&v);
h=hcf(u,v);
printf("H.C.F=%d\n",h);
l=lcd(u,v,h);
printf("L.C.D=%d\n",l);
}
7.2
#include"math.h"
float x1,x2,disc,p,q;
greater_than_zero(a,b)
float a,b;
{x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}
equal_to_zero(a,b)
flaot a,b;
{x1=x2=-b/(2*a);}
smaller_than_zero(a,b)
float a,b;
{p=-b/(2*a);
q=sqrt(-disc)/(2*a);
}
main()
{float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(fabs(disc)<=1e-5)
{equal_to_zero(a,b);
printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);
}
else if(disc>0)
{greater_than_zero(a,b);
printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);
}
else
{smaller_than_zero(a,b);
printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q);
}
}
7.3
main()
{int number;
scanf("%d",&number);
if(prime(number))
printf("yes");
else
printf("no");
}
int prime(number)
int number;
{int flag=1,n;
for(n=2;n<number/2&&flag==1;n++)
if(number%n==0)
flag=0;
return(flag);
}
7.4
#define N 3
int array[N][N];
convert(array)
int array[3][3];
{int i,j,t;
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{t=array[i][j];
array[i][j]=array[j][i];
array[j][i]=t;
}
}
main()
{int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&array[i][j]);
convert(array);
for(i=0;i<N;i++)
{printf("\n");
for(j=0;j<N;j++)
printf("%5d",array[i][j]);
}
}
7.5
main()
{char str[100];
scanf("%s",str);
inverse(str);
printf("%s\n",str);
}
inverse(str)
char str[];
{char t;
int i,j;
for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--)
{t=str[i];
str[i]=str[j-1];
str[j-1]=t;
}
}
7.6
char concate(str1,str2,str)
char str1[],str2[],str[];
{int i,j;
for(i=0;str1[i]!='\0';i++)
str[i]=str1[i];
for(j=0;str2[j]!='\0';j++)
str[i+j]=str2[j];
str[i+j]='\0';
}
main()
{char s1[100],s2[100],s[100];
scanf("%s",s1);
scanf("%s",s2);
concate(s1,s2,s);
printf("\ns=%s",s);
}
7.7
main()
{char str[80],c[80];
void cpy();
gets(str);
cpy(str,c);
printf("\n%s\n",c);
}
void cpy(s,c)
char s[],c[];
{int i,j;
for(i=0,j=0;s[i]!='\0';i++)
if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||
s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
{c[j]=s[i];j++;}
c[j]='\0';
}
7.8
main()
{char str[80];
scanf("%s",str);
insert(str);
}
insert(str)
char str[];
{int i;
for(i=strlen(str);i>0;i--)
{str[i*2]=str[i];
str[i*2-1]=' ';
}
printf("%s\n",str);
}
7.9
int alph,digit,space,others;
main()
{char text[80];
gets(text);
alph=0,digit=0,space=0,others=0;
count(text);
printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others);
}
count(str)
char str[];
{int i;
for(i=0;str[i]!='\0';i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
alph++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(strcmp(str[i],' ')==0)
space++;
else
others++;
}
7.10
int alph(c)
char c;
{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return(1);
else
return(0);
}
int longest(string)
char string[];
{int len=0,i,length=0,flag=1,place,point;
for(i=0;i<=strlen(string);i++)
if(alph(string[i]))
if(flag)
{point=i;
flag=0;
}
else
len++;
else
{flag=1;
if(len>length)
{length=len;
place=point;
len=0;
}
}
return(place);
}
main()
{int i;
char line[100];
gets(line);
for(i=longest(line);alph(line[i]);i++)
printf("%c",line[i]);
printf("\n");
}
7.11
#define N 10
char str[N];
main()
{int i,flag;
for(flag=1;flag==1;)
{scanf("%s",str);
if(strlen(str)>N)
printf("input error");
else
flag=0;
}
sort(str);
for(i=0;i<N;i++)
printf("%c",str[i]);
}
sort(str)
char str[N];
{int i,j;
char t;
for(j=1;j<N;j++)
for(i=0;(i<N-j)&&(str[i]!='\0');i++)
if(str[i]>str[i+1])
{t=str[i];
str[i]=str[i+1];
str[i+1]=t;
}
}
7.12
#include<math.h>
float solut(a,b,c,d)
float a,b,c,d;
{float x=1,x0,f,f1;
do
{x0=x;
f=((a*x0+b)*x0+c)*x0+d;
f1=(3*a*x0+2*b)*x0+c;
x=x0-f/f1;
}
while(fabs(x-x0)>=1e-5);
return(x);
}
main()
{float a,b,c,d;
scanf("%f,%f,%f,%f",&a,&b,&c,&d);
printf("x=%10.7f\n",solut(a,b,c,d));
}
7.13
main()
{int x,n;
float p();
scanf("%d,%d",&n,&x);
printf("P%d(%d)=%10.2f\n",n,x,p(n,x));
}
float p(tn,tx)
int tn,tx;
{if(tn==0)
return(1);
else if(tn==1)
return(tx);
else
return(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn);
}
7.14
#define N 10
#define M 5
float score[N][M];
float a_stu[N],a_cor[M];
main()
{int i,j,r,c;
float h;
float s_diff();
float highest();
r=0;
c=1;
input_stu();
avr_stu();
avr_cor();
printf("\n number class 1 2 3 4 5 avr");
for(i=0;i<N;i++)
{printf("\nNO%2d",i+1);
for(j=0;j<M;j++)
printf("%8.2f",score[i][j]);
printf("%8.2f",a_stu[i]);
}
printf("\nclassavr");
for(j=0;j<M;j++)
printf("%8.2f",a_cor[j]);
h=highest(&r,&c);
printf("\n\n%8.2f %d %d\n",h,r,c);
printf("\n %8.2f\n",s_diff());
}
input_stu()
{int i,j;
float x;
for(i=0;i<N;i++)
{for(j=0;j<M;j++)
{scanf("%f",&x);
score[i][j]=x;
}
}
}
avr_stu()
{int i,j;
float s;
for(i=0;i<N;i++)
{for(j=0,s=0;j<M;j++)
s+=score[i][j];
a_stu[i]=s/5.0;
}
}
avr_cor()
{int i,j;
float s;
for(j=0;j<M;j++)
{for(i=0,s=0;i<N;i++)
s+=score[i][j];
a_cor[j]=s/(float)N;
}
}
float highest(r,c)
int *r,*c;
{float high;
int i,j;
high=score[0][0];
for(i=0;i<N;i++)
for(j=0;j<M;j++)
if(score[i][j]>high)
{high=score[i][j];
*r=i+1;
*c=j+1;
}
return(high);
}
float s_diff()
{int i,j;
float sumx=0.0,sumxn=0.0;
for(i=0;i<N;i++)
{sumx+=a_stu[i]*a_stu[i];
sumxn+=a_stu[i];
}
return(sumx/N-(sumxn/N)*(sumxn/N));
}
7.15
#include<stdio.h>
#define N 10
void input_e(num,name)
int num[];
char name[N][8];
{int i;
for(i=0;i<N;i++)
{scanf("%d",&num[i]);
gets(name[i]);
}
}
void sort(num,name)
int num[];
char name[N][8];
{int i,j,min,temp1;
char temp2[8];
for(i=0;i<N-1;i++)
{min=i;
for(j=i;j<N;j++)
if(num[min]>num[j])min=j;
temp1=num[i];
num[i]=num[min];
num[min]=temp1;
strcpy(temp2,name[i]);
strcpy(name[i],name[min]);
strcpy(name[min],temp2);
}
for(i=0;i<N;i++)
printf("\n%5d%10s",num[i],name[i]);
}
void search(n,num,name)
int n,num[];
char name[N][8];
{int top,bott,min,loca;
loca=0;
top=0;
bott=N-1;
if((n<num[0])||(n>num[N-1]))
loca=-1;
while((loca==0)&&(top<=bott))
{min=(bott+top)/2;
if(n==num[min])
{loca=min;
printf("number=%d,name=%s\n",n,name[loca]);
}
else if(n<num[min])
bott=min-1;
else
top=min+1;
}
if(loca==0||loca==-1)
printf("number=%d is not in table\n",n);
}
main()
{int num[N],number,flag,c,n;
char name[N][8];
input_e(num,name);
sort(num,name);
for(flag=1;flag;)
{scanf("%d",&number);
search(number,num,name);
printf("continue?Y/N!");
c=getchar();
if(c=='N'||c=='n')
flag=0;
}
}
7.16
#include<stdio.h>
#define MAX 1000
main()
{int c,i,flag,flag1;
char t[MAX];
i=0;
flag=0;
flag1=1;
while((c=getchar())!='\0'&&i<MAX&&flag1)
{if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f')
{flag=1;
t[i++]=c;
}
else if(flag)
{t[i]='\0';
printf("\nnumber=%d\n",htoi(t));
printf("continue?");
c=getchar();
if(c=='n'||c=='N')
flag1=0;
else
{flag=0;
i=0;
}
}
}
}
htoi(s)
char s[];
{int i,n;
n=0;
for(i=0;s[i]!='\0';i++)
{if(s[i]>='0'&&s[i]<='9')
n=n*16+s[i]-'0';
if(s[i]>='a'&&s[i]<='f')
n=n*16+s[i]-'a'+10;
if(s[i]>='A'&&s[i]<='F')
n=n*16+s[i]-'A'+10;
}
return(n);
}
7.17
#include<stdio.h>
void convert(n)
int n;
{int i;
if((i=n/10)!=0)
convert(i);
putchar(n%10+'0');
}
main()
{int number;
scanf("%d",&number);
if(number<0)
{putchar('-');
number=-number;
}
convert(number);
}
7.18
main()
{int year,month,day;
int days;
scanf("\n%d,%d,%d",&year,&month,&day);
days=sum_day(month,day);
if(leap(year)&&(month>=3))
days+=1;
printf("days=%d\n",days);
}
static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}
int sum_day(month,day)
int month,day;
{int i;
for(i=1;i<month;i++)
day+=day_tab[i];
return(day);
}
int leap(year)
int year;
{int leap;
leap=year%4==0&&year%100!=0||year%400==0;
return(leap);
}
第八章 编译预处理
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]++;
else if(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);
}