用c++编 能够录入学生信息,包括(学号、姓名、性别、身高、成绩等)并且按身高排序;输出排序后的学生名

急用谢谢啦
2024-12-19 15:16:36
推荐回答(3个)
回答1:

做事能做,麻烦,参照这个改下,记得有人问过,做出来以后找不到那个人了,那个人的问题没有身高排序这个要求

//+====================================================================
//|FileName:Maim.cpp
//|Authro:AnubiEnvoy
//|Introduction:
//|DateCreated:
//|DateCompleted:
//|DateAmend:
//+====================================================================

#include"Student.h"

float g_fHighestValue = 0.0f; //最高分
float g_fClassAverage = 0.0f; //全班平均成绩
unsigned int g_nNumOfStu = 0;

CStudent *pArray = NULL;

//+-----------------------------------
//|创建班级成绩列表
//+-----------------------------------
void CreateClass()
{

cout<<"正在创建信息列表,请输入班级学生数目:"<
cin>>g_nNumOfStu;

pArray = new CStudent[g_nNumOfStu];

for(unsigned int i = 0; i < g_nNumOfStu; i++)
{
CStudent *Temp = new CStudent(3);
pArray[i] = *Temp;
}
cout<<"成绩列表创建完毕"<}

//+----------------------------------
//|设置学生信息,并求所需信息
//+----------------------------------
void SetAndCompute()
{
for(unsigned int i = 0; i {
cout<<"请输入第"< pArray[i].Set_stu_inf();
pArray[i].average();
pArray[i].Sum();
}
}

//+----------------------------------
//|求取全班最高分和总平均分
//+----------------------------------
void ComputeHighestAndAverage()
{
if(pArray != NULL)
{
float TempAll = 0.0f;

for(unsigned int i = 0; i {
//求最高分
float TempHighest = pArray[i].Sum();
if(g_fHighestValue < TempHighest)
{
g_fHighestValue = TempHighest;
}

float TempAverage = 0.0f;

TempAll += pArray[i].average();
}

g_fClassAverage = TempAll / g_nNumOfStu;

//输出全班最高分
cout<<"全班最高分:"< cout<<"全班平均分"< }else
{
cout<<"您还没有录入学生信息!"< }
}

//+-----------------------------------------
//|按学号查找学生信息
//+-----------------------------------------
void FineInfoById()
{
cout<<"请输入学生的学号:"< unsigned int TempInt = 0;
cin>>TempInt;

if(pArray != NULL)
{
for(unsigned int i = 0; i {
if(TempInt == pArray[i].GetID())
{
pArray[i].Print();
}
}
}else
{
cout<<"您还没有录入学生信息!"< }
}

//+------------------------------------------
//|显示所有学生信息
//+------------------------------------------
void ShowAll()
{
if(pArray != NULL)
{
for(unsigned int i = 0; i< g_nNumOfStu; i++)
{
if(pArray != NULL)
{
pArray[i].Print();
}
}
}else
{
cout<<"您还没有录入学生信息!"< }

}

//+---------------------------------
//|main
//+---------------------------------
int main()
{

cout<<"欢迎使用学生信息管理系统,系统应用如下:"< cout<<"输入C --> "<<"创建学生列表:"< cout<<"输入R --> "<<"重新设置列表:"< cout<<"输入S --> "<<"输入学生信息: "< cout<<"输入H --> "<<"查看最高分,全班平均分:"< cout<<"输入A --> "<<"查看所有学生信息:"< cout<<"输入F --> "<<"按照学号查询学生信息:"< cout<<"输入E --> "<<"退出查询系统:"<
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"< cout<<"请输入操作命令:"<
char TempChar = ' ';
while(TempChar != 'E')
{
cin>>TempChar;
switch(TempChar)
{
case 'C':
{
CreateClass();
}break;
case 'R':
{
//删除原来信息
cout<<"正在删除原有信息,请稍等...."< unsigned int a = sizeof(pArray);
unsigned int b = sizeof(CStudent);
unsigned int c = a / b;

delete [] pArray;

pArray = NULL;
cout<<"删除完毕!"<
CreateClass();
}break;
case 'S':
{
SetAndCompute();
}break;
case 'H':
{
ComputeHighestAndAverage();
}break;
case 'F':
{
FineInfoById();
}break;
case 'A':
{
ShowAll();
}break;
case 'E':
{}break;
}
}

if(pArray != NULL)
{
delete [] pArray;
}

cin.get();
cin.get();
return 0;
}

//+=========================================================================
//|FileName:Student.h
//|Author:AnubisEnvoy
//|Introduction:处理学生相关信息
//|DateCreated:
//|DateCompleted;
//|DateAmend:
//+==========================================================================

#include
using namespace std;
#include

#ifndef __Student_H__
#define __Student_H__

class CStudent
{
private:
int m_nNum; //学科数目
unsigned int m_nID; //注册号
string m_strName; //姓名
float m_fMath; //数学
float m_fEnglish; //英语
float m_fComputer; //计算机
public:
CStudent();
CStudent(int n);
~CStudent();

float Sum();//求总成绩
float average();//求平均成绩
void Print();//显示数据信息
unsigned int GetID();//获取学生ID
void Set_stu_inf(); //设置学生信息
};

#endif

//+=========================================================================
//|FileName:Student.cpp
//|Author:AnubisEnvoy
//|Introduction:处理学生相关信息
//|DateCreated:
//|DateCompleted;
//|DateAmend:
//+==========================================================================

#include"Student.h"

CStudent::CStudent()
{
}
//构造函数
CStudent::CStudent(int n):
m_nNum(n),
m_nID(0),
m_fComputer(0.0f),
m_fEnglish(0.0f),
m_fMath(0.0f),
m_strName("无名氏")
{
}

//析构函数
CStudent::~CStudent()
{
}

//求单个学生总成绩
float CStudent::Sum()
{
float fTemp = 0.0f;

fTemp = m_fComputer + m_fEnglish + m_fMath;
return fTemp;
}

//求单个学生平均成绩
float CStudent::average()
{
float fTemp = 0.0f;
fTemp = (m_fComputer + m_fEnglish + m_fMath)/m_nNum;
return fTemp;
}

//打印单个学生数据信息
void CStudent::Print()
{
cout<<"***************************************"< cout<<"学生注册号:"< cout<<"学生姓名:"< cout<<"学生成绩:"<<"英语:"<}

//获取学生ID
unsigned int CStudent::GetID()
{
return m_nID;
}

//设置学生信息
void CStudent::Set_stu_inf()
{
char TempName[30];

cout<<"请输入学生的姓名:"< cin>>TempName; m_strName = TempName;
cin.get();

cout<<"请输入 "< cin>>m_fMath;
cout<<"请输入 "< cin>>m_fEnglish;
cout<<"请输入 "< cin>>m_fComputer;
cout<<"请输入 "< cin>>m_nID;
}

回答2:

这种类型的程序以前写过,拿过来帮你修改了一下,基本都行了!!!

至于控制参数的有效性:比如学号的唯一性、身高不能超过多少cm、性别只能取男女之类的,你就自己加收入控制吧!!不过用DOS做这种程序也永不着考虑这么多了,要不写起来麻烦!!

#include
#include
#include
#include
#include
#include
#define OK 1
#define ERROR 0
using namespace std;

//数组长度的定义统一在前面定义好修改维护
#define NUM_MAX_LENGTH 20 //学号的最大长度
#define NAME_MAX_LENGTH 20 //名字的最大长度
#define SEX_MAX_LENGTH 4 //性别长度 汉字4个字符 英文 女:F,男:M

typedef int Status; /*返回值的类型*/

static int STU_COUNT=0;

typedef struct student
{
char num[NUM_MAX_LENGTH];
char name[NAME_MAX_LENGTH];
char sex[SEX_MAX_LENGTH];
float high;
float score;
student *next;

}node;

typedef struct LIST
{
node *head,*tail;

}link;

node *MakeNode(char num[],char name[],char sex[],int high, float score) /*生成存放学生信息的节点*/
{
node *p=(node *)malloc(sizeof(node));
strcpy(p->num,num); /*存放学号*/
strcpy(p->name,name);
strcpy(p->sex,sex);
p->high = high;
p->score = score;

p->next=NULL; /*刚生成的节点下一个为空*/
return p;
}

Status Init(link *s)
{
s->head=s->tail=(node *)malloc(sizeof(node)); /*头结点num用来存放学生信息条数。score放平均成绩-1表示没有求出平均成绩*/
if(NULL==s->head)
{
puts(" 分配失败!\n");
return ERROR;
}
s->head->next = NULL;
return OK;
}
Status Create(link *s,int n)
{
int i=0;
float high, score;
char num[NUM_MAX_LENGTH],name[NAME_MAX_LENGTH],sex[SEX_MAX_LENGTH];
node *p=NULL;
for(i=0; i {
cout<<" "<<"请输入第"< cout<<" ";
cin>>num>>name>>sex>>high>>score;
p=MakeNode(num,name,sex,high,score);
s->tail->next=p;
s->tail=p;
STU_COUNT++; /*学生信息条数每增加一个就要++1 */
}
return OK;
}

Status DisColum()
{
cout< cout< < < < < < return OK;
}
Status DisNode(node *p)
{
if(p)
{
cout<<" ";
cout<num
<name
<sex
<high
<score
< }
return OK;
}
Status display(link *s)
{
node *p=NULL;
if(s->head->next==NULL)
{
cout<<" "<<"没有任何学生信息记录!"< return ERROR;
}
else
{
DisColum();
p=s->head->next; /*让p指针指向头结点的下一个节点,因为头结点只用来存放了学生的人数*/
while(p)/*只要p指针指向不为空就要输出*/
{
DisNode(p);
p = p->next;
}
cout< return OK;
}

}
Status SortByHigh(link *s) /*按照身高升序排序*/
{
int i=0,j=0;
node *p,*cur,*next;
for(i=0; i {
p= s->head;
cur=p->next;
next = cur->next;
for(j=0; j {
if(cur->high > next->high)
{
p->next=next; /*若前面的大于后面的就要交换前后两个值*/
cur->next = next->next;
next->next = cur;

p=next; /*交换之后要修改当前指针与当前指针与下一个指针的位置*/
next=cur->next;
if(cur->next==NULL)
{
s->tail = cur;
}

}else{ /*挨着的两个值,前面的不大于后面的只要将指针往后面移动*/
p=cur;
cur=next;
next=next->next;
}
}

}

return OK;
}

Status SortByNum(link *s) /*按照学号升序排序*/
{
int i=0,j=0;
node *p,*cur,*next;
for(i=0; i {
p= s->head;
cur=p->next;
next = cur->next;
for(j=0; j {
if(strcmp(cur->num,next->num)>0)
{
p->next=next; /*若前面的大于后面的就要交换前后两个值*/
cur->next = next->next;
next->next = cur;

p=next; /*交换之后要修改当前指针与当前指针与下一个指针的位置*/
next=cur->next;
if(cur->next==NULL)
{
s->tail = cur;
}

}else{ /*挨着的两个值,前面的不大于后面的只要将指针往后面移动*/
p=cur;
cur=next;
next=next->next;
}
}

}

return OK;
}

node * QueryStu(node *p,char *num)
{
while(p->next)
{
if(strcmp(p->next->num,num)==0)
{
break;
}
else{
p = p->next;
}
}
return p;
}

Status DeleteStu(link *s,char *num)
{
node *cur,*p;
if(s->head->next==NULL)
{
puts(" 没有任何学生信息记录");
return ERROR;
}
else
{
cout<<" 请输入你要删除的学生的学号:";
cin>>num;
p=s->head;
///////////
p = QueryStu(p,num);

if(p->next)
{
cur=p->next;
if(cur->next==NULL)
{
s->tail=p;
}
p->next=cur->next;
cur->next=NULL;
free(cur);
STU_COUNT--;
return OK;
}else{
puts(" 不存在该学生的信息");
return ERROR;
}
}
}

Status DeleteStudent(link *s)
{
char num[NUM_MAX_LENGTH];
if(DeleteStu(s,num))
{
cout<<" 删除操作成功...";
}
else{
cout<<" 删除操作失败...";
}
getch();
return OK;
}

Status QueryStudent(link *s)
{
char num[NUM_MAX_LENGTH];
if(s->head->next==NULL)
{
puts(" 没有任何学生信息记录");
getch();
return ERROR;
}
else
{
cout<<" 请输入你要查询的学生的学号:";
cin>>num;
node *p = QueryStu(s->head,num);
if(p->next)
{
DisColum();
DisNode(p->next);
cout<<" 查询操作成功...";
}
else{
cout<<"没有该学生的信息!"< cout<<" 查询操作失败...";
}
getch();
}
return OK;
}

Status addStudentScore(link *s)
{
int n;
cout<<" "<<"你想录入多少学生的记录:N=";
cin>>n;
Create(s,n);
cout<<" "< getch();
return OK;
}

Status DisplayByHigh(link *s)
{
SortByHigh(s);/*按照姓名升序排输出*/
cout<<" "<<"所有学生的信息(学号、姓名、性别、专业):"< display(s);
cout<<" "<<"所有信息显示完毕...";
getch();
return OK;
}

Status DisplayByNum(link *s)
{
SortByNum(s);/*按照学号升序排输出*/
cout<<" "<<"所有学生的信息(学号、姓名、性别、专业):"< display(s);
cout<<" "<<"所有信息显示完毕...";
getch();
return OK;
}

Status SaveToFile(link *s)
{
char choose[10];
while(true){
cout<<" 是否需要保存到磁盘文件中?(Y/y OR N/n): ";
cin>>choose;
if(strcmp(choose,"Y")==0 || strcmp(choose,"y")==0 || strcmp(choose,"N")==0 || strcmp(choose,"n")==0)
{
if(strcmp(choose,"Y")==0 || strcmp(choose,"y")==0)
break;
else
return OK;
}
}
ofstream of("stu.txt"/*,ios_base::app*/);
node *p=NULL;
p=s->head->next; /*让p指针指向头结点的下一个节点,因为头结点只用来存放了学生的人数*/
while(p)/*只要p指针指向不为空就要输出*/
{
of<num<<" "<name<<" "<sex<<" "<high<<" "<score< p = p->next;
}
return OK;
}

Status LoadData(link *s)
{
node *p=NULL;
char num[NUM_MAX_LENGTH],name[NAME_MAX_LENGTH],sex[SEX_MAX_LENGTH];
float high,score;
if(_access("stu.txt",0)!=0)
{
return ERROR;
}
ifstream fin;
fin.open("stu.txt");
while(!fin.eof())
{
fin>>num>>name>>sex>>high>>score;
if(strcmp(num,"")!=0)
{
p=MakeNode(num,name,sex,high,score);
s->tail->next=p;
s->tail=p;
STU_COUNT++; /*学生信息条数每增加一个就要++1 */
}
}
fin.close();

return OK;
}
Status Menu()
{
int k=-1;
while(k<0||k>6){
system("cls");
cout<<" ";
cout<<"|-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-|\n "
"| c语言学生信息管理程序实现 |\n "
"| 1、从键盘输入学生信息 |\n "
"| 2、学生信息的显示 |\n "
"| 3、学生信息按身高排序 |\n "
"| 4、学生信息按学号排序 |\n "
"| 5、删除学生信息 |\n "
"| 6、查询学生信息 |\n "
"| 0、系统推出 |\n "
"|-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-|\n\n\n";
fflush(stdin);
if(k==-1)
{
cout<<" "<<"请选择功能:";
}else if(k<0 || k>6)
{
cout<<" "<<"(选择错误)请重新选择:";
}
scanf("%d",&k);

}
return k;
}

Status Operation()
{
link s;
int k=-1;
Init(&s);
LoadData(&s);/*就加了这个加载数据库文件*/
do{
switch(k=Menu())
{
case 0:
SaveToFile(&s);
cout<<" ";
exit(1);
break;
case 1:
addStudentScore(&s);
break;
case 2:
cout<<" "<<"所有学生的信息(学号、姓名、性别、专业):"< display(&s);
cout<<" "<<"所有信息显示完毕...";
getch();
break;
case 3:
DisplayByHigh(&s);
break;
case 4:
DisplayByNum(&s);
break;
case 5:
DeleteStudent(&s);
break;
case 6:
QueryStudent(&s);
break;
default:
break;
}
}while(k!=0);
return OK;
}

int main()
{
Operation();
return OK;
}

回答3:

//运行过了,符合要求,有问题联系我
#include
#include
using namespace std;
//按题目要求定义结构体
struct student
{
string name;
int height;
int score;
int num;
int sex;
};
void main()
{
//录入学生信息
int total,max,index;
cout<<"输入学生人数"< cin>>total;
student *a;
student temp;
a=new student[total];
for(int i=0;i {
cout<<"请按照姓名、性别、身高、成绩的顺序输入第"< cout<<"姓名:"< cin>>a[i].name;
cout<<"性别(0代表女生,1代表男生)"< cin>>a[i].sex;
while((1!=a[i].sex)&&(0!=a[i].sex))
{
cout<<"请输入正确的数字!"< cin>>a[i].sex;
}
cout<<"身高:"< cin>>a[i].height;
cout<<"成绩:"< cin>>a[i].score;
}
//按照身高排序
for(i=0;i {
max=a[i].height;
index=i;
for(int j=i+1;j {
if(a[j].height>max)
{
temp.height=a[j].height;
temp.name=a[j].name;
temp.sex=a[j].sex;
temp.score=a[j].score;

a[j].height=a[i].height;
a[j].name=a[i].name;
a[j].sex=a[i].sex;
a[j].score=a[i].score;

a[i].height=temp.height;
a[i].name=temp.name;
a[i].sex=temp.sex;
a[i].score=temp.score;
}
}
}
//输出结果
cout<<"按身高排序的结果如下,只输出每个学生的姓名:"< for(i=0;i cout<}