Curriculum design of C language student grade management system

  • 2020-06-01 10:39:48
  • OfStack

Student grade management system is more suitable for beginners. It covers almost all knowledge of the c language. For those who have learned c, design the course well (of course, you have to have a good foundation to design it yourself). Whether you can do it or not, the most important thing is to understand it. It's a good idea to refer to other sources and try to write your own. I wrote this course design by myself with reference to the materials. I added 1 function appropriately. But it's not that professional.


#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
#define size 100 
 
 
char* classname[]={" Chinese language and literature "," mathematics "," English "}; 
int N=0,falg=0; // Defining global variables  N The number of representatives, falg The identifier that represents the information saved.  
int arrysize=size; //arrysize Represents the size of the array of structs.   
struct stu{ 
char num[10]; 
char name[10]; 
float score[3]; 
float sum; 
}; 
typedef struct stu STU; // Defines the structure of the new type.  
STU*pro=NULL; // pro An array of structs to hold information.  
 
 
int chack(int a,int b,int c) // The conditional judgment function, c That's the condition number.  
{ 
if(c==1||c==2) 
{ 
if(pro[a].sum !=pro[b].sum ) 
if(c==1) 
 return pro[a].sum >pro[b].sum ; 
else 
return pro[a].sum <pro[b].sum; 
else return strcmp(pro[a].num ,pro[b].num)<0; 
} 
if(c==3||c==4) 
{ 
 if(c==3) return strcmp(pro[a].num,pro[b].num )<0; 
 else return strcmp(pro[a].num,pro[b].num )>0; 
} 
else printf(" There is no option for this condition !\n"); 
return 0; 
} 
 
 
void incharge(int a,int b) // Exchange function.  
{ 
 struct stu t; 
 t=pro[a]; 
 pro[a]=pro[b]; 
 pro[b]=t; 
} 
 
 
void het(int m,int n,int c) // Fast row function  
{ if(m>n) return; 
int i=m-1; 
for(int j=m;j<n;j++) 
{ 
if(chack(j,n,c)) 
incharge(++i,j); 
} 
incharge(i+1,n); 
het(1,i,c); 
het(i+2,n,c); 
} 
 
 
void newsect(int i) // Add record function  
{ float s=0; 
 printf(" Please enter the student id :"); 
 scanf("%s",&pro[i].num); 
 printf(" Please enter the student's name :"); 
 scanf("%s",&pro[i].name); 
 for(int j=0;j<3;j++) 
{ 
 printf(" Please enter the %s results :",classname[j]); 
 scanf("%f",&pro[i].score[j]); 
 s+=pro[i].score[j]; 
} 
 pro[i].sum=s; 
 falg=0; 
} 
 
 
void showlist()  // All print table functions  
{ if(N==0){printf(" Record is empty !\n");return;}; 
 printf("\n ┏ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┓ \n"); 
 printf("  ┃  student id   The name   Total grade   Chinese language and literature   mathematics   English   ┃  \n"); 
 for(int i=1;i<=N;i++) 
 { 
 printf(" ┃ %-11s %-10s %-9.1f ",pro[i].num,pro[i].name,pro[i].sum); 
 for(int j=0;j<3;j++) 
 printf(j==2?"%-5.1f":"%-8.1f",pro[i].score[j]); 
 printf(" ┃ \n"); 
 if(i==N) 
 printf("\n ┗ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┛ \n"); 
 } 
} 
 
 
void showtable(int n)   // Individual print list function  
{ 
printf("\n ┏ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┓ \n"); 
 printf("  ┃  student id :%-11s The name :%-10s Total grade :%-6.1f",pro[n].num,pro[n].name,pro[n].sum); 
for(int j=0;j<3;j++) 
printf("%s:%-4.1f",classname[j],pro[n].score[j]); 
printf("  ┃  \n"); 
printf(" ┗ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┛ \n"); 
} 
int lookout( ) // Lookup function  
{ int tar; 
 char num[10],name[10]; 
if(N==0) 
{ 
printf(" The record sheet is empty !\n"); 
return 0; 
} 
printf("1. According to the student id  2. According to the name \n Please select the appropriate number :"); 
scanf("%d",&tar); 
getchar(); 
if(tar!=1&&tar!=2) 
{printf(" Selection is invalid !\n");return 0;} 
 if(tar==2) 
{ 
printf(" Please enter the name of the person you are looking for :"); 
 scanf("%s",&name); 
 for(int i=1;i<=N;i++) 
if(strcmp(name,pro[i].name)==0) 
{ 
showtable(i); 
return i; 
} 
} 
else 
{ 
printf(" Please enter the student number of the person you are looking for :"); 
 gets(num); 
for(int i=1;i<=N;i++) 
if(strcmp(num,pro[i].num)==0) 
{ 
showtable(i); 
return i; 
} 
} 
printf(" No the record \n"); 
return 0; 
} 
 
 
void copyrecored(STU *p,STU *q) // Copy the function . 
{ 
strcpy(q->num,p->num); 
strcpy(q->name,p->name); 
for(int j=0;j<3;j++) 
q->score[j]=p->score[j]; 
 q->sum=p->sum; 
} 
 
 
void remove() // Delete the personal information function  
{ int b; 
 char tag[10]; 
if(N==0){ 
printf(" The record sheet is empty !\n"); 
return; 
} 
b=lookout(); 
if(b) 
{ 
printf(" Whether to delete or not (y/n):"); 
 gets(tag); 
if(tag[0]=='y'||tag[0]=='Y') 
{ 
N--; 
for(int j=b;j<=N;j++) 
copyrecored(&pro[j+1],&pro[j]); 
printf(" Deleted successfully! \n"); 
falg=0; 
} 
else printf(" Delete failed !\n"); 
} 
} 
void change() // Modify the personal information function  
{ int m,k,f; 
 if(N==0) 
{printf(" The record sheet is empty !\n");return;} 
 k=lookout(); 
 if(k) 
 { 
 printf("1. Student id  2. The name  3 Subject result  4. All changes  0. exit \n Please enter the information to modify :"); 
scanf("%d",&f); 
switch(f) 
{ 
case 1:printf(" Please enter your student number again :"); 
 scanf("%s",&pro[k].num );falg=0;break; 
case 2:getchar(); 
 printf(" Please re-enter your name :"); 
 scanf("%s",&pro[k].name);falg=0;break; 
case 3:printf("0. Chinese language and literature  1. mathematics  2. English \n Please select the subject :"); 
 scanf("%d",&m); 
 if(m==0||m==1||m==2) 
{ 
printf(" Please enter the %s results :",classname[m]); 
 scanf("%f",&pro[k].score[m]); 
falg=0; 
} 
 else printf(" The input is invalid !\n"); 
 pro[k].sum=pro[k].score[0]+pro[k].score[1]+pro[k].score[2]; 
 break; 
case 4:newsect(k);break; 
default:printf(" Modify the failure !"); break; 
} 
 } 
} 
 
 
void save()  // File save function  
{ 
FILE *fp; 
 char fname[30]; 
if(N==0) 
{ 
printf(" No record to keep !\n");return; 
} 
loop:printf(" Please enter the file to be saved ( Press enter directly to save in the default file ):"); 
gets(fname); 
if(strlen(fname)==0) 
 strcpy(fname,"stu_info"); 
 if((fp=fopen(fname,"wb"))==NULL){ 
 printf(" Cannot save to file !\n");goto loop; 
 } 
 printf("  File save ....\n"); 
 for(int i=1;i<=N;i++) 
 if((fwrite(&pro[i],sizeof(STU),1,fp))==0) 
 {printf(" Save failed !\n");break;} 
 fclose(fp); 
 printf(" File saved !\n"); 
 falg=1; 
} 
 
 
void loadrecords() // Read file function  
{ 
FILE *fp; 
void abc(int s); 
char fname[10]; 
int c; 
struct stu records[100]; 
printf(" Please enter the file name to open ( Directly press enter to open the default file ):"); 
gets(fname); 
if(strlen(fname)==0) 
strcpy(fname,"stu_info"); 
if((fp=fopen(fname,"rb"))==NULL) 
{ 
printf(" The file won't open !\n");return; 
} 
printf("1. Overrides the newly created information  2. Add to new information  3. View the saved information \n Please select a :"); 
scanf("%d",&c); 
if(c==3){ 
printf("\n ┏ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┓ \n"); 
 printf("  ┃  student id   The name   Total grade   Chinese language and literature   mathematics   English   ┃  \n"); 
for(int i=0;fread(&records[i],sizeof(STU),1,fp)!=0;i++) 
{ 
printf(" ┃ %-12s% -11s% -9.1f ",records[i].num ,records[i].name,records[i].sum ); 
for(int j=0;j<3;j++) 
printf(j==2?"%-5.1f":"%-8.1f",records[i].score [j]); 
printf("  ┃ \n"); 
} 
printf("\n ┗ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┉ ┛ \n"); 
} 
if(c==1){ 
abc(0); 
for( N=1;fread(&pro[N],sizeof(STU),1,fp)!=0;N++); 
N--;    // Out of the loop, N It's actually larger than the number of people you get 1 , so need to get 1 .  
falg=0;     
fclose(fp); 
showlist(); 
} 
if(c==2){falg=0; 
for(++N;fread(&pro[N],sizeof(STU),1,fp)!=0;N++) 
if(N>arrysize){ 
pro=(STU*)realloc(pro,sizeof(STU)*(arrysize+size));  // When memory is exceeded, new memory is allocated.  
} 
arrysize+=size; 
N--;     // Out of the loop, N It's actually larger than the number of people you get 1 , so need to get 1 .  
fclose(fp); 
showlist(); 
}  
} 
 
 
void abc(int s) // Information saving judgment function  s Represents the corresponding operation to be performed.  
{ char v[10]; 
if(N!=0&&falg!=1) 
{ 
 printf(" The new information has not been saved !, Continuing will cause the new information to disappear ,"); 
 printf(" Whether to save first or not (y/n):"); 
 while(1){ 
gets(v); 
if(v[0]=='y'||v[0]=='Y') {save();break;} 
if(v[0]=='n'||v[0]=='N') break; 
else printf(" Input is wrong ! Please re-enter :"); 
 } 
} 
if(s==1){ 
printf(" Please enter the number of statisticians :"); 
 scanf("%d",&N); 
gets(v); // Eliminate offending data.  
for(int i=1;i<=N;i++) 
 newsect(i); 
} 
else return; 
} 
 
 
void main() // The main function  
{ char v[10]; 
 int choice,c; 
pro=(STU*)malloc(size*sizeof(STU)); 
if(pro==NULL) 
{ 
printf(" Memory allocation error !\n"); 
return; 
} 
printf("\t\t\t\t Student grade management system \n\n"); 
do 
{ choice=-1;  
printf("\n0. new \t\t1. add \t\t\t2. To find the \t3. delete \n4. The list of output \t5. The sorting \t\t\t6 Modify the \n7. file \t8. Read the saved record file \t9 exit \n Please select the appropriate action :"); 
scanf("%d",&choice); 
gets(v);  // Eliminate illegal data to prevent it from becoming a dead loop  
switch(choice) 
{case 0:abc(1);break; 
case 1:++N;newsect(N);break; 
case 2:lookout();break; 
case 3:remove();break; 
case 6:change();het(1,N,1);break; 
case 5:if(N==0){printf(" Record is empty !\n");break;} 
 else {printf("1. In descending order  2. In ascending order  3. In ascending order of student number  4. In descending order \n Please select the appropriate function :"); 
 scanf("%d",&c);het(1,N,c);} 
case 4:showlist();break; 
case 7:save();break; 
case 8:loadrecords();break; 
case 9:abc(0);printf(" Thank you for using !\n");break; 
default:printf(" Selection is invalid !\n");break; 
} 
} 
while(choice!=9); 
} 

For more information, please refer to the topic "management system development".


Related articles: