C++:程序设计实例

简介: 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;
        }
        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 <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;
}
目录
相关文章
|
4月前
|
存储 人工智能 算法
【一站式备考指南】一文掌握 C++ 程序设计 课程 知识点
【一站式备考指南】一文掌握 C++ 程序设计 课程 知识点
119 0
|
3月前
|
存储 C++
C++类的实例:Stock(股票)类。
C++类的实例:Stock(股票)类。
|
2月前
|
安全 编译器 C++
C++一分钟之-模板元编程实例:类型 traits
【7月更文挑战第15天】C++的模板元编程利用编译时计算提升性能,类型traits是其中的关键,用于查询和修改类型信息。文章探讨了如何使用和避免过度复杂化、误用模板特化及依赖特定编译器的问题。示例展示了`is_same`类型trait的实现,用于检查类型相等。通过`add_pointer`和`remove_reference`等traits,可以构建更复杂的类型转换逻辑。类型traits增强了代码效率和安全性,是深入C++编程的必备工具。
53 11
|
3月前
|
C++
C++ : 程序设计简单实例
C++ : 程序设计简单实例
36 3
|
3月前
|
C++
C++程序设计实践一上(题目来自杭州电子科技大学ACM)
C++程序设计实践一上(题目来自杭州电子科技大学ACM)
29 2
|
3月前
|
C++
C++程序设计实践一下(题目来自杭州电子科技大学ACM)
C++程序设计实践一下(题目来自杭州电子科技大学ACM)
35 1
|
3月前
|
存储 JavaScript 前端开发
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
|
3月前
|
域名解析 网络协议 程序员
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
48 0
|
3月前
|
C++
技术经验分享:C++程序设计的技巧
技术经验分享:C++程序设计的技巧
30 0
|
3月前
|
域名解析 网络协议 程序员
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
43 0