1. 程式人生 > >學生成績管理系統(順序表+連結串列)

學生成績管理系統(順序表+連結串列)

順序表

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;	//然後在加上這句 



void New_pro();		//新建順序表
void Log_data();	//錄入學生資料
void Inquire();		//查詢學生資訊
void Insert();		//插入學生資訊
void Delete();		//刪除學生資訊
void Num_of_peo();	//統計學生人數
void Show_all();	//顯示所有學生資訊
void Menu();		//顯示使用者介面 

int num=0;	//統計學生人數 
typedef struct Student{
	string no;
	string name;
	int price;
}Student;
Student student[100];

int main(){
	
	printf("歡迎進入學生資訊管理系統!\n\t\t\t\t作者:Pibuyi\n\n"); 
	int a;
	
	while(1){
		Menu();
		cout<<"請輸入數字選擇:";
		cin>>a;
		if(a==1){
			New_pro();
		}else if(a==2){
			Log_data();	
		}else if(a==3){
			Inquire();
		}else if(a==4){
			Insert();	
		}else if(a==5){
			Delete();	
		}else if(a==6){
			Num_of_peo();
		}else if(a==7){
			Show_all();	
		}else break;
	}
	 
	return 0;
}


void Menu(){
	printf("\t\t******************************\n");
	printf("\t\t*         1.新建順序表       *\n");
	printf("\t\t*         2.錄入學生資料     *\n");
	printf("\t\t*         3.查詢學生資訊     *\n");
	printf("\t\t*         4.插入學生資訊     *\n");
	printf("\t\t*         5.刪除學生資訊     *\n");
	printf("\t\t*         6.統計學生人數     *\n");
	printf("\t\t*         7.顯示所有學生資訊 *\n");
	printf("\t\t*         8.退出             *\n");
	printf("\t\t******************************\n\n");
	
}

void New_pro(){
	system("cls");
	Student student[100];
	num=0;
	printf("已重新新建順序表!\n\n");
}

void Log_data(){
	system("cls");
	int n;
	printf("\n請輸入學生人數(小於100):");
	cin>>n;
	num=n;
	for(int i=0;i<n;i++){	
		printf("\n請輸入第%d個學生的名字:",i+1);
		cin>>student[i].name;	
		printf("                 學號:",i+1);
		cin>>student[i].no;
		printf("                 成績:",i+1);
		cin>>student[i].price;
		printf("\n");
	}
	system("cls");
	printf("學生資訊錄入完畢!\n\n");
	
}
void Inquire(){
	system("cls");
	int n,flag=0;
	printf("\t\t1.姓名\n");
	printf("\t\t2.學號\n");
	printf("\t\t3.位置\n\n");
	printf("請選擇查詢方式:");
	scanf("%d",&n);
	
	if(n==1){
		string st;
		system("cls");
		printf("請輸入學生姓名:");
		cin>>st;
		system("cls");
		for(int i=0;i<100;i++){
			if(st==student[i].name){
				flag=1;
				cout<<endl<<st<<"的相關資訊如下:\n";
				
				cout<<"學號:"<<student[i].no<<endl;
				cout<<"成績:"<<student[i].price<<endl<<endl;
			break;
			}
		}
		if(flag==0)
			cout<<"沒有找到該學生資訊!"<<endl;				
	}
	else if(n==2){
		string st;
		printf("請輸入學生學號:");		
		cin>>st;
		system("cls");
		for(int i=0;i<100;i++){
			if(st==student[i].no){
				flag=1;
				cout<<endl<<"學號為"<<st<<"學生的相關資訊如下:\n";
				cout<<"名字:"<<student[i].name<<endl;
				cout<<"成績:"<<student[i].price<<endl<<endl;
				break;
			}
		}
		if(flag==0)
			cout<<"沒有找到該學生資訊!"<<endl;
	}
	else if(n==3){		
		system("cls");
		int i;
		cout<<"請輸入學生的位置:";
		cin>>i;
		if(i<=num){
			printf("已找到該位置學生資訊如下:\n\n");
			cout<<"\t\t\t名字:"<<student[i-1].name<<endl;
			cout<<"\t\t\t學號"<<student[i-1].no<<endl;
			cout<<"\t\t\t成績:"<<student[i-1].price<<endl<<endl;
		}
		else				
			cout<<"沒有找到該學生資訊!"<<endl;			
	}
	else printf("成功退出!\n");
}

