私のC/C++の道-010課(構造体使用の簡単な例(学生成績管理))
最近は忙しくて、何日間書きませんでした.今日作ったのは構造体の応用(学生の成績管理)です.多くの友達がやったと信じています.ここで恥をさらして、Cをコントロールするのは難しいです.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/**
:
1. ( , , , , )
( , )
2. , , ,
*/
typedef struct Student
{
char no[4];//3 ,
char name[10];
int chinese;
int math;
int english;
struct Student *next;//
}StudentList;
void init(StudentList *&);
void select(StudentList *); //
void insert(StudentList *&); //
void update(StudentList *&); //
void deleteData(StudentList *);//
void studentScoreManege()
{
StudentList *stuList = NULL;
int oprate;
init(stuList);//
do{
system("cls");
fflush(stdin);
puts("************* Student Data Oprate *******************");
puts("* 1. select student score *");
puts("* 2. insert student score *");
puts("* 3. update student score *");
puts("* 4. delete student data *");// , ,
puts("* 0. exit *");
puts("*****************************************************");
printf("please select a number: ");
scanf("%d",&oprate);
switch(oprate)//
{
case 1:
select(stuList);
break;
case 2:
insert(stuList);
break;
case 3:
update(stuList);
break;
case 4:
deleteData(stuList);
break;
case 0:
default:
break;
}
}while(oprate!=0);
}
//
void init(StudentList *&stuList)
{
stuList = (StudentList *)malloc(sizeof(StudentList));
stuList->next = NULL;
}
//
void select(StudentList *stuList)
{
StudentList *p = stuList->next;
if(p==NULL)
{
puts("there is no data,please insert!!");
puts("press any key to return!!!");
getchar();
}
puts("************** student score insert *********************");
puts("No\tName\tChinese\tMath\tEnglish");
while(p!=NULL)
{
printf("%s\t%s\t%d\t%d\t%d
",p->no,p->name,p->chinese,p->math,p->english);
p = p->next;
}
fflush(stdin);
puts("press any key to return!!!");
getchar();
}
//
void insert(StudentList *&stuList)
{
StudentList *p ,*s;
p = stuList;
while(p->next != NULL)
{
p = p->next;
}
do{
s = (StudentList *)malloc(sizeof(StudentList));
puts("please insert student data(NO,Name,Chinese,Math,English):");
scanf("%3s%10s",&s->no,&s->name);
scanf("%d%d%d",&s->chinese,&s->math,&s->english);
// ,
if(s->chinese<0||s->chinese>100||s->math<0||s->math>100||s->english<0||s->english>100)
{
puts("input data error !!!");
return;
}
s->next = NULL;
p->next = s;
p = p->next;//
fflush(stdin);// '
' getchar()
puts("Continue?ENTER to yes or input no to exit");//
}while(getchar()=='
');
}
//
void update(StudentList *&stuList)
{
StudentList *p = stuList->next;
char no[4];//3 , 4~ , ,
fflush(stdin);//
puts("please input student No: ");
gets(no);
//
if(p==NULL)
{
puts("there is no record,please insert!!");
puts("press any key to return!!!");
getchar();
return;
}
else
{
while(p!=NULL)
{
if(!strcmp(p->no,no)){// ,
puts("NO\tNAME\tChinese\tMath\tEnglish");
printf("%s\t%s\t%d\t%d\t%d
",p->no,p->name,p->chinese,p->math,p->english);
break;
}
else{
p = p->next;
}
}
if(p==NULL)//
{
puts("can't find this student's record!!!");
puts("press any key to return!!!");
getchar();
return;
}
}
// ,
puts("please update student data(Name,Chinese,Math,English):");
scanf("%10s",&p->name);
scanf("%d%d%d",&p->chinese,&p->math,&p->english);
fflush(stdin);
puts("update successful,press any key to return!!!");
getchar();
}
//
void deleteData(StudentList *stuList)
{
/* , , ,
a->b->c, b, a->c , free(b),
a->next=b->next
*/
StudentList *p = stuList,*q;
char no[4];//3 , 4~ , ,
fflush(stdin);//
puts("please input student No: ");
gets(no);
//
if(p->next==NULL)
{
puts("there is no record !!!");
puts("press any key to return!!!");
getchar();
return;
}
else
{
while(p->next!=NULL)
{
if(!strcmp(p->next->no,no)){// ,
puts("NO\tNAME\tChinese\tMath\tEnglish");
printf("%s\t%s\t%d\t%d\t%d
",p->next->no,p->next->name,p->next->chinese,p->next->math,p->next->english);
break;
}
else{
p = p->next;
}
}
if(p->next==NULL)//
{
puts("can't find this student's record!!!");
puts("press any key to return!!!");
getchar();
return;
}
}
puts("Are you sure delete this record? press ENTER to confirm or other key to cancel!!!");
if(getchar()=='
')// , , GOOGLE OR BAIDU
{
q = p->next;
p->next = q->next;
free(q);//
}
puts("delete successful,press any key to return!!!");
getchar();
}