?? school.cpp
字號:
#include "School.h"
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
void School::load_total(int *total)
{
ifstream datafile("total.txt", ios::nocreate); //如果是第一次進入該系統,則total.txt文件應該不存在
if (datafile.fail()) //創建文件total.txt,并寫入total的值
{
*total = 0;
}
else //文件total.txt已經存在,則讀取total的值
{
datafile >> *total;
datafile.close();
}
}
void School::save_total()
{
ofstream datafile("total.txt");
datafile << total;
datafile.close();
}
void School::save_info()
{
ofstream datafile("stu_info.txt");
CsStuInfo *p = head->next;
while (p != NULL)
{
p->foutput(datafile);
p = p->next;
}
datafile.close();
}
void School::load_info()
{
ifstream datafile("stu_info.txt");
CsStuInfo *p1 = head, *p2; //p1指向鏈表的最后一個結點
int i;
for (i=1; i<=total; i++)
{
p2 = new CsStuInfo;
p2->finput(p2, datafile);
p1->next = p2;
p2->next = NULL;
p1 = p2;
}
datafile.close();
}
School::School()
{
head = new CsStuInfo;
head->next = NULL;
load_total(&total);
load_info();
}
School::~School()
{
CsStuInfo *p;
while (head)
{
p = head;
head = head->next;
delete p;
}
}
void School::input()
{
CsStuInfo *p1, *p2 = head; //p2指向當前鏈表的最后一個結點
bool t = true, temp = true;
char ch;
while (p2->next != NULL)
{
p2 = p2->next;
}
while (t)
{
p1 = new CsStuInfo;
total++;
p1->input();
p2->next = p1;
p1->next = NULL;
p2 = p2->next;
while (temp)
{
cout << "\t\t要繼續錄入下一學生信息嗎?(Y/N):" << flush;
ch = getch();
ch = toupper(ch);
cout << ch << endl;
switch(ch)
{
case 'Y':
system("cls");
temp = false;
break;
case 'N':
t = false;
temp = false;
break;
default:
break;
}
}
}
cout << "\t\t已成功錄入!" << endl;
ofstream datafile("total.txt");
datafile << total;
datafile.close();
}
void School::output()
{
CsStuInfo *p = head->next;
if (p == NULL )
{
cout << "\n\n\t\t暫無任何學生信息!" << endl;
return ;
}
else
{
while (p != NULL)
{
p->output();
p = p->next;
cout << "\n\t\t按任意鍵顯示下一學生信息..." << flush;
getch();
system("cls");
}
}
cout << "\n\n\t\t顯示完畢!" << endl;
}
void School::search_stu()
{
char ch;
bool t = true;
while (t)
{
cout << "\n\n\t\t\t查詢\n\t\t1.按姓名查找\n\t\t2.按學號查找" << endl;
cout << "\t\t請選擇:" << flush;
ch = getch();
switch(ch)
{
case '1':
system("cls");
search_by_name();
t = false;
break;
case '2':
system("cls");
search_by_number();
t = false;
break;
default:
system("cls");
break;
}
}
}
void School::search_by_name()
{
char name[20];
CsStuInfo *p = head->next;
cout << "\n\t\t請輸入要查詢的學生姓名:" << flush;
cin >> name;
system("cls");
while( p != NULL )
{
if(strcmp(p->getname(), name) == 0)
{
p->output();
break;
}
p = p->next;
}
if (p == NULL)
{
cout << "\n\t\t不存在該學生!" << endl;//未找到進行操作
}
}
void School::search_by_number()
{
char number[20];
CsStuInfo *p = head->next;
cout << "\n\t\t請輸入要查詢的學生學號:" << flush;
cin >> number;
system("cls");
while( p != NULL )
{
if(strcmp(p->getnumber(), number) == 0)
{
p->output();
break;
}
p = p->next;
}
if (p == NULL)
{
cout << "\n\t\t不存在該學生!" << endl;//未找到進行操作
}
}
void School::delete_stu_info()
{
char ch;
bool t = true;
while (t)
{
cout << "\n\n\t\t\t刪除\n\t\t1.按姓名刪除\n\t\t2.按學號刪除" << endl;
cout << "\t\t請選擇:" << flush;
ch = getch();
switch(ch)
{
case '1':
system("cls");
delete_by_name();
t = false;
break;
case '2':
system("cls");
delete_by_number();
t = false;
break;
default:
system("cls");
break;
}
}
}
void School::delete_by_name()
{
char name[20];
CsStuInfo *p = head, *temp; //p指向要刪除結點的前一結點
char ch;
bool t = true;
cout << "\n\t\t請輸入要刪除的學生姓名:" << flush;
cin >> name;
system("cls");
while ((p->next != NULL) && (strcmp(p->next->getname(), name) != 0))
{
p = p->next;
}
temp = p->next;
if ( p->next != NULL )
{
p->next->output();
while (t)
{
cout << "\n\t\t確定刪除嗎?(Y/N)a" << flush;
ch = getch();
ch = toupper(ch);
switch(ch)
{
case 'Y':
p->next = temp->next;
delete temp;
total--;
system("cls");
cout << "\n\n\t\t刪除成功!" << endl;
t = false;
break;
case 'N':
t = false;
break;
default:
break;
}
}
}
else
{
cout << "\n\n\t\t該學生不存在!" << endl;
}
}
void School::delete_by_number()
{
char number[20];
CsStuInfo *p = head, *temp; //p指向要刪除結點的前一結點
char ch;
bool t = true;
cout << "\n\t\t請輸入要刪除的學生學號:" << flush;
cin >> number;
system("cls");
while ((p->next != NULL) && (strcmp(p->next->getnumber(), number) != 0))
{
p = p->next;
}
temp = p->next;
if ( p->next != NULL )
{
p->next->output();
while (t)
{
cout << "\n\t\t確定刪除嗎?(Y/N)" << flush;
ch = getch();
ch = toupper(ch);
switch(ch)
{
case 'Y':
p->next = temp->next;
delete temp;
total--;
system("cls");
cout << "\n\n\t\t刪除成功!" << endl;
t = false;
break;
case 'N':
t = false;
break;
default:
break;
}
}
}
else
{
cout << "\n\n\t\t該學生不存在!" << endl;
}
}
struct info
{
char name[20], number[20];
float gpa;
};
void qs(info stu[], int low, int high)
{
int i=low, j=high;
info temp = stu[low];
while (i<j)
{
while (i<j && stu[j].gpa>temp.gpa)j--;
if (i<j)
{
stu[i] = stu[j];
i++;
}
while (i<j && stu[i].gpa<temp.gpa)i++;
if (i<j)
{
stu[j] = stu[i];
j--;
}
}
stu[i] = temp;
if (i>low)qs(stu, low, i-1);
if (i<high)qs(stu, i+1, high);
}
void School::calc_stu_score()
{
info stu[50];
int i = 0;
CsStuInfo *p = head->next;
if (p == NULL)
{
cout << "\n\n\t\t暫無任何學生信息!" << endl;
return ;
}
else
{
while (p != NULL)
{
i++;
strcpy(stu[i].name, p->getname());
strcpy(stu[i].number, p->getnumber());
stu[i].gpa = p->getavegpa();
p = p->next;
}
}
qs(stu, 1, i);
int k;
cout << "\n\n\t\t\t 學生排名" << endl;
for (k=i; k>=1; k--)
{
cout << "\t\t+---+-----------+--------+-------+" << endl;
cout << "\t\t" << "| " << total+1-k << " "
<< "| " << stu[k].number << " "
<< "| " << setw(6) << stu[k].name << " "
<< "| " << setprecision(3) << setiosflags(ios::fixed) << stu[k].gpa << " |" << endl;
}
cout << "\t\t+---+-----------+--------+-------+" << endl << endl << '\t' << flush;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -