⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 有4个小程序
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
struct student
{
 int num;
 char name[20];
 int yuwenscore;
 int shuxuescore;
 int englishscore;
    struct student *next;
};


void menu()
{
 cout<<"        高考成绩排名"<<endl;
 cout<<"  按以下步骤输入信息"<<endl;
 cout<<"    1.依次输入学号,姓名,语文,数学,英语"<<endl;
 cout<<"    2.总成绩"<<endl;
 cout<<"    3.总排名"<<endl;
 cout<<"    4.语文排名"<<endl;
 cout<<"    5.数学排名"<<endl;
 cout<<"    6.英语排名"<<endl;
 cout<<"    7.查询"<<endl;
 cout<<"    -------------------------------------------------------"<<endl;
}

struct student *creat(struct student *head) // 函数返回的是与节点相同类型的指针
{
    struct student *p1,*p2;
    p1=p2=(struct student*) malloc(sizeof(struct student)); // 申请新节点
    cin>>p1->num>>p1->name>>p1->yuwenscore>>p1->shuxuescore>>p1->englishscore; // 输入节点的值
    p1-> next = NULL; // 将新节点的指针置为空
    for(int i=1;i<=4;i++) 
    {
        if (head==NULL) head=p1; //空表,接入表头
        else p2->next=p1; // 非空表,接到表尾
        p2 = p1;
        p1=(struct student *)malloc(sizeof(struct student)); //申请下一个新节点
			p1->next=NULL;
  if(i<=3)
  { 
  cin>>p1->num>>p1->name>>p1->yuwenscore>>p1->shuxuescore>>p1->englishscore;
  } 
  //输入节点的值
    }

    return head; //返回链表的头指针
}

 

void count(struct student *head)
{
 struct student *temp;
 temp=head; //取得链表的头指针
    for(int i=1;i<=4;i++) 
    {
  int m;
  m=temp->yuwenscore+temp->shuxuescore+temp->englishscore;
       cout<<i<<" "<<m<<endl;//输出链表节点的值
        
        temp=temp->next; //跟踪链表增长
 }
}

void sort(struct student *head)
{
 student *tp;
 tp=head;
 int a[4];//定义总分数组
 int i,j,k;
 a[1]=tp->yuwenscore+tp->shuxuescore+tp->englishscore;
 tp=tp->next;
 a[2]=tp->yuwenscore+tp->shuxuescore+tp->englishscore;
 tp=tp->next;
 a[3]=tp->yuwenscore+tp->shuxuescore+tp->englishscore;
 tp=tp->next;
 a[4]=tp->yuwenscore+tp->shuxuescore+tp->englishscore;
 for(j=1;j<=3;j++)//冒泡法排序
  for(k=1;k<=4-j;k++)
   if(a[k]<a[k+1])
   {
    int t=a[k];a[k]=a[k+1];a[k+1]=t;
   }
   for(i=1;i<5;i++){
	  
      cout<<a[i]<<endl;
	 }
  }

void sort1(struct student *head,int number)
{struct student *tp;
tp=head;
int b[4];
int i,j,k;
b[1]=tp->yuwenscore;
tp=tp->next;
b[2]=tp->yuwenscore;
tp=tp->next;
b[3]=tp->yuwenscore;
tp=tp->next;
b[4]=tp->yuwenscore;
for(j=1;j<=3;j++)//冒泡法排序
  for(k=1;k<=4-j;k++)
   if(b[k]<b[k+1])
   {
    int t=b[k];b[k]=b[k+1];b[k+1]=t;
   }
 if(number==5)
   for(i=1;i<5;i++)
	cout<<" "<<b[i]<<endl;
}

void sort2(struct student *head,int number)
{struct student *tp;
tp=head;
int c[4];
int i,j,k;
c[1]=tp->shuxuescore;
tp=tp->next;
c[2]=tp->shuxuescore;
tp=tp->next;
c[3]=tp->shuxuescore;
tp=tp->next;
c[4]=tp->shuxuescore;
for(j=1;j<=3;j++)//冒泡法排序
  for(k=1;k<=4-j;k++)
   if(c[k]<c[k+1])
   {
    int t=c[k];c[k]=c[k+1];c[k+1]=t;
   }
if(number==6)
   for(i=1;i<5;i++)
	  cout<<" "<<c[i]<<endl;
}

void sort3(struct student *head,int number)
{struct student *tp;
tp=head;
int d[4];
int i,j,k;
d[1]=tp->englishscore;
tp=tp->next;
d[2]=tp->englishscore;
tp=tp->next;
d[3]=tp->englishscore;
tp=tp->next;
d[4]=tp->englishscore;
for(j=1;j<=3;j++)//冒泡法排序
  for(k=1;k<=4-j;k++)
   if(d[k]<d[k+1])
   {
    int t=d[k];d[k]=d[k+1];d[k+1]=t;
   }
if(number==7)
   for(i=1;i<5;i++)
	  cout<<" "<<d[i]<<endl;
}


void query(struct student *head)
{
 struct student *temper;
 temper=head;
 int number;
 cin>>number;
 for(int i=1;i<=4;i++) 
 {
 if(number==temper->num)
 {
  cout<<"            name is:"<<temper->name<<endl;
  cout<<"       yuwen score is:"<<temper->yuwenscore<<endl;
  cout<<"         shuxue score is:"<<temper->shuxuescore<<endl;
  cout<<"   English score is:"<<temper->englishscore<<endl;
 } 
 temper=temper->next;
 }
}
void main()
{int number;
 menu();
 cout<<"    1.输入信息"<<endl;
 struct student *head;
 head=NULL; /* 建一个空表*/
    head=creat(head); /* 创建单链表*/
 cout<<"    计算每个学生的总分:"<<endl;
 count(head);
 cout<<"    总分排名:"<<endl;
 sort(head);
 cout<<"    输入想要查询学生的学号"<<endl;
 query(head);
 for(;;){
 cout<<"    要查语文排名,请按5:"<<endl;
 
 cout<<"    要查数学排名,请按6:"<<endl;
 
 cout<<"    要查英语排名,请按7:"<<endl;
 cout<<"    退出查询      请按0"<<endl;
 cin>>number;
 if(number==5)
	 sort1(head,number);
 else if(number==6)
	 sort2(head,number);
 else if(number==7)
	 sort3(head,number);
 else if(number==0) break;
	else cout<<"  请输入正确的序号  "<<endl;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -