📄 credit[1].c
字号:
#include "stdio.h"
#include "ctype.h"
#include "conio.h"
#include "stdlib.h"
#include "process.h"
#include "string.h"
/*Creat student's information data type*/
struct stuinfo{
char num[9];
int base;
int major;
int optional;
int humanity;
int exp;
};
/*Creat link node data type and creat head, rear and current pointer*/
struct LinkNode{
struct stuinfo data;
struct LinkNode *next;
}*first,*end,*current;
char menu_select(void);/*menu function*/
void EnterCredit(void);/*insert a information*/
void DisplayOne(void);/*display a information*/
void DisplayAll(void);/*display all the information*/
void DisplayClass(void);/*display all the information in the same class*/
void Delete(void);/*delete one information*/
void Modify(void);/*modify one information*/
void Sort();
void SortByBase();
void SortByMajor();
void SortByOptional();
void SortByHum();
void SortByExp();
void Statistic();
int SaveData();/*save the information to binary file and txt file*/
int LoadData();/*load information*/
int IsValid(struct stuinfo);/*test the data whether valid or not*/
void DestroyLink(void);/*destroy the link list*/
main()
{
char choice;
first=end=current=NULL;
system("cls");/*run dos command to clean screen*/
/*display the menu and get choice, then call function*/
for(;;){
choice=menu_select();
switch(choice){
case '1':EnterCredit();break;
case '2':DisplayOne();break;
case '3':DisplayClass();break;
case '4':Delete();break;
case '5':Modify();break;
case '6':Sort();break;
case '7':Statistic();break;
case '8':SaveData();break;
case '9':LoadData();break;
case '0':DestroyLink();return 1;
}
}
}
char menu_select(void)
{
/*Print the menu and get the chioce.*/
char temp='\0',ch='\0';
system("cls");
printf("1.Enter a student's credit.\n");
printf("2.Display a student's credit by student's number.\n");
printf("3.Display all the student's credit in the same class.\n");
printf("4.Delete a student's credit by student's number.\n");
printf("5.Modify a student's credit.\n");
printf("6.Sort the information by one credit.\n");
printf("7.List some statistic.\n");
printf("8.Save data to file.\n");
printf("9.Load data to file.\n");
printf("0.Exit System.\n");
printf("Enter your choice:");
/*using getch() function get choice and using enter key to confirm*/
while(!isdigit(temp)&&temp!='\10'&&temp!='\15'){
temp=getch();
if(isdigit(ch)){
if(temp=='\15')break;
if(temp=='\10'){
ch='\0';
putchar(temp);
putchar(' ');
putchar(temp);
temp='\0';
}
}
else if(isdigit(temp)){
ch=temp;
putchar(ch);
}
temp='\0';
}
putchar('\n');
return ch;
}
void EnterCredit(void)
{
struct stuinfo temp;
struct LinkNode *p;
char *num,ch;
/*get the information from keybroad*/
num=(char*)malloc(sizeof(char)*9);
fflush(stdin);/*clean the input stream*/
printf("Please input student's number:");
gets(num);
printf("Please input student's basic credit:");
scanf("%d",&temp.base);
printf("Please input student's major credit:");
scanf("%d",&temp.major);
printf("Please input student's optional credit:");
scanf("%d",&temp.optional);
printf("Please input student's humanity credit:");
scanf("%d",&temp.humanity);
printf("Please input student's experiment credit:");
scanf("%d",&temp.exp);
strcpy(temp.num,num);
if(IsValid(temp)){/*if the data is valid the insert to the link list*/
if(first!=NULL){
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
p->data=temp;
p->next=NULL;
end->next=p;
end=p;
}
else{
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
p->data=temp;
p->next=NULL;
first=end=current=p;
}
}
else{
printf("The data you entered is invalid. The following is probable problem:\n");
printf(" 1.The student's number is not unique.\n");
printf(" 2.The student's number is invalid.\n");
printf(" 3.There some of credit you entered is below zero\n");
}
printf("Press any key to continue...");
ch=getch();
}
void DisplayOne(void)
{
char ch,num[9];
printf("Enter the number you want to browse(default is browse all):");
fflush(stdin);
gets(num);
printf(" Number Basic Major Optional Humanity Experiment\n");
current=first;
while(current){/*if num is null, then display all*/
if(strlen(num)==0){
DisplayAll();
break;
}
else if(strcmp(current->data.num,num)==0){
printf("%s\t %d\t %d\t %d\t\t %d\t\t %d\n",current->data.num,current->data.base,current->data.major,current->data.optional,current->data.humanity,current->data.exp);
break;
}
current=current->next;
}
printf("Press any key to continue...");
ch=getch();
}
void DisplayAll(void)
{
while(current){
printf("%s\t %d\t %d\t %d\t\t %d\t\t %d\n",current->data.num,current->data.base,current->data.major,current->data.optional,current->data.humanity,current->data.exp);
current=current->next;
}
}
void DisplayClass(void)
{
char ch,num[5];
printf("Enter the class you want to browse(default is browse all):");
fflush(stdin);
gets(num);
printf(" Number Basic Major Optional Humanity Experiment\n");
current=first;
while(current){
if(strlen(num)==0){
DisplayAll();
break;
}
else if((current->data.num[4]==num[4])&&(current->data.num[5]==num[5])){/*the class number is same*/
printf("%s\t %d\t %d\t %d\t\t %d\t\t %d\n",current->data.num,current->data.base,current->data.major,current->data.optional,current->data.humanity,current->data.exp);
}
current=current->next;
}
printf("Press any key to continue...");
ch=getch();
}
void Delete()
{
char ch,num[9];
struct LinkNode *pre,*q;
printf("Enter the number you want to delete:");
fflush(stdin);
gets(num);
if(strcmp(first->data.num,num)==0){/*delete the head link node*/
q=first;
first=first->next;
free(q);
}
else{
current=pre=first;
current=pre->next;
while(current){
if(strcmp(current->data.num,num)==0){
q=current;
pre->next=q->next;
free(q);
break;
}
pre=pre->next;
current=pre->next;
}
}
printf("Press any key to continue...");
ch=getch();
}
void Modify(void)
{
char ch,num[9];
int t=0;
struct stuinfo temp;
printf("Please input the student's number you want to modify:");
fflush(stdin);
gets(num);
current=first;
while(current){
if(strcmp(current->data.num,num)==0){
printf("Please input student's basic credit:");
scanf("%d",&temp.base);
printf("Please input student's major credit:");
scanf("%d",&temp.major);
printf("Please input student's optional credit:");
scanf("%d",&temp.optional);
printf("Please input student's humanity credit:");
scanf("%d",&temp.humanity);
printf("Please input student's experiment credit:");
scanf("%d",&temp.exp);
strcpy(temp.num,num);
if(AsValid(temp))
current->data=temp;
else{
printf("The data you entered is invalid. The following is probable problem:\n");
printf(" 1.The student's number is not unique.\n");
printf(" 2.There some of credit you entered is below zero\n");
}
t=1;
break;
}
current=current->next;
}
if(t==0)
printf("The number you entered is not exist!\n");
printf("Please press any key to continue!\n");
ch=getch();
}
void Sort()
{
char ch='\0',temp='\0',any;
printf("There five choices you can sort the data:\n");
printf("1.Sorted by the basic class credit.\n");
printf("2.Sorted by the major class credit.\n");
printf("3.Sorted by the optional class credit.\n");
printf("4.Sorted by the humanity class credit.\n");
printf("5.Sorted by the experiment class credit.\n");
printf("Enter your choice:");
while(temp!='1'&&temp!='2'&&temp!='3'&&temp!='4'&&temp!='5'&&temp!='\10'&&temp!='\15'){ temp=getch();
if(ch=='1'||ch=='2'||ch=='3'||ch=='4'||ch=='5'){
if(temp=='\15')break;
if(temp=='\10'){
ch='\0';
putchar(temp);
putchar(' ');
putchar(temp);
temp='\0';
}
}
else if(temp=='1'||temp=='2'||temp=='3'||temp=='4'||temp=='5'){
ch=temp;
putchar(ch);
}
temp='\0';
}
putchar('\n');
switch(ch){
case '1':SortByBase();break;
case '2':SortByMajor();break;
case '3':SortByOptional();break;
case '4':SortByHum();break;
case '5':SortByExp();break;
}
printf("Press any key to continue...");
any=getch();
}
void SortByBase()
{
struct LinkNode *q,*newfirst=NULL,*newend=NULL,*pre,*p,*old;
struct stuinfo temp;
int base;
while(first){
q=NULL;
current=first->next;
pre=first;
base=first->data.base;
while(current){/*get the max basic class credit from the link*/
if(base<current->data.base){
q=pre;
base=current->data.base;
}
pre=pre->next;
current=pre->next;
}
/*delete the found link node*/
if(q==NULL){
temp=first->data;
old=first;
first=first->next;
free(old);
}
else{
temp=q->next->data;
old=q->next;
q->next=old->next;
free(old);
}
/*insert to the new link list*/
if(newfirst!=NULL){
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
p->data=temp;
p->next=NULL;
newend->next=p;
newend=p;
}
else{
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
p->data=temp;
p->next=NULL;
newfirst=newend=p;
}
}
first=newfirst;
end=newend;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -