首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

C语言程序设计实验指导(颜晖_主编)高等教育出版社

2023-09-22 来源:华佗小知识
C语言程序设计部分实验参考答案

实验2

#include int main(){//2-1-2 int c,f=150; c=f*5/9-32*5/9;

printf(\"fahr=%d, celsius=%d\\n\",f, c); return 0; }

#include int main(){//2-1-3 int c=26,f; f=c*9/5+32;

printf(\"celsius=%d, fahr=%d\\n\",c, f); return 0; }

#include int main(){//2-1-4

int math=87,eng=72,comp=93;

printf(\"math=%d, eng=%d, comp=%d, average=%d\\n\",math, eng, comp, (math+eng+comp)/3); return 0; }

#include int main(){//2-1/5 int n=152,a,b,c; c=n%10;

b=(n/10)%10; a=n/100;

printf(\"整数%d 的个位数字是%d, 十位数字是%d, 百位数字是%d\\n\",n, c, b, a);

return 0; }

#include int main(){//2-1-6 int x=3,y; y=x*x;

printf(\"%d=%d*%d\\n\",y,x,x); printf(\"%d*%d=%d\\n\",x,x,y); return 0; }

#include int main(){//2-2-1 double x, y;

printf(\"Enter x:\"); scanf_s(\"%lf\",&x); if(x!=0){ y=1/x; }else{ y=0; }

printf(\"f(%.2f)=%.1f\\n\",x,y); return 0; }

#include int main(){//2-2-2 int celsius, fahr; fahr=100;

celsius=5*(fahr-32)/9;

printf(\"fahr=%d, celsius=%d\\n\",fahr,celsius); return 0; }

#include int main(){//2-2-3

double money, rate,x=1.0; int i,year;

printf(\"Enter money, year and rate:\"); scanf(\"%lf %d %lf\",&money,&year,&rate); for(i=1;i<=year;i++) x=x*(1+rate);

printf(\"intrest=%.2f\\n\",money*x-money); return 0; }

#include #include int main(){//2-2-4 double x, y;

printf(\"Enter x\"); scanf_s(\"%lf\",&x); if(x<0.0)

y=pow(x+1,2)+2*x+1/x; else

y=sqrt(x);

printf(\"f(%.2f)=%.2f\\n\",x,y); return 0; }

#include #include

int main(){//2-2-5 double x, y;

printf(\"Enter x:\"); scanf_s(\"%lf\",&x); if(x==10) y=1/x; else y=x;

printf(\"f(%.2f)=%.1f\\n\",x,y); return 0; }

#include int main(){//2-2-6 int x, y;

printf(\"Enter num1:\"); scanf_s(\"%d\",&x);

printf(\"Enter num2:\"); scanf_s(\"%d\",&y);

printf(\"%d+%d=%d\\n\",x,y,x+y); printf(\"%d-%d=%d\\n\",x,y,x-y); printf(\"%d*%d=%d\\n\",x,y,x*y); printf(\"%d/%d=%d\\n\",x,y,x/y); printf(\"%d%%d=%d\\n\",x,y,x%y); return 0; }

#include int main(){//2-3-1 int i,sum=0;

for(i=1;i<=100;i++){ sum=sum+i; }

printf(\"sum=%d\\n\",sum); return 0; }

#include int main(){//2-3-2 int i,m,s=0;

printf(\"Enter m:\"); scanf(\"%d\",&m); for(i=m;i<=100;i++) s=s+i;

printf(\"sum=%d\\n\",s); return 0; }

#include int main(){//2-3-3 int i,m,n; double s=0.0;

printf(\"Enter m:\"); scanf(\"%d\",&m); printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=m;i<=n;i++) s=s+1.0/i;

printf(\"sum=%f\\n\",s); return 0; }

#include int main(){//2-3-4 int i,n;

double s=0.0;

printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;i<=n;i++) s=s+1.0/(2*i-1); printf(\"sum=%f\\n\",s); return 0; }

#include int main(){//2-3-5 int i,n;

double s=1.0,t=1.0,sign=1.0; printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;is=s+1/t*sign; }

printf(\"sum=%.3f\\n\",s); return 0; }

#include int main(){//2-3-6 int low, uper;

double fahr,celsius; printf(\"Enter low:\"); scanf_s(\"%d\",&low);

printf(\"Enter uper:\"); scanf_s(\"%d\",&uper);

printf(\"fahr celsius\\n\");

for(fahr=low;fahr<=uper;fahr+=2){ celsius=5.0/9.0*(fahr-32.0);

printf(\"%3.0f %6.1f\\n\",fahr,celsius); }

return 0; }

#include int main(){//2-3-7 int m, n, i;

double p,sum=0.0; printf(\"Enter m:\"); scanf(\"%d\",&m); printf(\"Enter n:\"); scanf(\"%d\",&n); p=1.0;

for(i=1;i<=m;i++) p=p*i; sum=sum+p; p=1.0;

for(i=1;i<=n;i++) p=p*i; sum=sum+p;

printf(\"sum=%.0f\\n\",sum); return 0; }

#include int main(){//2-3-8 int x, n,i; double p=1.0;

printf(\"Enter x:\"); scanf(\"%d\",&x); printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;i<=n;i++) p=p*x;

printf(\"%d的%d次方是%.0f\\n\",x, n,p); return 0; }

#include #include int main(){//2-4-1

int n,i;

printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=0;i<=n;i++)

printf(\"pow(3,%d)=%.0f\\n\",i,pow(3.0,i)); return 0; }

#include #include int main(){//2-4-4 double sum=0.0,i;

for(i=100;i<=1000;i++){ sum+=sqrt(i); }

printf(\"sum=%.2f\\n\",sum); return 0; }// return 0; }

#include #include int main(){//2-2-5 double x, y;

printf(\"Enter x:\"); scanf_s(\"%lf\",&x); if(x==10) y=1/x; else y=x;

printf(\"f(%.2f)=%.1f\\n\",x,y); return 0; }

#include int main(){//2-2-6 int x, y;

printf(\"Enter num1:\"); scanf_s(\"%d\",&x);

printf(\"Enter num2:\"); scanf_s(\"%d\",&y);

printf(\"%d+%d=%d\\n\",x,y,x+y); printf(\"%d-%d=%d\\n\",x,y,x-y); printf(\"%d*%d=%d\\n\",x,y,x*y); printf(\"%d/%d=%d\\n\",x,y,x/y); printf(\"%d%%d=%d\\n\",x,y,x%y); return 0;

}

#include int main(){//2-3-1 int i,sum=0;

for(i=1;i<=100;i++){ sum=sum+i; }

printf(\"sum=%d\\n\",sum); return 0; }

#include int main(){//2-3-2 int i,m,s=0;

printf(\"Enter m:\"); scanf(\"%d\",&m); for(i=m;i<=100;i++) s=s+i;

printf(\"sum=%d\\n\",s); return 0; }

#include int main(){//2-3-3 int i,m,n; double s=0.0;

printf(\"Enter m:\"); scanf(\"%d\",&m); printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=m;i<=n;i++) s=s+1.0/i;

printf(\"sum=%f\\n\",s); return 0; }

#include int main(){//2-3-4 int i,n;

double s=0.0;

printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;i<=n;i++) s=s+1.0/(2*i-1); printf(\"sum=%f\\n\",s); return 0; }

#include int main(){//2-3-5 int i,n;

double s=1.0,t=1.0,sign=1.0; printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;is=s+1/t*sign; }

printf(\"sum=%.3f\\n\",s); return 0; }

#include int main(){//2-3-6 int low, uper;

double fahr,celsius; printf(\"Enter low:\"); scanf_s(\"%d\",&low); printf(\"Enter uper:\"); scanf_s(\"%d\",&uper);

printf(\"fahr celsius\\n\");

for(fahr=low;fahr<=uper;fahr+=2){ celsius=5.0/9.0*(fahr-32.0);

printf(\"%3.0f %6.1f\\n\",fahr,celsius); }

return 0; }

#include int main(){//2-3-7 int m, n, i;

double p,sum=0.0; printf(\"Enter m:\"); scanf(\"%d\",&m); printf(\"Enter n:\"); scanf(\"%d\",&n); p=1.0;

for(i=1;i<=m;i++) p=p*i; sum=sum+p; p=1.0;

for(i=1;i<=n;i++) p=p*i;

sum=sum+p;

printf(\"sum=%.0f\\n\",sum); return 0; }

#include int main(){//2-3-8 int x, n,i; double p=1.0;

printf(\"Enter x:\"); scanf(\"%d\",&x); printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=1;i<=n;i++) p=p*x;

printf(\"%d的%d次方是%.0f\\n\",x, n,p); return 0; }

#include #include int main(){//2-4-1 int n,i;

printf(\"Enter n:\"); scanf(\"%d\",&n); for(i=0;i<=n;i++)

printf(\"pow(3,%d)=%.0f\\n\",i,pow(3.0,i)); return 0; }

#include #include int main(){//2-4-4 double sum=0.0,i;

for(i=100;i<=1000;i++){ sum+=sqrt(i); }

printf(\"sum=%.2f\\n\",sum); return 0; }

实验3

#include #include int main(){//3-1

double a, b, c, d;

printf(\"Enter a, b, c: \"); scanf(\"%lf%lf%lf\",&a,&b,&c);

d=b*b-4*a*c; if(a==0) if(b==0) if(c==0)

printf(\"系数均为,方程无意义!\\n\"); else

printf(\"a、b为,c不为,方程不成立!\\n\"); else

printf(\"x=%.2f\\n\",c/b); else

if(d>=0){

printf(\"x1=%.2f\\n\",(-b+sqrt(d))/(2*a)); printf(\"x1=%.2f\\n\",(-b-sqrt(d))/(2*a)); }else{

printf(\"x1=%.2f+%.2fi\\n\",-b/(2*a),sqrt(-d)/(2*a)); printf(\"x1=%.2f-%.2fi\\n\",-b/(2*a),sqrt(-d)/(2*a)); }

return 0; }

#include int main(){//3-2 int x,y;

printf(\"Enter x: \"); scanf(\"%d\",&x); if(x<0) y=-1;

else if(x==0) y=0; else y=1;

printf(\"sign(%d)=%d\\n\",x,y); return 0; }

#include int main(){//3-3

int letter=0,blank=0,digit=0,other=0,i; char ch;

printf(\"Input 10 characters: \"); for(i=0;i<10;i++){ ch=getchar();

if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') letter++;

else if(ch==' '||ch=='\\n') blank++;

else if(ch<='9'&&ch>='0') digit++; else

other++; }

printf(\"letter=%d, blank=%d, digit=%d, other=%d\\n\",letter,blank,digit,other); return 0; }

#include int main(){//3-4 char grade;

printf(\"Input Grade: \"); grade=getchar(); switch(grade){ case 'A':;

case 'a':printf(\"%c对应的百分制成绩区间是~100\\n\",grade);break; case 'B':;

case 'b':printf(\"%c对应的百分制成绩区间是~90\\n\",grade);break; case 'C':;

case 'c':printf(\"%c对应的百分制成绩区间是~79\\n\",grade);break; case 'D':;

case 'd':printf(\"%c对应的百分制成绩区间是~69\\n\",grade);break; case 'E':;

case 'e':printf(\"%c对应的百分制成绩区间是~59\\n\",grade);break; default:printf(\"数据输入错误\\n\"); }

return 0; }

#include int main(){//3-5

int choice,count;

for(count=0;count<5;count++){ printf(\"[1]apple\\n\"); printf(\"[2]pear\\n\"); printf(\"[3]orange\\n\"); printf(\"[4]grape\\n\"); printf(\"[0]exit\\n\");

printf(\"Enter your choice:\"); scanf(\"%d\",&choice); switch(choice){

case 0: return 0;

case 1:printf(\"price is %.2f\\n\",3.0);break; case 2:printf(\"price is %.2f\\n\",2.5);break;

case 3:printf(\"price is %.2f\\n\",4.1);break; case 4:printf(\"price is %.2f\\n\",10.2);break; default:printf(\"price is %.2f\\n\",0.0); } }

return 0; }

#include int main(){//3-6 int i;

double grade; for(i=0;i<5;i++){

printf(\"Enter grade: \"); scanf(\"%lf\",&grade); if(grade<60.0)

printf(\"Fail\\n\"); else

printf(\"Pass\\n\"); }

return 0; }

#include #include int main(){//3-7 double a,b,c,s;

printf(\"Enter 3 sides of the triangle: \"); scanf(\"%lf %lf %lf\",&a,&b, &c); if(a+b>c&&a+c>b&&b+c>a){ s=(a+b+c)/2;

printf(\"area=%.2f;

perimeter=%.2f\\n\",sqrt(s*(s-a)*(s-b)*(s-c)),a+b+c); }else

printf(\"These sides do not correspond to a valid triangle\\n\"); return 0; }

#include int main(){//3-8

double salary,rate;

printf(\"Enter the salary: \"); scanf(\"%lf\",&salary); if(salary<=850) rate=0.0;

else if(salary<=1350.0) rate=0.05;

else if(salary<=2850.0) rate=0.1;

else if(salary<=5850.0) rate=0.15; else

rate=0.2;

printf(\"tax=%.2f\\n\",rate*(salary-850)); return 0; }

#include int main(){//3-9

int i,n,grade,a=0,b=0,c=0,d=0,e=0; printf(\"Enter n: \"); scanf(\"%d\",&n); for(i=0;iprintf(\"Enter grade %d: \",i+1); scanf(\"%d\",&grade); switch(grade/10){ case 9:

case 10:a++;break; case 8:b++;break; case 7:c++;break; case 6:d++;break; case 5: case 4: case 3: case 2: case 1:

case 0:e++;break; } }

printf(\"The number of A(90~100):%d\\n\",a); printf(\"The number of B(80~89):%d\\n\",b); printf(\"The number of C(70~79):%d\\n\",c); printf(\"The number of D(60~69):%d\\n\",d); printf(\"The number of E(0~59):%d\\n\",e); return 0; }

#include int main(){//3-10 char sign; int x,y;

printf(\"输入x运算符y:\"); scanf(\"%d%c%d\",&x,&sign, &y);

if(sign=='*')

printf(\"%d * %d = %d\\n\",x,y,x*y); else if(sign=='/')

printf(\"%d / %d = %d\\n\",x,y,x/y); else if(sign=='%')

printf(\"%d Mod %d = %d\\n\",x,y,x%y); else

printf(\"运算符输入错误!\\n\"); return 0; }

实验4

#include #include int main(){//4-1-2

int flag,denominator; double s,eps,item; printf(\"Input eps:\"); scanf(\"%lf\",&eps); s=0.0; flag=1;

denominator=1; item=1.0;

while(fabs(item)>eps){

item=flag*1.0/denominator; s=s+item; flag=-flag;

denominator=denominator+3; }

printf(\"s=%.6f\\n\",s); return 0; }

#include int main(){//4-1-3 int x,sum; sum=0;

printf(\"Input integers:\"); scanf(\"%d\",&x); while(x>0){ if(x%2!=0) sum=sum+x;

scanf(\"%d\",&x); }

printf(\"The sum of the odd numbers is %d\\n\",sum); return 0;

}

#include int main(){//4-1-4 int num,count;

printf(\"Input an integer:\"); scanf(\"%d\",&num); count=0; do{

num=num/10; count++; }while(num!=0);

printf(\"count=%d\\n\",count); return 0; }

#include int main(){//4-1-5 int n,x,min,i;

printf(\"Input n:\"); scanf(\"%d\",&n);

printf(\"Input numbers:\"); scanf(\"%d\",&x); min=x;

for(i=1;iprintf(\"min=%d\\n\",min); return 0; }

int main(){//4-1-6 int n,s=0,c=0;

printf(\"Input an integer:\"); scanf(\"%d\",&n); do{

s+=n%10; n/=10; c++; }while(n);

printf(\"count=%d, sum=%d\\n\",c,s); return 0; }

#include int main(){//4-1-7

double a,b,c,s=0.0; int i,n;

printf(\"Input n:\"); scanf(\"%d\",&n);

for(a=1.0,b=1.0,i=0;iprintf(\"s=%.2f\\n\",s); return 0; }

#include int main(){//4-1-8 int a,n,i,s=0,t=0; printf(\"Input a, n:\"); scanf(\"%d%d\",&a,&n); for(i=0;iprintf(\"s=%d\\n\",s); return 0; }

#include int main(){//4-1-9 int n,i;

double h,sum=0.0;

printf(\"Input height: \"); scanf(\"%lf\",&h); printf(\"Input n: \"); scanf(\"%d\",&n); sum+=h;

for(i=1;i<=n;i++){ sum+=h; h/=2.0; }

printf(\"distance=%.1f\\n\",sum); printf(\"height=%.1f\\n\",h); return 0; }

#include #include int main(){//4-2-2

int m,n,i,j,st,c=0; printf(\"Input m: \"); scanf(\"%d\",&m);

if(m==1) m++;

printf(\"Input n: \"); scanf(\"%d\",&n); for(i=m;i<=n;i++){

st=sqrt((double)i); for(j=2;j<=st;j++) if(!(i%j)) break; if(j>st){

printf(\"%d \",i); c++;

if(!(c%6))

printf(\"\\n\"); } }

return 0; }

#include int main(){//4-2-3

int money,f1,f2,f5,count=0; printf(\"Input money: \"); scanf(\"%d\",&money);

for(f1=1;f1<=money-7;f1++) for(f2=1;f2<=money-6;f2++) for(f5=1;f5<=money-3;f5++) if(f1+2*f2+5*f5==money) count++;

printf(\"count=%d\\n\",count); for(f1=1;f1<=money-7;f1++) for(f2=1;f2<=money-6;f2++) for(f5=1;f5<=money-3;f5++) if(f1+2*f2+5*f5==money)

printf(\"fen5: %d fen2: %d fen1: %d\\n\",f5,f2,f1);

return 0; }

#include int main(){//4-2-4

int m,n,k,a,b,c,tmp; printf(\"Input m: \"); scanf(\"%d\",&m);

printf(\"Input n: \"); scanf(\"%d\",&n);

for(k=m;k<=n;k++){ tmp=k; a=tmp%10; tmp/=10; b=tmp%10; tmp/=10; c=tmp;

if(a*a*a+b*b*b+c*c*c==k)

printf(\"%d\(%d*%d*%d+%d*%d*%d+%d*%d*%d=%d)\\n\",k,c,c,c,b,b,b,a,a,a,k); }

return 0; }

#include int main(){//4-2-5 int i,j,s;

for(i=1;i<=200;i++){ s=1;

for(j=2;j<=i/2;j++) if(i%j==0) s=s+j; if(s==i){

printf(\"%d=1\",i); for(j=2;j<=i/2;j++) if(i%j==0)

printf(\"+%d\",j); printf(\"\\n\"); } }

return 0; }

#include//5-2 int sign(int x){ if(x>0)

return 1; if(x==0)

return 0; return -1; }

int main(){ int x;

printf(\"Enter x:\"); scanf(\"%d\",&x);

printf(\"sign(%d)=%d\\n\",x,sign(x)); return 0; }

#include//5-3 int even(int x){ return (x%2==0); }

int main(){ int x,s=0;

printf(\"Input integers: \"); scanf(\"%d\",&x); while(x>0){

if(!even(x)) s+=x;

scanf(\"%d\",&x); }

printf(\"The sum of the odd is %d\\n\",s); return 0; }

#include//5-4 #include

double distance(double x1,double y1, double x2, double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); }

int main(){

double x1, y1, x2, y2; printf(\"Input (x1, y1):\"); scanf(\"%lf%lf\",&x1,&y1); printf(\"Input (x2, y2):\"); scanf(\"%lf%lf\",&x2,&y2);

printf(\"distance=%.2f\\n\",distance(x1,y1,x2, y2)); return 0; }

#include//5-5 #include int prime(int m){

int k,sqrtm=sqrt((double)m); for(k=2;k<=sqrtm;k++) if(m%k==0) return 0; return 1; }

int main(){

int m,n,c=0,s=0,k;

printf(\"Input m:\"); scanf(\"%d\",&m); printf(\"Input n:\"); scanf(\"%d\",&n);

for(k=(m==1?2:m);k<=n;k++) if(prime(k)){ c++; s+=k; }

printf(\"count=%d, sum=%d\\n\",c,s); return 0; }

#include//5-6

int countdigit(int number, int digit){ int c=0; if(number<0)

number=-number; do{

if(number%10==digit) c++; number/=10; }while(number); return c; }

int main(){ int n;

printf(\"Enter an integer:\"); scanf(\"%d\",&n);

printf(\"Number of digit 2: %d\\n\",countdigit(n,2)); return 0; }

#include//5-7 int is(int n){ int s=0,x,m=n; do{

x=m%10; s+=x*x*x; m/=10; }while(m); if(s==n)

return 1; return 0; }

int main(){

int m,n,i;

printf(\"Input m:\"); scanf(\"%d\",&m); printf(\"Input n:\"); scanf(\"%d\",&n); for(i=m;i<=n;i++) if(is(i))

printf(\"%d\\n\",i); return 0; }

#include//5-8 double fact(int n); int main(void){ int i;

double sum=0.0;/*需要初始化为*/ for(i=1;i<=10;i++)/*上界是*/ sum=sum+fact(i);

printf(\"1!+2!+...+10!=%f\\n\",sum); return 0; }

double fact(int n){ int i;

double result=1.0; for(i=1;i<=n;i++)

result=result*i;/*result中累积n!*/ return result; }

实验7

#include int main(){//7-2-4

int a[6][6],n,i,j,flag=1; printf(\"Input n:\"); scanf(\"%d\",&n);

printf(\"Input array:\\n\"); for(i=0;iscanf(\"%d\",&a[i][j]); for(j=0;j}

if(flag==0) break; }

if(flag==0)

printf(\"No\\n\"); else

printf(\"Yes\\n\"); return 0; }

#include int main(){//7-2-6

int a[6][6],n,i,j,row,col; printf(\"Input n:\"); scanf(\"%d\",&n);

printf(\"Input array:\\n\"); for(i=0;iscanf(\"%d\",&a[i][j]);

for(i=0;ifor(j=0;ja[row][col]){ col=j; }

for(j=0;jif(j>=n){//找到鞍点元素a[row][col]

printf(\"a[%d][%d]=%d\\n\",row,col,a[row][col]); break; } }

if(i>=n)/*所有行都检测过没有找到鞍点*/ printf(\"NO\\n\"); return 0; }

#include int main(){//7-3-2 char c,s[80]; int i,index=-1;

printf(\"Input a character:\"); c=getchar(); getchar();

printf(\"Input a string:\"); gets(s); i=0;

while(s[i]!='\\0'){ if(s[i]==c) index=i; i++; }

if(index>-1)

printf(\"index=%d\\n\",index); else

printf(\"Not found\\n\"); return 0; }

#include int main(){//7-3-3 char s[80];

int i,upcase=0,count=0; printf(\"Input a string:\"); gets(s); i=0;

while(s[i]!='\\0'){

if(s[i]>='A'&&s[i]<='Z'){ upcase++; switch(s[i]){ case 'A': case 'E': case 'I': case 'O':

case 'U':count++; } } i++; }

if(upcase)

count=upcase-count;

printf(\"count=%d\\n\",count); return 0; }

#include int main(){//7-3-4 char s[80]; int i;

printf(\"Input a string:\");

gets(s); i=0;

while(s[i]!='\\0'){

if(s[i]>='A'&&s[i]<='Z') s[i]='Z'-(s[i]-'A'); i++; }

printf(\"After replaced: %s\\n\",s); return 0; }

#include int main(){//7-3-5 char s[80]; int i,n=0;

printf(\"Input a string:\"); gets(s); i=0;

while(s[i]!='\\0'){

if(s[i]>='0'&&s[i]<='9'){ n=n*16+s[i]-'0'; i++;

continue; }

switch(s[i]){ case 'a':

case 'A':n=n*16+10;break; case 'b':

case 'B':n=n*16+11;break; case 'c':

case 'C':n=n*16+12;break; case 'd':

case 'D':n=n*16+13;break; case 'e':

case 'E':n=n*16+14;break; case 'f':

case 'F':n=n*16+15;break; } i++; }

printf(\"Hex=%d\\n\",n); return 0; }

实验8

#include//8-1-1

void move(int *, int, int); int main(){

int m, n, i, a[80], *p; printf(\"Input n, m:\"); scanf(\"%d%d\",&n, &m); for(p=a,i=0;iscanf(\"%d\",p+i);/*将&p++换成p+i*/ move(a,n,m);

printf(\"After moved:\"); for(i=0;iprintf(\"%5d\",a[i]); return 0; }

void move(int *x,int n, int m){ int i,j,k;

for(i=0;ik=x[n-1];/*暂存x[n-1],因为它将被覆盖*/ for(j=n-1;j>0;j--)

x[j]=x[j-1];/*设置断点*/ x[0]=k; } }

#include//8-1-2 #include

int search(int *, int, int); int main(){

int *a,n,i,x,index; printf(\"Input n:\"); scanf(\"%d\",&n);

if((a=(int*)malloc(n*sizeof(int)))!=NULL) exit(1);

printf(\"Input %d integers:\",n); for(i=0;iindex=search(a,n,x); if(index==-1)

printf(\"Not found\\n\"); else

printf(\"index=%d\\n\",index+1); free(a); return 0; }

int search(int *a,int n,int x){ int i;

for(i=0;i#include//8-1-3 #include void sort(int*,int); int main(){ int i,n,*a;

printf(\"Input n:\"); scanf(\"%d\",&n);

if((a=(int*)malloc(n*sizeof(int)))!=NULL) exit(1);

printf(\"Input %d integers:\",n); for(i=0;iprintf(\"After sorted the array is:\"); for(i=0;iprintf(\"%d \",a[i]); printf(\"\\n\"); return 0; }

int max(int *a,int n){ int i,index=0; for(i=1;iif(a[i]>a[index]) index=i; return index; }

void swap(int *x,int *y){ int t=*x; *x=*y; *y=t; }

void sort(int *a,int n){ int i,j;

for(i=n;i>0;i--){ j=max(a,i);

swap(a+i-1,a+j); }

}

#include//8-1-4 #include int main(){

int n,i,total,token,*a; printf(\"Input n:\"); scanf(\"%d\",&n);

if((a=(int*)malloc(n*sizeof(int)))==NULL) exit(1);

for(i=0;iwhile(total>1){

token=0;/*报数的当前值*/

while(token<3){/*未出队的依次报数*/ if(a[i]!=0) token++;

if(token<3)/*未满m继续报数*/ i=(i+1)%n; }

a[i]=0;/*报到的出队*/ i=(i+1)%n;

total--;/*队中人数减*/ }

for(i=0;iprintf(\"Last No. is: %d\\n\",a[i]); free(a); return 0; }

#include//8-2-2 #include int main(){

char s[80],m[80]; int length,max=0,i;

printf(\"Input 5 strings: \"); for(i=0;i<5;i++){ scanf(\"%s\",s); length=strlen(s); if(length>max){ max=length; strcpy(m,s);

} }

printf(\"The longest is: %s\\n\",m); return 0; }

#include//8-2-3 #include

void delchar(char *s,char c){ int i=0;

while(*s!='\\0'){

if(*s==c)/*遇到字符c*/

strcpy(s,s+1);/*将该字符后的部分拷贝成从当前位置开始的串*/ s++; } }

int main(){

char s[80],c;

printf(\"Input a string: \"); gets(s);

printf(\"Input a char: \"); scanf(\"%c\",&c); delchar(s,c);

printf(\"After deleted, the string is: %s\\n\",s); return 0; }

#include//8-2-4 #include

void strmcpy(char *s, char *t, int m){ strcpy(s,t+m-1); }

int main(){

char s[80],t[80]; int m;

printf(\"Input a string: \"); gets(t);

printf(\"Input a integer: \"); scanf(\"%d\",&m); strmcpy(s,t,m);

printf(\"Output is: %s\\n\",s); return 0; }

#include//8-2-5 #include int is_sym(char *s){

int i=0,j=strlen(s)-1; while(iif(s[i]!=s[j]) return 0; i++; j--; }

return 1; }

int main(){ char s[80];

printf(\"Input a string: \"); gets(s);

if(is_sym(s))

printf(\"YES\\n\"); else

printf(\"NO\\n\"); return 0; }

#include//8-2-6 int main(){ char s[80];

int up=0,low=0,space=0,digit=0,other=0,i; printf(\"Input a string: \"); gets(s);

for(i=0;s[i]!='\\0';i++){

if(s[i]>='A'&&s[i]<='Z'){ up++;

continue; }

if(s[i]>='a'&&s[i]<='z'){ low++; continue; }

if(s[i]>='0'&&s[i]<='9'){ digit++; continue; }

if(s[i]==' '||s[i]=='\'){ space++; continue; }

other++; }

printf(\"大写字母个数为:%d\\n小写字母个数为:%d\\n空格个数为:%d\\n数字个数为:%d\\n其他字符个数为:%d\\n\",up,low,space,digit,other); return 0; }

实验9

#include//9-2 int main(){ struct{

int hour,minit,second; }time; int n;

printf(\"输入时间:\");

scanf(\"%d:%d:%d\",&time.hour,&time.minit,&time.second); printf(\"输入秒:\"); scanf(\"%d\",&n);

time.second=time.second+n; time.minit+=time.second/60; time.second=time.second%60; time.hour+=time.minit/60; time.minit%=60; time.hour%=24;

printf(\"新时间:%d:%d:%d\\n\",time.hour,time.minit,time.second); return 0; }

#include//9-3 int main(){

struct student{ int num;

char name[20]; double score; }stu; int n,i;

double sum=0.0; printf(\"输入n:\"); scanf(\"%d\",&n); for(i=0;iprintf(\"输入%d个学生的学号、姓名和成绩:\",i+1); scanf(\"%d%s%lf\",&stu.num,stu.name,&stu.score); sum+=stu.score; }

printf(\"平均成绩:%.2f\\n\",sum/n); return 0; }

#include//9-4 int main(){

struct complex{ int a,b; }x,y,z;

printf(\"输入a1, a2, b1, b2:\");

scanf(\"%d%d%d%d\",&x.a,&x.b,&y.a,&y.b); z.a=x.a*y.a-x.b*y.b; z.b=x.a*y.b+x.b*y.a;

printf(\"(%d+%di)×(%d+%di)=%d+%di\\n\",x.a,x.b,y.a,y.b,z.a,z.b); return 0; }

#include//9-5 int main(){

struct book{

char name[80]; double price; }abook,min,max; int n,i;

printf(\"输入n:\"); scanf(\"%d\",&n); min.price=10000; max.price=-1; for(i=0;iprintf(\"输入第%d本书的名称和定价:\",i+1); scanf(\"%s%lf\",abook.name,&abook.price); if(min.price>abook.price) min=abook;

if(max.priceprintf(\"价格最高的书: %s ,价格:%.1f\\n\",max.name,max.price); printf(\"价格最低的书: %s ,价格:%.1f\\n\",min.name,min.price); return 0; }

#include//9-6 int main(){

struct addres{ char name[20]; int birthday; char phon[80]; }person[10],tmp; int n,i,j,index; printf(\"输入n:\");

scanf(\"%d\",&n); for(i=0;iprintf(\"输入第%d个人的姓名、生日、电话号码:\",i+1);

scanf(\"%s%d%s\",person[i].name,&person[i].birthday,person[i].phon); }

for(i=0;ifor(j=1;jif(person[j].birthday>person[index].birthday) index=j; tmp=person[n-i-1];

person[n-i-1]=person[index]; person[index]=tmp; }

for(i=0;iprintf(\"%s %d %s\\n\",person[i].name,person[i].birthday,person[i].phon);

return 0; }

#include//9-7 struct date{

int year,month,day; };

int day_of_year(struct date date){ int k,leap,d=date.day; int

tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};

leap=(date.year%4==0&&date.year%100!=0||date.year%400==0); for(k=0;kint main(){

struct date date; printf(\"输入日期:\");

scanf(\"%d%d%d\",&date.year,&date.month,&date.day);

printf(\"该日期是%d年第%d天\\n\",date.year,day_of_year(date)); return 0; }

#include//9-8

struct time{

int hour,minit,second; };

void chang_time(struct time* t,int n){ t->second=t->second+n; t->minit+=t->second/60; t->second=t->second%60; t->hour+=t->minit/60; t->minit%=60; t->hour%=24; }

int main(){

struct time t; int n;

printf(\"输入时间:\");

scanf(\"%d:%d:%d\",&t.hour,&t.minit,&t.second); printf(\"输入秒:\"); scanf(\"%d\",&n); chang_time(&t,n);

printf(\"新时间:%d:%d:%d\\n\",t.hour,t.minit,t.second); return 0; }

实验10

#include //10-2 #include int is_sqr(int m){

int s=sqrt((double)m); return s*s==m; }

int same_dig(int m){ int d[3],i,j; d[0]=m%10;

d[1]=(m/10)%10; d[2]=m/100;

for(i=0;i<3;i++)

for(j=i+1;j<3;j++) if(d[i]==d[j]) return 1; return 0; }

int fun(int number){ int i,c=0;

for(i=101;i<=number;i++)

if(is_sqr(i)&&same_dig(i))

c++; return c; }

int main(){

int number,i,count;

printf(\"Enter a number: \"); scanf(\"%d\",&number); count=fun(number);

printf(\"count=%d\\n\",count); return 0; }

#include //10-3 #include int prime(int m){

int k,sqrtm=sqrt((double)m); for(k=2;k<=sqrtm;k++) if(m%k==0) return 0; return 1; }

int main(){

int i,n,x,count=0; printf(\"Enter n:\"); scanf(\"%d\",&n);

printf(\"Enter %d numbers:\",n); for(i=0;iprintf(\"count=%d\\n\",count); return 0; }

#include //10-4 double P(int n, double x){ if(n==0)

return 1.0; if(n==1)

return x;

return ((2*n-1)*P(n-1,x)-(n-1)*P(n-2,x))/n; }

int main(){ int n; double x;

printf(\"Enter n:\"); scanf(\"%d\",&n); printf(\"Enter x:\"); scanf(\"%lf\",&x);

printf(\"P(%d,%.2f)=%.2f\\n\",n,x,P(n,x)); return 0; }

#include //10-5 int Ack(int m, int n){ if(m==0)

return n+1; if(n==0)

return Ack(m-1,1);

return Ack(m-1,Ack(m,n-1)); }

int main(){ int m,n;

printf(\"Enter m:\"); scanf(\"%d\",&m); printf(\"Enter n:\"); scanf(\"%d\",&n);

printf(\"Ackerman(%d,%d)=%d\\n\",m,n,Ack(m,n)); return 0; }

#include //10-6 void dectobin(int n){ if(n<=1){

printf(\"%d\",n); return; }

dectobin(n/2); printf(\"%d\",n%2); }

int main(){ int n;

printf(\"Enter n:\"); scanf(\"%d\",&n); dectobin(n); printf(\"\\n\"); return 0; }

#include //10-7 int f(int n){ if(n==0)

return 0; if(n==1)

return 1;

return f(n-1)+f(n-2); }

int main(){ int n;

printf(\"Enter n:\"); scanf(\"%d\",&n);

printf(\"f(%d)=%d\\n\",n,f(n)); return 0; }

#include //10-8 void inverse(int n){ if(n<10){

printf(\"%d\",n); return; }

printf(\"%d\",n%10); inverse(n/10); }

int main(){ int number;

printf(\"Enter a number:\"); scanf(\"%d\",&number); inverse(number); return 0; }

#include //10-9 int main(){

double fun(int n,double x);/*定义在调用之后,需声明*/ int n;

double x,root;

printf(\"Enter x:\"); scanf(\"%lf\",&x); printf(\"Enter n:\"); scanf(\"%d\",&n); root=fun(n,x);

printf(\"Root=%.2f\\n\",root); return 0; }

double fun(int n,double x){ if(n==1)/*指数为,应返回x*/ return x;

else

return x*fun(n-1,x); }

实验11

#include//11-1-2 int main(){ char*

months[12]={\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"}; int month;

printf(\"Enter a month: \"); scanf(\"%d\",&month);

printf(\"%s\\n\",months[month-1]); return 0; }

#include//11-1-3 #include int main(){ char

*weekdays[7]={\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"},s[80]; int i; gets(s);

for(i=0;i<7;i++)

if(strcmp(weekdays[i],s)==0) break; if(i<7)

printf(\"%d\\n\",i+1); else

printf(\"-1\\n\"); return 0; }

#include//11-1-4 #include #include

int max_len(char *s[],int n){ int maxLen=0,i; for(i=0;iif(strlen(s[i])>maxLen) maxLen=strlen(s[i]); }

return maxLen;

}

int main(){

char* s[10]; int n,i;

printf(\"Enter n: \"); scanf(\"%d\",&n); for(i=0;is[i]=(char*)malloc(80*sizeof(char)); scanf(\"%s\",s[i]); }

printf(\"length=%d\\n\",max_len(s,n)); return 0; }

#include//11-1-5

char * str_cat(char *s, char *t){ char *p=s,*q=t; while(*p) p++; while(*q){ *p=*q; p++;q++; }

*p='\\0'; return s; }

int main(){

char s[80],t[80]; gets(s); gets(t);

puts(str_cat(s,t)); return 0; }

#include//11-1-6 #include #include

char *sub_str(char *s, char a, char b){ int n=strlen(s),i,j;

char *t=(char*)malloc(n*sizeof(char));/*为t分配空间*/ for(i=0;s[i]!=a;i++);/*找到a在s中的位置*/ for(j=0;s[j]!=b;j++);/*找到b在s中的位置*/

strcpy(t,&s[i]);/*将s中从s[i]开始的部分拷贝到t*/

t[j-i+1]='\\0';/*将t中从值为b的元素后面的部分从串中去掉*/ return t; }

int main(){

char s[80],a,b; scanf(\"%s\",s); getchar();

scanf(\"%c\",&a); getchar();

scanf(\"%c\",&b);

printf(\"%s\\n\",sub_str(s,a,b)); return 0; }

#include//11-1-7 char *change(char s[][20]){ char t[10]; int i;

for(i=0;i<4;i++){ t[2*i]=s[i][8]; t[2*i+1]=s[i][9]; }

t[2*i]='\\0'; return t; }

int main(){

char s[4][20],*p; int i;

for(i=0;i<4;i++)

scanf(\"%s\",s[i]); p=change(s);

printf(\"%s\\n\",p);

return 0; }

#include//11-1-8

float matrix(float a[][3],int n){ int i,j;

float sum=0.0; for(i=0;i<3;i++)

for(j=0;j<3;j++){ if(i==j)

sum=sum+*(*(a+i)+j); if(j==2-i&&i!=j)

sum=sum+*(*(a+i)+j); }

return sum; }

int main(){ int i,j;

float a[3][3];

printf(\"输入×数组\\n\"); for(i=0;i<3;i++) for(j=0;j<3;j++)

scanf(\"%f\",&a[i][j]); printf(\"sum=%.2f\",matrix(a,3)); return 0; }

#include//11-2-1 #include #include struct stud_node{ int num;

char name[20]; int score;

struct stud_node* next; };

int main(){

struct stud_node *head, *tail, *p; int num,score; char name[20];

int size=sizeof(struct stud_node); head=tail=NULL;

printf(\"Input num,name,score:\\n\"); scanf(\"%d\",&num); while(num!=0){

p=(struct stud_node*)malloc(size); scanf(\"%s%d\",name,&score); p->num=num;

strcpy(p->name,name); p->score=score; p->next=NULL; if(head==NULL) head=p; else

tail->next=p; tail=p;

scanf(\"%d\",&num); }

for(p=head;p!=NULL;p=p->next)

printf(\"%d %s %d\\n\",p->num,p->name,p->score); return 0;

}

#include//11-2-2 #include #include struct stud_node{ int num;

char name[20]; int score;

struct stud_node* next; };

int main(){

struct stud_node *head, *tail, *p; int num,score,v; char name[20];

int size=sizeof(struct stud_node); head=tail=NULL; scanf(\"%d\",&num); while(num!=0){

p=(struct stud_node*)malloc(size); scanf(\"%s%d\",name,&score); p->num=num;

strcpy(p->name,name); p->score=score; p->next=NULL; if(head==NULL) head=p; else

tail->next=p; tail=p;

scanf(\"%d\",&num); }

scanf(\"%d\",&v);

for(p=head;p!=NULL;p=p->next){ if(p->score>=v)

printf(\"%d %s %d\\n\",p->num,p->name,p->score); }

return 0; }

#include//11-2-3 #include struct node{ int data;

struct node* next; };

int main(){

int size=sizeof(struct node),n; struct node *head=NULL,*p; scanf(\"%d\",&n); while(n>=0){

p=(struct node*)malloc(size); p->data=n; p->next=head; head=p;

scanf(\"%d\",&n); }

for(p=head;p!=NULL;p=p->next) printf(\"%d \",p->data); printf(\"\\n\"); return 0; }

#include//11-2-4 #include struct node{ int data;

struct node* next; };

int main(){

int size=sizeof(struct node),n;

struct node *head=NULL,*tail=NULL,*p,*q; scanf(\"%d\",&n); while(n>=0){

p=(struct node*)malloc(size); p->data=n; p->next=NULL; if(head==NULL) head=p; else

tail->next=p; tail=p;

scanf(\"%d\",&n); }

p=head;q=NULL;

while(p!=NULL){/*扫描链表*/

if(p->data%2==0){/*当前节点数据为偶数*/ if(q==NULL){/*若欲删除的节点为链表首*/ head=p->next;/*将p从链表中摘取*/ free(p);/*释放节点p*/

p=head;/*p指向下一个节点*/

}else{

q->next=p->next;/*将p从链表中摘取*/ free(p);/*释放节点p*/

p=q->next;/*p指向下一个节点*/ } }else{

q=p;/*q指向当前节点的前一个节点*/ p=q->next;/*调整当前节点指针*/ } }

for(p=head;p!=NULL;p=p->next) printf(\"%d \",p->data); printf(\"\\n\"); return 0; }

#include//11-2-5 #include struct node{ int data;

struct node* next; };

struct node* merge(struct node *l1,struct node *l2){/*将两个有序链表合并成一个有序链表*/

struct node *head=NULL,*tail=NULL,*p=l1,*q=l2;/*p,q分别指向两个链表的表首*/

while(l1!=NULL&&l2!=NULL)

if(l1->datadata){/*l1的表首小于l2的表首,将p从l1中摘下并添加到新表中*/

if(head==NULL)/*若新表为空*/ head=p; else

tail->next=p; tail=p;

l1=p->next;/*调整l1的新表首*/ p=l1;/*p跟踪l1的表首*/

}else{/*l2的表首不大于l1的表首,将q从l2中摘下并添加到新表中*/ if(head==NULL)/*若新表为空*//*若新表为空*/ head=q; else

tail->next=q; tail=q;

l2=q->next;/*调整l2的新表首*/ q=l2;/*q跟踪l2的表首*/ }

if(l1!=NULL)/*若l1还有元素*/ tail->next=l1;

if(l2!=NULL)/*若l2还有元素*/ tail->next=l2; return head; }

int main(){

int size=sizeof(struct node),n;

struct node *head1=NULL,*head2=NULL,*tail=NULL,*p,*head; scanf(\"%d\",&n);

while(n>=0){/*创建第一个有序链表*/ p=(struct node*)malloc(size); p->data=n; p->next=NULL; if(head1==NULL) head1=p; else

tail->next=p; tail=p;

scanf(\"%d\",&n); }

tail=NULL;

scanf(\"%d\",&n);

while(n>=0){/*创建第二个有序链表*/ p=(struct node*)malloc(size); p->data=n; p->next=NULL; if(head2==NULL) head2=p; else

tail->next=p; tail=p;

scanf(\"%d\",&n); }

head=merge(head1,head2);/*将两个有序链表合并成一个有序链表*/ for(p=head;p!=NULL;p=p->next)/*输出*/ printf(\"%d \",p->data); printf(\"\\n\"); return 0; }

#include//11-2-6 #include struct node{ int data;

struct node* next; };

int main(){

int size=sizeof(struct node),n;

struct node *head=NULL,*tail=NULL,*NEW=NULL,*p,*q; scanf(\"%d\",&n); while(n>=0){

p=(struct node*)malloc(size); p->data=n; p->next=NULL; if(head==NULL) head=p; else

tail->next=p; tail=p;

scanf(\"%d\",&n); }

p=head;q=NULL,tail=NULL; while(p!=NULL){/*扫描链表*/

if(p->data%2==0){/*当前节点数据为偶数*/ if(q==NULL){/*若欲删除的节点为链表首*/ head=p->next;/*将p从链表中摘取*/

if(NEW==NULL)/*加入新表中的元素是第一个*/ NEW=p; else

tail->next=p; tail=p;

p=head;/*p指向下一个节点*/ }else{

q->next=p->next;/*将p从链表中摘取*/ if(NEW==NULL) NEW=p; else

tail->next=p; tail=p;

p=q->next;/*p指向下一个节点*/ } }else{

q=p;/*q指向当前节点的前一个节点*/ p=q->next;/*调整当前节点指针*/ } }

tail->next=NULL;

for(p=NEW;p!=NULL;p=p->next)

printf(\"%d \",p->data); printf(\"\\n\"); return 0; }

实验12

#include//12-1 #include int main(){ char ch; FILE* fp;

if((fp=fopen(\"a.txt\",\"w\"))==NULL){ printf(\"Can’t open file!\"); exit(0); }

while((ch=getchar())!=’\\n’) fputc(ch,fp); fclose(fp); return 0; }

#include//12-2 #include int main(){ char ch;

int alphbet=0,digit=0,other=0; FILE* fp;

if((fp=fopen(\"a.txt\",\"r\"))==NULL){ printf(\"Can’t open file!\"); exit(0); }

for(ch=getchar();!feof(fp);ch=getchar())

if(ch>=’A’&&ch<=’Z’||ch>=’a’&&ch<=’z’) alphbet++;

else if(ch>=’’&&ch<=’’) digit++; else

other++;

printf(\"letters: %d, digits: %d, others: %d\\n\",alphbet,digit,other); fclose(fp); return 0; }

#include//12-3 #include int main(){

double x; FILE *fp;

if((fp=fopen(\"b.txt\",\"w\"))==NULL){ printf(\"Can’t open file!\"); exit(0); }

scanf(\"%lf\",&x); while(x!=-1.0){

fprintf(fp,\"%f\",x); scanf(\"%lf\",&x); }

fclose(fp); return 0; }

#include//12-4解 #include typedef struct{ char number[8]; char name[10]; int math; int chiness; int english; int total; float ave; }student;

int main(void){ student stu; int i; FILE *fp;

if((fp=fopen(\"f3.txt\",\"w\"))==NULL){/*打开文件写*/ printf(\"Canot open this file\\n\"); exit(0); }

for(i=0;i<10;i++){/*输入个学生的数据*/ printf(\"请输入学号: \"); scanf(\"%s\",stu.number); printf(\"请输入姓名: \"); scanf(\"%s\",stu.name);

printf(\"请输入数学成绩: \"); scanf(\"%d\",&stu.math);

printf(\"请输入语文成绩: \"); scanf(\"%d\",&stu.chianess); printf(\"请输入英语成绩: \"); scanf(\"%d\",&stu.english);

fprintf(fp,\"%s %s %d %d %d\\n\",stu.number,stu.name,stu.math,stu.chianess,stu.english); }

fclose(fp);

if((fp=fopen(\"f3.txt\",\"r\"))==NULL){/*打开文件读*/ printf(\"Canot open this file\\n\"); exit(0); }

for(i=0;i<10;i++){/*读个学生的数据并计算总分和平均分*/

fscanf(fp,\"%s %s %d %d %d\\n\",stu.number,stu.name,&stu.math,&stu.chianess,&stu.english);

stu.total=stu.math+stu.chianess+stu.english; stu.ave=(float)stu.total/3;

printf(\"%s %s %4d %4d %4d %4d %5.1f\\n\",stu.number,stu.name,stu.math,stu.chianess,stu.english,stu.total,stu.ave); }

fclose(fp);

return EXIT_SUCCESS; }

int main(void){//12-4解 student stu; int i,size=; FILE *fp;

if((fp=fopen(\"f3.dat\",\"wb\"))==NULL){/*打开文件写*/ printf(\"Canot open this file\\n\"); exit(0); }

for(i=0;i<10;i++){/*输入个学生的数据*/ printf(\"请输入学号: \"); scanf(\"%s\",stu.number); printf(\"请输入姓名: \"); scanf(\"%s\",stu.name);

printf(\"请输入数学成绩: \"); scanf(\"%d\",&stu.math);

printf(\"请输入语文成绩: \"); scanf(\"%d\",&stu.chianess); printf(\"请输入英语成绩: \"); scanf(\"%d\",&stu.english);

stu.total=stu.math+stu.chianess+stu.english; stu.ave=(float)stu.total/3;

fwrite(&stu,sizeof(student),1,fp);

}

fclose(fp);

if((fp=fopen(\"f3.dat\",\"rb\"))==NULL){/*打开文件读*/ printf(\"Canot open this file\\n\"); exit(0); }

for(i=0;i<10;i++){/*读个学生的数据并计算总分和平均分*/ fread(&stu,sizeof(student),1,fp);

printf(\"%s %s %4d %4d %4d %4d %5.1f\\n\",stu.number,stu.name,stu.math,stu.chianess,stu.english,stu.total,stu.ave); }

fclose(fp);

return EXIT_SUCCESS; }

#include//12-5 #include int main(void){ FILE *fp1, *fp2; char ch1,ch2; int row=0,col=0;

if((fp1=fopen(\"file1.txt\",\"r\"))==NULL){/*打开文件读*/ printf(\"Canot open this file\\n\"); exit(0); }

if((fp2=fopen(\"file2.txt\",\"r\"))==NULL){/*打开文件读*/ printf(\"Canot open this file\\n\"); exit(0); }

ch1=fgetc(fp1); ch2=fgetc(fp2);

while(!feof(fp1)&&!feof(fp2)&&(ch1==ch2)){/*文件未结束且两个文件的内容相同*/

if(ch1==’\\n’){/*一行结束*/ row++; col=0; }else col++;

ch1=fgetc(fp1); ch2=fgetc(fp2); }

if(!feof(fp1)||!feof(fp2))

printf(\"%d %d\\n\",row+1,col+1); else

printf(\"The files are same.\\n\"); return (EXIT_SUCCESS); }

#include//12-6 #include int main(void){ FILE *fp;

char ch,file_name[80]; int rows=0;

scanf(\"%s\",file_name);/*输入文件名*/

if((fp=fopen(file_name,\"r\"))==NULL){/*打开文件读*/ printf(\"Canot open this file\\n\"); exit(0); }

while((ch=fgetc(fp))!=EOF){/*只要文件未结束*/ if(ch==’\\n’)/*一行结束*/ rows++;

if(ch>=’A’&&ch<=’Z’)/*遇到大写字母*/ ch=’a’+ch-’A’; putchar(ch); }

printf(\"This file have %d rows.\\n\", rows+1);/*输出行数*/ return (EXIT_SUCCESS); }

#include//12-7 #include int main(){

FILE *fp;/*必须是文件类型指针*/ int n,sum=0;/*和数需初始化为*/

if((fp=fopen(\"int_data.dat\",\"r+\"))==NULL){/*文件打开方式必须是读写*/ printf(\"Can’t open file!\\n\"); exit(0); }

while(fscanf(fp,\"%d\",&n)!=EOF)/*循环条件应该是文件未读完*/ sum=sum+n;

fprintf(fp,\"%d\",sum); fclose(fp); return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容