您的位置:首页 > 其它

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

2015-11-11 23:50 369 查看


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



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


题目描述

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

输入

输入有N+1行:

第一行的整数N表示学生数目;

以下N行是N个学生的数据,每行中第一个是表示学号的字符串,第二个是表示学生成绩的整数。

输出

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

示例输入

5
01 89
02 78
03 56
04 92
05 76


示例输出

01 89
02 78
03 56
04 92
05 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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: