当前位置:编程学习 > C/C++ >>

C++编程问题

l编写一个简单的学生身高登记表,让用户输入所要登记的学生,以CRTL+Z结束输入。并把输入的资料按照身高的高低进行排序,并输出。 l要求: 1.登记表包括:姓名,年龄,专业,身高。 2.要求程序中需使用到struct, 数组,及指针。
补充:刚学习C++   求上述题目的工整代码来学习

写的好可以加分

答案:用插入排序的方法,一边输入,一遍排好序:

#include<iostream.h>


//////////////////////////////////////////////////////////////////
//学生类型结构体
struct student                            
{
 char name[100];   //姓名
 int age;      //年龄 
 char specialty[100];  //专业
 float stature;    //身高
 struct student *next; //指向下一个学生结构体的指针
};

//全局变量,用于存放学生信息链表的头指针
struct student *head;    
# define LEN sizeof(struct student)     //学生类型结构体的长度

//////////////////////////////////////////////////////////////////
//输入学生信息,一边输入一边使记录按身高从大到小排序,
//并输出所有学生的信息,直到输入Ctrl+Z时结束。

void main()
{
 //temp暂存当前指针,pre指向temp的前一个位置,p1指向当前的输入信息
 struct student *p1,*temp,*pre;
 int n=0;          //用于判断当前输入的信息是否是第一条记录的变量n
 head=NULL;   //置链表头指针为空
 cout<<"请输入姓名(输入z再回车结束):"<<endl;
 p1=new student[LEN];  //开辟一个学生结构体空间
 cin>>p1->name;    //输入姓名
 while (p1->name[0]!=122) //没有输入z则继续,122是z的Ascii码
 {
  n++;      //用于判断是否为第一条记录
  cout<<"请输入年龄:"<<endl;
  cin>>p1->age;
  cout<<"请输入专业:"<<endl;
  cin>>p1->specialty;
  cout<<"请输入身高:"<<endl;
  cin>>p1->stature;
  if(n==1)                 //输入的第一条记录作为头结点
  {
   head=p1;
     head->next=NULL;
  }
  else               //非第一条记录,用插入排序法找合适的位置插入
  {
   temp=head;           //预存头指针
     pre=temp;            //预存当前记录的前一记录的指针
   while(temp!=NULL)
   {
     //当前输入的记录较当前指向的记录较小,当前指针向后移
           if(p1->stature<temp->stature)
     {
      pre=temp;
      temp=temp->next;
     } 
     //当前输入的记录最大,插入到头指针
     else if(temp==head && p1->stature>=temp->stature)
     {
      head=p1;
      p1->next=temp;
      break;
     }
     else      //将当前输入的记录插入到中间某一合适的位置
     {
      p1->next=pre->next;
      pre->next=p1;
      break;
     }
   }
         if(temp==NULL) //将当前输入的记录最小,插入到链表的末尾
   {
    pre->next=p1;
      p1->next=NULL;
   }
  }
  cout<<endl;           //输出一个空行,为了美观
  cout<<"请输入姓名(输入z再回车结束):"<<endl;
  p1=new student[LEN];  //开辟一个学生结构体空间
  cin>>p1->name;    //输入姓名
  cout<<endl;           //输出一个空行,为了美观
 }
 ///////////////////////////////////////////
 //按身高排名并输出,因为输入时已经排好序,所以直接依次输出即可
 cout<<"学生信息如下(身高从高到矮):"<<endl;
 cout<<endl;              //输出一个空行,为了美观
 while(head)
 {
  cout<<"姓名:"<<head->name<<endl;
  cout<<"年龄:"<<head->age<<endl;
  cout<<"专业:"<<head->specialty<<endl;
  cout<<"身高:"<<head->stature<<endl;
  cout<<endl;            //输出一个空行,为了美观
  head=head->next;    //指针移到下一个位置输出
 }
 }

 

图:

//可能排序哪里稍稍复杂了点。。。

#include <iostream>
#include <memory.h>
#include <string.h>
using namespace std;

struct infos //主要信息存储结构
{
 char name[10],spec[100];   //使用数组存储名字,专业
 int age,height;
 infos *next,*prev;
};

int main()
{
 infos *head=NULL,*now,*thead,*tnow,*t;  //使用链表存储信息
 char name[10],sp[100];
 int  age,height,offs;
 bool isbreak;
 //信息输入
 do
 {
  cout<<"输入信息:\n"<<"1.学生姓名 (不超过9个字符,ctrl+z加两次回车结束输入)"<<endl;  //注意结束条件
  offs=0;
  isbreak=true;
  if (!(cin>>name))   //输入了ctrl+z
  {
   break;
  }
  cout<<"2.专业 (不超过99个字符)"<<endl;
  cin>>sp;
  cout<<"3.年龄"<<endl;
  cin>>age;
  cout<<"4.身高 (cm)"<<endl;
  cin>>height;
  if (head == NULL)
  {
   head=new infos;
   head->age = age;
   head->height= height;
   memcpy(head->name,name,strlen(name)+1);
   memcpy(head->spec,sp,strlen(sp)+1);
   head->prev=NULL;
   head->next=NULL;
   now=head;
  }
  else
  {
   now->next=new infos;
   now->next->prev=now;
   now=now->next;

   now->age = age;
   now->height= height;
   memcpy(now->name,name,strlen(name)+1);
   memcpy(now->spec,sp,strlen(sp)+1);
   now->next=NULL;
  }
 } while (true);   //死循环,用break退出

 //排序
 thead=NULL;
 while (head != NULL)
 {
  if (head->next == NULL)  //如果只有一个节点的话
  {
   if (thead == NULL)
   {
    thead=head;
   }
   else
   {
    tnow->next=head;
   }
   break;
 &nbs

上一个:c++编程题
下一个:c++怎么学好

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,