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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在C程序设计案例分析中,通常会涉及许多问题和解决方案。程序设计案例分析是一种通过分析实际问题,并运用C编程语言来解决问题的过程。 首先,在设计一个C程序之前,我们需要充分理解问题的需求和约束条件。这可以通过与客户、用户或相关利益相关者进行有效的沟通来实现。理解问题的根本需求对于正确设计和实现程序非常重要。 其次,通过分析问题,我们可以确定程序所需的输入和输出。这有助于定义程序的接口和功能。在C程序设计中,我们可以使用不同的数据类型来表示输入和输出。例如,整数、字符、浮点数、结构体等。 然后,我们可以开始设计程序的算法。在C编程中,我们可以使用各种语句、控制结构和函数来实现所需的功能。数据结构的选择也是很重要的,因为它们可以帮助我们高效地组织和处理数据。 在设计完成后,我们可以着手编写代码。在编写C程序时,我们需要注意代码的可读性和可维护性。良好的代码注释和模块化设计可以帮助其他程序员更容易地理解和修改代码。 编写完代码后,我们需要进行测试来确保程序的正确性和健壮性。测试可以通过提供不同的输入和检查输出来实现。对于复杂的程序,我们还可以使用调试器来帮助我们找到潜在的错误和问题。 最后,一旦测试通过,我们可以将程序部署到实际环境中。在部署过程中,我们需要考虑到程序的性能、可扩展性和安全性等方面的问题。 综上所述,C程序设计案例分析是一个综合性的过程,涉及需求分析、设计、编码、测试和部署等多个环节。正确理解问题的需求、合理设计算法、编写高质量的代码以及进行有效的测试是确保程序成功运行的关键。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tsglz3210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值