void Insert(){
	int c;
	system("cls") ;
	printf("\n請輸入插入位置:");
	cin>>c;
	for(int i=num;i>=c;i--) {
		student[i].name = student[i-1].name ;
		student[i].no = student[i-1].no ;
		student[i].price = student[i-1].price ;
	}
	printf("姓名:"); 
	cin>>student[c-1].name ;
	printf("學號"); 
	cin>>student[c-1].no ;
	printf("成績:");
	cin>>student[c-1].price ;
	system("cls");
	printf("插入成功!\n\n"); 
	num++;
}

void Delete(){
	system("cls");
	int a;
	printf("請輸入刪除學生的位置:"); 
	cin>>a;
	for(int i=a-1;i<num-1;i++){
		student[i].name = student[i+1].name ;
		student[i].no = student[i+1].no ;
		student[i].price = student[i+1].price ;
	}
	num--;
}

void Num_of_peo(){
	system("cls");
	cout<<endl<<"學生總人數為:"<<num<<endl<<endl;
}

void Show_all(){
	system("cls");
	
	if(num>=0) {
		printf("共有%d位學生\n\n",num);
		for(int i=0;i<num;i++){
			cout<<"姓名:"<<student[i].name<<endl ;
			cout<<"學號:"<<student[i].no<<endl ;
			cout<<"成績:"<<student[i].price <<endl<<endl; 
		}
		printf("全部學生資訊顯示完畢!\n\n");
	}
	else printf("請先錄入學生資訊!\n\n");
}

連結串列

#include<iostream>	
#include<cstdio>
#include<string>
#include<windows.h>
using namespace std;

void Menu();		//顯示使用者介面 
 
typedef struct Student{
	string no;
	string name;
	int score;
	struct Student* next;	//指標域 
}Student;

int n;
Student* New_pro() {
	system("cls");
	struct Student *head,*p1,*p2;
	n=0;
	p1=p2=new Student;	
	printf("已重新新建連結串列!\n\n請輸入學生資訊.\n");
	cout<<"輸入學生的學號為0時,停止錄入.\n"<<endl;
	cout<<"請輸入學生學號:" ; 
	cin>>p1->no;
	if(p1->no != "0"){
		printf("請輸入學生姓名:");
		cin>>p1->name;
		printf("請輸入學生成績:");
		cin>>p1->score ;
		cout<<endl;
	}
	while(p1->no != "0"){
		n++;
		if(n==1)
			head=p1;
		else
			p2->next = p1;
		p2 =p1;
		p1 =new Student;
		cout<<"請輸入學生學號:" ; 
		cin>>p1->no;
		if(p1->no != "0"){
			printf("請輸入學生姓名:");
			cin>>p1->name;
			printf("請輸入學生成績:");
			cin>>p1->score ;
			cout<<endl;
		}
	}
	if(p1->no=="0") {
		cout<<"您已退出錄入模式!"<<endl;
		system("pause");
		system("cls");
	}
	p2->next =NULL;
	return head;
}

Student* Delete(Student *head,string no){
	Student *p,*q;
	if(head==NULL){
		printf("\nlist null!\n");
        return head;
	}
	p=head;
	while(no!=p->no&&(p->next !=NULL)){
		q=p ;
		p =p->next;
	}
	if(no==p->no){
		printf("\n\n你刪除的學生資訊為:\n");
		cout<<"\t\t學號:"<<p->no <<endl;
		cout<<"\t\t姓名:"<<p->name <<endl;
		cout<<"\t\t成績:"<<p->score <<endl<<endl;		
		if(no==head->no ){
			p=head;
			head=head->next ;
			delete p;
		}
		else{
			q->next=p->next ;
			delete p;
		}
		n=n-1;
		cout<<"\n\n已刪除學生資訊!\n" <<endl; 
		system("pause");
		system("cls");		
	}
	else
		cout<<"輸入有誤"<<endl;
	
	return head; 
}

Student* Insert(Student* head,Student *stud){
	Student *p0,*p1,*p2;
	p1=head;
	p0=stud;
	if(head==NULL){
		head=p0;
		p0->next =NULL;
	}
	else{
		while((p0->no > p1->no)&&(p1->next != NULL)){	//尋找要插入的結點的前一個 
			p2=p1;
			p1=p1->next ;
		}
		
		if(p0->no <= p1->no){
			if(head==p1)
				head=p0;
			else
				p2->next = p0;
				p0->next=p1;
		}
		else{
			p1->next =p0;
		p0->next =NULL;
		}
	}
	n++;
	cout<<"\n\n已插入學生資訊!\n" <<endl; 
	system("pause");
	system("cls");
	return head;
}

