• C++:程序设计实例


     学生信息管理系统

    仅供参考,请不要直接用于实际用途

    缺陷:对输入变量的管理存在漏洞

    1. #include
    2. #include
    3. using namespace std;
    4. const int N = 20;
    5. // 定义普通函数
    6. void Display()
    7. {
    8. cout << "\n************0.退 出*************" << endl;
    9. cout << "************1.录入信息*************" << endl;
    10. cout << "************2.查询信息*************" << endl;
    11. cout << "************3.浏览信息*************" << endl;
    12. }
    13. // 定义类
    14. class Student
    15. {
    16. protected:
    17. string name;
    18. int number;
    19. char sex;
    20. int age;
    21. public:
    22. bool operator==(string p);
    23. virtual void input() = 0;
    24. virtual void output();
    25. };
    26. class Undergraduate : public Student
    27. {
    28. private:
    29. string speciality;
    30. public:
    31. virtual void input();
    32. virtual void output();
    33. };
    34. class Pupil : public Student
    35. {
    36. private:
    37. string school;
    38. int start_year;
    39. public:
    40. virtual void input();
    41. virtual void output();
    42. };
    43. class Interface
    44. {
    45. protected:
    46. public:
    47. Undergraduate dut[N];
    48. Pupil pup[N];
    49. int numU, numP;
    50. public:
    51. Interface();
    52. void Browse(int c);
    53. void Run(int c);
    54. void Input(int c);
    55. bool Search(int c);
    56. };
    57. // 成员函数定义
    58. void Student::output()
    59. {
    60. cout << "Name " << '\t';
    61. cout << "Number " << '\t';
    62. cout << "Sex " << '\t';
    63. cout << "Age " << '\t';
    64. }
    65. bool Student::operator==(string p)
    66. {
    67. return (name == p);
    68. }
    69. void Undergraduate::input()
    70. {
    71. cout << "Enter name: ";
    72. cin >> name;
    73. cout << "Enter number: ";
    74. cin >> number;
    75. cout << "Enter sex: ";
    76. cin >> sex;
    77. cout << "Enter age: ";
    78. cin >> age;
    79. cout << "Enter speciality: ";
    80. cin >> speciality;
    81. }
    82. void Undergraduate::output()
    83. {
    84. Student::output();
    85. cout << "Speciality " << '\n';
    86. }
    87. void Pupil::input()
    88. {
    89. cout << "Enter name: ";
    90. cin >> name;
    91. cout << "Enter number: ";
    92. cin >> number;
    93. cout << "Enter sex: ";
    94. cin >> sex;
    95. cout << "Enter age: ";
    96. cin >> age;
    97. cout << "Enter school: ";
    98. cin >> school;
    99. cout << "Enter start year: ";
    100. cin >> start_year;
    101. }
    102. void Pupil::output()
    103. {
    104. Student::output();
    105. cout << "School " << '\t';
    106. cout << "Start year " << '\n';
    107. }
    108. Interface::Interface()
    109. {
    110. numU = 0;
    111. numP = 0;
    112. }
    113. void Interface::Input(int c)
    114. {
    115. Student *p;
    116. switch (c)
    117. {
    118. case 1:
    119. if (numU == N)
    120. {
    121. cout << "The number of undergraduates is full." << endl;
    122. return;
    123. }
    124. p = &dut[numU];
    125. p->input();
    126. numU++;
    127. break;
    128. case 2:
    129. if (numP == N)
    130. {
    131. cout << "The number of pupils is full." << endl;
    132. return;
    133. }
    134. p = &pup[numP];
    135. p->input();
    136. numP++;
    137. }
    138. }
    139. void Interface::Browse(int c)
    140. {
    141. Student *p;
    142. int num;
    143. if (c == 1)
    144. num = numU;
    145. if (c == 2)
    146. num = numP;
    147. cout << "\n你要浏览的数据\n";
    148. if (num == 0)
    149. {
    150. cout << "没有数据。" << endl;
    151. return;
    152. }
    153. switch (c)
    154. {
    155. case 1:
    156. cout << "Name" << '\t' << "Number" << '\t' << "Sex" << '\t' << "Age" << '\t' << "Speciality" << endl;
    157. for (int i = 0; i < numU; i++)
    158. {
    159. p = &dut[i];
    160. p->output();
    161. }
    162. break;
    163. case 2:
    164. cout << "Name" << '\t' << "Number" << '\t' << "Sex" << '\t' << "Age" << '\t' << "School" << '\t' << "Start year" << endl;
    165. for (int i = 0; i < numP; i++)
    166. {
    167. p = &pup[i];
    168. p->output();
    169. }
    170. break;
    171. }
    172. }
    173. bool Interface::Search(int c)
    174. {
    175. string name;
    176. Student *p;
    177. int num, i;
    178. if (c == 1)
    179. num = numU;
    180. if (c == 2)
    181. num = numP;
    182. cout << "请输入要查询的姓名:";
    183. cin >> name;
    184. switch (c)
    185. {
    186. case 1:
    187. for (i = 0; i < num; i++)
    188. {
    189. p = &dut[i];
    190. if (*p == name)
    191. break;
    192. }
    193. break;
    194. case 2:
    195. for (i = 0; i < num; i++)
    196. {
    197. p = &pup[i];
    198. if (*p == name)
    199. break;
    200. }
    201. if (i == num)
    202. {
    203. cout << "没有找到该学生。" << endl;
    204. return false;
    205. }
    206. else
    207. p->output();
    208. return true;
    209. }
    210. }
    211. void Interface::Run(int c)
    212. {
    213. int choice;
    214. do
    215. {
    216. Display();
    217. cout << "请输入你的选择:";
    218. cin >> choice;
    219. switch (choice)
    220. {
    221. case 1:
    222. Input(c);
    223. break;
    224. case 2:
    225. Search(c);
    226. break;
    227. case 3:
    228. Browse(c);
    229. break;
    230. case 0:
    231. break;
    232. default:
    233. cout << "Error input." << endl;
    234. break;
    235. }
    236. } while (choice);
    237. }
    238. int main()
    239. {
    240. Interface interface;
    241. cout << "大学生信息管理 >>> " << endl;
    242. interface.Run(1);
    243. cout << "小学生信息管理 >>> " << endl;
    244. interface.Run(2);
    245. return 0;
    246. }

     多态性编程

    1. #include
    2. using namespace std;
    3. const double PI = 3.1415;
    4. class shape
    5. {
    6. public:
    7. virtual double volume() = 0;
    8. };
    9. class cylinder : public shape
    10. {
    11. private:
    12. double r, h;
    13. public:
    14. cylinder(double r, double h) : r(r), h(h) {}
    15. double volume()
    16. {
    17. return PI * r * r * h;
    18. }
    19. };
    20. class sphere : public shape
    21. {
    22. private:
    23. double r;
    24. public:
    25. sphere(double r) : r(r) {}
    26. double volume()
    27. {
    28. return 4.0 / 3.0 * PI * r * r * r;
    29. }
    30. };
    31. int main()
    32. {
    33. shape *p;
    34. double r, h;
    35. cin >> r >> h;
    36. cylinder cy(r, h);
    37. sphere sp(r);
    38. p = &cy;
    39. cout << p->volume() << endl;
    40. p = &sp;
    41. cout << p->volume() << endl;
    42. return 0;
    43. }

    运算符重载编程

    1. #include
    2. using namespace std;
    3. class Matrix
    4. {
    5. private:
    6. int row,col;
    7. int *m;
    8. public:
    9. Matrix(int r,int c):row(r),col(c)
    10. {
    11. m = new int[c*r];
    12. for (int i=0;i
    13. {
    14. for (int j=0;j
    15. cin>>m[i*col+j];
    16. }
    17. }
    18. Matrix()
    19. {
    20. m = new int[9];
    21. }
    22. void disp()
    23. {
    24. for(int i=0;i
    25. {
    26. for(int j=0;j
    27. cout<'\t';
    28. cout<
    29. }
    30. }
    31. Matrix operator+(Matrix &O)
    32. {
    33. if (col!=O.col || row!=O.row)
    34. {
    35. cout<<"program terminated!"<
    36. exit(0);
    37. }
    38. Matrix temp;
    39. temp.col=col;
    40. temp.row=row;
    41. for(int i=0;i
    42. for(int j=0;j
    43. temp.m[i*col+j] = m[i*col+j] + O.m[i*col+j];
    44. return temp;
    45. }
    46. Matrix operator=(Matrix D)
    47. {
    48. col=D.col;
    49. row=D.row;
    50. for (int i = 0; i
    51. {
    52. for (int j = 0; j
    53. m[i*col+j] = D.m[i*col+j];
    54. }
    55. return *this;
    56. }
    57. };
    58. int main()
    59. {
    60. int row_a,col_a,row_b,col_b;
    61. cin>>row_a>>col_a;
    62. Matrix A(row_a,col_a);
    63. cin>>row_b>>col_b;
    64. Matrix B(row_b,col_b),C;
    65. C = A + B;
    66. C.disp();
    67. cout<
    68. A = B;
    69. A.disp();
    70. return 0;
    71. }

  • 相关阅读:
    上海亚商投顾:沪指高开低走 钠离子电池、储能概念崛起
    Python中跨越多个文件使用全局变量
    设计模式之桥接模式
    AAAI2018-Spatial As Deep: Spatial CNN for Traffic Scene Understanding
    Windows版 PostgreSQL 利用 pg_upgrade 进行大版升级操作
    【地铁上的面试题】--基础部分--数据结构与算法--数组和链表
    定时器如何计算触发频率?
    spring boot整合mybatis,mybatis generator ,自定义typhandler
    【k8s源码篇之Informer篇3】理解Informer中的Reflector组件
    RabbitMQ 笔记
  • 原文地址:https://blog.csdn.net/2301_79018328/article/details/139586426