當前位置:編程學習大全網 - 編程語言 - 高手們,C語言編學生檔案管理,幫忙下

高手們,C語言編學生檔案管理,幫忙下

# include<iostream.h>

# include<string.h>

# include<stdio.h>

# include<stdlib.h>

# include<fstream.h>

//*****定義壹個學生原子的的數據結構*****//

struct stuatom

{

char *name;

int id;

char sex;

float math, eng, comp, totll, aver;

void show();

void setup();

};

//*********定義壹系列對學生的操作**********//

class student

{

private:

stuatom ob[100];

int stulen;

public:

student();

void input();

void order();

void save();

void Query();

void read();

void add();

void del();

};

//********對學生數據的初始化(類的構造函數)**********//

student::student()

{

//用for循環對全部數組中的數據初始化

for(int i=0;i<100;i++)

{

ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;

ob[i].id =0;

ob[i].sex =' ';

ob[i].name =NULL;

}

this->stulen =0;

}

//********輸入學生的數據,並判斷是否在規定數據域內*******//

void stuatom::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 stuatom::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 student::input()

{

int n;

cout<<endl<<"輸入將要錄入的學生數目: ";

cin>>n;

int j;

//通過循環輸入要求輸入學生個數的學生的數據。

for(j=0; j<n; j++)

{

cout<<" 輸入學生信息 "<<j<<endl;

ob[j].setup();

}

this->stulen=n; //學生個數賦值

//學生數據顯示格式

system("cls");

cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;

cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;

//通過循環輸出所有學生數據。

for(j=0; j<n; j++)

{

ob[j].show();

}

cout<<endl;

cout<<" 是否保存? (Y/N): ";

char Y;

cin>>Y;

system("cls");

}

//**************按照壹定格式顯示所要查詢學生的信息。**************//

void student::Query()

{

int x , i;

cout<<endl<<" 輸入要查詢學生的學號: ";

cin>>x;

cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;

for(i=0;i<=this->stulen ;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 student::save()

{

int i;

ofstream outfile;

outfile.open("list.txt",ios::trunc);

if(!outfile)

{

cout<<"Cannot open output file!\n,";

}

//通過循環把所有的學生數據保存在list.txt裏邊。

for(i=0; i<this->stulen; i++)

{

outfile<<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;

}

outfile.close();

}

//*************顯示所有學生數據*********************//

void student::read()

{

int i;

system("cls");

cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;

cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;

for(i=0; i<this->stulen; i++)

{

ob[i].show();

}

getchar();

}

//*******************壹個學生的數據****************//

void student::add()

{

int i, d=this->stulen ;

cout<<"輸入要添加學生的信息:"<<endl;

ob[d].setup();

cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;

cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;

for(i=0; i<=d; i++)

{

ob[i].show();

}

ofstream fout("list.txt",ios::app);

if(!fout)

{

cout<<"Cannot open output file!\n,";

}

//把添加的學生數據添加到list.txt裏邊去。

fout<<ob[d].id<<" "<<ob[d].name<<" "<<ob[d].sex<<" "<<

ob[d].math<<" "<<ob[d].eng<<" "<<ob[d].comp<<" "<<ob[d].totll<<" "<<ob[d].aver<<endl;

fout.close();

getchar();

}

//*********************刪除指定名字學生的數據*******************//

void student::del()

{

int i,p; char x[8];

cout<<" 輸入要刪除學生名字:"<<endl;

cin>>x;

//通過for循環查找要刪除學生的姓名

for(i=0; i<stulen; i++)

{

if(strcmp(ob[i].name,x)==0)

{

p=i;

cout<<endl<<"學號 姓名 性別 數學 英語 計算機 總成績 平均成績"<<endl;

cout<<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;

break;

}

else

continue;

}

cout<<endl<<"----------------------------- 學生信息表 ------------------------------------"<<endl;

cout<<endl<<" 學號 姓名 性別 數學 英語 計算機 總分 平均分"<<endl;

//刪除了之後,應該把後面的數據往前移,把要刪除的數據覆蓋,並且學生長度減1

stulen--;

for(i;i<stulen;i++)

{

ob[i]=ob[i+1];

}

this->read ();

cout<<" 刪除成功!"<<endl;

getchar();

}

//***********把學生成績排序******************//

void student::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=0; k<=stulen; k++)

{

cout<<" ";

cout.setf(ios::left);

cout.width(9);

cout<<ob[k].name;

cout.width(9);

cout<<ob[k].totll<<k<<endl;

}

getchar();

}

}

}

//********************選擇菜單。*****************//

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"); a.del(); break;

case 4:

system("cls"); a.add();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;

}

}

}

  • 上一篇:長沙哪裏有cad培訓班
  • 下一篇:XO公鏈引起全球熱議,或將引領第三代區塊鏈
  • copyright 2024編程學習大全網