void Inquire(Student *head){
	int flag=0;
	Student *p;
	int qw;
	string Name,No;	
	printf("1.姓名.\n2.學號.\n請選擇查詢方式:");
	cin>>qw;
	if(head==NULL){
		printf("\n\nlist null!\n");
	}
	p=head;
	if(qw==1){
		printf("\n\n請輸入要查詢的姓名:");cin>>Name; 
		while(p){	
			if(p->name ==Name){			
				cout<<"\n\n已查詢到該學生!\n資訊如下:\n"<<endl<<"學號:"<<p->no <<endl;
				cout<<"姓名:"<<p->name <<endl;
				cout<<"成績:"<<p->score <<endl;
				flag=1;
				break;
			}
			p=p->next ;
		} 		
	}
	else if(qw==2){
		printf("請輸入要查詢的學號:");cin>>No; 
		while(p){	
			if(p->no ==No){		
				cout<<"\n\n已查詢到該學生!\n資訊如下:\n"<<endl<<"學號:"<<p->no <<endl;	
				cout<<"學號:"<<p->no <<endl;
				cout<<"姓名:"<<p->name <<endl;
				cout<<"成績:"<<p->score <<endl;
				flag=1;
				break;
			}
			p=p->next ;
		}
	}
	if(flag==0){
		cout<<"沒有扎到該學生資訊!\n\n";
	}
	system("pause") ;
	system("cls");
}

int Num_of_peo(Student *head){
	Student* p=head;
	int i=0;
	while(p){
		p->next ;
		i++;
		p=p->next ;
	}
	return i;
}

void printMark() {
    string Mark = "歡迎進入學生成績管理系統";
    string Madeby = "作者:Pibuyi";
	cout<<"\n\n"<<Mark<<","<<Madeby<<endl<<endl<<endl; 
}

void Show_all(Student *head){
	Student* p;
	if(head==NULL){
		printf("\nlist null!\n");
	}
	else{		
		p=head;
		while(p){
			cout<<"姓名:"<<p->name<<endl ; 
			cout<<"學號:"<<p->no<<endl ; 
			cout<<"成績:"<<p->score<<endl<<endl ; 
			p=p->next ;
		}
	}
	cout<<"全部學生資訊已顯示完畢!"<<endl;
	system("pause") ;
	system("cls");
}

void Menu(){
	printf("\t\t******************************\n");
	printf("\t\t*         1.新建連結串列         *\n");
	printf("\t\t*         2.插入學生資訊     *\n");
	printf("\t\t*         3.刪除學生資訊     *\n");
	printf("\t\t*         4.查詢學生資訊     *\n");
	printf("\t\t*         5.統計學生人數     *\n");
	printf("\t\t*         6.顯示所有學生資訊 *\n");
	printf("\t\t*         7.退出             *\n");
	printf("\t\t******************************\n\n");
	
}

int main(){
	int choose,n;
	string num;
	Student *head,*p;
	choose=-1;
	printMark();
	Sleep(1000);
	while(1){
		Menu();
		cout<<"請輸入數字選擇:";
		cin>>choose;
		if(choose==1){
			head=New_pro();		//新建順序表
		}else if(choose==2){
			system("cls");
			cout<<"請輸入要學生的相關資訊,輸入0將結束\n\n" ;
			p=new Student;
			printf("學號:");
			cin>>p->no ;
			if(p->no !="0"){
				printf("姓名:");cin>>p->name;
				printf("成績:");cin>>p->score;
				head=Insert(head,p);		//插入學生資訊
			}
		}else if(choose==3){
			system("cls");			
			p=new Student;
			printf("\n\n請輸入要刪除學生學號:");
			cin>>num ;
			head=Delete(head,num);		//刪除學生資訊
		}else if(choose==4){
			system("cls");
			Inquire(head);		//查詢學生資訊
		}else if(choose==5){
			cout<<"當前共有: "<<Num_of_peo(head)<<"個學生資訊已錄入系統!"<<endl;	//統計學生人數
			system("pause");
			system("cls");
		}else if(choose==6){
			system("cls");
			Show_all(head);		//顯示所有學生資訊
		}
		else break;
	}
	return 0;
}

無檔案操作,實力有限0.0