題目要求如下:
---------------------------------------------------------------------------------------------------------------------------
代碼如下:
/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作 者: 沈遠宏
* 完成日期:2014 年06月18日
* 版 本 號:v1.0
* 問題描述:Description
建立一個對象數組,內放n(n<10)個學生的數據(學號、成績),用指針指向數組首元素,輸出第奇數(1,3,5,7)個學生的數據。
Input
n和n個學生的學號、成績
Output
奇數學生的數據
*/#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
class Student
{
private:
string no;
double score;
public:
void input()
{
cin>>no>>score;
}
void display()
{
cout<<no<<" "<<score<<endl;
}
};
//主函數已給定如下,提交時不需要包含下述主函數
int main()
{
const int NUM=10;
Student stud[NUM];
int n,i;
freopen("12.txt","r",stdin);
cin>>n;
for(i=0; i<n; i++)
stud[i].input();
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Student *p=stud;
for(i=0; i<n; p+=2,i+=2)
p->display();
return 0;
}
運行結果:
OJ要求結果輸出例樣: