当前位置: 首页 > news >正文

C++:程序设计实例

 学生信息管理系统

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

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

#include <iostream>
#include <string>
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;}elsep->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 <iostream>
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<iostream>
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<row;i++){for (int j=0;j<col;j++)cin>>m[i*col+j];}}Matrix(){m = new int[9];}void disp(){for(int i=0;i<row;i++){for(int j=0;j<col;j++)cout<<m[i*col+j]<<'\t';cout<<endl;}}Matrix operator+(Matrix &O){if (col!=O.col || row!=O.row){cout<<"program terminated!"<<endl;exit(0);}Matrix temp;temp.col=col;temp.row=row;for(int i=0;i<row;i++)for(int j=0;j<col;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 <row; i++){	for (int j = 0; j <col; 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<<endl;A = B;A.disp();return 0;
}

相关文章:

  • 2024/06/11--代码随想录算法1/17|理论基础、509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
  • Spark的开发环境配置
  • LeakSearch:针对网络公开凭证的安全扫描与检测工具
  • 【设计模式】创建型设计模式之 建造者模式
  • 【机器学习】让计算机变得更加智能
  • IDEA创建Maven项目
  • 【设计模式】创建型设计模式之 工厂模式
  • 我要成为算法高手-双指针篇
  • 34.打印K型
  • Vue10-事件修饰符
  • React@16.x(25)useReducer
  • orbslam2代码解读(4):loopclosing回环检测线程
  • 从票务到游戏:Celestia 首届黑客松亮点项目盘点
  • 笨蛋学算法之LeetCodeHot100_2_字母异位词分组(Java)
  • 【机器学习理论基础】定量变量和定性变量
  • JavaScript-如何实现克隆(clone)函数
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 2017-08-04 前端日报
  • 4个实用的微服务测试策略
  • Angular6错误 Service: No provider for Renderer2
  • ES6 ...操作符
  • es6要点
  • hadoop集群管理系统搭建规划说明
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • log4j2输出到kafka
  • Median of Two Sorted Arrays
  • react 代码优化(一) ——事件处理
  • Redash本地开发环境搭建
  • Vue实战(四)登录/注册页的实现
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 爬虫模拟登陆 SegmentFault
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 线性表及其算法(java实现)
  • 1.Ext JS 建立web开发工程
  • Semaphore
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 数据可视化之下发图实践
  • ​学习一下,什么是预包装食品?​
  • #pragma multi_compile #pragma shader_feature
  • #QT(QCharts绘制曲线)
  • (1)(1.9) MSP (version 4.2)
  • (6)添加vue-cookie
  • (LeetCode C++)盛最多水的容器
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (分布式缓存)Redis分片集群
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (正则)提取页面里的img标签