《C语言程序设计》苏小红课后习题答案高等教育出版社
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2 2 #include<stdio.h>
main()
{
float x=2.5,y=2.5,z=2.5;
printf("x=%f\n",x);
printf("y=%f\n",y);
printf("z=%f\n",z);
}
3.1(1)
#include<stdio.h>
main()
{
int a=12,b=3;
float x=18.5,y=4.6;
printf("%f\n",(float)(a*b)/2);
printf("%d\n",(int)x%(int)y);
}
3.1(2)
#include<stdio.h>
main()
{
int x=32,y=81,p,q;
p=x++;
q=--y;
printf("%d %d\n",p,q);
printf("%d %d\n",x,y);
}
3.2
#include<stdio.h>
main()
{
int x,b0,b1,b2,s;
printf("Inputx:");
scanf("%d",&x);
b2=x/100;
b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10;
b0=x%10;
s=b0*100+b1*10+b2;
printf("s=%d\n",s);
}
3.3
#include<stdio.h>
#include<math.h>
main()
{
float rate=0.0225;
float n,capital,deposit;
printf("Input n,capital:");
scanf("%f,%f",&n,&capital);
deposit=capital*pow(1+rate,n);
printf("deposit=%f\n",deposit); }
3.4
#include<stdio.h>
#include<math.h>
main()
{
float a, b, c;
double x, y;
printf("Input a, b, c:");
scanf("%f %f %f", &a, &b, &c);
x=(-b+sqrt(b*b-4*a*c))/(2*a);
y=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x=%f,y=%f\n",x,y);
}
习题4
4.1(1)
#include<stdio.h>
main()
{
char c1='a',c2='b',c3='c';
}
4.1(2)
#include<stdio.h>
main()
{
int a=12,b=15;
}
4.1(3)
#include<stdio.h>
main()
{
int a,b;
printf("%d,%d\n",a,b);
}
4.2
#include<stdio.h>
main()
{
long a,b;
float x,y;
scanf("%d,%d\n",&a,&b);
scanf("%f,%f\n",&x,&y);
printf("a=%d,b=%d\n",a,b);
printf("x=%f,b=%f\n",x,y);
}
5.1
#include<stdio.h>
main()
{
float a;
printf("Input a:");
scanf("%f",&a);
if(a>=0)
{
a=a;
printf("a=%f\n",a);
}
else
{
a=-a;
printf("a=%f\n",a);
}
}
5.2
#include<stdio.h>
main()
{
int a;
printf("Input a:");
scanf("%d", &a);
if(a%2==0)
{
printf("a是偶数");
}
else
{
printf("a是奇数");
}
}
5.3
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,s,area;
printf("Input a, b, c:");
scanf("%f %f %f", &a, &b, &c);
if(a+b>c&&a+c>b&&b+c>a)
{
s=(a+b+c)/2;
area=(float)sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%f\n",area);
else
{
printf("不是三角形");
}
}
5.4
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,x,y;
printf("Inputa,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
if(a==0)
{
printf("该方程不是一元二次方程\n");
}
if(b*b-4*a*c>0)
{
x=(-b+sqrt(b*b-4*a*c))/(2*a);
y=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x=%f,y=%f\n",x,y);
}
else if(b*b-4*a*c==0)
{
x=-b/(2*a);
y=-b/(2*a);
printf("x=%f,y=%f\n",x,y);
}
else
{
printf("该方程无实根\n");
}
}
5.5
#include<stdio.h>
main()
{
int year,flag;
printf("Input a year:");
scanf("%d",&year);
if(year%4==0&&year%400!=0||year%400==0) {
flag=1;
}
else
{
flag=0;
if(flag==1)
{
printf("%d is a leap year!\n",year);
}
else
{
printf("%d is not a leap year!\n",year);
}
}
5.6
#include<stdio.h>
main()
{
int year,flag;
printf("Input a year:");
scanf("%d",&year);
flag=year%400==0||year%4==0&&year%100!=0?1:0;
if(flag==1&&flag!=0)
{
printf("%d is a leap year!\n",year);
}
else
{
printf("%d is not a leap year!\n",year);
}
}
5.7
#include<stdio.h>
main()
{
char ch;
printf("Inputch:");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
{
ch=getchar();
ch=ch-32;
printf("%c,%d\n",ch,ch);
}
else if(ch>='A'&&ch<='Z')
{
ch=getchar();
ch=ch+32;
printf("%c,%d\n",ch,ch);
}
else
{
printf("%c",ch);
}
5.8
#include<stdio.h>
main()
{
char ch;
printf("Inputch:");
scanf("%c",&ch);
if(ch>=48&&ch<=57)
{
printf("ch是数字字符\n");
}
else if(ch>=65&&ch<=90)
{
printf("ch是大写字母\n");
}
else if(ch>=97&&ch<=122)
{
printf("ch是小写字母\n");
}
else if(ch==32)
{
printf("ch是空格\n");
}
else
{
printf("ch是其他字符\n");
}
}
5.9
#include<stdio.h>
main()
{
int score,grade;
printf("Input score:");
scanf("%d",&score);
grade=score/10;
if(score<0||score>100)
{
printf("Input error\n");
}
if(score>=90&&score<=100)
{
printf("%d--A\n",score);
}
else if(score>=80&&score<90)
{
printf("%d--B\n",score);
}
else if(score>=70&&score<80)
{
printf("%d--C\n",score);
}
else if(score>=60&&score<70)
{
printf("%d--D\n",score);
}
else if(score>=0&&score<60)
{
printf("%d--E\n",score);
}
}
5.10
#include<stdio.h>
main()
{
int year,month;
printf("Input year,month:");
scanf("%d,%d",&year,&month);
if(month>12||month<=0)
{
printf("error month\n");
}
else
{
switch(year,month)
{
case12:
case10:
case8:
case7:
case5:
case3:
case1:
printf("31天\n");
break;
case11:
case9:
case6:
case4:
printf("30天\n");
break;
case2:
if(year%4==0&&year!=0||year%400==0)
{
printf("29天\n");
}
else
{
printf("28天\n");
}
break;
default:
printf("Input error\n");
}
}
}
6.1(1)
#include<stdio.h>
main()
{
int i,j,k;
char space='';
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",space);
}
for(k=1;k<=6;k++)
{
printf("*");
}
printf("\n");
}
}
6.1(2)#include<stdio.h>
main()
{
int k=4,n;
for(n=0;n<k;n++)
{
if(n%2==0)continue;
k--;
}
printf("k=%d\n,n=%d\n",k,n);
}
6.1(3)#include<stdio.h>
main()
{
int k=4,n;
for(n=0;n<k;n++)
{
if(n%2==0)break;
k--;
}
printf("k=%d,n=%d\n",k,n);
}
6.2(1)#include<stdio.h>
main()
{
int i,sum=0;
for(i=1;i<=101;i++)
{
sum=sum+i;
}
printf("sum=%d\n",sum);
}
6.2(2)#include<stdio.h>
main()
{
long i;
long term,sum=0;
for(i=1;i<=101;i=i+2)
{
term=i*(i+1)*(i+2);
sum=sum+term;
}
printf("sum=%ld\n",sum);
}
6.2(4)#include<stdio.h>
#include<math.h>
main()
{
int n=1;
float term=1.0,sign=1,sum=0;
while(term<=-1e-4||term>=1e-4)
{
term=1.0/sign;
sum=sum+term;
sign=sign+n;
n++;
}
printf("sum=%f\n",sum);
}
6.2(5)#include<stdio.h>
#include<math.h>
main()
{
int n=1,count=1;
float x;
double sum,term;
printf("Input x:");
scanf("%f",&x);
sum=x;
term=x;
do{
term=-term*x*x/((n+1)*(n+2));
sum=sum+term;
n=n+2;
count++;
}while(fabs(term)>=1e-5);
printf("sin(x)=%f,count=%d\n",sum,count);
}
6.3#include<stdio.h>
main()
{
int x=1,find=0;
while(!find)
{
if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)
{
printf("x=%d\n",x);
find=1;
x++;
}
}
}
/*int x,find=0;
for(x=1;!find;x++)
{
if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)
{
printf("x=%d\n",x);
find=1;
}
}
}*/
6.4#include<stdio.h>
main()
{
int i,n;
long p=1,m=1;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=i*i;
m=i*i*i;
printf("p=%d,m=%d\n",i,p,i,m);
}
}
6.5#include<stdio.h>
main()
{
float c,f;
for(c=-40;c<=110;c=c+10)
{
f=9/5*c+32;
printf("f=%f\n",f);
}
}
6.6#include<stdio.h>
#include<math.h>
main()
{
int n;
double c=0.01875,x;
do{
x=x*pow(1+c,12)-1000;
n++;
}while(x>0);
printf("x=%d\n",x);
}
6.7#include<stdio.h>
main()
{
int n=0;
float a=100.0,c;
printf("Inputc:");
scanf("%f",&c);
do
{
a=a*(1+c);
n++;
}while(a<=200);
printf("n=%d\n",n);
}
6.8#include<stdio.h>
#include<math.h>
main()
{
int n=1,count=1;
double sum=1,term=1;
while(fabs(term)>=1e-5)
{
term=pow(-1,count)*(1.0/(n+2));
sum=sum+term;
n=n+2;
count++;
}
sum=4*sum;
printf("sum=%f,count=%d\n",sum,count); }
6.9#include<stdio.h>
#include<math.h>
main()
{
int n=1,count=1;
double sum=1,term=1;
while(fabs(term)>=1e-5)
{
term=term*(1.0/n);
sum=sum+term;
n++;
count++;
}
printf("sum=%f,count=%d\n",sum,count);
}
6.10#include<stdio.h>
#include<math.h>
main()
{
int x;
for(x=100;x<=999;x++)
{
if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))
printf("x=%d\n",x);
}
}
6.11#include<stdio.h>
main()
{
int i=0,n;
long sum=0,term=1;
printf("Inputn:");
scanf("%d",&n);
do{
i++;
term=term*i;
sum=sum+term;
}while(sum<n);
printf("%d\n",i-1);
}
6.12#include<stdio.h>
main()
{
int i,n,m,count=0,sum=0;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Inputm:");
scanf("%d",&m);
if(m>0)
{
sum=sum+m;
count++;
}
else
{
break;
}
printf("sum=%d,count=%d\n",sum,count);
}
}
6.13#include<stdio.h>
main()
{
int i,n,m,count=0,sum=0;
printf("Inputn:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Inputm:");
scanf("%d",&m);
if(m>0||m<0)
{
sum=sum+m;
count++;
}
else
{
break;
}
printf("sum=%d,count=%d\n",sum,count);
}
}
6.14#include<stdio.h>
main()
{
int x,y,z;
for(x=0;x<=17;x++)
{
for(y=0;y<=25;y++)
{
3*x+2*y+z==50;
z=30-x-y;
if(3*x+2*y+z==50&&x+y+z==30)
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
}
6.15#include<stdio.h>
main()
{
int x,y;
for(x=0;x<=98;x++)
{
y=98-x;
2*x+4*y==386;
if(x+y==98&&2*x+4*y==386)
{
printf("x=%d,y=%d\n",x,y);
}
}
}
6.16#include<stdio.h>
main()
{
int x,y,z;
for(x=0;x<=20;x++)
{
for(y=0;y<=33;y++)
{
3*y+5*x+z/3.0==100;
z=100-x-y;
if(5*x+3*y+z/3.0==100&&z+x+y==100)
{
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
}
}
6.17#include<stdio.h>
main()
{
int x,y,z;
for(x=1;x<=9;x++)
{
for(y=1;y<=17;y++)
{
10*x+5*y+z==100;
z=50-x-y;
if(10*x+5*y+z==100&&x+y+z==50&&z>0)
{
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
}
}
7.1#include<stdio.h>
int Square(int i)
{
return i*i;
}
int main()
{
int i=0;
i=Square(i);
for(;i<3;i++)
{
static int i=1;
i+=Square(i);
printf("%d,",i);
}
printf("%d\n",i);
return0;
}
7.2#include<stdio.h>
int hour,minute,second;
void update()
{
second++;
if(second==60)
{
second=0;
minute++;
}
if(minute==60)
{
minute=0;
hour++;
}
if(hour==24)
hour=0;
}
void display()
{
printf("%d,%d,%d\n",hour,minute,second); }
void delay()
{
int t;
for(
}
int main()
{
int i;
void updaye(),display(),delay();
for(i=0;i<1000000;i++)
{
update();
display();
delay();
}
return0;
}
7.3#include<stdio.h>
int GetMax(int a,int b);
int main()
{
int x,y,max;
printf("Inputx,y:");
scanf("%d,%d",&x,&y);
max=GetMax(x,y);
printf("max=%d\n",max);
return0;
}
int GetMax(int m,int n)
{
if(m>=n)
return m;
else
return n;
}
7.4#include<stdio.h>
int LCM(int n,int m);
int main()
{
int a,b;
printf("Inputa,b:");
scanf("%d,%d",&a,&b);
printf("%d\n",LCM(a,b));
return0;
}
int LCM(int n,int m)
{
int x;
int find=0;
for(x=1;!find;x++)
{
if(x%n==0&&x%m==0)
{
find=1;
}
}
return x-1;
}
7.5#include<stdio.h>
long Fact(int n);
int main()
{
int m,a;
printf("Inputm:");
scanf("%d",&m);
for(a=1;a<=m;a++)
{
printf("%d!=%ld\n",a,Fact(a));
}
return0;
}
long Fact(int n)
{
int i;
long result=1;
for(i=2;i<=n;i++)
result*=i;
return result;
}
7.6#include<stdio.h>
long Fact(int n);
int main()
{
int m;
long ret;
printf("Inputm:");
scanf("%d",&m);
ret=Fact(m);
printf("ret=%d\n",ret);
return0;
}
long Fact(int n)
{
int i;
long result=1,sum=0;
for(i=2;i<=n;i++)
{
result*=i;
sum=sum+result;
}
return sum;
}
7.7(1)#include<stdio.h>
int Gcd(int a,int b);
int main()
{
int m,n;
printf("Inputm,n:");
scanf("%d,%d",&m,&n);
printf("%d\n",Gcd(m,n));
return0;
}
int Gcd(int a,int b)
{
int t,min,find=0;
min=a<b?a:b;
t=min;
for(t=min;!find;t--)
{
a%t==0;
b%t==0;
if(a%t==0&&b%t==0)
return t;
}
find=1;
}
7.7(2)#include<stdio.h> int Gcd(int a,int b);
int main()
{
int m,n;
printf("Inputm,n:");
scanf("%d,%d",&m,&n);
printf("%d\n",Gcd(m,n));
return0;
}
int Gcd(int a,int b)
{
int r,temp;
r=a%b;
if(r==0)
return b;
else
do{
temp=b;
b=r;
a=temp;
r=a%b;
}while(r!=0);
return b;
}
8.1(1)#include<stdio.h> void Func(int x)
{
x=2;
}
int main()
{
int x=10;
Func(x);
printf("%d",x);
return0;
}
8.1(2)#include<stdio.h> void Func(int b[])
{
int j;
for(j=0;j<4;j++)
{
b[j]=j;
}
}
int main()
{
static int a[]={5,6,7,8},i;
Func(a);
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
return0;
}
8.2(1)int PositiveNum(int a[],int n) {
int i,count=0;
for(i=0;i<n;i++)
{
if(a[i]>0)
count++;
}
return0;
}
8.2(2)void Fib(long f[],long n)
{
int i;f[0]=0;
f[1]=1;
for(i=2;i<n;i++)
{
f[i]=f[i-1]+f[i-2];
}
}
8.2(3)#include<stdio.h>
int main()
{
int a[10],n,max,min,maxPos,minPos;
for(n=0;n<10;n++)
{
scanf("%d",&a[n]);
}
max=min=a[0];
maxPos=minPos=0;
for(n=0;n<10;n++)
{
if(a[n]>max)
{
max=a[n];
maxPos=n;
}
else if(a[n]<min)
{
min=a[n];
minPos=n;
}
}
printf("max=%d,pos=%d\n",max,maxPos);
printf("min=%d,pos=%d\n",min,minPos);
return0;
}
8.3void DivArray(int*pArray,int n)
{
int i;
for(i=0;i<n;i++)
{
pArray[i]/=pAttay[0];
}
}
8.4#include<stdio.h>
#define N40
int FailNum(int score[],int n);
void ReadScore(int score[],int n);
int main()
{
int score[N],n;
printf("Input n:");
scanf("%d",&n);
ReadScore(score,n);
FailNum(score,n);
printf("FailNum students are%d\n",FailNum(score,n));
return0;
}
int FailNum(int score[],int n)
{
int i,count=0;
for(i=0;i<n;i++)
{
if(score[i]<60)
count++;
}
return count;
}
void ReadScore(int score[],int n)
{
int i;
printf("Input score:");
for(i=0;i<n;i++)
{
scanf("%d",&score[i]);
}
}
8.5#include<stdio.h>
#define N40
int HighAver(int score[],int n);
int ReadScore(int score[]);
int main()
{
int score[N],n;
n=ReadScore(score);
printf("HighAver students are%d\n",HighAver(score,n));
return0;
}
int HighAver(int score[],int n)
{
int i,count=0,sum=0,aver;
for(i=0;i<n;i++)
{
sum=sum+score[i];
aver=sum/n;
}
for(i=0;i<n;i++)
{
if(score[i]>aver)
count++;
}
return count;
}
int ReadScore(int score[])
{
int i=-1;
do{
i++;
printf("Input score:");
scanf("%d",&score[i]);
}while(score[i]>=0);
return i;
}
8.6#include<stdio.h>
#define N40
int ReadScore(int score[],long num[]);
int FindMax(int score[],long num[],int n);
int main()
{
int score[N],n,max,num,t;
long num[N];
t=FindMax(score,n);
max=score[t];
num=num[t];
printf("max=%d,num=%d\n",max,num);
return0;
}
int ReadScore(int score[],long num[])
{
int i=-1;
do{
i++;
printf("Input student's ID and score:");
scanf("%ld%d",&num[i],&score[i]);
}while(num[i]>0&&score[i]>=0);
return i;
}
int FindMax(int score[],long num[],int n)
{
int max,i;
max=score[0];
for(i=1;i<n;i++)
{
if(score[i]>max)
max=score[i];
}
return i;
}
8.7#include<stdio.h>
#define N40
int Read(int a[]);
int ChangeArry(int a[],int n);
int main()
{
int a[N],n;
printf("%d\n",ChangeArry(a,n));
return0;
}
int Read(int a[])
{
int i;
for(i=0;i<9;i++)
{
printf("Input a:");
scanf("%d",&a[i]);
return i;
}
int ChangeArry(int a[],int n)
{
int max,min,i,maxpos,minpos,term;
max=min=score[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=score[i];
maxpos=n;
else(a[i]<min)
min=a[i];
minpos=n;
temp=maxpos;
maxpos=minpos;
minpos=temp;
}
return a[i];
}
8.10#include<stdio.h>
#define N5
void main()
{
int a[N][N];
int sum=0;
int i,j;
printf("请输入一个%d*%d的矩阵\n",N,N);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==j||i+j==N-1)
sum=sum+a[i][j];
}
}
printf("对角线元素之和为:%d\n",sum);
}
8.11#include<stdio.h>
#define N2
#define M3
void main()
{
int a[N][M],b[N][M];
int i,j,sum1=0,sum2=0,sum=0;
printf("请输入一个%d*%d的矩阵\n",N,M);
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%d",&a[i][j]);
sum1=sum1+a[i][j];
}
}
printf("请输入一个%d*%d的矩阵\n",N,M);
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%d",&b[i][j]);
sum2=sum2+a[i][j];
}
}
sum=sum1+sum2;
printf("sum=%d\n",sum);
}
8.12#include<stdio.h>
#define N7
void YangHui(int a[N][N],int n);
void main()
{
int i,j,n,b[N][N];
printf("Input n:");
scanf("%d",&n);
YangHui(b,n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}
}
void YangHui(int a[N][N],int n)
{
int i,j;
for(i=0;i<n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(i=2;i<n;i++)
{
for(j=1;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
8.13#include<stdio.h>
#define N12
void main()
{
int i;
int f[N]={1,1};
printf("%d:%d\n",0,f[0]);
printf("%d:%d\n",1,f[1]);
for(i=2;i<N;i++)
{
f[i]=f[i-1]+f[i-2];
printf("%d:%d\n",i+1,f[i]);
}
}
8.14#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N6000
void main()
{
int a[N],i;
int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;
float p1,p2,p3,p4,p5,p6;
srand(time(NULL));
for(i=0;i<N;i++)
{
a[i]=rand()%6+1;
printf("%3d",a[i]);
if(a[i]==1)
b1++;
if(a[i]==2)
b2++;
if(a[i]==3)
b3++;
if(a[i]==4)
b4++;
if(a[i]==5)
b5++;
if(a[i]==6)
b6++;
p1=(float)(b1/6000.0);
p2=(float)(b2/6000.0);
p3=(float)(b3/6000.0);
p4=(float)(b4/6000.0);
p5=(float)(b5/6000.0);
p6=(float)(b6/6000.0);
}
printf("骰子出现1的概率:%.2f\n",p1);
printf("骰子出现2的概率:%.2f\n",p2);
printf("骰子出现3的概率:%.2f\n",p3);
printf("骰子出现4的概率:%.2f\n",p4);
printf("骰子出现5的概率:%.2f\n",p5);
printf("骰子出现6的概率:%.2f\n",p6);
}
9.1(1)void Swap(int*x,int*y)
{
int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;
*x=*y;
*y=*pTemp;
}
9.1(2)void Swap(int*x,int*y)
{
int*pTemp;
pTemp=x;
x=y;
y=pTemp;
}
//错误,x,y没有指向类型
9.2#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5};
int*p=a;
printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);
return0;
}
9.3#include<stdio.h>
void Swap(int*x,int*y);
int main()
{
int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};
int i;
printf("Before Array\n");
for(i=0;i<6;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<6;i++)
{
printf("%d\n",b[i]);
}
for(i=0;i<6;i++)
{
Swap(&a[i],&b[i]);
}
printf("After Array\n");
for(i=0;i<6;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<6;i++)
{
printf("%d\n",b[i]);
}
return0;
}
void Swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
9.4#include<stdio.h>
#define N10
void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);
int main()
{
int a[N];
int i,maxa,mina,maxnum,minnum;
printf("Input a:");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);
printf("maxa=%d,maxnum=%d,mina=%d,minnum=%d\n",maxa,maxnum,mina,minnum);
return0;
}
void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum)
{
int i;
*pMaxa=a[0];
*pMina=a[0];
*pMaxnum=0;
*pMinnum=0;
for(i=0;i<10;i++)
{
if(a[i]>*pMaxa)
{
*pMaxa=a[i];
*pMaxnum=i;
}
else if(a[i]<*pMina)
{
*pMina=a[i];
*pMinnum=i;
}
}
}
10.1void MYStrcpy(char*dstStr,const char*srcStr)
{
while((*dstStr++=*srcStr++)!='\0')
{
}
}
10.2#include<stdio.h>
main()
{char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)
main()
{
unsigned int len;
len=0;
for(;*pa!='\0';pa++)
{
len++;
}
return len;
}
10.3(2)unsigned int MyStrlen(char s[])
{
char*p=s;
while(*p!='\0')
{
p++;
}
return(p-*p);
}
10.3(3)int MyStrcmp(char*p1,char*p2)
{
for(;*p1==*p2;p1++,p2++)
{
if(*p1=='\0')return-1;
}
return1;
}
10.3(4)int MyStrcmp(char s[],char t[])
{
int i;
for(i=0;s[i]==t[i];i++)
{
if(s[i]=='\0')return0;
}
return i;
}
10.3(5)#include<stdio.h>
#include<string.h>
int main()
{
char password[7]="secret";
char userInput[81];
printf("Input Password:");
scanf("%s",userInput);
if(strcmp(userInput,password)==0)
printf("Coorrect password!Welcome to the system...\n");
else if(strcmp(userInput,password)<0)
printf("Invalid password!user input<password...\n");
else
printf("Invalid password!user input>password...\n");
return0;
}
10.4#include<stdio.h>
#define N24
unsigned int CountLetter(char str[]);
int main()
{
char a[N];
printf("Input a letter:\n");
gets(a);
printf("The length of the letter is:%d\n",CountLetter(a));
return0;
}
unsigned int CountLetter(char str[])
{
char*p=str;
int c=0,flag=0;
while(*p!='\0')
{
if(*p!='')
flag=1;
else if(flag==1)
{
c++;
flag=0;
}
p++;
}
return c+1;
}
12.1#include<stdio.h>
typedef struct data
{
int year;
int month;
int day;
}DATA;
typedef struct work
{
char a[14];
char b[10];
char c[6];
}WORK;
typedef struct student
{
char studentName[10];
char studentSex;
DATA birthday;
WORK profession;
}STUDENT;
int main()
{
STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};
printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.bi
rthday.year,
stu.profession.a,stu.profession.b,stu.profession.c);
return0;
}
12.4#include<stdio.h>
#include<string.h>
#define N10
typedef struct people
{
char peopleName[10];
}PEOPLE;
int main()
{
int sum1=0,sum2=0,sum3=0,sum4=0;
int i;
char name[N][N];
PEOPLE stu[5]={"zhang","li","wang"};
printf("Enter their name:\n");
for(i=0;i<10;i++)
{
gets(name[i]);
if(strcmp(name[i],stu[0].peopleName)==0)
sum1++;
else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;
else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;
else
sum4++;
}
printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);
printf("stu:%4s%2d\n",stu[1].peopleName,sum2);
printf("stu:%4s%2d\n",stu[2].peopleName,sum3);
printf("%2d\n",sum4);
return0;
}。