链表实现:学生信息管理系统--SIMS

以下是要求:

1. Objective

a) Learn how to define a structure;

b) Describe the process of algorithm design and analysis;

c) Learn how to use the fundamental linear data structures;

d) Get familiar with the file access;

2. Problem Description

a) Design and Implementation of Student Information Management System

Student Information Management System (SIMS) is a student-level data collection system that allows the Department to collect and analyze more accurate and comprehensive information.

Basic four features make up most of this project, but you can write your own code to add more features, and make this project more effective and better overall. You can add “Search Records” feature. Here’s a brief overview of the features:

Add Records: This feature allows you to add general information records. That includes the student’s name, ID, sex, age and score of computer programming. All the added records in this student database management system are stored in file.

List Records: It lists all the added records from the first feature. There are some improvements you can make to this feature to make the list look more attractive.

Modify Records: This feature allows you to modify the added records. The modified records are stored in file.

Delete Records: This feature is for deleting the added students’ information from the file.

Requriments

1) Write the code with comment

2) Design the main menu and sub menu firstly. For example, 4 items including Add Record, List Record, Modify Record and Exit System could be designed in the main menu. One can give your choice by inputing 1, 2, 3 or 4.

3) Manage the student information using the linked list;

4) Realize the following basic functions:

− List records

− Modify records

− Delete records

5) Select a sort algorithm, and then sort the data by score;

6) Write the student information into the file which is located in the same path with the executable file;

7) The program also should have the ability to :

− load the file created previously

− append new data to the file

− modify or delete the specified data, and then update the file

主要实现功能就是,可以增加记录,修改记录,删除记录,列出所有记录

以下是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct StudentNode{     //链式存储结构定义 
	char name[20];              //姓名 
	char sex[20];
	//char *name;
	//char *sex;
	int ID;                     //ID  
	int age;                    //年龄 
	int score;                  //得分 
	struct StudentNode *next; 
}*Stu,stu_node;

void init(Stu &s){             // 初始化链表    
    s = (Stu)malloc(sizeof(stu_node));  
    s->ID = 0;  
    s->age = 0;
    char tmp[20]={'\0'};
    char tmp1[20]={'\0'};
	//s->name = "0";
	//s->sex[10] = {0};
	strcpy(s->name,tmp);
	strcpy(s->name,tmp1);
	s->score = 0;  
    s->next = NULL;
	printf("Initing.....\n");
	printf("Init success!!\n");    
}  

Stu CreateRecords(){                  //根据文件内容创建信息库链表 
	Stu S;
	init(S);
	stu_node *p;
	p=S;
	
	FILE *fp;  
    fp = fopen("records.txt","r");
	printf("Loading file....\n");  
    if(fp == NULL){
    	printf("Open file failed!!\n");
    	printf("CreateRecords failed!!\n");
    	return S;
	}  
	printf("Load file success!!\n");
    while(!feof(fp)){
    	stu_node *s;
    	s = (Stu)malloc(sizeof(stu_node));
    	
	    fscanf(fp,"%d",&s->ID);
		fscanf(fp,"%s",s->name);
	    fscanf(fp,"%d",&s->age);
		fscanf(fp,"%s",s->sex);
		fscanf(fp,"%d",&s->score);
		
		p->next=s;
		p=s;
	}
	printf("CreateRecords success!!\n");
	return S;
}

void printRecords(Stu s){                      //打印当前链表内容,调试时候使用 
	stu_node *p;
	p=s->next;
	while(p != NULL){
		printf("%d %s %d %s %d\n",p->ID,p->name,p->age,p->sex,p->score);
		p=p->next;
	}
}
//删除记录 
void DeleteRecords(Stu &s,int id){
	stu_node *p;
	p=s->next;
	bool flag = false;
	while(p!=NULL){
		if(p->next != NULL){
			if(p->next->ID == id){
				p->next = p->next->next;
				flag = true;
				break;
			}
		}
		p=p->next;
	}
	if(flag==false) printf("This id is not find!!\n");
	else printf("Delete success!!!\n");
}
//增加记录 
void ADDRecords(Stu &S,int id,char name[20],int age,char sex[10],int score){
	stu_node *s;
	s = (Stu)malloc(sizeof(stu_node));
	s->ID=id;
    s->age = age;
	strcpy(s->name,name);
	strcpy(s->sex,sex);
	//s->name = name;
	//s->sex = sex;
	s->score = score;
	
	s->next=S->next;
	S->next=s;
	printf("Add records success!!\n");
}
//这里有两个修改记录的函数,第一个可以修改整数类型的年龄和分数
//第二个可以修改字符串类型的姓名和性别,默认ID不能修改 
void ModifyRecords1(Stu &s,int id,int num,int value){
	stu_node *p;
	p=s->next;
	bool flag = false;
	while(p!=NULL){
		if(p->ID==id){
			if(num==2) p->age = value;
			else if(num==4) p->score = value;
			flag=true;
			break;
		}
		p=p->next;
	}
	if(flag==false) printf("This id is not find!!\n");
	else printf("Modify success!!!\n");
}
void ModifyRecords2(Stu &s,int id,int num,char value[20]){
	stu_node *p;
	p=s->next;
	bool flag = false;
	while(p!=NULL){
		if(p->ID==id){
			if(num==1) strcpy(p->name,value);
			else if(num==3) strcpy(p->sex,value);
			flag=true;
			break;
		}
		p=p->next;
	}
	if(flag==false) printf("This id is not find!!\n");
	else printf("Modify success!!!\n");
}
/*
	使用桶排序进行排序:
		假定分数上限是100分,一个班级最多有100人;
		桶内记录的不是得该分数的学生的个数,而是学生的ID。所以用了矩阵。 
		桶装完以后,对链表进行重新组合;
		代码如下 
*/ 

