今天下午有個面試,但是不想複習,就將寫好的這個簡單學生關係系統發佈出來。
另:祝願自己面試順利,加油!
============================================================
本篇涉及到的知識點:
1、單鏈表的遍歷、插入、刪除
2、switch分支處理鍵盤輸入
3、類外實現、多文件編程
4、ofstream操作文件、寫文件
- ofstream打開文件
- 使用對象打開文件 outfile.open(“student.txt”, ios::out || ios::app);
- 默認方式打開文件 ofstream out(“student.txt”);
- 參數如下
- 關閉文件 outfile.close();
========================================================
說明:
1、Student類中有學號、姓名、分數三個公有成員,私有成員next用於組織鏈表
2、系統的功能:
- 錄入學生信息(學生信息以鏈表形式存儲)
- 修改學生信息
- 刪除學生信息(鏈表刪除節點)
- 查看學生信息
- 將全部的學生信息寫入student.txt文件(遍歷鏈表,寫入文件)
- 根據學生成績排名(對鏈表節點排序,已經更新實現)
- 單鏈表的排序一般針對帶頭節點的而言(交換節點、交換數據域)
- 本篇採用了一個交換節點的方法實現
- 單鏈表排序、逆置以後可能會在某一個小項目中具體再討論
3|、代碼中可能會缺少註釋,有時間再補上。
===========================================================
student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
class Student{
public:
/*******構造***********/
Student(char* num, char* name, float score);
Student();
char stu_num[15];
char stu_name[30];
float stu_score;
void afterInsert(Student* p); //後插
void afterDelete(); //後刪
Student* getNext();
/*******查詢學生信息**********/
void getMesg();
/*******修改學生信息**********/
void changeMesg(int n,char* ptr);
void changeGrade(float f);
private:
Student* next;
};
#endif
student.cpp(負責類外實現)
#define _CRT_SECURE_NO_WARNINGS
#include "student.h"
#include <cstring>
#include <iostream>
using namespace std;
Student::Student(){
strcpy(this->stu_num, "");
strcpy(this->stu_name, "");
this->stu_score = 0;
this->next = NULL;
}
Student::Student(char* num, char* name, float score){
strcpy(this->stu_num, num);
strcpy(this->stu_name, name);
this->stu_score = score;
this->next = NULL;
}
void Student::afterInsert(Student* p){ //後插
p->next = this->next;
this->next = p;
}
void Student::afterDelete(){ //後刪
Student* pTmp = this->next;
this->next = pTmp->next;
delete pTmp;
}
Student* Student::getNext(){
return this->next;
}
void Student::getMesg(){
cout << "學號:" << stu_num << " 姓名" << stu_name << " 成績" << stu_score<<endl;
}
void Student::changeMesg(int n, char* ptr){
switch (n)
{
case 1:
strcpy(stu_num, ptr);
break;
case 2:
strcpy(stu_name, ptr);
break;;
default:
break;
}
}
void Student::changeGrade(float f){
this->stu_score = f;
}
主函數main(主菜單、構建鏈表、查詢學生、修改信息、刪除學生)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include "student.h"
using namespace std;
//建立鏈表函數(創建學生)
void creatList(Student* stu){
Student* p = new Student;
char str[30];//學號或者姓名
float score;//分數
cout << "輸入學號:";
cin >> str;
p->changeMesg(1, str);
memset(str, 0, sizeof(str));//清空str
cout << "輸入姓名:";
cin >> str;
p->changeMesg(2, str);
memset(str, 0, sizeof(str));//清空str
cout << "輸入得分:";
cin >> score;
p->changeGrade(score);
system("cls");
cout << "信息輸入完成!" << endl;
while (stu->getNext()) //尋找尾節點
stu = stu->getNext();
stu->afterInsert(p);
//TODO...文件處理
}
//學生信息查詢
Student* findMesg(Student* stu){
loop:
cout << "1、按姓名查詢\t2、按學號查詢\tq、返回上一級菜單" << endl;
char p[5], ptr[30];
Student* pCur = stu;
cin >> p;
if (p[0] != '1' && p[0] != '2' && p[0] != 'q' || strlen(p) > 1){
cout << "您的輸入有誤,重新輸入" << endl;
goto loop;
}
switch (p[0]){
case '1':
cout << "輸入要查找的姓名:";
cin >> ptr;
for (; strcmp(ptr, pCur->stu_name) != 0;){
if (pCur->getNext() == NULL){
cout << "對不起,您要查找的人不存在!" << endl;
goto loop;
}
pCur = pCur->getNext();
}
return pCur;
case '2':
cout << "輸入要查找的學號:";
cin >> ptr;
for (; strcmp(ptr, pCur->stu_num) != 0;){
if (pCur->getNext() == NULL){
cout << "對不起,您要查找的學號不存在!" << endl;
goto loop;
}
pCur = pCur->getNext();
}
return pCur;
case 'q':
system("cls");
cout << "您的輸入有誤,請重新輸入!" << endl;
goto loop;
}
}
//刪除鏈表節點
void delList(Student* stu){
Student* p = NULL;
char selet[4];
//system("cls");
cout << "刪除前,會根據您的提示找到您要刪除的學生信息" << endl;
p = findMesg(stu);
if (p != NULL){
cout << "確認刪除嗎?(yes/任意鍵返回)" << endl;
cin >> selet;
if (strcmp(selet, "yes") == 0){
for (; stu->getNext() != p;){
stu = stu->getNext();
}
stu->afterDelete();
cout << "該學生信息刪除成功!" << endl;
}
}
}
//修改節點信息
void changeInfo(Student* stu){
system("cls");
cout << "修改前,會根據您提供的信息查找到您要修改的信息" << endl;
Student* p = NULL;
float score;
p = findMesg(stu);
if (p != NULL){
cout << "請輸入成績" << endl;
cin >> score;
p->changeGrade(score);
cout << "修改成功" << endl;
}
}
void putStu(Student* stu){
system("cls");
cout << "1、查看指定學生信息\t2、查看所有學生信息\t3、分段輸出學生信息" << endl;
char ch;
int n = 0;
stu = stu->getNext();
cin >> ch;
switch (ch){
case '1':
stu = findMesg(stu);
if (stu == NULL){
break;
}
stu->getMesg();
break;
case'2':
while (stu){
stu->getMesg();
stu = stu->getNext();
}
break;
case'3':
cout << "a-60分以下;b-60~70分之間;c-70~80分之間;d-80~90分之間;e-90~100分之間:" << endl;
cin >> ch;
switch (ch){
case'a':
while (stu){
if (stu->stu_score <= 60){
stu->getMesg();
++n;
}
stu = stu->getNext();
}
break;
case'b':
while (stu){
if (stu->stu_score > 60 && stu->stu_score <= 70){
stu->getMesg();
++n;
}
stu = stu->getNext();
}
break;
case'c':
while (stu){
if (stu->stu_score > 70 && stu->stu_score <= 80){
stu->getMesg();
++n;
}
stu = stu->getNext();
}
break;
case'd':
while (stu){
if (stu->stu_score > 80 && stu->stu_score <= 90){
stu->getMesg();
++n;
}
stu = stu->getNext();
}
break;
case'e':
while (stu){
if (stu->stu_score > 90 && stu->stu_score <= 100){
stu->getMesg();
++n;
}
stu = stu->getNext();
}
break;
}
if (n == 0){
cout << "沒有改分數段的學生" << endl;
}
}
}
//對學生成績排序(2018.11.15更新)
void insertSort(Student* stu){
Student* list = NULL;
if (stu == NULL)
return;
else{
list = (Student*)malloc(sizeof(Student));
strcpy(list->stu_name, "");
strcpy(list->stu_num, "");
list->stu_score = 0;
list->setNext(stu);
}
//保證有2個數據才排序
if (list->getNext() == NULL || (list->getNext()->getNext() == NULL))
return;
Student *head, *p1, *p2, *prep1, *prep2, *temp;
head = list;
prep1 = head->getNext();
p1 = prep1->getNext();
bool flag;
while (p1 != NULL){
flag = true;
temp = p1;
for (prep2 = head, p2 = prep2->getNext(); p2 != p1; prep2 = prep2->getNext(), p2 = p2->getNext()){
//從小打到排序
if (p2->stu_score > p1->stu_score){
//將p1節點拿出來
p1 = p1->getNext();
prep1->setNext(p1) ;
//p1節點插入到p2之前
prep2->setNext(temp);
temp->setNext(p2);
flag = false;//一個數據插入完成
break;
}
}
if (flag){ //處理後續數據
prep1 = prep1->getNext();
p1 = p1->getNext();
}
}
}
//主菜單
void menu(Student* stu){
char selet[10];
int n = 1;
ofstream outfile;
//ifstream infile;
Student *p, *ptr,*mid;
cout << "*************************歡迎進入學生信息管理系統*************************" << endl;
do {
cout << "**************************************************************************" << endl;
cout << "1.插入信息; 2.刪除信息; 3.修改信息; 4.查看信息; 5.保存 " << endl;
cout << "按'q'鍵退出 " << endl;
cout << "**************************************************************************" << endl;
cin >> selet;
if (((selet[0]<'1' || selet[0]>'6') && selet[0] != 'q') || strlen(selet) > 1){
//system("cls");
cout << "您的輸入有誤,請重新輸入!" << endl;
break;
}
switch (selet[0]){
case'1':
creatList(stu);
break;
case'2':
delList(stu);
break;
case'3':
changeInfo(stu);
break;
case'4':
putStu(stu);
break;
case'5':
outfile.open("student.txt", ios::out || ios::app);
for (p = stu->getNext(); p != NULL; p = p->getNext()){
outfile << p->stu_num << " ";
outfile << p->stu_name << " ";
outfile << p->stu_score << " ";
outfile << endl;
}
outfile.close();
system("cls");
cout << "保存成功!" << endl;
break;
case'q':
break;
}
} while (selet[0] != 'q');
}
int main(){
Student head;
menu(&head);
system("pause");
}