C++:程序设计实例

发布于:2024-06-18 ⋅ 阅读:(20) ⋅ 点赞:(0)

 学生信息管理系统

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

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

#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;
}


网站公告

今日签到

点亮在社区的每一天
去签到