C++构建MVC学生信息管理系统

一、头文件创建数据结构

student.h

cpp 复制代码
#pragma once
#include<vector>

struct student //定义student结构体 说明学生的基本信息
{	
	int id;
	char name[8];
	int age;
	int score;
};

class View {  //定义窗口类 涵盖基本功能和学生数据
private:
	int id = 1001; //初始学生id
	vector<student> students; //定义含有student结构体的向量students
	void add(); //定义add方法
	void show();
	void remove();
public:
	void list(); //定义控制端展示
};
二、构建方法

student.cpp

cpp 复制代码
#include<iostream>
#include<locale>

using namespace std;
#include"student.h"

void View::list(){
	while (true)
	{
		int num;
		cout << "学生信息管理系统!" << endl;
		cout << "1.添加信息" << endl;
		cout << "2.查看信息" << endl;
		cout << "3.删除信息" << endl;
		cout << "0.退出" << endl;
		cout << "请输入:";	cin >> num;
		if (num == 0) break;
		else if (num == 1) this->add();
		else if (num == 2) this->show();
		else if (num == 3) this->remove();
		else {
			cout << "输入有误,请重新输入!";
			continue;
		};
	}
}

void View::add() {
	while (true)
	{
		student stu;
		char name[9]; int age, score;
		cout << "请输入姓名,年龄,分数:"; cin >> name >> age >> score;
		stu.age = age; stu.score = score; stu.id = this->id;
		strcpy_s(stu.name, sizeof(stu.name), name);
		this->students.push_back(stu); // 使用 push_back 向 students vector 中添加元素student结构体
		this->id++;
		cout << "添加成功!" << endl;
		break;
	}

}
void View::show() {
	int length = this->students.size();
	for (int i = 0; i < length; i++)
	{
		cout << this->students[i].id << "\t";
		cout << this->students[i].name << "\t";
		cout << this->students[i].age << "\t";
		cout << this->students[i].score << endl;
	}
}

void View::remove() {
	int num;
	int flag;
	cout << "请输入需要删除的编号:"; cin >> num;
	int length = this->students.size();
	for (int i = 0; i < length; i++)
	{
		if (this->students[i].id == num)
		{
			this->students.erase(this->students.begin() + i);
			flag = 0;
			break;
		}else { flag = 1; };
	};
	if (flag == 1) {
		cout << "没有该学生" << endl;
	}else{
		cout << "删除成功!" << endl;
	}
}
三、主函数初始化窗口

main.cpp

cpp 复制代码
#include<locale>
#include <iostream>
using namespace std;
#include "student.h"



int main()
{
	View view;
	view.list();
}
相关推荐
007php0071 小时前
PHP与Java项目在服务器上的对接准备与过程
java·服务器·开发语言·分布式·面试·职场和发展·php
Evand J2 小时前
【MATLAB程序,一维非线性EKF与RTS】MATLAB,用于一维的位移与速度滤波和RTS平滑/高精度定位,带滤波前后的误差对比
开发语言·matlab·卡尔曼滤波·rts平滑·正向滤波
火云洞红孩儿7 小时前
告别界面孤岛:PyMe如何用一站式流程重塑Python GUI开发?
开发语言·python
叫我辉哥e17 小时前
新手进阶Python:办公看板集成ERP跨系统同步+自动备份+AI异常复盘
开发语言·人工智能·python
小丑西瓜6667 小时前
CMake基础用法,cmake_minimum_required,project,add_executable
linux·服务器·c++·camke
晚风吹长发7 小时前
初步了解Linux中的命名管道及简单应用和简单日志
linux·运维·服务器·开发语言·数据结构·c++·算法
fpcc7 小时前
设计心得——隔离隐藏的初步实践
c++
C++ 老炮儿的技术栈8 小时前
不调用C++/C的字符串库函数,编写函数strcpy
c语言·开发语言·c++·windows·git·postman·visual studio
布局呆星8 小时前
闭包与装饰器
开发语言·python
fyzy8 小时前
C++写后端实现,实现前后端分离
开发语言·c++