#include "stdafx.h" #include "iostream" #include "string" #include "stdio.h" using namespace std; using std::string; struct score { string No; string Name; bool is_score; union achieve { char grade; int mark; }outcome; }; score * input(int); void output(score *, int); int _tmain(int argc, _TCHAR* argv[]) { int i, count; score * p; cout<<"Howmany students? "; cin>>count; output(input(count),count); return 0; } score * input(int n) { int i = 0; char c; score * h, * p = newscore[n]; h = p; while (i++ < n) { cout<<"No:\t"; cin>>p->No; cout<<"Name:\t"; cin>>p->Name; cout<<"Isachievement a score? (Y/N): "; cin>>c; c = c & 223; if (c == 'Y') { p->is_score= 1; cout<<"Score:\t"; cin>>p->outcome.mark; } else { p->is_score= 0; cout<<"Grade(A,B, C, D, E): "; cin>>p->outcome.grade; } p++; cout<<"--------------"<<endl; } return h; } void output(score * p, int count) { cout<<"No\tName\tAchieve"<<endl; for (int i=0;i<count; i++) { cout<<p->No<<"\t"<<p->Name<<"\t"; if(p->is_score) cout<<p->outcome.mark<<endl; else cout<<p->outcome.grade<<endl; p++; } } 为什么这个程序中使用联合体的类型?使用联合体的主要目的是什么? 使用#include“iostream",namespace std这两个语句的作用是什么?没有两个语句会发生什么情况? “->” 和 “.”这两个操作符之间有什么不同?
|