(完整word版)《C语言程序设计》(苏小红) 课后习题答案 高等教育出版社
C语言程序设计苏小红课后习题答案高等教育出版社
include<>main{float x=,y=,z=;printf"x=%f\n",x;printf"y=%f\n",y;printf"z=%f\n",z;}1include<>main{int a=12,b=3;float x=,y=;printf"%f\n",floatab/2;printf"%d\n",intx%inty;}2include<>main{int x=32,y=81,p,q;p=x++;q=--y;printf"%d %d\n",p,q;printf"%d %d\n",x,y;}include<>main{int x,b0,b1,b2,s;printf"Inputx:";scanf"%d",&x;b2=x/100;b1=x-b2100/10;f\n",p1;printf"骰子出现2的概率:printf"骰子出现3的概率:printf"骰子出现4的概率:printf"骰子出现5的概率:printf"骰子出现6的概率:}1void Swapint x,int y{int pTemp;.\n";else ifstrcmpuserInput,password<0printf"Invalid passworduser input<password...\n";elseprintf"Invalid passworduser input>password...\n";return0;}include<>define N24unsigned int CountLetterchar str;int main{char aN;printf"Input a letter:\n";getsa;printf"The length of the letter is:%d\n",CountLettera;return0;}unsigned int CountLetterchar str{char p=str;int c=0,flag=0;whilep='\0'{ifp=''flag=1;else ifflag==1{c++;flag=0;}p++;}return c+1;}include<>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a14;char b10;char c6;}WORK;typedef struct student{char studentName10;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",,,,0;}include<>include<>define N10typedef struct people{char peopleName10;}PEOPLE;int main{int sum1=0,sum2=0,sum3=0,sum4=0;int i;char nameNN;PEOPLE stu5={"zhang","li","wang"};printf"Enter their name:\n";fori=0;i<10;i++{getsnamei;ifstrcmpnamei,stu0.peopleName==0sum1++;else ifstrcmpnamei,stu1.peopleName==0sum2++;else ifstrcmpnamei,stu2.peopleName==0sum3++;elsesum4++;}printf"stu:%4s%2d\n",stui.peopleName,sumj;printf"stu:%4s%2d\n",stu1.peopleName,sum2;printf"stu:%4s%2d\n",stu2.peopleName,sum3;printf"%2d\n",sum4;return0;}。
第三版苏小红c语言答案C语言程序设计苏小红版答案
第三版苏小红c语言答案C语言程序设计苏小红版答案导读:就爱阅读网友为您分享以下“C语言程序设计苏小红版答案”的资讯,希望对您有所帮助,感谢您对 的支持!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("%d\n",(float)(a*b)/2); printf("%d\n",(int)x%(int)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;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("Inputa,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.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c'; printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); } 4.1(3) #include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&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("Innputa:"); 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("Inputa:"); scanf("%d",&a); if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}11。
苏小红c语言程序设计第二版课后答案
苏小红c语言程序设计第二版课后答案苏小红教授所著的《C语言程序设计》第二版是计算机科学与技术专业学生学习C语言的经典教材之一。
该书不仅系统地介绍了C语言的基础知识,还通过丰富的实例和练习题帮助学生加深理解。
以下是部分课后习题的答案,以供参考:第一章:C语言概述1. 问题一:简述C语言的特点。
- 答案: C语言是一种结构化编程语言,具有高效性、灵活性和可移植性等特点。
它支持多种数据类型,拥有丰富的运算符,并且能够直接访问内存地址。
2. 问题二:为什么说C语言是高级语言?- 答案: C语言被称为高级语言是因为它抽象了底层硬件操作,使得程序员可以不必关心计算机的硬件细节,而专注于程序的逻辑结构。
第二章:数据类型、运算符和表达式1. 问题一:说明C语言中基本数据类型的种类及其特点。
- 答案: C语言中的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)。
每种类型都有其特定的取值范围和占用内存大小。
2. 问题二:解释赋值运算符的右结合性。
- 答案:赋值运算符的右结合性意味着当多个赋值运算符同时出现时,从右向左进行计算。
例如,a = b = c; 会先计算 b = c,然后将结果赋值给 a。
第三章:控制结构1. 问题一:什么是条件语句?举例说明。
- 答案:条件语句是程序中根据条件是否满足来决定执行哪一段代码的结构。
例如,if语句就是典型的条件语句。
2. 问题二:循环语句有哪些?它们各自的特点是什么?- 答案: C语言中有三种循环语句:for循环、while循环和do-while循环。
for循环常用于已知循环次数的情况;while循环适用于循环次数未知,需要在循环体内部判断是否继续循环;do-while循环至少执行一次循环体,然后在循环末尾判断是否继续。
第四章:数组1. 问题一:一维数组和多维数组的区别是什么?- 答案:一维数组是线性的,只有一个索引来访问数组元素;而多维数组可以看作是数组的数组,需要多个索引来定位元素。
C语言程序设计 苏小红版 第五六单元 答案
printf("该方程不是一元二次方程.\n"); } else if(b*b-4*a*c>0) {
d=sqrt(b*b-4*a*c); x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a); printf("x1=%f,x2=%f\n",x1,x2); } else if(b*b-4*a*c==0)
#include<stdio.h> #include<math.h> main() {
float a,b,c,s,area; printf("请输入三边长:"); scanf("%f,%f,%f",&a,&b,&c); s=(a+b+c)/2; area=(float)sqrt(s*(s-a)*(s-b)*(s-c)); if((a+b>c)&&(a+c>b)&&(b+c>a)) {
printf("%d 年是闰年\n",year); } else {
printf("%d 年不是闰年\n",year); } }
5.7 #include<stdio.h> main() {
char ch; printf("请输入一个字母:"); ch=getchar(); if((ch>=65)&&(ch<=90)) {
int a,b; printf("请输入一个整数:"); scanf("%d",&a); b=a%2; if(b==0) {
《C语言程序设计》(苏小红)-课后习题答案-高等教育出版社
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;}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);}习题44.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(t=0;t<100000000;t++);}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;elsereturn 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;elsedo{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 N40int 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 N40int 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 N40int 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 N40int 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 N5void 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 M3void 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 N7void 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 N12void 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 N6000void 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 N10void 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");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned 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.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef 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++;elsesum4++;}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;}。
最新《C语言程序设计》(苏小红) 课后习题答案 高等教育出版社资料
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 Array 4.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2)main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b);}#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(t=0;t<100000000;t++);}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;elsereturn 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;elsedo{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 N40int 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 N40int 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 N40int 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 N40int 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 N5void 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 M3void 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 N7void 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 N12void 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 N6000void 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 N10void 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");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned 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.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef 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++;elsesum4++;}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;}精品文档。
C语言程序的设计苏小红版答案
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("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)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;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("Inputa,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.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&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("Innputa:");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("Inputa:");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("Inputa,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>{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);}{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){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2: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);return 0;}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(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return 0;}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);return 0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn 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));return 0;}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));}return 0;}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);return 0;}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));return 0;}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));return 0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{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);return 0;}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]);}return 0;}8.2(1) int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return 0;}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);return 0;}8.3 void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4 #include<stdio.h>#define N 40int 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));return 0;}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 N 40int 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));return 0;}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 N 40int 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);return 0;}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 N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return 0;}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 N 5void 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 N 2#define M 3void 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 N 7void 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 N 12void 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 N 6000void 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);return 0;}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]);}return 0;}void Swap(int *x,int *y){int temp;temp=*x;*x=*y;*y=temp;}9.4 #include<stdio.h>#define N 10void 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);return 0;}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.1 void 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;}return 1;}10.3(4) int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0') return 0;}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");elseprintf("Invalid password!user input>password...\n");return 0;}10.4 #include<stdio.h>#define N 24unsigned 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));return 0;}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.studentSe x,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return 0;}12.4 #include<stdio.h>#include<string.h>#define N 10typedef 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++;elsesum4++;}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);return 0;}。
《C语言程序设计》 课后习题答案 高等教育出版社
《C语言程序设计》课后习题答案高等教育出版社《C语言程序设计》课后习题答案高等教育出版社第一章:C语言概述1. C语言的特点C语言是一种以处理底层任务和系统编程为目标的高级编程语言。
其特点包括语法简洁、执行效率高、可移植性强等。
第二章:C语言基本数据类型1. C语言中的基本数据类型C语言中的基本数据类型包括整型、字符型、浮点型等。
整型可以进一步细分为有符号整型和无符号整型。
第三章:C语言运算符1. C语言中的运算符C语言中常见的运算符包括算术运算符、赋值运算符、关系运算符、逻辑运算符等。
这些运算符用于执行各种数学和逻辑操作。
第四章:C语言控制语句1. C语言中的条件语句C语言提供了if语句和switch语句来实现条件判断。
if语句用于执行基于布尔表达式的条件分支,而switch语句用于根据不同的值执行不同的代码块。
第五章:C语言函数1. C语言中的函数定义和调用函数是C语言中的基本模块,用于封装可重用的代码。
函数定义包括函数返回类型、函数名、参数列表和函数体等部分。
第六章:C语言数组1. C语言中的数组定义和使用数组是一组相同类型的数据元素的集合。
C语言中可以使用数组来存储和操作大量数据。
第七章:C语言指针1. C语言中的指针概念指针是一种变量,它存储了内存地址。
通过指针,可以直接访问和修改对应内存地址中的数据。
第八章:C语言字符串1. C语言中的字符串操作字符串是由一系列字符组成的数据类型。
C语言通过字符数组来表示和操作字符串。
第九章:C语言结构体1. C语言中的结构体定义和使用结构体是一种自定义的复合数据类型,它可以包含多个不同类型的成员变量。
第十章:C语言文件操作1. C语言中的文件读写操作文件操作是一种重要的数据输入和输出方式。
C语言提供了一系列函数来实现文件的读写操作。
总结:通过解答以上习题,我们可以更好地掌握C语言的各个方面,提升我们的编程能力和解决问题的能力。
希望本文对读者有所帮助。
苏小红c语言程序设计课后答案
苏小红c语言程序设计课后答案苏小红的《C语言程序设计》是一本广泛使用的教材,它以清晰的结构和丰富的示例,帮助学生掌握C语言的基础知识和编程技巧。
课后答案对于学生来说是一个重要的学习资源,可以帮助他们检查自己的学习成果,加深对知识点的理解。
以下是一些可能的课后答案示例,用于帮助学生复习和理解C语言程序设计的相关概念。
第一章:C语言概述1. C语言的发展历史:C语言由Dennis Ritchie在20世纪70年代初期开发,最初用于UNIX操作系统的编写。
2. C语言的特点:C语言是一种结构化语言,具有高度的灵活性和强大的功能,能够进行底层系统编程。
第二章:C语言基础1. 数据类型:C语言提供了多种数据类型,包括整型(int)、浮点型(float和double)、字符型(char)等。
2. 变量声明:变量在使用前必须声明,声明时需要指定数据类型和变量名。
第三章:运算符和表达式1. 算术运算符:包括加(+)、减(-)、乘(*)、除(/)等。
2. 赋值运算符:如`=`,`+=`,`-=`等,用于给变量赋值或进行运算后赋值。
第四章:控制结构1. 条件语句:如`if`,`else if`,`else`,用于根据不同的条件执行不同的代码块。
2. 循环语句:包括`for`循环、`while`循环和`do-while`循环,用于重复执行代码块。
第五章:数组1. 一维数组:存储相同类型的元素,可以通过下标访问数组元素。
2. 多维数组:如二维数组,可以看作是数组的数组。
第六章:函数1. 函数定义:使用`return`类型和函数名来定义函数。
2. 函数调用:通过函数名和必要的参数来调用函数。
第七章:指针1. 指针变量:存储另一个变量的内存地址。
2. 指针运算:包括地址运算和指针的增减。
第八章:结构体和联合体1. 结构体:可以包含不同类型的数据成员。
2. 联合体:所有成员共享相同的内存位置。
第九章:预处理指令1. 宏定义:使用`#define`来定义常量或代码片段。
(word完整版)C语言程序设计课后习题1-8参考答案
C语言程序设计课后习题1—8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分?各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。
运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。
数据运算包括算术运算和逻辑运算。
控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的.存储器是计算机中具有记忆能力的部件,用来存放程序和数据.输入设备是用来输入程序和数据的部件。
输出设备正好与输入设备相反,是用来输出结果的部件。
2、简述计算机的工作原理。
答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。
整个过程由控制器进行控制。
3、计算机软件系统分为哪几类?答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。
系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。
这类软件一般与具体应用无关,是在系统一级上提供的服务。
系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。
另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。
在操作系统的基础上运行。
4、什么叫软件?说明软件与硬件之间的相互关系。
答:软件是指计算机程序及有关程序的技术文档资料。
两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。
在不太严格的情况下,认为程序就是软件。
硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作.在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。
《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社
printf("y=%f\n",y);printf("z=%f\n",z);}3.1(1)main(){}3.1(2)main(){}3.2main(){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.4main(){}习题44.1(1)main(){}4.1(2)main(){}4.1(3)main(){}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.1main(){{}{}}5.2main(){intscanf("%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);}{}}5.4main(){{}{}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{}{}{}}5.6main(){int{}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);}{}}5.8#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");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);}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;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);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;}}/*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);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);}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);}}{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("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>{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);hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();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;}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;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{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[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;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 N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int 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 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(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 M3void main()#define N7void 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){}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#define N6000void 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++;*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;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 N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum); int main(){int a[N];{*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;{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){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];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++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);。
最新C语言程序设计苏小红版第五六单元答案
C语言程序设计苏小红版第五六单元答案第五章5.1#include<stdio.h>void main(){double x;printf("请输入一个实数:\n");scanf("%lf",&x);if(x>0){printf("%f\n",x);}else{printf("%f\n",-x);}}5.2#include <stdio.h>main(){int a,b;printf("请输入一个整数:");scanf("%d",&a);b=a%2;if(b==0){printf("%d为偶数\n",a);}else{printf("%d为奇数\n",a);}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("请输入三边长:");scanf("%f,%f,%f",&a,&b,&c);s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));if((a+b>c)&&(a+c>b)&&(b+c>a)){printf("这个三角形的面积为%f\n",area);}else{printf("这三条边不能组成三角形\n");}}5.4#include<stdio.h>#include<math.h>void main(){double a,b,c,d,e,x1,x2;printf("请输入a,b,c:");scanf("%lf,%lf,%lf",&a,&b,&c);if(a==0){printf("该方程不是一元二次方程.\n");else if(b*b-4*a*c>0){d=sqrt(b*b-4*a*c);x1=(-b+d)/(2*a);x2=(-b-d)/(2*a);printf("x1=%f,x2=%f\n",x1,x2);}else if(b*b-4*a*c==0){x1=(-b)/(2*a);printf("x1=x2=%f\n",x1);}else if(b*b-4*a*c<0){printf("该方程没有实数解.\n");}}5.5#include<stdio.h>main(){int year,flag;printf("输入年份:");scanf("%d",&year);if(((year%4==0)&&(year%100!==0))||(year%400==0)){flag=1;}else{flag=0;}if(flag==1){printf("%d是闰年\n",year);else{printf("%d不是闰年\n",year);}}5.6#include<stdio.h>void main(){int year,flag;printf("输入年份:");scanf("%d",&year);flag=(((year%4==0)&&(year%100!=0))||(year%400==0))?1:2; if(flag==1){printf("%d年是闰年\n",year);}else{printf("%d年不是闰年\n",year);}}5.7#include<stdio.h>main(){char ch;printf("请输入一个字母:");ch=getchar();if((ch>=65)&&(ch<=90))ch=ch+32;printf("%c,%d\n",ch,ch);}else if((ch>=97)&&(ch<=122)){ch=ch-32;printf("%c,%d\n",ch,ch);}else{printf("%c\n",ch);}}5.8#include<stdio.h>main(){char ch;printf("请输入一个字符:");ch=getchar();if((ch>=48)&&(ch<=57)){printf("这是一个数字字符.\n");}else if((ch>=65)&&(ch<=90)){printf("这是一个大写字母.\n");}else if((ch>=97)&&(ch<=122)){printf("这是一个小写字母.\n");}else if(ch==32){printf("这是一个空格字符.\n");}else{printf("这是一个其它字符.\n");}}5.9#include<stdio.h>main(){int score,mark;printf("Please enter score:");scanf("%d",&score);if(score<0||score>100){printf("Input error!\n");}else 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{printf("%d--E\n",score);}}5.10#include <stdio.h>#include <stdlib.h>main(){int flag, year,month,day,day1=31,day2=30,day3=29,day4=28;printf("请以2012,03这种形式输入某年某月 :");scanf("%d,%d",&year,&month);if (month>12||month<1){printf("你输入的年份不在合法范围内\n");exit(0);}else{flag=(year%4==0 && year%100 !=0||year% 400 ==0)?1:0;switch (flag){case 1:printf("闰年");break;case 0:printf("平年");break;}if (month==2&&flag==1)day=day3;else if (month==2&&flag==0)day=day4;switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("%d年,%d月拥有的天数是31. \n",year,month);break;case 4:case 6:case 9:case 11:printf("%d年,%d月拥有的天数是30. \n",year,month);break;default:printf("%d年,%d月拥有的天数是%d: \n",year,month,day);}}}本章实验题1#include<stdio.h>main(){double faheight,maheight;char sex,sports,diet,F,M,Y,N;printf("请输入父母的身高:\n");scanf("%lf,%lf",&faheight,&maheight);printf("请输入孩子是男孩还是女孩(F/M):\n");scanf(" %c",&sex);printf("请输入孩子是否喜欢锻炼身体(Y/N):\n");scanf(" %c",&sports);printf("请输入孩子是否有良好的饮食习惯(Y/N):\n");scanf(" %c",&diet);if(sex==77&&sports==89&&diet==89){printf("该孩子成人时身高约为:%.2f.\n",(faheight+maheight)*0.54*1.02*1.015);}else if(sex==77&&sports==89&&diet==78){printf("该孩子成人时身高约为:%.2f.\n",(faheight+maheight)*0.54*1.02);}else if(sex==77&&sports==78&&diet==89){printf("该孩子成人时身高约为:%.2f.\n",(faheight+maheight)*0.54*1.015);}else if(sex==77&&sports==78&&diet==78){printf("该孩子成人时身高约为:%.2f.\n",(faheight+maheight)*0.54);}else if(sex==70&&sports==89&&diet==89){printf("该孩子成人时身高约为:%.2f.\n",(faheight*0.923+maheight)/2*1.02*1.015);}else if(sex==70&&sports==89&&diet==78){printf("该孩子成人时身高约为:%.2f.\n",(faheight*0.923+maheight)/2*1.02);}else if(sex==70&&sports==78&&diet==89){printf("该孩子成人时身高约为:%.2f.\n",(faheight*0.923+maheight)/2*1.015);}else if(sex==70&&sports==78&&diet==78){printf("该孩子成人时身高约为:%.2f.\n",(faheight*0.923+maheight)/2);}}本章实验题2#include<stdio.h>main(){double t,w,h;printf("请输入体重身高:\n");scanf("%lf,%lf",&w,&h);t=w/(h*h);if(t<18){printf("低体重.\n");}if(t>=18&&t<25){printf("正常体重.\n");}if(t>=25&&t<27){printf("超重体重.\n");}if(t>=27){printf("肥胖.\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=i+2){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<=99;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum =%ld\n",sum);}6.2(3)#include <stdio.h>main(){long term=0,sum=0;int a,i,n;printf("Input a,n:");scanf("%d,%d",&a,&n);for (i = 1;i<=n;i++){term = term * 10 + a;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 (fabs(term) >= 1e-4){term = sign / n;sum = sum + term;sign = -sign;n++;}printf("sum = %f\n");}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,coumt = %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){find=1;}x++;}printf("x=%d\n",x-1);}6.4#include<stdio.h>main(){int i,n;long p,j;printf("Please enter n:");scanf("%d",&n);for (i=1;i<=n;i++){p=i*i;j=i*i*i;printf("%d * %d = %ld\n",i,i,p);printf("%d * %d * %d= %ld\n",i,i,i,j);}}6.5#include<stdio.h>main(){double C,F,i;for (F = -40;F <= 110;F = F + 10){C = (F-32)*5/9;printf("F=%f C=%f\n",F,C);}}6.6#include<stdio.h>main(){double p = 1 + 0.01875*12,x4,x3,x2,benjin;x4 = 1000/p;x3 = (x4 + 1000)/p;x2 = (x3 + 1000)/p;benjin = (x2 + 1000)/p;printf("本金为:%f",benjin);}6.7#include<stdio.h>main(){double count = 0,c ,d , sum = 100;printf("Input c :");scanf("%lf",&c);d = c / 100.0;do{sum = sum * (1+d);count++;}while (sum <= 200);printf("count = %f\n",count);}6.8#include<stdio.h>#include<math.h>main(){int count = 1;double sum = 1.0 ,term,n = 3.0,i = 1.0;do {term = -i/n;sum = sum + term;n = n + 2;i = -i;count++;}while (fabs(term) >= 1e-5);printf("∏ = %lf,count = %d.\n",4 * sum,count); }6.9#include <stdio.h>#include <math.h>main(){int count = 1;double sum = 1.0 , term = 1.0, n = 1.0;while (fabs(term) >= 1e-5){term = term / n;sum = sum + term;n++;count++;}printf("e = %lf,count = %d.\n",sum,count); }6.10#include <stdio.h>main(){int i , a , b , c ,d;for (i = 100 ; i <= 999 ; i++){a = i /100;c = i % 10;b = (i - a * 100 - c) / 10;d = a * a * a + b * b * b + c * c * c;if (i == d){printf("i = %d\n",i);}}}6.11#include <stdio.h>main(){long m = 2 , n ,term = 1 ,sum = 1;printf("Input n:");scanf("%ld",&n);do {term = term * m;sum = sum + term ;m++;}while(sum <= n);printf("m = %d.\n",m - 2);6.12#include <stdio.h>main(){int num = 1,sum = 0 , count = 0;while (num > 0){printf("Input num :");scanf("%d",&num);sum = sum + num ;count++;}printf ("sum = %d,count = %d.\n",sum, count - 1); }6.13#include <stdio.h>main(){int term , sum = 0 , count = 0;do {begin:printf("Input term : ");scanf ("%d",&term);if (term > 0){sum = sum + term ;count++;}else if (term < 0)goto begin;elsegoto end ;}while (term != 0);end : printf ("sum = %d. count = %d.\n",sum,count );}6.14#include <stdio.h>main()int a,b,c;for (a = 0;a < 17;a++)for (b = 0;b <= 25 ;b++){c = 30 - a - b ;if (a * 3 + b * 2 + c == 50)printf ("男 = %d,女 = %d,孩子 = %d.\n",a,b,c);}}6.15#include<stdio.h>main(){int a,b;for (a = 0; a <=98 ; a++){b = 98 - a;if (a * 2 + b * 4 == 386){printf ("鸡有%d只,免有%d只.\n",a,b);}}}6.16#include <stdio.h>main(){int x,y,z;for (x = 0;x <= 20;x++){for (y = 0;y <= 33;y++){z = 100 - x - y;if (5 * x + 3 *y + z / 3.0 == 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 < 10; x++){for (y = 1; y < 20; y++){z = 50 - x - y;if (x * 10 + y * 5 + z == 100)printf("x = %d, y = %d, z = %d\n",x ,y ,z);}}}6.18上:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= 9; j++){printf("%-5d",i * j);}printf("\n");}}左:#include<stdio.h>main(){int i, j;for (i = 1; i <= 9; i++){for (j = 1; j <= i; j++){printf("%-5d",i * j);}printf("\n");}}右:#include <stdio.h>main(){int i, j, a = 1, k;char b = ' ';for (i = 1; i <= 9; i++){for (k = 1; k <= i; k++){printf("%5c",b);}for(j = i; j <= 9; j++){a=j * i;printf("%5d",a);}printf("\n");}}6.19#include<stdio.h>main(){long i,summo;double sumfu = 0,term = 0.01;summo = 100000 * 30;for (i = 1;i <= 30;i++){term = term * 2.0;sumfu = sumfu + term;}printf("陌生人付%ld,富翁付%lf.\n",summo,sumfu ); }本章实验题2.1#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {printf ("%d * %d = \n",x1, x2);scanf("%d",&answer);if (c == answer){printf("Right !\n");goto begin;}else{printf("Wrrong !Please try again.\n");}}while(c != answer);}2.2#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int x1, x2, c, answer, count = 0;begin:srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;do {printf ("%d * %d = \n",x1, x2);scanf("%d",&answer);if (c == answer){printf("Right !\n");goto begin;}else{printf("Wrrong !Please try again.\n");count++;}}while((c != answer)&&(count <= 2));printf ("Wrong ! You have tried three times ! Test over !\n"); }2.3#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int count1 = 0, count2 = 0;int x1, x2, c, answer ,i;for (i = 1;i <= 10; i++){srand(time(NULL));x1 = rand() % 10 + 1;x2 = rand() % 10 + 1;c = x1 * x2;printf ("%d * %d = \n",x1 , x2 );scanf("%d",&answer);if (c == answer){printf ("Right !\n");count1++;}else{printf ("Wrong !\n");count2++;}}printf ("你得了%d分.\n正确率为%d %%.\n",count1,count1 *10); }。
c语言程序设计苏小红版答案
c语言程序设计苏小红版答案C语言程序设计是计算机科学与技术专业学生的基础课程之一,它不仅教授了编程的基本概念和技巧,还培养了学生的逻辑思维和问题解决能力。
苏小红版的《C语言程序设计》教材以其系统性和实用性广受好评。
以下是对该书部分章节的习题答案的总结,以帮助学生更好地理解和掌握C语言程序设计。
第一章:C语言概述1. C语言的特点:C语言是一种结构化语言,具有高级语言的语法和汇编语言的控制能力。
它具有可移植性、简洁性、高效性等特点。
2. C语言的发展历史:C语言由Dennis Ritchie在20世纪70年代初期开发,最初用于UNIX操作系统。
第二章:数据类型、运算符和表达式1. 基本数据类型:包括整型(int)、浮点型(float和double)、字符型(char)等。
2. 运算符:包括算术运算符、关系运算符、逻辑运算符、位运算符等。
3. 表达式:由变量、常量、运算符和函数调用组成的,可以计算出结果的代码片段。
第三章:控制结构1. 顺序结构:程序按照编写顺序依次执行。
2. 选择结构:使用if、switch等语句根据条件选择执行不同的代码块。
3. 循环结构:使用while、do-while、for等循环语句重复执行代码块。
第四章:数组1. 一维数组:连续存储的相同类型元素集合。
2. 二维数组:可以看作是一维数组的数组,用于存储矩阵等数据结构。
3. 字符数组:特殊的一维数组,用于存储字符串。
第五章:函数1. 函数的定义:使用关键字`void`或返回类型定义函数的返回值类型。
2. 函数的调用:在程序中通过函数名和参数列表调用函数。
3. 递归函数:函数调用自身,用于解决某些递归问题。
第六章:指针1. 指针变量:存储另一个变量的内存地址的变量。
2. 指针与数组:指针可以方便地操作数组元素。
3. 函数指针:指向函数入口地址的指针变量,可以调用函数。
第七章:结构体与联合体1. 结构体:可以包含不同类型的数据成员的复合数据类型。
高等教育出版社《C语言程序设计(苏小红)》课后习题答案
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);}习题44.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2)#include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3)#include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&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>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(t=0;t<100000000;t++); }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;elsereturn 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;elsedo{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 N40int 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 N40int 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 N40int 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 N40int 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 N5void 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 M3void 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 N7void 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 N12void 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 N6000void 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 N10void 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");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned 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.stud entSex,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef 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++;elsesum4++;}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;}。
《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社
《C语言程序设计》(苏小红) 课后习题参考答案高等教育出版社《C语言程序设计》(苏小红) 课后习题参考答案一、基础知识题1. C语言是一种通用的、面向过程的程序设计语言。
它在20世纪70年代由贝尔实验室的Dennis M. Ritchie开发。
2. C程序的基本组成部分有:预处理指令、函数、变量和语句。
3. C语言的数据类型包括:整型、浮点型、字符型、枚举型和指针型等。
4. C语言的运算符包括:算术运算符、关系运算符、逻辑运算符和位运算符等。
5. C语言的控制语句包括:顺序结构、分支结构和循环结构等。
6. C语言的函数由函数头和函数体组成,其中函数头包括函数的返回类型、函数名和参数列表。
7. C语言的数组是一种存储相同类型数据元素的连续存储区域。
8. C语言中的字符串是由字符组成的字符数组。
二、程序设计题1. 编写一个程序,输入一个整数n,计算并输出1到n之间的所有正整数的平方和。
```c#include<stdio.h>int main() {int n;int sum = 0;printf("请输入一个整数n:");scanf("%d", &n);for (int i = 1; i <= n; i++) {sum += i * i;}printf("1到%d之间的所有正整数的平方和为:%d\n", n, sum);return 0;}```2. 编写一个程序,输入一个正整数n,判断该数是否为素数(只能被1和自身整除)。
```c#include<stdio.h>int isPrime(int n) {if (n <= 1)return 0;for (int i = 2; i <= n / 2; i++) {if (n % i == 0)return 0;}return 1;}int main() {int n;printf("请输入一个正整数n:"); scanf("%d", &n);if (isPrime(n))printf("%d是素数\n", n);elseprintf("%d不是素数\n", n);return 0;}```3. 编写一个程序,输入一个正整数n,输出n的阶乘。
c语言程序设计苏小红课后答案
c语言程序设计苏小红课后答案C语言程序设计是一门基础而重要的计算机编程课程,它为学生提供了学习程序设计的基本概念、原理和方法。
苏小红编写的《C语言程序设计》是一本广泛使用的教材,其课后习题答案对于学生理解和巩固知识点非常有帮助。
以下是部分课后习题的答案示例,以供参考。
第一章:C语言概述1. 问题:C语言的特点是什么?答案:C语言具有简洁、紧凑、灵活、高效等特点。
它是一种结构化语言,支持多种数据类型,并且具有丰富的运算符。
C语言的程序设计语言简洁,语法结构清晰,易于学习。
2. 问题:C语言的主要应用领域有哪些?答案:C语言广泛应用于系统软件、应用软件、嵌入式系统、操作系统、数据库管理系统等领域。
第二章:数据类型、运算符和表达式1. 问题:C语言中的基本数据类型有哪些?答案:C语言的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)等。
2. 问题:如何理解C语言中的运算符优先级?答案:C语言中的运算符优先级决定了表达式中各个运算符的执行顺序。
例如,乘除运算符的优先级高于加减运算符,括号内的运算优先级最高。
第三章:控制结构1. 问题:C语言中的三种基本控制结构是什么?答案:C语言中的三种基本控制结构是顺序结构、选择结构和循环结构。
2. 问题:if语句的基本形式有哪些?答案:if语句的基本形式包括单分支if语句、双分支if-else语句和多分支if-else if-else语句。
第四章:数组1. 问题:什么是一维数组?答案:一维数组是具有相同数据类型元素的集合,这些元素在内存中连续存储,可以通过索引访问。
2. 问题:如何声明和初始化一个一维数组?答案:声明一维数组的语法是 `type arrayName[arraySize];`。
初始化可以使用 `{value1, value2, ...}` 的方式,例如 `intarr[5] = {1, 2, 3, 4, 5};`。
- 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 Array 4.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2)main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b);}#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(t=0;t<100000000;t++);}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;elsereturn 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;elsedo{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 N40int 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 N40int 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 N40int 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 N40int 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 N5void 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 M3void 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 N7void 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 N12void 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 N6000void 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 N10void 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");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned 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.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef 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++;elsesum4++;}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;}。