仅供参考,请不要直接用于实际用途
缺陷:对输入变量的管理存在漏洞
- #include
- #include
- using namespace std;
-
- const int N = 20;
-
- // 定义普通函数
- void Display()
- {
- cout << "\n************0.退 出*************" << endl;
- cout << "************1.录入信息*************" << endl;
- cout << "************2.查询信息*************" << endl;
- cout << "************3.浏览信息*************" << endl;
- }
-
- // 定义类
- class Student
- {
- protected:
- string name;
- int number;
- char sex;
- int age;
-
- public:
- bool operator==(string p);
- virtual void input() = 0;
- virtual void output();
- };
-
- class Undergraduate : public Student
- {
- private:
- string speciality;
-
- public:
- virtual void input();
- virtual void output();
- };
-
- class Pupil : public Student
- {
- private:
- string school;
- int start_year;
-
- public:
- virtual void input();
- virtual void output();
- };
-
- class Interface
- {
- protected:
- public:
- Undergraduate dut[N];
- Pupil pup[N];
- int numU, numP;
-
- public:
- Interface();
- void Browse(int c);
- void Run(int c);
- void Input(int c);
- bool Search(int c);
- };
-
- // 成员函数定义
- void Student::output()
- {
- cout << "Name " << '\t';
- cout << "Number " << '\t';
- cout << "Sex " << '\t';
- cout << "Age " << '\t';
- }
-
- bool Student::operator==(string p)
- {
- return (name == p);
- }
-
- void Undergraduate::input()
- {
- cout << "Enter name: ";
- cin >> name;
- cout << "Enter number: ";
- cin >> number;
- cout << "Enter sex: ";
- cin >> sex;
- cout << "Enter age: ";
- cin >> age;
- cout << "Enter speciality: ";
- cin >> speciality;
- }
-
- void Undergraduate::output()
- {
- Student::output();
- cout << "Speciality " << '\n';
- }
-
- void Pupil::input()
- {
- cout << "Enter name: ";
- cin >> name;
- cout << "Enter number: ";
- cin >> number;
- cout << "Enter sex: ";
- cin >> sex;
- cout << "Enter age: ";
- cin >> age;
- cout << "Enter school: ";
- cin >> school;
- cout << "Enter start year: ";
- cin >> start_year;
- }
-
- void Pupil::output()
- {
- Student::output();
- cout << "School " << '\t';
- cout << "Start year " << '\n';
- }
-
- Interface::Interface()
- {
- numU = 0;
- numP = 0;
- }
-
- void Interface::Input(int c)
- {
- Student *p;
- switch (c)
- {
- case 1:
- if (numU == N)
- {
- cout << "The number of undergraduates is full." << endl;
- return;
- }
- p = &dut[numU];
- p->input();
- numU++;
- break;
- case 2:
- if (numP == N)
- {
- cout << "The number of pupils is full." << endl;
- return;
- }
- p = &pup[numP];
- p->input();
- numP++;
- }
- }
-
- void Interface::Browse(int c)
- {
- Student *p;
- int num;
- if (c == 1)
- num = numU;
- if (c == 2)
- num = numP;
- cout << "\n你要浏览的数据\n";
-
- if (num == 0)
- {
- cout << "没有数据。" << endl;
- return;
- }
- switch (c)
- {
- case 1:
- cout << "Name" << '\t' << "Number" << '\t' << "Sex" << '\t' << "Age" << '\t' << "Speciality" << endl;
- for (int i = 0; i < numU; i++)
- {
- p = &dut[i];
- p->output();
- }
- break;
- case 2:
- cout << "Name" << '\t' << "Number" << '\t' << "Sex" << '\t' << "Age" << '\t' << "School" << '\t' << "Start year" << endl;
- for (int i = 0; i < numP; i++)
- {
- p = &pup[i];
- p->output();
- }
- break;
- }
- }
-
- bool Interface::Search(int c)
- {
- string name;
- Student *p;
- int num, i;
- if (c == 1)
- num = numU;
- if (c == 2)
- num = numP;
-
- cout << "请输入要查询的姓名:";
- cin >> name;
-
- switch (c)
- {
- case 1:
- for (i = 0; i < num; i++)
- {
- p = &dut[i];
- if (*p == name)
- break;
- }
- break;
- case 2:
- for (i = 0; i < num; i++)
- {
- p = &pup[i];
- if (*p == name)
- break;
- }
- if (i == num)
- {
- cout << "没有找到该学生。" << endl;
- return false;
- }
- else
- p->output();
- return true;
- }
- }
-
- void Interface::Run(int c)
- {
- int choice;
- do
- {
- Display();
- cout << "请输入你的选择:";
- cin >> choice;
- switch (choice)
- {
- case 1:
- Input(c);
- break;
- case 2:
- Search(c);
- break;
- case 3:
- Browse(c);
- break;
- case 0:
- break;
- default:
- cout << "Error input." << endl;
- break;
- }
- } while (choice);
- }
-
- int main()
- {
- Interface interface;
- cout << "大学生信息管理 >>> " << endl;
- interface.Run(1);
- cout << "小学生信息管理 >>> " << endl;
- interface.Run(2);
-
- return 0;
- }
- #include
- using namespace std;
-
- const double PI = 3.1415;
-
- class shape
- {
- public:
- virtual double volume() = 0;
- };
-
- class cylinder : public shape
- {
- private:
- double r, h;
- public:
- cylinder(double r, double h) : r(r), h(h) {}
- double volume()
- {
- return PI * r * r * h;
- }
- };
-
- class sphere : public shape
- {
- private:
- double r;
-
- public:
- sphere(double r) : r(r) {}
- double volume()
- {
- return 4.0 / 3.0 * PI * r * r * r;
- }
- };
-
-
- int main()
- {
-
- shape *p;
- double r, h;
- cin >> r >> h;
- cylinder cy(r, h);
- sphere sp(r);
- p = &cy;
- cout << p->volume() << endl;
- p = &sp;
- cout << p->volume() << endl;
- return 0;
- }
- #include
- using namespace std;
- class Matrix
- {
- private:
- int row,col;
- int *m;
- public:
- Matrix(int r,int c):row(r),col(c)
- {
- m = new int[c*r];
- for (int i=0;i
|
- {
- for (int j=0;j
- cin>>m[i*col+j];
- }
- }
- Matrix()
- {
- m = new int[9];
- }
- void disp()
- {
- for(int i=0;i
|
- {
- for(int j=0;j
- cout<
'\t'; - cout<
- }
- }
- Matrix operator+(Matrix &O)
- {
- if (col!=O.col || row!=O.row)
- {
- cout<<"program terminated!"<
- exit(0);
- }
- Matrix temp;
- temp.col=col;
- temp.row=row;
- for(int i=0;i
|
- for(int j=0;j
- temp.m[i*col+j] = m[i*col+j] + O.m[i*col+j];
- return temp;
- }
- Matrix operator=(Matrix D)
- {
- col=D.col;
- row=D.row;
- for (int i = 0; i
|
- {
- for (int j = 0; j
- m[i*col+j] = D.m[i*col+j];
- }
- return *this;
- }
-
- };
- int main()
- {
- int row_a,col_a,row_b,col_b;
- cin>>row_a>>col_a;
- Matrix A(row_a,col_a);
- cin>>row_b>>col_b;
- Matrix B(row_b,col_b),C;
- C = A + B;
- C.disp();
- cout<
- A = B;
- A.disp();
- return 0;
- }
-
相关阅读:
上海亚商投顾:沪指高开低走 钠离子电池、储能概念崛起
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