赞
踩
学习-用for循环求 s=1!+ 2!+ 3!+ …+n!的值
练习-编写函数输入输出M行N列的二维数组所有元素的值并且求最大元素的值及其所在的行号和列号
学习-输入正整数m,判断m是否是素数练习-用for循环求s=a+aa+aaa+aaaa+......的值练习
学习-用do…while循环按公式e=1+1/1!+1/2!+1/3!+…+1/n!计算 e 的值(精度为 1e-6)
练习-用while循环求s=1+(1+2)+(1+2+3)+....+(1+2+3+....+n) 的值
练习-用switch语句实现输入某年某月某日,判断这一天是这一年的第几天
练习-用switch语句实现输入一个不多余5位的正整数,要求①求出它是几位数②分别输出每一位数字③按逆序输出各位数字
练习-用switch语句实现对输入的数字1~7转换成文字星期几,对其它数字不转换
练习-一个学习小组有5人, 每个人有3门课的考试成绩,求每个人的总分和该学习小组各门课的平均成绩
学习-定义日期结构体类型(包括年、月、日),实现输入一个日期显示它是该年第几天
- #include <stdio.h>
- int main()
- {
- int i=1,n;
- long s=1,sum=0;
- scanf("%d",&n);
- /********** Begin **********/
- for(sum=0,s=1,i=1;i<=n;i++)
- {
- s=s*i;
- sum=sum+s;
- printf("%d!=%d\n",i,s);
- }
- printf("1!+2!+ …+%d!=%d",n,sum);
- /********** End **********/
- return 0;
- }
- #include<stdio.h>
- #define N 80
- int mystrlen(const char ch[]);
- int main()
- {
- char ch[N];
- //printf("请输入字符串:\n");
- gets(ch);
- printf("%d\n",mystrlen(ch));
- return 0;
- }
- /********** Begin **********/
- int mystrlen(const char str[ ])
- {
- int i ;
- int len = 0; //计数器置0
- for (i=0; str[i]!='\0'; i++)
- {
- len++;
- }
- return len;
- }
- /********** End **********/
- #include<stdio.h>
- int main()
- {
- char str1[15],str2[15] = "love C!";
- /*****输入字符串存放在数组str1中*****/
- /********** Begin **********/
- gets(str1);
-
- /********** End **********/
- printf("%s %s\n",str1 ,str2);
- fflush(stdin);
- /*****再次输入字符串存放在数组str1中*****/
- /********** Begin **********/
- gets(str1);
-
- /********** End **********/
- printf("%s %s\n",str1 ,str2);
- return 0;
- }
- #include <stdio.h>
- #define M 10
- #define N 10
- void inputarr( int x[ ][N], int m, int n );
- void printarr( int x[ ][N], int m, int n );
- int maxvalue(int array[][N] , int m, int n);
- /********** Begin **********/
- int row=0,col=0; //将变量row,col定义在函数体外,可以被main()函数和maxvalue()函数共用
- int main(void)
- {
- int a[M][N] = {0},m,n,max;
- //printf("Input m:");
- scanf("%d", &m); //输入行数m
- //printf("Input n:");
- scanf("%d", &n); //输入列数n
- inputarr(a, m, n);
- max = maxvalue(a, m, n);
- printf("max=%d row=%d col=%d\n",max,row+1,col+1 );//行数和列数从1算起
- printarr(a, m, n);
- return 0;
- }
- void inputarr( int x[ ][N], int m, int n )
- {
- int i, j;
- for(i = 0; i < m; i++)
- {
- for(j = 0; j < n; j++)
- scanf("%d", &x[i][j]);
- }
- }
- void printarr( int x[ ][N], int m, int n )
- {
- int i, j;
- for(i = 0; i < m; i++)
- {
- for(j = 0; j < n; j++)
- printf("%d ", x[i][j]);
- printf("\n");
- }
- }
- int maxvalue(int array[][N] , int m, int n)
- {
- int i,j,max;
- max = array[0][0]; //选择第1个数组元素作擂主
- for (i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- if (array[i][j]>max)
- {
- max = array[i][j]; row=i; col=j;
- }
- }
- return (max);
- }
- /********** End **********/
- #include <stdio.h>
- #define N 40
- void inputarr (int array[ ] , int n);
- float average (int array[ ] , int n);
- void outputarr (int array[ ] , int n);
-
-
- int main()
- {
- int score[N],n;
- float aver;
- //printf("Input n:");
- scanf("%d", &n); //输入学生人数n
- inputarr (score,n);
- /*****调用average()函数*****/
- /********** Begin **********/
- aver=average(score,n)/n;
- /********** End **********/
- /*****调用outputarr()函数*****/
- /********** Begin **********/
- outputarr(score,n);
- /********** End **********/
- printf("\nAverage score is %f\n",aver);
- return 0;
- }
-
- void inputarr (int array[ ], int n)
- {
- int i;
- for (i=0; i<n; i++)
- {
- scanf("%d", & array[i]);
- }
- }
- /*****定义float average(int array[ ] , int n)函数*****/
- /********** Begin **********/
- float average(int array[ ] , int n)
- {
- int i;
- float aver=0,s=0;
- for (i=0; i<n; i++)
- {
- s+=array[i];
- }
-
- return s;
- }
- /********** End **********/
-
- /*****定义void outputarr (int array[ ] , int n)函数*****/
- /********** Begin **********/
- void outputarr (int array[ ] , int n)
- {
- int i;
- for (i=0; i<n; i++)
- {
- printf("%d ",array[i]);
- }
- }
- /********** End **********/
-
- #include <stdio.h>
- int main(void)
- {
- // 请在此添加代码
- /********** Begin *********/
- int s=0,a,n,i,t;
- scanf("%d%d",&a,&n);
- t=a;
- for(i=1;i<=n;i++)
- {
- s+=t;
- a*=10;
- t+=a;
- }
- printf("%d",s);
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- /*****定义fact()函数*****/
- /********** Begin **********/
- double fact(int n);
- double fact(int n){
- int i,s=1;
- for(i=1;i<=n;i++){
- s*=i;
- }
- return s;
- }
- /********** End **********/
- /*****定义fun()函数*****/
- /********** Begin **********/
- double fun(int n);
- double fun(int n){
- int i,sum=0;
- for(i=1;i<=n;i++){
- sum+=fact(i);
- }
- return sum;
- }
- /********** End **********/
- /*****定义main()函数*****/
- /********** Begin **********/
- int main(){
- int n;
- scanf("%d",&n);
- printf("sum=%0.lf",fun(n));
- return 0;
- }
- /********** End **********/
- #include <stdio.h>
- int main()
- {
- /********** Begin **********/
- int i ;
- int j ;
-
- for ( i = 1; i <= 9; i++)
- {
- for ( j = 1; j <= 9; j++)
- {
- if(j<i){
- printf("\t");
- }else{
- printf("%d*%d=%d\t", i, j, i * j);
- }
- }
- printf("\n");
- }
-
-
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- /********** Begin **********/
- int f1=1,f2=1,f3,i;
- printf("第1月:1\n");
- printf("第2月:1\n");
- for(i=3;i<=12;i++){
- f3=f1+f2;
- printf("第%d月:%d\n",i,f3);
- f1=f2;
- f2=f3;
- }
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- /********** Begin **********/
- double a,n,sum,t;
- int i=1;
- n=1;
- sum=1;
- scanf("%lf",&a);
-
- do{
- n*=i;
- t=(1.0/n);
- sum+=t;
- i++;
- }while(t>=a);
- printf("i=%d\n",i);
- printf("e=%.6lf\n",sum);
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int num,zsum=0,fsum=0;
- //循环输入与求和
- /********** Begin **********/
- do{
- scanf("%d",&num);
- if(num>0){
- zsum++;
- }
- if(num<0){
- fsum++;
- }
- }while(num!=0);
- printf("正数个数为%d\n",zsum);
- printf("负数个数为%d",fsum);
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int i,n,sum=0,result=0;
- scanf("%d",&n);
- /********** Begin *********/
- while(i<=n)
- {
- result+=i;
- sum+=result;
- i++;
- }
- printf("sum=%ld\n",sum);
- /********** End **********/
- return 0;
- }
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- double a,b;
- char c;
- scanf("%lf%c%lf",&a,&c,&b);
- /********** Begin **********/
- switch(c)
- {
- case '+':printf("%.2lf+%.2lf=%.2lf",a,b,a+b);
- break;
- case '-':printf("%.2lf-%.2lf=%.2lf",a,b,a-b);
- break;
- case '*':printf("%.2lf*%.2lf=%.2lf",a,b,a*b);
- break;
- case '/':
- if(b==0){
- printf("error");
- }else{
- printf("%.2lf/%.2lf=%.2lf",a,b,a/b);
- }
- }
- /********** End **********/
- return 0;
- }
- #include<stdio.h>
- int main()
- {
- int day,month,year,num=0,leap;
- scanf("%d%d%d",&year,&month,&day);
- /*****先计算某月以前月份的总天数*****/
- /********** Begin **********/
- switch(month)
- {
- case 12: num +=30;
- case 11: num+=31;
- case 10: num+=30;
- case 9: num+=31;
- case 8: num+=31;
- case 7: num+=30;
- case 6: num+=31;
- case 5: num+=30;
- case 4: num+=31;
- case 3: num+=28;
- case 2: num+=31;
- }
-
- /********** End **********/
- /*****再加上当月的天数*****/
- num+=day;
- /*****判断是不是闰年*****/
- /********** Begin **********/
- if ((year%4==0&&year%100!=0)||year%400==0){
- if(month > 2){
- num += 1;
- }
- }
- /********** End **********/
- printf("这一天是这一年的第%d天",num);
- return 0;
- }
- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int num,y,x1,x2,x3,x4,x5,place;
- scanf("%d",&num);
- place=log10((double)num)+1;//
- /********** Begin **********/
- printf("%d为%d位数\n",num,place);
-
- x1=num%10;
- x2=num/10%10;
- x3=num/100%10;
- x4=num/1000%10;
- x5=num/10000%10;
-
- switch(place)
- {
- case 5 :printf("顺序输出为:%d,%d,%d,%d,%d\n逆序输出为:%d,%d,%d,%d,%d",x5,x4,x3,x2,x1,x1,x2,x3,x4,x5);break;
- case 4 :printf("顺序输出为:%d,%d,%d,%d\n逆序输出为:%d,%d,%d,%d",x4,x3,x2,x1,x1,x2,x3,x4);break;
- case 3 :printf("顺序输出为:%d,%d,%d\n逆序输出为:%d,%d,%d",x3,x2,x1,x1,x2,x3);break;
- case 2 :printf("顺序输出为:%d,%d\n逆序输出为:%d,%d",x2,x1,x1,x2);break;
- case 1 :printf("顺序输出为:%d\n逆序输出为:%d",x1,x1);break;
- }
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- /********** Begin **********/
- int day;
- scanf("%d",&day);
- switch(day)
- {
- case 1:printf("Monday\n");break;
- case 2:printf("Tuesday\n");break;
- case 3:printf("Wednesday\n");break;
- case 4:printf("Thurday\n");break;
- case 5:printf("Friday\n");break;
- case 6:printf("Saturday\n");break;
- case 7:printf("Sunday\n");break;
- default:printf("error\n");break;
- }
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int i=1,sum=0,n;
- scanf("%d",&n);
- /********** Begin **********/
- while (i<=n)
- {
- sum=sum+i;
- i++;
- }
- /********** End **********/
- printf("sum=%d\n",sum);
- return 0;
- }
-
- #include <stdio.h>
- int main ()
- {
- char a2, b2[5],c2[10],d2[3][4],e2[2][5];
- double a3, b3[5],c3[10],d3[3][4],e3[2][5];
- float a4, b4[5],c4[10],d4[3][4],e4[2][5];
- /*****输出a2,b2, b2[0],c2,d2,d2[0], d2[0][0],e2,e2[0],e2[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a2 = %d\n",sizeof(a2) );
- printf("b2 = %d\n",sizeof(b2) );
- printf("b2[0] = %d\n",sizeof(b2[0]) );
- printf("c2 = %d\n",sizeof(c2) );
- printf("d2 = %d\n",sizeof(d2) );
- printf("d2[0] = %d\n",sizeof(d2[0]) );
- printf("d2[0][0] = %d\n",sizeof(d2[0][0]) );
- printf("e2 = %d\n",sizeof(e2) );
- printf("e2[0] = %d\n",sizeof(e2[0]) );
- printf("e2[0][0] = %d\n",sizeof(e2[0][0]) );
- /********** End **********/
-
- /*****输出a3,b3, b3[0],c3,d3,d3[0],d3[0][0],e3,e3[0],e3[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a3 = %d\n",sizeof(a3) );
- printf("b3 = %d\n",sizeof(b3) );
- printf("b3[0] = %d\n",sizeof(b3[0]) );
- printf("c3 = %d\n",sizeof(c3) );
- printf("d3 = %d\n",sizeof(d3) );
- printf("d3[0] = %d\n",sizeof(d3[0]) );
- printf("d3[0][0] = %d\n",sizeof(d3[0][0]) );
- printf("e3 = %d\n",sizeof(e3) );
- printf("e3[0] = %d\n",sizeof(e3[0]) );
- printf("e3[0][0] = %d\n",sizeof(e3[0][0]) );
- /********** End **********/
-
- /*****输出a4,b4, b4[0],c4,d4,d4[0], d4[0][0],e4,e4[0],e4[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a4 = %d\n",sizeof(a4) );
- printf("b4 = %d\n",sizeof(b4) );
- printf("b4[0] = %d\n",sizeof(b4[0]) );
- printf("c4 = %d\n",sizeof(c4) );
- printf("d4 = %d\n",sizeof(d4) );
- printf("d4[0] = %d\n",sizeof(d4[0]) );
- printf("d4[0][0] = %d\n",sizeof(d4[0][0]) );
- printf("e4 = %d\n",sizeof(e4) );
- printf("e4[0] = %d\n",sizeof(e4[0]) );
- printf("e4[0][0] = %d\n",sizeof(e4[0][0]) );
- /********** End **********/
- return 0;
- }
-
-
- #include <stdio.h>
- int main()
- {
- int a[3][4],i,j;
- int(*p)[4]; //定义p为指针,指向一个长度为4的整型数组
- p = a;
- /********** Begin *********/
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<4; j++)
- {
- scanf("%d ", *(p+i)+j);
- }
- printf("\n");
- }
- for(int i=0; i<3; i++)
- {
- for(int j=0; j<4; j++)
- {
- printf("%d ", p[i][j]);
- }
- printf("\n");
- }
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main ()
- {
- char * a2,**b2,*c2[4], (*d2)[5];
- double * a3,**b3,*c3[4], (*d3)[5];
- float * a4,**b4,*c4[4], (*d4)[5];
- /*****输出a2,b2,c2,d2所占内存的字节数*****/
- /********** Begin *********/
- printf("a2 = %d \n", sizeof(a2) );
- printf("b2 = %d \n", sizeof(b2) );
- printf("c2 = %d \n", sizeof(c2) );
- printf("d2= %d \n", sizeof(d2) );
- /********** End **********/
- /*****输出a3,b3,c3,d3所占内存的字节数*****/
- /********** Begin *********/
- printf("a3 = %d \n", sizeof(a3) );
- printf("b3 = %d \n", sizeof(b3) );
- printf("c3 = %d \n", sizeof(c3) );
- printf("d3 = %d \n", sizeof(d3) );
- /********** End **********/
- /*****输出a4,b4,c4,d4所占内存的字节数*****/
- /********** Begin *********/
- printf("a4 = %d \n", sizeof(a4) );
- printf("b4 = %d \n", sizeof(b4) );
- printf("c4 = %d \n", sizeof(c4) );
- printf("d4 = %d \n", sizeof(d4) );
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int a[10],*p;
- /*****输入10个整数存入数组a *****/
- for( p=a;p<a+10;p++ ) // p指向a[0]
- {
- scanf("%d",p);
- }
-
- /*****输出数组a 的所有数组元素的值*****/
- /********** Begin *********/
- for( p=a;p<a+10;p++ ) // p指向a[0]
- {
- printf("%d ",*p);
- }
-
- /********** End **********/
- printf("\n");
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int x,y;
- int *p; //定义指针变量p
- p=&x; //使指针变量p指向一个变量x
- /***** 请在以下填写通过p输入输出x值代码 *****/
- /********** Begin **********/
- scanf("%d",&x);
- printf("%d\n",*p);
- /********** End **********/
- p=&y; //使指针变量p指向另一个变量y
- /***** 请在以下填写通过p输入输出y值代码 *****/
- /********** Begin **********/
- scanf("%d",&y);
- printf("%d\n",*p);
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- int a=100,b=200;
- int *p1, *p2, *p;
- p1=&a;
- p2=&b;
- scanf("%d%d",p1,p2);
- /***** 交换p1、p2的值 *****/
- /********** Begin **********/
- p=p1;
- p1=p2;
- p2=p;
- /********** End **********/
- printf("%d %d\n",*p1,*p2);
- return 0;
- }
- #include<stdio.h>
- #define M 3
- main()
- {
- int a[M],i;
- int *max,*min;
- for(i=0 ;i<M; i++)
- {
- scanf("%d",&a[i]);
- }
- /***** 将指针变量max,min初始化 *****/
- /********** Begin **********/
- max=&a[0];
- min=&a[0];
-
- /********** End **********/
- /***** 将每个数组元素依次与max,min指向的数组元素比较,使max,min分别指向的最大、最小的数组元素 *****/
- /********** Begin **********/
- for (i = 0; i < M; i++)
- {
- if (a[i] > *max) { max = &a[i]; }
- if (a[i] < *min) { min = &a[i]; }
- }
-
- /********** End **********/
- /***** 通过指针变量max,min输出最大值和最小值 *****/
- /********** Begin **********/
- printf("min=%d\nmax=%d", *min, *max);
-
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- #define M 5
- #define N 3
- int main()
- {
- int m,n;
- int x[M][N],s[5];
- float v[3],sum=0,sum1=0;
- for(m=0;m<M;m++)
- for(n=0;n<N;n++)
- scanf("%d",&x[m][n]);
- // 计算每科的平均分数
- for (m=0;m<N;m++)
- {
- for (n=0;n<M;n++)
- {
- sum=sum+x[n][m];
- }
- v[m]=sum/5;
- sum=0;
- }
- //计算每人的总分
- for (n=0;n<M;n++)
- {
-
- for (m=0;m<N;m++)
- {
- sum1=sum1+x[n][m];
- }
- s[n]=sum1;
- sum1=0;
- }
- for(m=0;m<M;m++)
- {
- for(n=0;n<N;n++)
- {
- printf("%d ",x[m][n]);
- }
- printf("\n");
- }
- printf("每个学生的总分:\n%6d%6d%6d%6d%6d",s[0],s[1],s[2],s[3],s[4]);
- printf("\n每门课的平均分:\n%8.2f%8.2f%8.2f\n",v[0],v[1],v[2]);
- //85 78 88 60 90 80 91 79 92 50 84 83 45 86 80
- return 0;
- }
- #include <stdio.h>
- #define N 100
- void inputarr(int *p,int *n);
- void bubblesort(int *arr,int length);
- void outputarr(int *arr,int n);
- void swap(int *a,int *b);
- int main()
- {
- int a[N];
- int i,n;
- inputarr(a,&n);
- bubblesort(a,n);
- outputarr(a,n);
- return 0;
- }
- void inputarr(int *p,int *n)
- {
- *n = 0;
- do
- {
- scanf("%d",&p[*n]);
- (*n)++;
- } while (p[*n-1] != 0);
- *n = *n -1;
- }
- void bubblesort(int *arr,int length)
- {
- int i,j;
- for(i = 0; i < length -1; i++)
- {
- for (j = 0; j < length -1-i;j++)
- {
- if(arr[j] > arr[j+1])
- swap(&arr[j],&arr[j+1]);
- }
- }
- }
- void outputarr(int *arr,int n)
- {
- int i = 0;
- for (; i < n;i++)
- {
- if(i < n-1)
- printf("%d ",arr[i]);
- else
- printf("%d",arr[i]);
- }
- printf("\n");
- }
- void swap(int *a,int *b)
- {
- int tmp;
- tmp = *a;
- *a = *b;
- *b = tmp;
- }
- #include <stdio.h>
- void swap(int *p1,int *p2);
- /*****定义main()函数*****/
- /********** Begin **********/
- int main(){
-
- int a,b;
-
- scanf("%d %d",&a,&b);
-
- swap(&a,&b);
-
- printf("%d %d",a,b);
-
- return 0;
-
- }
- /********** End **********/
- /*****定义swap()函数*****/
- /********** Begin **********/
- void swap(int *p1, int *p2)
- {
- int temp;
- temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
-
- /********** End **********/
- #include<stdio.h>
- /********** Begin **********/
- #include<math.h>
- float fun(int m)
- {
- int q,p=0;
- float w;
- for(q=1;q<=m;q++)
- {
- p+=q;
- }
- w=1.0/p;
- return w;
- }
- int main()
- {
- float s=0;
- int n;
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- float x=fun(i);
- s=s+x;
- }
- printf("s=%f\n",s);
- return 0;
- }
- /********** End **********/
- #include<stdio.h>
- typedef struct date
- {
- int year;
- int month;
- int day;
- }DATE;
-
- int main()
- {
- /********** Begin **********/
- DATE p;
- scanf("%d%d%d",&p.year,&p.month,&p.day);
- int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
- int sum,i;
- sum=p.day;//当前月的天数先算入总数
- for(i=0;i<p.month-1;i++)//之前所有月份总天数累加
- sum+=m[i];
- //考虑闰年2月多一天
- if(((p.year%4==0&&p.year%100!=0)||p.year%400==0)&&p.month>2)
- printf("它是%d年的第%d天",p.year,sum+1);
- else
- printf("它是%d年的第%d天",p.year,sum);
- return 0;
-
- /********** End **********/
-
- }
- #include<stdio.h>
- #include<string.h>
- struct date
- {
- int year;
- int month;
- int day;
- };
- struct stu
- {
- int num;
- char name[20];
- char sex;
- struct date birth;
- float score;
- };
- int main()
- {
- struct stu s1 = {10010,"zhangsan",'m',2000,5,4,84.5}, s2, s3;
- scanf("%d %s",&s2.num,&s2.name);
- getchar();
- scanf("%c %d %d %d %f",&s2.sex,&s2.birth.year,&s2.birth.month,&s2.birth.day,&s2.score);
- getchar();
- /********** End **********/
- /*****交换两个结构体变量s1和s2*****/
- /********** Begin **********/
- s3=s1;
- s1=s2;
- s2=s3;
- /********** End **********/
-
- /*****输出结构体变量s1和s2的成员*****/
- /********** Begin **********/
- printf("学号:%d\n姓名:%s\n性别:%c\n",s1.num,s1.name,s1.sex);
- printf("出生日期:%d年%d月%d日\n成绩:%.1f\n",s1.birth.year,s1.birth.month,s1.birth.day,s1.score);
-
- printf("学号:%d\n姓名:%s\n性别:%c\n",s2.num,s2.name,s2.sex);
- printf("出生日期:%d年%d月%d日\n成绩:%.1f\n",s2.birth.year,s2.birth.month,s2.birth.day,s2.score);
- /********** End **********/
- return 0;
- }
-
- #include <stdio.h>
- struct datatype1
- {
- char b;
- int a;
- char c;
- }s1;
- struct datatype2
- {
- char b;
- short a;
- char c;
- }s2;
- struct datatype3
- {
- char b;
- char c;
- int a;
- }s3;
- struct stu
- {
- int num;
- char name[20];
- char sex;
- float score;
- }s4;
- struct date
- {
- int year;
- int month;
- int day;
- }s5;
- struct student
- {
- int num;
- char name[20];
- char sex;
- struct date birth;
- float score;
- }s6;
-
- int main()
- {
- /*****输出上述六种结构体类型所占字节数*****/
- /********** Begin **********/
- printf("1:%d\n", sizeof(s1));
- printf("2:%d\n", sizeof(s2));
- printf("3:%d\n", sizeof(s3));
- printf("4:%d\n", sizeof(s4));
- printf("5:%d\n", sizeof(s5));
- printf("6:%d\n", sizeof(s6));
-
- /********** End **********/
- return 0;
- }
-
- #include<stdio.h>
- #include<string.h>
- typedef struct date
- { int year;
- int month;
- int day;
- }DATE;
- typedef struct student
- {
- int num;
- char name[20];
- char sex;
- DATE birthday;
- float score;
- }STUDENT;
- void inputarr(STUDENT s[ ], int n);
- void outputarr(STUDENT s[ ], int n);
- int maxarr(STUDENT s[ ], int n);
- int main()
- {
- STUDENT b[5];
- int n;
- inputarr(b,5);
- n=maxarr(b,5);
- outputarr(b,5);
- printf("第%d个同学的成绩最高。",n+1);
- return 0;
- }
- /********** 三个子函数定义 **********/
- /********** Begin **********/
- void inputarr(STUDENT s[],int n)
- {
- int i = 0;
-
- for (; i < n; i++)
- {
- scanf("%d",&s[i].num);
- scanf("%s",s[i].name);
- scanf(" %c%*c",&s[i].sex);
- scanf("%d %d %d",&s[i].birthday.year,&s[i].birthday.month,&s[i].birthday.day);
- scanf("%f",&s[i].score);
- }
- }
- int maxarr(STUDENT s[],int n)
- {
- int i,max;
- max=0;
- for(i=1;i<n;i++)
- if(s[i].score>s[max].score)
- max=i;
- return max;
- }
-
- void outputarr(STUDENT s[],int n)
- {
- int i = 0;
- for (; i < n; i++)
- {
- printf("学号:%d\t",s[i].num);
- printf("姓名:%s\t",s[i].name);
- printf("性别:%c\t",s[i].sex);
- printf("出生日期:%d-%d-%d\t",s[i].birthday.year,s[i].birthday.month,s[i].birthday.day);
- printf("成绩:%.1f\n",s[i].score);
- }
- }
- /********** End **********/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。