我以前写过一个控制台的,要求和你这个不太一样,也没写文件操作,不过你可以参考一下
#include
#include
struct st
{
int no;
char name[20];
int math;
int eng;
int pro;
int sum;
double ave;
};
struct Node
{
st content;
Node *next;
};
void input(st ,Node *&head);
void output(Node *head);
void del(int a,Node *&head);
void out(Node *p);
using namespace std;
Node *head=NULL;
int main()
{
A:
cout << "请不要搞恶,会崩的!!!!\n";
cout << "请输入要求操作的代号\n";
cout << "1 添加新数据。\n";
cout << "2 查找数据。\n";
cout << "3 修改数据。\n";
cout << "4 删除数据。\n";
cout << "5 统计。\n";
cout << "6 说明文档\n";
cout << "7 退出。\n";
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "开始输入,请依次输入这位同学的学号、姓名、数学、英语、程序设计的成绩;输完请输-1" << endl;
st cont;
for(;;)
{
cin >> cont.no ;
if(cont.no==-1) break;
cin >> cont.name >> cont.math >> cont.eng >> cont.pro;
cont.sum = cont.math + cont.eng + cont.pro;
cont.ave = cont.sum / 3.0;
input(cont,head);
}
break;
}
case 2:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要查找的类型" <
cout << "2 姓名\n" ;
cout << "3 数学成绩\n" ;
cout << "4 英语成绩\n" ;
cout << "5 程序设计成绩\n" ;
cout << "6 总成绩\n" ;
cout << "7 平均成绩\n" ;
int type;
cin >> type;
switch (type)
{
case 1:
{
int target;
cout << "请输入学号。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.no==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.no==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 2:
{
char target[20];
cout << "请输入姓名。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(strcmp(target , p->content.name)==0)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 3:
{
int target;
cout << "请输入数学成绩。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.math==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.math==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 4:
{
int target;
cout << "请输入英语成绩。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.eng==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.eng==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 5:
{
int target;
cout << "请输入程序设计成绩。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.pro==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.pro==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 6:
{
int target;
cout << "请输入总成绩。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.sum==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.sum==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
case 7:
{
int target;
cout << "请输入平均成绩。" <
int i = 1;
Node *p;
for (p=head;p->next!=NULL;p=p->next)
{
if(p->content.ave==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
i++;
}
if(p->content.ave==target)
{
cout << "第" << i << "个" << "为" << target << endl;
out(p);
}
cout << "是否删除y or n\n" ;
char y_n;
cin >> y_n;
if(y_n=='y') del(i,head);
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch (type)
break;
}
case 3:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要修改的数据的编号"<
cin >> a;
cout << "请输入要更改的类型"<
cout << "2 姓名\n" ;
cout << "3 数学成绩\n" ;
cout << "4 英语成绩\n" ;
cout << "5 程序设计成绩\n" ;
int typ;
cin >> typ;
switch(typ)
{
case 1:
{
cout << "请输入新值。" <
cin >> n;
Node *p=head;
if(a==1)
{
p->content.no = n;
}
else
{
for(int i=1;i {
p=p->next;
}
p->content.no = n;
}
break;
}
case 2:
{
cout << "请输入新值。" <
cin >> n;
Node *p=head;
if(a==1)
{
strcpy(p->content.name , n);
}
else
{
for(int i=1;i {
p=p->next;
}
strcpy(p->content.name , n);
}
break;
}
case 3:
{
cout << "请输入新值。" <
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.math ;
p->content.math = n;
}
else
{
for(int i=1;i {
p=p->next;
}
temp=p->content.math ;
p->content.math = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 4:
{
cout << "请输入新值。" <
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.eng ;
p->content.eng = n;
}
else
{
for(int i=1;i {
p=p->next;
}
temp=p->content.eng;
p->content.eng = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
case 5:
{
cout << "请输入新值。" <
cin >> n;
Node *p=head;
int temp ;
if(a==1)
{
temp=p->content.pro ;
p->content.pro = n;
}
else
{
for(int i=1;i {
p=p->next;
}
temp=p->content.pro;
p->content.pro = n;
}
p->content.sum=p->content.sum - temp + n;
p->content.ave=p->content.sum/3.0;
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch(typ)
break;
}
case 4:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请输入要删除的数据的位置" <
cin >> loc;
del(loc,head);
break;
}
case 5:
{
if(head==NULL) {cout << "已经为空!!\n"; break;}
cout << "请选择统计类型" << endl;
cout << "1 每门课90分以上的" <
cin >> tp;
switch (tp)
{
case 1:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程序设计\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math>90) out(q);
}
if(q->content.math>90) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng>90) out(q);
}
if(q->content.eng>90) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro>90) out(q);
}
if(q->content.pro>90) out(q);
}
else
cout << "输入错误\n";
break;
}
case 2:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程设\n";
int dec;
cin >> dec;
if(dec == 1)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.math<60) out(q);
}
if(q->content.math<60) out(q);
}
else if(dec == 2)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.eng<60) out(q);
}
if(q->content.eng<60) out(q);
}
else if(dec == 3)
{
Node *q;
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->content.pro<60) out(q);
}
if(q->content.pro<60) out(q);
}
else
cout << "输入错误\n";
break;
}
case 3:
{
cout << "选择科目\n" << "1 数学\n2 英语\n3 程序设计\n";
int dec;
cin >> dec;
if(dec == 1)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.math > q_max->content.math)
q_max=q2;
if(q1->content.math !=q_max->content.math )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}
output(head);
}
else if(dec == 2)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.eng > q_max->content.eng )
q_max=q2;
if(q1->content.eng !=q_max->content.eng )
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}
}
output(head);
}
else if(dec == 3)
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.pro > q_max->content.pro)
q_max=q2;
if(q1->content.pro!=q_max->content.pro)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}
}
output(head);
}
else
cout << "输入错误\n";
break;
}
case 4:
{
for(Node *q=head;q->next!=NULL;q=q->next)
{
for(Node *q1=head;q1->next!=NULL;q1=q1->next)
{
Node *q_max=q1;
for(Node *q2=q1->next;q2->next!=0;q2=q2->next)
{
if(q2->content.sum > q_max->content.sum)
q_max=q2;
if(q1->content.sum!=q_max->content.sum)
{
Node temp;
temp.content =q1->content;
q1->content=q2->content;
q2->content=temp.content;
}
}
}
}
output(head);
break;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
}//switch(tp)
break;
}
case 6:
{
cout <<"################################################################################\n";
cout <<"这是B版,容易崩溃,请按照正常方式输入,如果您想测试它是否会崩,那肯定崩!!!!\n";
cout <
break;
}
case 7:
{
cout << "谢谢使用,没崩说明你强\n";
return 0;
break ;
}
default:cerr << "跟你说了别恶搞,真的会崩的\n" ;
} //switch(choice)
goto A;
}
void del (int a,Node *&head)
{
if(head==NULL) {cout << "已经为空!!\n" <
if(a==1)
{
head=p->next;
delete p;
}
else
{
Node *q=p->next;
for(int i=1;i
p=p->next;
q=p->next;
}
p->next=q->next;
delete q;
}
}
void out(Node *p)
{
cout << p->content.no <<'\t';
cout << p->content.name <<'\t';
cout << p->content.math <<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro <<'\t';
cout << p->content.sum <<'\t';
cout << p->content.ave <<'\n';
}
void output(Node *head)
{
cout << "学号\t" << "姓名\t" << "数学\t"<< "英语\t"<< "程设\t"<< "总分\t" << "平均分\n" ;
for (Node *p=head;p!=NULL;p=p->next)
{
cout << p->content.no<<'\t';
cout << p->content.name<<'\t';
cout << p->content.math<<'\t';
cout << p->content.eng <<'\t';
cout << p->content.pro<<'\t';
cout << p->content.sum<<'\t';
cout << p->content.ave<<'\t';
cout << endl;
}
}
void input(st cont,Node *&head)
{
Node *p=new Node;
p->content=cont;
if (head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
}
程序已经发送到你的邮箱了