求详解
#include "StdAfx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Student
{
char num[10];
char name[10];
double score;
Student *next;
};
void PrintOut(Student *head)
{
Student *p=head->next;
cout<<"学生学号"<<"\t"<<"姓名"<<"\t"<<"分数"<<endl;
while (p!=NULL)
{
cout<<p->num<<"\t\t"<<p->name<<"\t"<<p->score<<endl;
p=p->next;
}
cout<<endl;
}
struct Student *InsertSort(struct Student *head)
{
struct Student *first;
struct Student *t;
struct Student *p;
struct Student *q;
first = head->next;
head->next = NULL;
while (first != NULL)
{
for (t=first, q=head; ((q!=NULL)&&(q->score<t->score)); p=q, q=q->next);
first = first->next;
if (q == head)
{
head = t;
}
else
{
p->next = t;
}
t->next = q;
}
return head;
}
struct Student *OpenFile1(struct Student *head)
{
struct Student *q,*p;
q=head;
ifstream class_1_file("class_list_1.txt");
if (!class_1_file)
{
cout<<"文件打开失败!"<<endl;
return NULL;
}
for (int i=0;i<10;i++)
{
p=new Student();
class_1_file>>p->num>>p->name>>p->score;
p->next=NULL;
q->next=p;
q=q->next;
}
return head;
}
struct Student *OpenFile2(struct Student *head)
{
struct Student *q,*p;
q=head;
ifstream class_2_file("class_list_2.txt");
if (!class_2_file)
{
cout<<"文件打开失败!"<<endl;
return NULL;
}
for (int i=0;i<10;i++)
{
p=new Student();
class_2_file>>p->num>>p->name>>p->score;
p->next=NULL;
q->next=p;
q=q->next;
}
return head;
}
struct Student *Combine(struct Student *c1,struct Student *c2)
{
Student *h,*p,*q;
Student *head=new Student();
h=head;
p=c1->next;
q=c2->next;
while (p!=NULL&&q!=NULL)
{
if (p->score>q->score)
{
h->next=p;
p=p->next;
(h->next)->next=NULL;
h=h->next;
}
else
{
h->next=q;
q=q->next;
(h->next)->next=NULL;
h=h->next;
}
}
if (p!=NULL)
{
h->next=p;
}
else if(q!=NULL)
{
h->next=q;
}
c1=NULL;
c2=NULL;
return head;
}
void Search(struct Student *head)
{
string n;
Student *h=head;
h=head;
cout<<"请输入要查找的学生学号:";
cin>>n;
bool search=false;
while(h->next!=NULL)
{
h=h->next;
string (h->num);
if (h->num==n)
{
cout<<"学生学号"<<"\t"<<"姓名"<<"\t"<<"成绩"<<endl;
cout<<h->num<<"\t"<<h->name<<"\t"<<h->score<<endl;
search=true;
break;
}
}
if (!search)
{
cout<<"没有这个学生"<<endl;
}
}
void main()
{
Student *q=NULL;
Student *p=NULL;
Student *headClass1=new Student();
Student *headClass2=new Student();
Student *head;
headClass1=OpenFile1(headClass1);
cout<<" 班级1的学生成绩单:"<<endl;
PrintOut(headClass1);
headClass2=OpenFile2(headClass2);
cout<<"班级2的学生成绩单:"<<endl;
PrintOut(headClass2);
headClass1=InsertSort(headClass1);
cout<<"---------------------------"<<endl<<endl<<endl;
cout<<"班级1的成绩排序结果为:"<<endl;
PrintOut(headClass1);
headClass2=InsertSort(headClass2);
cout<<"班级2的成绩排序结果为:"<<endl;
PrintOut(headClass2);
head=Combine(headClass1,headClass2);
cout<<"---------------------------"<<endl<<endl<<endl;
cout<<"两班的成绩合并后:"<<endl;
PrintOut(head);
cout<<"---------------------------"<<endl<<endl<<endl;
Search(head);
return ;
}
[解决办法]
哪有问题,是打开文件报错还是怎么的?把里面涉及到的像这用class_list_2.txt文件放在程序目录下
