?? studentsys.cpp
字號(hào):
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>
struct student {
int num;
char name[10];
float math, eng, comp, avg;
struct student *next;
}; // 學(xué)生節(jié)點(diǎn)定義
struct student *head;
const int PLEN = sizeof(struct student);
int n = 0;
void loadInfo(void)
{ fstream fi("student.dat", ios::in | ios::binary | ios::nocreate);
if(!fi) {
return;
cout << "文件 student.dat 打開(kāi)失敗 !" << endl;
exit(1);
}
fi.unsetf(ios::skipws);
head = NULL;
if( fi.eof() ) return;
struct student *p1, *p2;
n = 0;
int pcc = 0;
while( !fi.eof()) {
n ++;
p1 = new struct student;
fi.read((char *)p1, PLEN);
pcc = fi.gcount();
if(pcc < PLEN) {
delete p1;
break;
}
if(n == 1) head = p1;
else p2->next = p1;
p2 = p1;
}
p2->next = NULL;
fi.close();
}
void saveInfo(void)
{ if(head == NULL) return;
ofstream fo("student.dat", ios::out | ios::binary);
if(!fo) {
cout << "文件 student.dat 不能建立 !" << endl;
exit(1);
}
struct student *p;
p = head;
while( p != NULL ) {
fo.write((char *)p, PLEN);
p = p->next;
}
fo.close();
}
void createInfo(void)
{ struct student *p1, *p2;
cout << endl << "請(qǐng)依次輸入每個(gè)學(xué)生的學(xué)號(hào) 姓名 數(shù)學(xué) 英語(yǔ) 計(jì)算機(jī) ... " << endl;
p1 = p2 = new struct student;
cin >> p1->num >> p1->name >> p1->math >> p1->eng >> p1->comp;
p1->avg = ceil((p1->math + p1->eng + p1->comp) / 3);
head = NULL;
n = 0;
while(p1->num != 0) {
n ++;
if(n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = new struct student;
cin >> p1->num >> p1->name >> p1->math >> p1->eng >> p1->comp;
p1->avg = ceil((p1->math + p1->eng + p1->comp) / 3);
}
p2->next = NULL;
}
void printInfo()
{ struct student *p = head;
if(head != NULL) {
cout << endl << "全部學(xué)生數(shù)據(jù)如下:" << endl;
cout << endl << "學(xué)號(hào) 姓名 數(shù)學(xué) 英語(yǔ) 計(jì)算機(jī) 平均分" << endl;
cout << "--------------------------------------------" << endl;
do {
cout.setf(ios::left, ios::adjustfield);
cout << setw(8) << p->num << setw(8) << p->name;
cout.setf(ios::right, ios::adjustfield);
cout << setw(4) << p->math << setw(8) << p->eng << setw(8) << p->comp << setw(8) << p->avg << endl;
p = p->next;
} while(p != NULL);
}
else
cout << endl << "數(shù)據(jù)庫(kù)中沒(méi)有學(xué)生檔案 !請(qǐng)按菜單 7 新建學(xué)生檔案庫(kù) 。" << endl;
}
void deleteStudent()
{ struct student *p1, *p2;
int num;
if(head == NULL) {
cout << endl << "數(shù)據(jù)庫(kù)中沒(méi)有學(xué)生檔案 !" << endl;
return ;
}
cout << endl << "請(qǐng)輸入需要?jiǎng)h除的學(xué)號(hào):";
cin >> num;
p1 = head;
while(p1->num != num && p1->next != NULL){
p2 = p1;
p1 = p1->next;
}
if(p1->num == num) {
if(p1 == head) head = p1->next; // 刪除首結(jié)點(diǎn)
else p2->next = p1->next; // 刪除中間或尾結(jié)點(diǎn)
cout << endl << "學(xué)號(hào)為:" << num << " 的學(xué)生已被刪除 !" << endl;
delete p1;
}
else cout << endl << "找不到學(xué)號(hào)為:" << num << " 的學(xué)生 !" << endl;
}
void addStudent()
{ struct student *p0, *p1, *p2;
p1 = head; p0 = new struct student;
cout << endl << "請(qǐng)輸入新增加學(xué)生的學(xué)號(hào) 姓名 數(shù)學(xué) 英語(yǔ) 計(jì)算機(jī) ... " << endl;
cin >> p0->num >> p0->name >> p0->math >> p0->eng >> p0->comp;
p0->avg = ceil((p0->math + p0->eng + p0->comp) / 3);
if(head == NULL) {
head = p0;
p0->next = NULL;
}
while((p0->num > p1->num) && (p1->next != NULL)) {
p2 = p1;
p1 = p1->next;
}
if(p0->num <= p1->num) {
if(head == p1) head = p0; // 插在頭部 //
else p2->next = p0; // 插在 p2和p1之間 //
p0->next = p1;
} else { // 插到最后 //
p1->next = p0;
p0->next = NULL;
}
cout << endl << "數(shù)據(jù)已被追加到數(shù)據(jù)庫(kù) !" << endl;
}
void searchStudent()
{
struct student *p;
int num;
if(head == NULL) {
cout << endl << "數(shù)據(jù)庫(kù)中沒(méi)有學(xué)生檔案 !" << endl;
return ;
}
cout << endl << "請(qǐng)輸入需要查詢的學(xué)號(hào):";
cin >> num;
p = head;
while(p != NULL && p->num != num) p = p->next;
if(p != NULL) {
cout << endl << "學(xué)號(hào)為:" << num << " 學(xué)生資料:" << endl << endl;
cout << "學(xué)號(hào) 姓名 數(shù)學(xué) 英語(yǔ) 計(jì)算機(jī) 平均分" << endl;
cout << "--------------------------------------------" << endl;
cout.setf(ios::left, ios::adjustfield);
cout << setw(8) << p->num << setw(8) << p->name;
cout.setf(ios::right, ios::adjustfield);
cout << setw(4) << p->math << setw(8) << p->eng << setw(8) << p->comp << setw(8) << p->avg << endl;
}
else
cout << endl << "找不到學(xué)號(hào)為:" << num << " 的學(xué)生 !" << endl;
}
void scoreSort()
{
struct student *p = head;
float maxscore = 0;
if(head == NULL) {
cout << endl << "數(shù)據(jù)庫(kù)中沒(méi)有學(xué)生檔案 !請(qǐng)按菜單 7 新建學(xué)生檔案庫(kù) 。" << endl;
return;
}
cout << endl << "學(xué)生成績(jī)排行榜:" << endl;
cout << endl << "名次 學(xué)號(hào) 姓名 數(shù)學(xué) 英語(yǔ) 計(jì)算機(jī) 平均分" << endl;
cout << "--------------------------------------------------" << endl;
float sval[50], tv;
struct student *studp[50], *tp;
int ncc = 0;
do {
sval[ncc]= p->avg;
studp[ncc ++] = p;
p = p->next;
} while(p != NULL);
int i,j, k;
for (i = 0; i < ncc-1; i++){
k = i;
for(j = i+1; j < ncc; j++)
if(sval[j] > sval[k]) k = j;
if(k != i) {
tv = sval[i];
sval[i] = sval[k];
sval[k] = tv;
tp = studp[i];
studp[i] = studp[k];
studp[k] = tp;
}
}
for(i = 0; i < ncc; i ++) {
tp = studp[i];
cout.setf(ios::left, ios::adjustfield);
cout << " " << setw(5) << i+1 << setw(8) << tp->num << setw(8) << tp->name;
cout.setf(ios::right, ios::adjustfield);
cout << setw(4) << tp->math << setw(8) << tp->eng << setw(8) << tp->comp << setw(8) << tp->avg << endl;
}
}
void menu_display()
{
cout << endl;
cout << " 學(xué)生成績(jī)管理系統(tǒng) " << endl;
cout << " -------------------------------------" << endl;
cout << " 1. 顯示所有學(xué)生資料 " << endl;
cout << " 2. 按編號(hào)查詢學(xué)生 " << endl;
cout << " 3. 增加學(xué)生資料 " << endl;
cout << " 4. 刪除某個(gè)學(xué)生 " << endl;
cout << " 5. 裝入學(xué)生資料 " << endl;
cout << " 6. 保存學(xué)生資料 " << endl;
cout << " 7. 新建學(xué)生檔案 " << endl;
cout << " 8. 學(xué)生成績(jī)排行榜 " << endl;
cout << " 0. 退出系統(tǒng) " << endl;
cout << " -------------------------------------" << endl;
}
void anykey()
{
cout << endl << "按任意鍵繼續(xù) ......";
cin.get();
cin.get();
cout << endl;
}
void menu_select()
{ int c;
while(1) {
menu_display();
cout << "請(qǐng)選擇:";
cin >> c;
switch(c) {
case 1: printInfo();
anykey();
break;
case 2: searchStudent();
anykey();
break;
case 3: addStudent();
anykey();
break;
case 4: deleteStudent();
anykey();
break;
case 5: loadInfo();
anykey();
break;
case 6: saveInfo();
anykey();
break;
case 7: createInfo();
anykey();
break;
case 8: scoreSort();
anykey();
break;
case 0: cout << endl;
return;
}
}
}
void main()
{ head = NULL;
loadInfo();
menu_select();
saveInfo();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -