您的位置:首页 > 其它

学生信息管理系统

2017-09-06 15:59 239 查看
半个月前写的程序,因为军训的缘故没写完。当时写的时候懒得一个个函数分别测试,今天编译的时候才发现各种bug,搞了半天还是搞不定,十分烦躁,遂决定先搁着一段时间,等啥时候心情好了再debug。

同时,这个项目了我很大的教训,以后写大项目时一定要分别测试!

////////////////////////////////////////////////////////////////

更新1:忙过了开学终于有时间debug了,搞了一上午终于基本上搞定。很多地方懒得考虑错误输出了,所以鲁棒性应该会很弱,但好歹第一次写完一个几百行的程序,还是很开心的。

//head.h

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
#define ALL 50
#define LEN sizeof(struct student)

typedef struct student
{                         // 不要写成typeof
int number;
char name[15];
double Chinese;
double Math;
double English;
double sum;
struct student *next;
}Student;

Student * head;
Student* EDIT(Student* head);
Student* DELETE(Student* head);
Student* ADD(Student* head);
Student* FIND(Student* head);
Student* SHOW(Student* head);
void COUNT(Student* head);
Student* ORDER(Student* head);
Student*  INPUT(Student* head);
void OUTPUT(Student* head);
void INITIAL(void);
void PASSWORD(void);
void FREE(void);
/////////////////////////////////////////////////////////////////////////////////

//main.cpp

#include"head.h"
int main(void)
{
//PASSWORD();
INITIAL();
return 0;
} //main函数里定义的变量不是全局变量