void SortRecords(Stu &S){
	int bucket[101][101]={0};
	int count[101]={0};             //记录每个桶里元素的个数; 
	stu_node *pos;                //记录已排好序的最后一个元素; 
	stu_node *p,*tmp;
	pos=S;
	p=S->next;
	while(p != NULL){
		bucket[p->score][count[p->score]++] = p->ID;
		p=p->next;
	}
	//p=S->next;
	/*--------------
	for(int i = 0; i < 101;i++){
		printf("%d: ",i);
		for(int j = 0; j < count[i]; ++j){
			printf("%d ",bucket[i][j]);
		}
		printf("\n");
		//printf("%d:%d\n",i,count[i]);
	}
	*///--------------
	for(int i = 100; i >= 1; i--){
		for(int j=0; j<count[i]; j++){
			p=pos->next;
			if(p->ID==bucket[i][j]) pos=pos->next;
			else{
				while(p != NULL){
					if(p->next != NULL){
						if(p->next->ID==bucket[i][j]){
							tmp=p->next;
							p->next=p->next->next;
							tmp->next=pos->next;
							pos->next=tmp;
							pos=tmp;
							break;
						}
					}
					p=p->next;
				}
			}
		}
	}
}
//列出最后结果函数:
//在屏幕上打印一遍,将结果记录在reault.txt里,这里默认排序 
void ListRecords(Stu &s){
	SortRecords(s);
	FILE *ffp;
	ffp=fopen("result.txt","w");
	stu_node *p;
	p=s->next;
	while(p != NULL){
		fprintf(ffp,"%d %s %d %s %d\n",p->ID,p->name,p->age,p->sex,p->score);
		printf("%d %s %d %s %d\n",p->ID,p->name,p->age,p->sex,p->score);
		p=p->next;
	}
	fclose(ffp);
}
/*成型菜单 
按 1 增加记录
按 2 删除记录
按 3 列出结果
按 4 修改记录
按 0 退出程序  
*/ 
void menu();
int main(){
	int choice;
	menu();
	Stu S;
	S = CreateRecords();
	printf("Please input the number of your operation: ");
	scanf("%d",&choice);
	while(choice != 0){
		switch(choice){
		case 1:
			printf("You are adding a record.....\n");
			printf("Please input the infoemation of the student: ID,name,age,sex,score\n");
			int ID,age,score;
			char name[20],sex[20];
			scanf("%d%s%d%s%d",&ID,name,&age,sex,&score);
			ADDRecords(S,ID,name,age,sex,score);
			break;
		case 2:
			printf("You are deleting a record.....\n");
			printf("Please input the ID of student you want to delete: ID\n");
			int id;
			scanf("%d",&id);
			DeleteRecords(S,id);
			break;
		case 3:
			ListRecords(S);
			break;
		case 4:
			printf("You are modifying a record.....\n");
			printf("Please input ID,num which you want to modify as follows, value you want to modify: \n");
			printf("1.name\n");
			printf("2.age\n");
			printf("3.sex\n");
			printf("4.score\n");
			int num;
			//int id;
			scanf("%d%d",&id,&num);
			if(num==1 || num==3){
				char value[20];
				scanf("%s",value);
				ModifyRecords2(S,id,num,value);
			}
			else{
				int value;
				scanf("%d",&value);
				ModifyRecords1(S,id,num,value);
			}
			break;
		}
		printf("Please input the number of your operation: ");
		scanf("%d",&choice);
	}
	return 0;
}

void menu(){
	printf("+--------------------------------------------------------+\n");
	printf("|********************************************************|\n");
	printf("|*** Welcome to Student Information Mangement System ****|\n");
	printf("|********************************************************|\n");
	printf("|***              1. ADDRecords                      ****|\n");
	printf("|***              2. DeleteRecords                   ****|\n");
	printf("|***              3. ListRecords                     ****|\n");
	printf("|***              4. ModifyRecords                   ****|\n");
	printf("|***              0. exit                            ****|\n");
	printf("|********************************************************|\n");
	printf("|********************************************************|\n");
}


猜你喜欢

转载自blog.csdn.net/shengsikandan/article/details/79011687