发布网友
共3个回答
热心网友
#include<iostream.h>
/*head.h*/
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct student
{
char num[6]; //定义学号
char name[8];//定义姓名
// int tax;//定义学生的按总分排名的名次,这个我不做了,自己可以加上,看看怎样修改,都做出来就没意思了呵呵。
int english;//定义英语成绩
int math;//定义数学成绩
// int score;//定义总分 这个我不要了
struct student *next;//指向下一个学生的指针链域
}student;
int getint(int a) //字符转换成数字
{
return (int)(a-'0');
}
student * head1;//头指针,全局变量
void student_read() //从文件中读入学生成绩原始数据
{
int i=7;//共有7个学生的记录,需要时可以修改该初始值
ifstream infile ("student.txt",ios::in);//读文件的操作使用C++
while(i>0)
{
student * p;//定义临时结构体指针变量p
p=new student;//开辟内存空间,并将p指向该地址的引用
infile>>p->num>>p->name>>p->english>>p->math;//为了简化
p->next=head1->next;
head1->next=p;
i--;//下标减1
}
}
void Reverselist(student *p)
{ student *p1,*p2,*p3;
student *_data;
_data=new student;
p1=p->next;
if(!p1->next) return;
p2=p1->next;
if(!p2->next)
{
strcpy(_data->num,p1->num);
strcpy(_data->name,p1->name);
// _data->tax=p1->tax;
_data->english=p1->english;
_data->math=p1->math;
//_data->score=p1->score;
strcpy(p1->num,p2->num);
strcpy(p1->name,p2->name);
// p1->tax=p2->tax;
p1->english=p2->english;
p1->math=p2->math;
//p1->score=p2->score;
strcpy(p2->num,_data->num);
strcpy(p2->name,_data->name);
// p2->tax=_data->tax;
p2->english=_data->english;
p2->math=_data->math;
//p2->score=_data->score;
return;
}//if
p3=p2->next;
p1->next=NULL;
while(p3->next)
{p2->next=p1;
p1=p2;
p2=p3;
p3=p3->next;
}//while
p2->next=p1;
p3->next=p2;
p->next=p3;
}//Reverselist
void student_order(student *p)
{ student *_temp = p->next;
student *_node = p->next;
student *temp;
temp=new student;
temp->next=NULL;
for (;_temp->next;_temp = _temp->next)
{
for (_node = p->next;_node->next;_node = _node ->next)
{
if ((_node->english+_node->math)>(_node->next->english+_node->next->math))
{
strcpy(temp->num,_node->num);
strcpy(temp->name,_node->name);
temp->english=_node->english;
temp->math=_node->math;
// temp->score=_node->score;
strcpy(_node->num,_node->next->num);
strcpy(_node->name,_node->next->name);
_node->english=_node->next->english;
_node->math=_node->next->math;
//_node->score=_node->next->score;
strcpy(_node->next->num,temp->num);
strcpy(_node->next->name,temp->name);
_node->next->english=temp->english;
_node->next->math=temp->math;
//_node->next->score=temp->score;
}//if
}//for
}//for
Reverselist(p);
}//student_order
void student_output(student *p)
{ cout<<"学号 姓名 英语 数学\t\n";
p=p->next;
while(p)
{cout<<" "<<p->num<<"\t"<<p->name<<"\t"<<p->english<<"\t"<<p->math<<endl;
p=p->next;
}
}
void student_output1(student *p)
{ cout<<"学号 姓名 英语 数学 总分\t\n";
p=p->next;
while(p)
{ cout<<" "<<p->num<<"\t"<<p->name<<"\t"<<p->english<<"\t"<<p->math<<"\t "<<(p->english+p->math)<<endl;
p=p->next;
}
}
void student_write() //将项目数据写入文本文档
{
student * p;
p=head1;
p=p->next;
ofstream outfile("result.txt",ios::out);
while (p!=NULL)
{
outfile<<" "<<p->num<<"\t"<<p->name<<"\t"<<p->english<<"\t"<<p->math<<"\t "<<p->english+p->math<<endl;
p=p->next;
}
outfile.close();
}
/*主程序*/
#include<iostream.h>
#include<fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include"head.h"
void main()
{ system("color 1b");
head1=new student;
head1->next=NULL;
student_read(); //读入学生数据
student * p1;
p1=head1;
p1=p1->next;
int temp;
student_output(head1); /*显示原有的记录*/
student_order(head1);
student_output1(head1); //信息的输出函数
student_write();//将学校数据写入文本
getch();
}
参考资料:原创
热心网友
书上应该有类似的题。
虽然前三个星期我们刚考了两道和这类似的题,但我用手机输入太痛苦了。如果没人答的话可以在13号我放假后再给你答案,如果有人回答的话最好了。
热心网友
1.用链表读入数据
2.用冒泡法排序
3.输出数据
有不懂的再问我!