?? task_deeds.cpp
字號:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "student.h"
#include "deeds.h"
#include "filework.h"
void Delay(short i)
{
double x;
while (i)
{
i--;
for (int j=0;j<2000;j++)
x=log(exp(log(exp(log(exp(log(pow(1.2123,2.2324)+pow(1.3243,2.324))))))));
}
}
void PrintMenu()
{
cout<<"********************************************************************"<<endl
<<" 1. 從文件中讀入數據以初始化"<<endl
<<" 2. 輸入課程名稱或刪除課程"<<endl
<<" 3. 輸入或查詢某門課程的成績"<<endl
<<" 4. 增加或刪除學生記錄"<<endl
<<" 5. 按姓名或學號查找某個學生的成績"<<endl
<<" 6. 對學生成績按照姓名、學號或某門成績排序"<<endl
<<" 7. 計算某個學生的平均績點"<<endl
<<" 8. 統計某門課程成績在100-90,89-80,79-70,69-60,60以下各分數段的人數"<<endl
<<" 0. 退出或幫助"<<endl
<<"********************************************************************"
<<endl;
}
void EditCourses(student *&headOfStudent,courselist *&headOfCourse)
{
short i;
cout<<" 1. 輸入課程 2. 刪除課程 0. 返回上一級菜單\n"
<<"請選擇:";
cin>>i;
while (cin.fail() || (i<0 || i>2))
{
cout<<char(7);
cout<<"\n輸入錯誤!輸入 0-2 即可\n"
<<"請重新輸入:";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
switch (i)
{
case 1: InputCourses(headOfStudent,headOfCourse);break;
case 2: DeleteCourses(headOfStudent,headOfCourse);break;
case 0: return;
}
}
void InputCourses(student *&headOfStudent,courselist *&headOfCourse)
{
courselist *temp=NULL,*endv=headOfCourse;
if (endv)
{
cout<<"\n您已輸入的課程有:"<<endl;
cout<<setw(20)<<setiosflags(ios::left)<<endv->courseName<<' '<<endv->credits<<"學分"<<endl;
while (endv->next)
{
endv=endv->next;
cout<<setw(20)<<endv->courseName<<' '<<endv->credits<<"學分"<<endl;
}
}
char s[20];
cout<<"\n請輸入課程名稱和該課程的學分,輸入所有的課程后,再輸入一個 * "<<endl
<<"請輸入:";
bool flag=true;
while (flag)
{
cin>>s;
if (s[0]=='*') break;
temp=headOfCourse;
while (temp)
{
if (strcmp(temp->courseName,s)==0)
{
cout<<char(7);
cout<<"\n輸入的課程名重復!請重新輸入!"<<endl;
break;
}
temp=temp->next;
}
if (temp!=NULL)
continue;
temp=new courselist;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n申請內存空間失??!"
<<"課程"<<s<<"輸入失敗!"<<endl;
return;
}
strcpy(temp->courseName,s);
temp->next=NULL;
cin>>temp->credits;
while (cin.fail())
{
cout<<char(7);
cout<<"\n輸入錯誤! 課程學分是一個整型數!"<<endl
<<"請重新輸入";
if (endv)
cout<<' '<<endv->courseName<<" 后的課程: ";
else
cout<<": ";
cin.clear();
cin.ignore(100,'\n');
cin>>temp->courseName;
if (temp->courseName[0]=='*')
{
flag=false;
break;
}
cin>>temp->credits;
}
if (flag)
{
if (headOfCourse==NULL)
{
headOfCourse=temp;
endv=temp;
}
else
{
endv->next=temp;
endv=temp;
}
}
}
cout<<"\n您輸入的課程有:"<<endl;
temp=headOfCourse;
while (temp)
{
cout<<setw(20)<<setiosflags(ios::left)<<temp->courseName<<' '<<temp->credits<<"學分"<<endl;
temp=temp->next;
}
if (headOfStudent)
AddCourses(headOfStudent,headOfCourse);
}
void DeleteCourses(student *&headOfStudent,courselist *&headOfCourse)
{
courselist *temp=headOfCourse;
if (temp)
{
cout<<"\n已經輸入的課程有:"<<endl;
while (temp)
{
cout<<temp->courseName<<' ';
temp=temp->next;
}
cout<<endl;
}
else
{
cout<<char(7);
cout<<"\n還未輸入任何課程信息!不能進行刪除操作!"<<endl;
return;
}
cout<<"\n請輸入要刪除的課程名稱,退出請輸入 * \n"
<<"請輸入:";
char coursename[20];
short place=0;
while (cin>>coursename)
{
if (coursename[0]=='*')
return;
temp=headOfCourse;
place=0;
while (temp)
{
place++;
if (strcmp(temp->courseName,coursename)==0)
break;
temp=temp->next;
}
if (temp==NULL)
{
cout<<char(7);
cout<<"\n輸入課程名不存在!\n"
<<"\n已輸入的課程有:"<<endl;
temp=headOfCourse;
while (temp)
{
cout<<temp->courseName<<' ';
temp=temp->next;
}
cout<<endl;
cout<<"\n請重新輸入,退出請輸入 * "<<endl;
}
else
break;
}
if (temp==headOfCourse)
{
headOfCourse=headOfCourse->next;
}
else
{
courselist *p=headOfCourse;
while (p->next!=temp)
p=p->next;
p->next=temp->next;
delete temp;
}
if (headOfStudent)
DeleteCourseOfStudent(headOfStudent,place);
cout<<"\n "<<coursename<<" 的所有信息已刪除!"<<endl;
}
void DeleteCourseOfStudent(student *&headOfStudent,short place)
{
if (headOfStudent->pNameLc)
DeleteCourseOfStudent(headOfStudent->pNameLc,place);
short i=place;
courseOfStudent *temp=headOfStudent->courses,*p;
while (i>2)
{
temp=temp->next;
i--;
}
if (place==1)
{
p=headOfStudent->courses;
headOfStudent->courses=headOfStudent->courses->next;
delete p;
}
else
{
p=temp->next;
temp->next=temp->next->next;
delete p;
}
if (headOfStudent->pNameRc)
DeleteCourseOfStudent(headOfStudent->pNameRc,place);
}
void ScoresOfCourse(student *&headOfStudent,courselist *&headOfCourse)
{
cout<<" 1. 輸入某門課程的成績 \n"
<<" 2. 查詢某門課程的成績 \n"
<<" 0. 返回上一級菜單\n"
<<"請選擇:";
short i;
cin>>i;
while (cin.fail() || (i!=1 && i!=2 && i!=0))
{
cout<<char(7);
cout<<"\n輸入錯誤!輸入 0-2 即可!\n"
<<"請重新輸入:";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
switch (i)
{
case 1: InputScoresOfCourse(headOfStudent,headOfCourse);break;
case 2: SearchOfCourse(headOfStudent,headOfCourse);break;
case 0:return;
}
}
void SearchOfCourse(student *&headOfStudent,courselist *&headOfCourse)
{
char coursename[20];
courselist *temp=headOfCourse;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n未輸入任何課程信息!查詢失?。?quot;<<endl;
return;
}
else
{
cout<<"\n已輸入的課程有:"<<endl;
while (temp)
{
cout<<temp->courseName<<' ';
temp=temp->next;
}
cout<<endl;
cout<<"\n請輸入要查詢的課程名稱:";
cin>>coursename;
temp=headOfCourse;
short place=0;
while (temp)
{
if (strcmp(temp->courseName,coursename)==0)
break;
temp=temp->next;
place++;
}
if (temp==NULL)
{
cout<<char(7);
cout<<"\n輸入課程不存在!要添加課程信息請返回上一級菜單!"<<endl;
return;
}
bool flag=false;
if (headOfStudent)
{
if (headOfStudent->pNameLc)
Tree(headOfStudent->pNameLc,headOfStudent,temp->courseName,Sort_Score);
if (headOfStudent->pNameRc)
Tree(headOfStudent->pNameRc,headOfStudent,temp->courseName,Sort_Score);
PrintScoresOfCourse(headOfStudent,place,flag);
}
else
{
cout<<char(7);
cout<<"\n還未錄入任何學生信息!查詢失??!"<<endl;
return;
}
if (!flag)
{
cout<<char(7);
cout<<"\n該門課程還未錄入成績!"<<endl;
}
}
}
void PrintScoresOfCourse(student *&headOfStudent,short place,bool &flag)
{
if (headOfStudent->pScoreRc)
PrintScoresOfCourse(headOfStudent->pScoreRc,place,flag);
short i=place;
courseOfStudent *temp=headOfStudent->courses;
while (i)
{
temp=temp->next;
i--;
}
if (temp->score>=0)
{
if (!flag)
{
cout<<endl<<" <"<<temp->courseName<<"> "<<endl;
flag=true;
}
cout<<setw(10)<<setiosflags(ios::left)<<headOfStudent->studentName
<<setiosflags(ios::fixed)<<setprecision(2)<<temp->score<<endl;
}
if (headOfStudent->pScoreLc)
PrintScoresOfCourse(headOfStudent->pScoreLc,place,flag);
headOfStudent->pScoreLc=NULL;
headOfStudent->pScoreRc=NULL;
}
void InputScoresOfCourse(student *&headOfStudent,courselist *&headOfCourse)
{
courselist *temp=headOfCourse,*p;
short i;
char s[20];
bool flag;
if (temp)
{
cout<<"\n已經輸入的課程有:"<<endl;
while (temp)
{
cout<<temp->courseName<<' ';
temp=temp->next;
}
cout<<endl;
}
do
{
cout<<"\n請輸入您要輸入成績的課程名稱,如果課程輸入結束,請輸入一個 * "<<endl
<<"請輸入課程名稱:";
cin>>s;
if (s[0]=='*') break;
flag=false;
temp=headOfCourse;
while (temp)
{
if (strcmp(temp->courseName,s)==0)
{
flag=true;
break;
}
else
if (temp->next)
temp=temp->next;
else
break;
}
if (!flag)
{
cout<<"\n您輸入的課程名稱不在當前課程列表中,是否要加入課程列表?"<<endl
<<" 1. 是 2. 否 "<<endl
<<"請選擇:";
cin>>i;
while (cin.fail() || (i!=2 && i!=1))
{
cout<<char(7);
cout<<"\n輸入錯誤 !"<<endl
<<"輸入菜單中您要選擇的項目前的序號即可,即 1 或 2 "<<endl
<<"請選擇: ";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
switch (i)
{
case 1: p=new courselist;
if (p)
{
if (temp)
{
temp->next=p;
temp=p;
}
else
{
headOfCourse=p;
temp=p;
}
flag=true;
strcpy(temp->courseName,s);
p->next=NULL;
cout<<"請輸入"<<temp->courseName<<"的學分:";
cin>>temp->credits;
while (cin.fail())
{
cout<<char(7);
cout<<"\n輸入錯誤! 課程學分是一個整型數!"<<endl
<<"請重新輸入:";
cin.clear();
cin.ignore(100,'\n');
cin>>temp->credits;
}
}
else
{
cout<<char(7);
cout<<"\n申請內存空間失敗,不能再添加新的課程!"<<endl;
}
break;
case 2: cout<<char(7);
cout<<"\n輸入課程名稱不存在,輸入成績失??!"<<endl;
break;
}
}
if (flag)
{
if (headOfStudent==NULL)
{
cout<<char(7);
cout<<"\n還沒有輸入任何學生信息,請先添加學生記錄!"<<endl;
break;
}
InputScoresOfCourse_(headOfStudent,s);
}
}while (1);
}
void InputScoresOfCourse_(student *&head,char coursename[20])
{
if (head->pNameLc)
InputScoresOfCourse_(head->pNameLc,coursename);
cout<<"請輸入"<<head->studentName<<"的"<<coursename<<"的成績:";
courseOfStudent *temp=head->courses;
while (temp)
{
if (strcmp(temp->courseName,coursename)==0)
break;
if (temp->next)
temp=temp->next;
else
break;
}
if (temp)
{
if (strcmp(temp->courseName,coursename)!=0)
{
temp->next=new courseOfStudent;
if (temp->next==NULL)
{
cout<<char(7);
cout<<"\n申請內存空間失敗,輸入數據失??!"<<endl;
return;
}
temp=temp->next;
}
}
else
{
temp=new courseOfStudent;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n申請內存空間失敗,輸入數據失敗!"<<endl;
return;
}
}
strcpy(temp->courseName,coursename);
temp->next=NULL;
cin>>temp->score;
while (cin.fail())
{
cout<<char(7);
cout<<"\n輸入成績錯誤!"<<endl
<<"請輸入一個100以內的整型數或實型數!"<<endl;
cin.clear();
cin.ignore(100,'\n');
cin>>temp->score;
}
if (head->pNameRc)
InputScoresOfCourse_(head->pNameRc,coursename);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -