乱写~~乱写,都不知道自己在写什么~标准做法是用链表,可惜我不懂~呵呵,下面只是一种是实现的方法,具体的操作可由用户自己更改~
#include<iostream>
#include<string>
#include<vector>
#include<iterator>
#include<iomanip>
#include<algorithm>
#include<windows.h>
using namespace std;
const int MAX=1010;
#define s setw(10)
#define flag setiosflags(ios::fixed)//左对齐
#define Left setiosflags(ios::left)
struct Student{
string name,ID; //姓名和学号
int Chinese; //语文
int math; //数学
int English; //英语
}Stu;
vector<Student> V;
istream& operator>>(istream &cin,Student &S)
{ cin>>S.name>>S.ID>>S.Chinese>>S.math>>S.English;
return cin;
}
ostream& operator<<(ostream &cout,const Student &S)//用copy的时候这个里面必须const??
{ cout<<s<<Left<<S.name<<s<<flag<<S.ID<<s<<flag<<S.Chinese<<s<<flag<<S.math<<s<<flag<<S.English;
return cout;
}
void Delete(string Name) //删除学生信息
{ for(vector<Student>::size_type i=0;i<V.size();i++)
if(V[i].name==Name) V.erase(V.begin()+i);
}
void Insert(const Student& S)
{ vector<Student>::size_type i=0;
for(;i<V.size();i++)
if(V[i].ID>S.ID)
{ V.insert(V.begin()+i,Stu);
break;
}
if(i==V.size()) V.push_back(Stu);
}
int Find(string Name) //返回该学生所在的位置
{ for(vector<Student>::size_type i=0;i<V.size();i++)
if(V[i].name==Name) return i;
}
bool mysort(const Student &S1,const Student &S2)
{ return S1.ID<S2.ID;
}
bool mysort2(const Student &S1,const Student &S2)//按照数学成绩从小到打排序
{ return S1.math<S2.math;
}
void Output()
{ cout<<"*****************学生信息*****************"<<endl;
cout<<"姓名 "<<"学号 "<<"语文 "<<"数学 "<<"英语"<<endl;
copy(V.begin(),V.end(),ostream_iterator<Student>(cout,"\n"));
cout<<"******************************************"<<endl;
}
int main()
{ system("title,学生管理系统");
cout<<"输入要插入的学生的信息: "<<endl;
while(cin>>Stu&&Stu.name!="End")
Insert(Stu);
Output();
string Name,Id;
cout<<"所要删除学生信息的名字: ";
cin>>Name;
Delete(Name);
Output();
cout<<"输入所要查询的学生的名字: ";
cin>>Name;
cout<<"姓名 "<<"学号 "<<"语文 "<<"数学 "<<"英语"<<endl;
cout<<V[Find(Name)]<<endl;
int low,high;
cin>>low>>high;
sort(V.begin(),V.end(),mysort2);//按数学成绩的降序排列,自己可以取定义其它的科目~~~~
cout<<"**********输出范围内学生信息: **********"<<endl;
for(vector<Student>::size_type i=0;i<V.size();i++)
if(V[i].math>=low&&V[i].math<=high) cout<<V[i]<<endl;
return 0;
}
完全没有入门吧~~自我感觉~下面是另一个版本,用链表做的,但是要求可能不同,转自:
空云万里晴的文章,唉,这就是差距啊~~加油!!稍作修改,因为我用的是DEV,
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define NULL __null
typedef struct Stud_Node
{
int num;
char name[20];
int score;
struct Stud_Node *next;
}StudNode;
StudNode *Create_Stu();
StudNode *Insert(StudNode *head , StudNode *stud);
StudNode *Modify(StudNode *head,int num,int score);
StudNode *Seach(StudNode *head,int num);
StudNode *Delete(StudNode *head,int num);
void Print_Stu(StudNode *head);
int main (void)
{
StudNode *head,*p;
int num,score,key;
char name[20];
int size=sizeof(StudNode*);
head=NULL;
do
{
system("cls");
printf("************************************************\n");
printf("* please select key: *\n");
printf("* 1 Create *\n");
printf("* 2 Insert *\n");
printf("* 3 Modify *\n");
printf("* 4 Seach *\n");
printf("* 5 Dlete *\n");
printf("* 6 Print *\n");
printf("* 0 Exit *\n");
printf("************************************************\n");
scanf("%d",&key);
switch(key)
{
case 1:
system("cls");
head=Create_Stu();
break;
case 2:
system("cls");
printf("please input number name and score:\n");
scanf("%d%s%d",&num,name,&score);
p=(StudNode *)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
head=Insert(head,p);
break;
case 3:
system("cls");
printf("Input The Number:\n");
scanf("%d",&num);
printf("Input xiugai score:\n");
scanf("%d",&score);
head=Modify(head,num,score);
break;
case 4:
system("cls");
printf("Input The Number:\n");
scanf("%d",&num);
head=Seach(head,num);
break;
case 5:
system("cls");
printf("Input The Number:\n");
scanf("%d",&num);
head=Delete(head,num);
break;
case 6:
system("cls");
Print_Stu(head);
break;
case 0:
break;
defaut:
break;
}
}
while(key!=0);
printf("Thank you !\n");
getchar();
return 0;
}
/*新建链表*/
StudNode *Create_Stu()
{
StudNode *head,*p;
int num,score;
char name[20];
int size=sizeof(StudNode);
head=NULL;
printf("Input number:\n");
scanf("%d",&num);
while(num!=0)
{
printf("Input name:\n");
scanf("%s",name);
printf("Input score:\n");
scanf("%d",&score);
p=(StudNode *)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
head=Insert(head,p);
printf("Input number:\n");
scanf("%d",&num);
}
return head;
}
/*插入操作*/
StudNode *Insert(StudNode *head , StudNode *stud)
{
StudNode *ptr,*ptr1,*ptr2;
ptr2=head;
ptr=stud;
if(head==NULL) /*原链表为空时插入*/
{
head=ptr;
}
else /*原链表不为空时插入*/
{
while((ptr->num>ptr2->num)&&(ptr2->num))
{
ptr1=ptr2; /*ptr1,ptr2各后移一个结点*/
ptr2=ptr2->next;
}
if(ptr->num<=ptr2->num) /*在ptr1与ptr2之间插入*/
{
if(head==ptr2)
head=ptr;
else
ptr1->next=ptr;
ptr->next=ptr2;
}
else /*新结点成为末结点*/
{
ptr2->next=ptr;
}
}
return head;
}
/*修改操作*/
StudNode *Modify(StudNode *head,int num,int score)
{
StudNode *q;
if(head==NULL) /*链表为空时*/
return NULL;
q=head;
while(q!=NULL)
{
if(q->num==num)
{
q->score=score;
break;
}
q=q->next;
}
return head;
}
/*查找操作*/
StudNode *Seach(StudNode *head,int num)
{
StudNode *q;
if(head==NULL) /*链表为空时*/
return NULL;
q=head;
while(q!=NULL)
{
if(q->num==num)
{
printf("Number %d\n",q->num);
printf("Name %s\n",q->name);
printf("Score %d\n",q->score);
getchar();
break;
}
q=q->next;
}
return head;
}
/*删除操作*/
StudNode *Delete(StudNode *head,int num)
{
StudNode *p,*q;
while(head!=NULL&&head->num==num) /*删除结点为表头*/
{
q=head;
head=head->next;
free(q);
}
if(head==NULL) /*链表为空*/
return NULL;
p=head; /*删除结点为非表头*/
q=head->next;
while(q!=NULL)
{ /*q所指的结点符合要求*/
if(q->num==num)
{
p->next=q->next;
free(q);
}
else
p=q; /*p后移一个结点*/
q=p->next; /*q指向p的下一个结点*/
}
return head;
}
/*遍历操作*/
void Print_Stu(StudNode *head)
{
StudNode *p;
if(head==NULL)
{
printf("\n No RecordS !!\n");
printf("\n Press any key to continue...\n");
getchar();
return;
}
printf("The student's records:\n");
for(p=head;p;p=p->next)
{
printf("Number:%d\n",p->num);
printf("Name:%s\n",p->name);
printf("Score:%d\n",p->score);
printf("\n");
}
printf("\n Press any key to continue...\n");
getchar();
}
2、球员管理系统:
定义球员类,其中至少包括编号,姓名,球员类型(3类:足球,篮球,排球),身高,体重,国籍和状态(退役,现役)
1、设计菜单实现功能选择,
2、添加功能:输入球员信息,并保存到文件;
3、删除功能:输入球员编号实现删除并保存到文件中;
4、修改功能:输入球员编号及其它相关信息,实现修改并保存到文件中;
5、查询功能:
a)、能够根据球员的编号精确查询球员的信息。
b)、能够根据所在国籍查询所有该国球员的信息。
6、统计功能:根据球员类型分析统计其数量,并输出结果。
1、设计菜单实现功能选择,
2、添加功能:输入球员信息,并保存到文件;
3、删除功能:输入球员编号实现删除并保存到文件中;
4、修改功能:输入球员编号及其它相关信息,实现修改并保存到文件中;
5、查询功能:
a)、能够根据球员的编号精确查询球员的信息。
b)、能够根据所在国籍查询所有该国球员的信息。
6、统计功能:根据球员类型分析统计其数量,并输出结果。
具体实现:
#include<stdio.h>
#include<string.h>
#include<windows.h>
const int MAX=1001;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
FILE *fp;
int sum;
char Type[3][20]={"篮球","足球","排球"};
struct Player{
int num; //编号
char name[10]; //姓名
char type[5]; //球员类型:足球,篮球,排球
int height; //身高
int weight; //体重
char nation[10]; //国籍
char state[5]; //状态:退役,现役
}P[MAX];
void Save(Player Py)
{ fprintf(fp,"%-10d %-10s %-10s %-10d %-10d %-20s %-10s\n",Py.num,Py.name,Py.type,Py.height,Py.weight,Py.nation,Py.state);
}
void Cout(Player Py)
{ printf("%-10d %-10s %-10s %-10d %-10d %-10s %-10s\n",Py.num,Py.name,Py.type,Py.height,Py.weight,Py.nation,Py.state);
}
void Output()
{ int i;
for(i=0;i<sum;i++) Cout(P[i]);
}
void Add()
{ if((fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\Play.txt","w"))==NULL)
{ printf("不能打开文件!!");
exit(1);
}
printf("请输入要添加球员的信息:\n");
scanf("%d%s%s%d%d%s%s",&P[sum].num,P[sum].name,P[sum].type,&P[sum].height,&P[sum].weight,P[sum].nation,P[sum].state);
while(P[sum].num!=-1)
{ Save(P[sum]);
sum++;
scanf("%d%s%s%d%d%s%s",&P[sum].num,P[sum].name,P[sum].type,&P[sum].height,&P[sum].weight,P[sum].nation,P[sum].state);
}
fclose(fp);
}
int Delete(int number)
{ int i,pos;
for(i=0;i<sum;i++)
if(P[i].num==number)
{ pos=i;
break;
}
if(i==sum) return 0;
else
{ for(i=pos;i<sum-1;i++)
P[i]=P[i+1];
sum--;
}
for(i=0;i<sum;i++) Save(P[i]);
fclose(fp);
return 1;
}
int Modify(Player Py)
{ int i;
for(i=0;i<sum;i++)
{ if(P[i].num==Py.num) P[i]=Py;
Save(P[i]);
}
if(i==sum) return 0;
fclose(fp);
}
void Find1(int number)
{ int i,pos;
for(i=0;i<sum;i++)
if(P[i].num==number)
{ pos=i;
break;
}
if(i==sum) printf("不存在这个编号的球员信息!!!\n");
else Cout(P[pos]);
}
void Find2(char *Nation)
{ int i;
printf("\n%s国籍的所有球员信息为:\n",Nation);
for(i=0;i<sum;i++)
if(strcmp(P[i].nation,Nation)==0) Cout(P[i]);
}
void Count()
{ int i,j,total[3];
CLR(total,0);
for(i=0;i<sum;i++)
for(j=0;j<3;j++)
if(strcmp(P[i].type,Type[j])==0) total[j]++;
for(i=0;i<3;i++)
printf("球员类型为%s的人数共有:%d人\n",Type[i],total[i]);
}
int main()
{ system("title 球员管理系统");
system("color 2e");
printf(" ________________________________________ \n");
printf("║ ║\n");
printf("║ 球员管理系统功能选择: ║\n");
printf("║ 1 添加功能 ║\n");
printf("║ 2 删除功能 ║\n");
printf("║ 3 修改功能 ║\n");
printf("║ 4 查询功能 ║\n");
printf("║ 5 统计功能 ║\n");
printf("║ 6 退出系统 ║\n");
printf("║_______________________________________║\n");
int commend,number;
char Nation[20];
Player Py;
printf("请输入命令行:\n");
sum=0;
while(scanf("%d",&commend)!=EOF)
{ system("cls"); //清屏函数
switch(commend)
{ case 1:
Add();
system("cls");
printf("已有球员信息如下:\n");
Output();break;
case 2:
fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\Play.txt","w");
printf("输入要删除的球员的编号:\n");
scanf("%d",&number);
system("cls");
if(Delete(number))
{ printf("\n删除后文件夹中的数据为:\n");
Output();
}
else printf("找不到该球员的信息!!!\n");
break;
case 3:
fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\Play.txt","w");
printf("输入需要修改的信息:\n");
scanf("%d%s%s%d%d%s%s",&Py.num,Py.name,Py.type,&Py.height,&Py.weight,Py.nation,Py.state);
system("cls");
if(Modify(Py))
{ printf("\n修改后文件夹中的数据为:\n");
Output();
}
else printf("找不到需要修改球员的信息!!!\n");
break;
case 4:
int size;
printf("0 根据编号查询 | 1 根据国籍查询\n");
printf("输入查询依据:");
scanf("%d",&size);
switch(size)
{ case 0:
printf("输入要查询的编号:\n");
scanf("%d",&number);
system("cls");
Find1(number); break;
case 1:
printf("输入要查询的国籍:\n");
scanf("%s",Nation);
system("cls");
Find2(Nation);break;
}
break;
case 5:
printf("每种球员类型的数量为:\n");
system("cls");
Count();break;
case 6: exit(0);break;
}
printf("\n请输入命令行:\n");
}
return 0;
}