面向对象程序设计上机练习八(对象数组)

来源:互联网 发布:爱的算法 在线阅读 编辑:程序博客网 时间:2024/05/29 21:32

面向对象程序设计上机练习八(对象数组)

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

利用类对象数组完成N个学生数据(学号是字符串类型、成绩是整型)的输入、输出。

输入

输入有N+1行:
第一行的整数N表示学生数目;
以下N行是N个学生的数据,每行中第一个是表示学号的字符串,第二个是表示学生成绩的整数。

输出

输出N个学生数据。每个学生的数据占一行。

示例输入

501 8902 7803 5604 9205 76

示例输出

01 8902 7803 5604 9205 76

提示

 

来源

zlh

示例程序

 

C

#include<stdio.h>  #include<stdlib.h>  struct node  {      int data;      char name[20];      struct node *next;  };  struct node * inset(struct node *head,int n)  {      int i;      struct node *p,*q,*tai;      q=head;      for(i=0;i<n;i++)      {          p=(struct node *)malloc(sizeof(struct node));          scanf("%s %d",p->name,&p->data);          p->next=q->next;          q->next=p;          q=p;      }      return(head);  }  int main()  {      int i,j,n,m;      struct node *head,*p,*q;      scanf("%d",&n);      head=(struct node *)malloc(sizeof(struct node));      head->next=NULL;      head=inset(head,n);      p=head;      p=p->next;      while(p!=NULL)      {          printf("%s %d\n",p->name,p->data);          p=p->next;      }  }  

C++

#include<iostream>  using namespace std;  class numble  {  private:  int i,num[1000],n;  char name[1000][20];  public:  void setint()  {  cin>>n;  for(i=0;i<n;i++)  cin>>name[i]>>num[i];  }  void setput()  {  for(i=0;i<n;i++)  cout<<name[i]<<" "<<num[i]<<endl;  }  };  int main()  {  numble t;  t.setint();  t.setput();  }


0 0