C程序设计Week12晚上练习
本周仅仅进行一个程序,曾经的一个程序。
自己定义例如以下函数,输入n(n<46)个学生的姓名和成绩,顺序输出这n个学生的姓名和成绩,并输出最高成绩的姓名和成绩。预习struct结构体,思考怎样改进这一程序。
//为count个学生输入姓名和成绩 void getStudentsInfo(char names[][20], int scores[] , int count); void getStudentsInfo(char* names[], int scores[] , int count); //依次打印count个学生的姓名和成绩 void printStudentsInfo(char* names[], int scores[],int count); //获取最高成绩的学生的index int getIndexOfMaxScore(int scores[],int count);
知识点:
1 字符串数组用于处理多个人的姓名
2 数组作为函数的參数
the core code:
/* Note:Your choice is C IDE */ #include "stdio.h" void inputStudents(char name[][20],int score[],int num); void outputStudents(char name[][20],int score[],int num); main() { char name[45][20]; int score[45]; inputStudents(name,score,2); outputStudents(name,score,2); } void inputStudents(char name[][20],int score[],int num){ int i ; for(i=0;i<num;i++) scanf("%s %d",name[i],&score[i]); } void outputStudents(char name[][20],int score[],int num){ int i ; for(i=0;i<num;i++) printf("%s %d",name[i],score[i]); }
an example :
/* Note:Your choice is C IDE */ #include "stdio.h" #define N 45 int inputSS(char names[][20],int score[],int num); void printSS(char names[][20],int score[],int num); void getMAX(char names[][20],int score[],int num); main() { char names[N][20]; int score[N]; int num=0; int choose; printf("What do you want to do: INPUT(1),OUTPUT(2),MAX(3),EXIT(0):"); scanf("%d",&choose); do{ switch(choose){ case 1: num = inputSS(names,score,num); break; case 2: printSS(names,score,num); break; case 3: getMAX(names,score,num); } printf("\nWhat do you want to do: INPUT(1),OUTPUT(2),MAX(3),EXIT(0):"); scanf("%d",&choose); }while(choose != 0); } int inputSS(char names[][20],int score[],int num){ int n,i; printf("\nThis Time, How many students do you want to input :"); scanf("%d",&n); if((n+num)>N || n <1){ printf("not valid sum\n"); return -1; } printf("NOW INPUT AS ( NAME SCORE ):\n"); for(i=0;i<n;i++){ printf("%d. ",i); scanf("%s %d",&names[i+num],&score[i+num]); } printf("THIS TIME , INPUT IS OVER\n"); return num+n; } void printSS(char names[][20],int score[],int num){ int i ; if(num==0) { printf("NO STUDENTS NOW\n"); return; } printf("\nNOW , THE STUDENTS SCORES AS FOLLOWS \n"); for(i = 0 ;i<num;i++) printf("%2d. name:%10s score:%3d\n",i,names[i],score[i]); } void getMAX(char names[][20],int score[],int num){ int i ,max_index,max_score; if(num==0) { printf("NO STUDENTS NOW\n"); return; } max_index=0; max_score=score[0]; for(i = 1 ;i<num;i++) if( score[i] > max_score ){ max_score = score[i]; max_index = i; } printf("The Top Score is %d by %s \n",score[max_index],names[max_index]); }
=====================华丽的切割线====================================================
有非常多知识点须要大家复习。以下是一些比較cute的程序。弄懂啊弄懂
//0 #include "stdio.h" void main(){ int i=0, a[]={3,4,5,4,3}; do { a[i]++; }while(a[++i]<5); for(i=0;i<5;i++) printf("%d",a[i]) ; } //1 #include "stdio.h" void main(){ int a = 7; int b = 8; printf ( "a&b = %d\n",a&b); printf( "a&&b = %d\n",a&&b); } //2 #include "stdio.h" void main(){ int i ; for(int i = 0; i<4; i++){ if( i==2) break; printf("%d ",i); } printf("\n"); for(int i = 0; i<4; i++){ if( i==2) continue; printf("%d ",i); } printf("\n"); } //3 #include "stdio.h" void main(){ int sum=0,item=0; while(item<7){ item++; sum+=item; if(sum==7) break; } printf("%d\n",sum); } //4 #include "stdio.h" void main(){ int a[]={1,2,3,4,5,6,7,8}; int i,x ,*p; x=1; p=&a[3]; for( i=0; i<3; i++ ) x *= *(p+i); printf("x=%d\n",x); } //5 #include "stdio.h" void main(){ int i=5,x=1; for(;i<5;i++) x=x+1; printf("%d\n",x); } //6 #include "stdio.h" void main(){ int x,y; for (x=0, y=0 ; (y!=123) &&(x<4); x++) y++; printf("x=%d,y=%d\n",x,y); } //7 #include "stdio.h" void main(){ int a[7]={3,4,5,6,7,8,9}; int *p,*q; int i,x; p=&a[0]; q=&a[6]; for (i=0;i<3;i++) if(*(p+i)==*(q-i) ) x=*(p+i)*2; } //8 #include "stdio.h" void main(){ int a[5][5]; printf("&a[3][2]-a=%d\n",&a[3][2]-a); } //9 #include "stdio.h" void main(){ int i=2,n=2; for(;i<5;i++){ continue; n=n+i; } printf("%d\n",n); }