例4-11
题目描述:
设计一个结构体存储学生的成绩信息,需要包括学号年级和成绩三项内容,学号的范围是0到99 999 999,年级分为freshman、sophomore、junior、、senior四种,成绩包括A、S、C、D四个等级。
分析:
1.学号包括27个二进制位有效信息,而年级、成绩各包括2个二进制位有效信息,采用位域,需要31个二进制位。
2.定义枚举类型年级和成绩。
3.根据输入值调用swith来输出对应的结果。
流程图:
代码部分:
#include<iostream> using namespace std; enum Level{FRESHMAN,SOPHOMORE,IUNIOR,SENIOR}; enum Grade{A,B,C,D}; class Student { private: unsigned number:27; Level level: 2; Grade grade : 2; public: Student(unsigned number,Level level,Grade grade):number(number),level(level),grade(grade){} void show() { cout << "Number:" << number<<endl; cout << "Level:"; switch (level) { case FRESHMAN:cout << "FRESHMAN"; break; case SOPHOMORE:cout << "SOPHOMORE"; break; case IUNIOR:cout << "IUNIOR"; break; case SENIOR:cout << "SENIOR"; break; } cout << endl; cout << "Grade:"; switch (grade) { case A:cout << "A"; break; case B:cout << "B"; break; case C:cout << "C"; break; case D:cout << "D"; break; } cout << endl; } }; int main() { Student s(12345678, SOPHOMORE, B); cout << "Size of Student:" << sizeof(Student) << endl; s.show(); return 0; }
标签:10,level,grade,Level,number,二进制位,年级,打卡 From: https://www.cnblogs.com/xuechenhao173/p/17389142.html