?? 0008-1.cpp
字號:
# include<iostream.h>
# include<string.h>
# include<stdio.h>
# include<stdlib.h>
# include<fstream.h>
const int s=2;
class student{
public:
char *name;
int id;
char sex;
float math, eng, comp, totll, aver;
void setup()
{
char n[20]; char s;
int b;
do { cout<<" 學號: ";
cin>>b;
if(b>1020||b<1001)
cout<<"Bad data input!!"<<endl<<endl;
}while (b<1001||b>1020);
id=b;
do{ name=new char[strlen(n)+1];
cout<<" 姓名: ";
cin>>n;
if( strlen(n)>6 || strlen(n)<4 )
cout<<"Bad data input!!"<<endl<<endl;
}while ( strlen(n)>6 && strlen(n)<4 );
strcpy(name,n);
cout<<" 性別(m/f):" ;
cin>>s;
while (s!='m' && s!='f')
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 性別(m/f):";
cin>>s;
}
sex=s;
float m, e, co;
cout<<" 數學: ";
cin>>m;
while (m<0 || m>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 數學: ";
cin>>m;
}
math=m;
cout<<" 英語: ";
cin>>e;
while (e<0 || e>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 英語: ";
cin>>e;
}
eng=e;
cout<<" 計算機: ";
cin>>co;
while (co<0 || co>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 計算機: ";
cin>>co;
}
comp=co;
totll=math+eng+comp;
aver=(math+eng+comp)/3;
}
void show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<id<<" ";
cout.width(8);
cout<<name;
cout.width(10);
cout<<sex;
cout.width(9);
cout<<math;
cout.width(9);
cout<<eng;
cout.width(11);
cout<<comp;
cout.width(10);
cout<<totll<<aver<<endl;
}
//---------------------------------------------------------------------------------------
void input()
{ int n;
cout<<endl<<"輸入將要錄入的學生數目: ";
cin>>n;
int j;
for(j=1; j<=n; j++)
{ cout<<" 輸入學生信息 "<<j<<endl;
ob[j].setup();
}
int i;
i=n;
system("cls");
cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;
cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;
for(i=1; i<=n; i++)
{
ob[i].show();
}
cout<<endl;
cout<<" 是否保存? (Y/N): ";
char Y;
cin>>Y;
}
//-------------------------------------------------------------------------------------
void Query()
{
int x , i;
cout<<endl<<" 輸入要查詢學生的學號: ";
cin>>x;
cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;
for(i=1;i<=s;i++)
{ if (x==ob[i].id)
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<ob[i].id<<" ";
cout.width(8);
cout<<ob[i].name;
cout.width(10);
cout<<ob[i].sex;
cout.width(9);
cout<<ob[i].math;
cout.width(9);
cout<<ob[i].eng;
cout.width(11);
cout<<ob[i].comp;
cout.width(10);
cout<<ob[i].totll<<ob[i].aver<<endl;
}
}
getchar();
}
//---------------------------------------------------------------------------------------
void save()
{ int i;
ofstream fout("list_1.tet");
if(!fout)
{
cout<<"Cannot open output file!\n,";
}
for(i=1; i<=s; i++)
{ fout<<ob[i].id<<" "<<ob[i].name<<" "<<ob[i].sex<<" "<<
ob[i].math<<" "<<ob[i].eng<<" "<<ob[i].comp<<" "<<ob[i].totll<<" "<<ob[i].aver<<endl;
}
fout.close();
}
void read()
{
int i;
system("cls");
cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;
cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;
for(i=1; i<=s; i++)
{
ob[i].show();
}
getchar();
}
//-------------------------------------------------------------------------------------
void add()
{
cout<<"輸入要添加學生的信息:"<<endl;
cout<<" 學號: ";
}
//--------------------------------------------------------------------------------------
void del()
{
}
//--------------------------------------------------------------------------------------
void order()
{
int k,j;
float t; char n[20];
for(j = 0; j<=(2-1); j++)
for(k=1; k<=(2-j); k++)
if(ob[k].totll < ob[k + 1].totll)
{
t = ob[k].totll;
ob[k].totll = ob[k+1].totll;
ob[k+1].totll = t;
strcpy(n, ob[k].name);
strcpy(ob[k].name, ob[k+1].name);
strcpy(ob[k+1].name, n);
}
cout<<" 成績排名:"<<endl;
cout<<" 姓名 總成績 名次"<<endl;
for(k=1; k<=s; k++)
{
cout<<" ";
cout.setf(ios::left);
cout.width(9);
cout<<ob[k].name;
cout.width(9);
cout<<ob[k].totll<<k<<endl;
}
getchar();
}
}ob[s];
//--------------------------------------------------------------------------------------
void menu()
{
cout<<"\n\n";
cout<<"------------------ 學生成績系統 -----------------"<<endl<<endl;
cout<<"\t\t1.錄入與保存學生信息.\n";
cout<<"\t\t2.讀取學生信息.\n";
cout<<"\t\t3.刪除學生信息.\n";
cout<<"\t\t4.追加學生信息.\n";
cout<<"\t\t5.查詢學生信息.\n";
cout<<"\t\t6.顯示成績名次.\n";
cout<<"\t\t7.退出系統......\n\n\n";
cout<<"\t\t請選擇功能項: ";
}
//---------------------------------------------------------------------------------------
void main()
{ student a;
while(1)
{
int SEL;
system("cls");
menu();
cin>>SEL;
switch(SEL)
{
case 1:
system("cls"); a.input(); a.save(); break;
case 2:
system("cls"); a.read(); break;
case 3:
system("cls"); break;
case 4:
system("cls"); break;
case 5:
system("cls"); a.Query(); break;
case 6:
system("cls"); a.order(); break;
case 7:
cout<<endl<<" 按任意鍵退出.... "<<endl;
getchar();
exit(0);
default:
cout<<"Bad input!!\n";
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -