[セットトップ]学生成績管理システム


解析:
最近友达を手伝って1つの课程の设计に相当する学生の成绩の管理システムをして、彼女达はc言语で书くことを要求して、1人の惯れたc++の子供にとって本当に书く各种の制限が本当に无言だと感じて、直接コードを言いません
#include "student.h"


void main()
{
	char ch='0';
	int index=0;
	char id[ID_SIZE];
	student students;
	student phead;

	FILE *output=fopen("output.txt","w+");
	FILE *input=fopen("input.txt","r");	
	
	if(input==NULL)
	{
		printf("    input
"); exit(1); } if(input==NULL) { printf(" output
"); exit(1); } phead.next=NULL; InitStudent(&students); while(1) { ShowList(); printf(" :"); ch=getchar(); switch(ch) { case '1': Student_Insert(&students); break; case '2': Student_Display(&students); break; case '3': { printf(" :"); scanf("%d",&index); Student_DisplaySingle(&students,index); } break; case '4': { printf(" :"); scanf("%s",id); Student_Delete(&students,id); } break; case '5': Student_Modify(&students); break; case '6': { printf(" :"); scanf("%s",id); Student_Select(&students,id); } break; case '7': Student_SortByAverage(&students,&phead); break; case '8': { IO_ReadInfo(&students,output); fclose(output); } break; case '9': { IO_WriteInfo(&students,output); fclose(output); } break; case '0': { printf("
"); exit(1); } default: break; } getchar(); } }
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#define ID_SIZE 128
#define NAME_SIZE 128
#define SIZE 125


struct student
{
	char Id[ID_SIZE];  //    
	char Name[NAME_SIZE]; //    
	float Math;   //  
	float English; //  
	float Chinese;  //  
	float TotalScore; //  
	float AverageScore; //   
	int PassStudent;  //    
	struct student *next;
};
typedef struct student student;
static float PassRate=0.0;   //   

//       
void InitStudent(student *head)
{
	strcpy(head->Id,"");
	strcpy(head->Name,"");
	head->Chinese=0.0;
	head->English=0.0;
	head->Math=0.0;
	head->PassStudent=0;
	head->TotalScore=0;
	head->next=NULL;
}

//      
void Student_Insert(student *head)
{
	int flag=0;
	student *cur=head;
	student *tmp=(student *)malloc(sizeof(student));
	tmp->next=NULL;
	assert(tmp !=NULL);

	printf("***        ***
"); printf(" :"); scanf("%s",tmp->Id); printf(" :"); scanf("%s",tmp->Name); printf(" :"); scanf("%f",&tmp->Math); printf(" :"); scanf("%f",&tmp->English); printf(" :"); scanf("%f",&tmp->Chinese); tmp->TotalScore=tmp->Chinese+tmp->English+tmp->Math; tmp->AverageScore=tmp->TotalScore/3; tmp->PassStudent=0; if(tmp->Chinese >=60) tmp->PassStudent++; if(tmp->English >=60) tmp->PassStudent++; if(tmp->Math >=60) tmp->PassStudent++; if(tmp->PassStudent==3) flag++; tmp->AverageScore=tmp->TotalScore/3; printf(" :%d
",flag); while(cur->next !=NULL) cur=cur->next; cur->next=tmp; } /* */ void Student_Delete(student *head,char *id) { student *del=NULL; student *cur=head; while(cur->next !=NULL) { if(strcmp(cur->next->Id,id)==0) { del=cur->next; cur->next=del->next; free(del); printf("
"); return; } cur=cur->next; } printf("
"); } /* */ void Student_Modify(student *head) { char number[ID_SIZE]; int flag=0; student *cur=head->next; assert(cur !=NULL); printf(" :"); scanf("%s",number); printf("
"); while(cur !=NULL) { if(strcmp(cur->Id,number)==0) { printf(" :"); scanf("%f",&cur->Chinese); printf(" :"); scanf("%f",&cur->Math); printf(" :"); scanf("%f",&cur->English); flag=1; break; } cur->next; } if(flag) printf("
"); else printf("
"); } /* */ void Student_Select(student *head,char *name) { student *cur=head->next; while(cur!=NULL) { if(strcmp(cur->Name,name)==0) { printf("
"); return; } cur=cur->next; } printf("
"); } /* */ void Student_DisplaySingle(student *head,int index) { int count=1; student *cur=head->next; while(count <index && cur !=NULL) { cur=cur->next; count++; } if(count >index) printf("
"); else { printf("%s\t%s\t%.3f\t%.3f\t%.3f\t",cur->Id,cur->Name,cur->Chinese,cur->Math,cur->English); printf("%.3f\t%.3f
",cur->AverageScore,cur->TotalScore); } } // void Student_Display(student *head) { int count=0; int number=0; student *cur=head->next; printf(" \t \t \t \t \t \t
"); while(cur !=NULL) { printf("%s\t%s\t%.3f\t%.3f\t%.3f\t",cur->Id,cur->Name,cur->Chinese,cur->Math,cur->English); printf("%.3f\t%.3f
",cur->AverageScore,cur->TotalScore); if(cur->Chinese >=60 && cur->English>=60 && cur->Math>=60) count++; cur=cur->next; number++; } PassRate=(float)count/number*100; printf(" :%.1f%%
",PassRate); } /* */ void Student_SortByAverage(student *head,student *phead) { student *pcur=NULL; student *tmp=NULL; student *ptmp=NULL; student *cur=head->next; while(cur !=NULL) { pcur=cur; while(pcur !=NULL && pcur->AverageScore ==INT_MAX) pcur=pcur->next; if(pcur==NULL) break; tmp=pcur; while(pcur !=NULL) { if(pcur->AverageScore > tmp->AverageScore && pcur->AverageScore !=INT_MAX) { tmp=pcur; } pcur=pcur->next; } if(tmp->AverageScore !=INT_MAX) { ptmp=(student *)malloc(sizeof(student)); assert(ptmp !=NULL); strcpy(ptmp->Id,tmp->Id); strcpy(ptmp->Name,tmp->Name); ptmp->Chinese=tmp->Chinese; ptmp->English=tmp->English; ptmp->Math=tmp->Math; ptmp->AverageScore=tmp->AverageScore; ptmp->PassStudent=tmp->PassStudent; ptmp->TotalScore=tmp->TotalScore; ptmp->next=phead->next; phead->next=ptmp; tmp->AverageScore=INT_MAX; } } Student_Display(phead); } /* */ void IO_ReadInfo(student *head,FILE *path) { student *cur=head->next; while(cur !=NULL) { if(fread(cur,sizeof(student),1,path) < 0) { printf("fread failed
"); break; } cur=cur->next; } printf("
"); } /* */ void IO_WriteInfo(student *head,FILE *path) { student *cur=head->next; while(cur !=NULL) { fprintf(path,"%s %s %f %f %f %f %f %d %s",\ cur->Id,cur->Name,cur->Math,cur->English,cur->Chinese,\ cur->TotalScore,cur->AverageScore,cur->PassStudent,"
"); cur=cur->next; } printf("
"); } // void ShowList() { printf("**********************************************
"); printf("**【1】 【2】 **
"); printf("**【3】 【4】 **
"); printf("**【5】 【6】 **
"); printf("**【7】 【8】 **
"); printf("**【9】 【0】 **
"); printf("**********************************************
"); }