void PASSWORD(void)
{
char password[10];//不能使用字符串指针形式,因为那样并没有初始化,第15行会报错
char* right_password = "12345";
printf("Welcome!\n");
printf("Please enter your password:");
scanf("%s", password);//passwordb本身就是地址了,不用再加&
getchar();
int i = 0;
while (strcmp(password, right_password)!=0 && i<3)
{
printf("wrong password!please enter again:");
i++;
scanf("%s", password);
getchar();//处理回车符
}
if (i == 3)//小心 == 写成 =
{
printf("you enter too much wrong password!\n");
exit(0);
}
if (i < 3)
{
printf("succeed!\n");
}
}
void INITIAL(void)
{

while (1)
{
printf("please choose what fuction you want to use:\n"
"1.ADD  2.SHOW  3.DELETE    4.EDIT  \n"
"5.FIND 6.COUNT 7.ORDER 8.INPUT\n"
"9.OUTPUT   0.QUIT\n");
char choice;
scanf("%c", &choice);
while (getchar() != '\n') continue;
switch (choice)
{
case'1':
head = ADD(head);
break;
case'3':
head = DELETE(head);
break;
case'4':
head = EDIT(head);
break;
case'2':
head = SHOW(head);
break;
case'5':
head = FIND(head);
break;
case'6':
COUNT(head);
break;
case'7':
head = ORDER(head);
break;
case'8':
head=INPUT(head);
break;
case'9':
OUTPUT(head);
break;
case'0':
FREE();
exit(0);
default:
printf("Invalid enter,please enter again\n");
break;
}
}

}
Student* ADD(Student* head)  //直接传变量传递的是副本!!
{
while (1)
{
Student* temp = (Student*)malloc(LEN);
printf("please enter sutdents' information,enter 0 to exit!\n");

tag1:
printf("please enter student's number:");
int a = -1;
scanf("%d", &a); //防止退出时改变上一个number的值
while (getchar() != '\n') continue;
if (a)
{
temp->number = a;

if (temp->number < 0)
{
printf("Be serious!\n");
goto tag1;
}
}
else break;

Student *p1 = head, *p2 = head;//为了不改变head
while (p1 != NULL)
{

if (p1->number != temp->number)
{
p1 = p1->next;
}
else
{
printf("this number has already existed,please enter again\n");
goto tag1;
}

}
printf("please enter stduent's name:");
scanf("%s", &(temp->name));
while (getchar() != '\n') continue;
printf("please enter stduent's English grade:");
scanf("%lf", &(temp->English));
while (getchar() != '\n') continue;
printf("please enter stduent's Chinese grade:");
scanf("%lf", &(temp->Chinese));
while (getchar() != '\n') continue;
printf("please enter stduent's Math grade:");
scanf("%lf", &(temp->Math));
while (getchar() != '\n') continue;
temp->sum = temp->English + temp->Chinese + temp->Math;
temp->next = NULL;
if(head==NULL) head = temp;
else
{
while(p2->next != NULL) p2 = p2->next;
p2->next = temp;
temp = NULL;
}
}
return head;
}
Student* FIND(Student* head)
{
Student* p1=head;
printf("please enter student's number you want to search:");
int num;
scanf("%d", &num);
getchar();
while (p1->number != num&&p1->next != NULL)
{
p1 = p1->next;
}
if (num == p1->number)
{
printf("number    name      English   Chinese   Math      sum\n");
printf("%-10d%-10s%-10.1f%-10.1f%-10.1f%-10.1f\n", p1->number, p1-&g
c3a0
t;name, p1->English, p1->Chinese, p1->Math, p1->sum);
}
if (num != p1->number) printf("this stduent doesn't exist\n");
return head;
}
Student* EDIT(Student* head)
{
Student* p1 = head;
printf("please enter student's number you want to edit:");
int num;
scanf("%d", &num);
getchar();
while (p1->number != num&&p1->next != NULL)
{
p1 = p1->next;
}
if (num == p1->number)
{
while (1)
{
printf("please enter which part you want to edit(enter EXIT to exit):");
char part[20];
scanf("%s", part);
getchar();
if (strcmp(part, "name")==0)//strcmp相等返回0
{
printf("What do you want to change into:");
scanf("%s", &p1->name);
getchar();

}
else if(strcmp(part, "Chinese")==0)
{
printf("What do you want to change into:");
scanf("%lf", &p1->Chinese);
getchar();
}
else if(strcmp(part, "English")==0)
{
printf("What do you want to change into:");
scanf("%lf", &p1->English);
getchar();
}
else if (strcmp(part, "Math")==0)
{
printf("What do you want to change into:");
scanf("%lf", &p1->Math);
getchar();
}
else if (strcmp(part, "EXIT")==0)
{
break;
}
else
{
printf("Be serious!\n");
break;
}
}
}
if (num != p1->number) printf("this student doesn't exist\n");
return head;
}
void COUNT(Student* head)
{
int count = 1;
Student* p1 = head;
if (p1 == NULL) printf("No stduent\n");
else
{
while (p1->next != NULL)
{
p1 = p1->next;
count++;
}
printf("totally %d stduents\n", count);
}
return ;
}
Student* DELETE(Student* head)
{
printf("please enter which student you want to delete:");
int num;
scanf("%d", &num);
getchar();
Student* p1 = head, *p2=NULL;//*p2=NULL是无意义的,然而由于231行如果没进入循环的话242行p2会出现未定义,多以必须提前定义p2
while (p1->number != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}

if (num == p1->number)
{
if (p1 == head)//记得考虑首节点情况
head = p1->next;
else
p2->next = p1->next;

}
else printf("this stduent doesn't exist\n");
return head;
}
Student* SHOW(Student* head)
{
Student* p1 = head;
if (p1 == NULL) printf("no student!\n");
else
{
printf("number    name      English   Chinese   Math      sum\n");
printf("%-10d%-10s%-10.1f%-10.1f%-10.1f%-10.1f\n", p1->number, p1->name, p1->English, p1->Chinese, p1->Math, p1->sum);
while (p1->next != NULL)
{
p1 = p1->next;
printf("%-10d%-10s%-10.1f%-10.1f%-10.1f%-10.1f\n", p1->number, p1->name, p1->English, p1->Chinese, p1->Math, p1->sum);
}
}
return head;
}
Student* INPUT(Student* head)
{
printf("please enter the name of file you want to input:");
char name[50];
scanf("%s", name);
getchar();
FILE * fp1;
if ((fp1 = fopen(name, "r+")) == NULL)
{
printf("can't open this file!\n");
return head;
}
int test;
while (1) //如果用feof判断,则会多一次循环
{
Student* temp = (Student*)malloc(LEN);//malloc若放在外面 head会随着temp变化
test=fscanf(fp1, "%d %s %lf %lf %lf %lf", //fscanf不能加格式限定符
&temp->number, temp->name, &temp->English, &temp->Chinese, &temp->Math, &temp->sum);//记住加&
temp->next = NULL;
if (test != 6) break;
Student* p2 = head;
if (head == NULL) head = temp;
else
{
while (p2->next != NULL) p2 = p2->next;
p2->next = temp;
}
}
fclose(fp1);
return head;
}
void OUTPUT(Student* head)
{
printf("please enter the name of file you want to output:");
char name[50];
scanf("%s", name);
getchar();
FILE * fp1;
if ((fp1 = fopen(name, "a+")) == NULL)
{
printf("can't open this file!\n");
return;
}
Student* p1 = head;
while (p1!=NULL)//也可用feof判断结尾
{
fprintf(fp1,"%-10d%-10s%-10.1f%-10.1f%-10.1f%-10.1f\n", p1->number, p1->name, p1->English, p1->Chinese, p1->Math, p1->sum);
p1 = p1->next;
}
fclose(fp1);
return ;
}
Student* ORDER(Student* head)
{
Student* first = NULL,*new_head=NULL,*min,*p_last=head,*p_now;
while (head != NULL)
{
for (p_now = head, min = head; p_now->next != NULL; p_now = p_now->next)
{
if (p_now->next->number < min->number)
{
p_last = p_now;
min = p_now->next;
}
}

if (first == NULL)
{
first = min;
new_head = first;
}
else
{
first->next = min;
first = first->next;
}

if (min == head)
{
head = head->next;
}
else
{
p_last->next = min->next;
}
}
first-> next = NULL;
head=new_head;
return head;
}//按编号排序
void FREE(void)
{
Student* p;
while (head)
{
p = head;
head = head->next;
free(p);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: