C++学生信息管理系统(精简版)+读写txt功能

不想复制的:源代码下载地址

精选几张运行结果图:
主界面
在这里插入图片描述

需求分析:在这里插入图片描述
完成结果:

文件:
1.student.h

#include<string>
#include <iostream>
using namespace std;
class Student{
public:
	int stunumber = 0;//学号 
	string name="";//这是名字 
	int math;//数学成绩 
	int Chinese; //语文成绩 
	Student(){		//构造函数 
	}
	void SetStudent(int a,string name,int m,int c){//设置成员变量的内容 
		this->stunumber=a;
		this->name=name;
		this->math=m;
		this->Chinese=c;
	}
	int getstunumber(){//得到学号 
		return this->stunumber;
	};
	
};



直接复制粘贴即可

2.main.cpp

#include <iostream>
#include "student.h"
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv) {
	int num=-1;
	Student stu[10];//暂时定义10个学生,后续可用变长数组搞定。 
	int numer;
	string name;
	int math;
	int Chinese;
	
	
	while (num!=0){
		
		system("cls");
		printf("********************欢迎使用学生信息管理系统**********************\n"); 
		printf("*                ---------------------------------               *\n");
		printf("*                |        Powered *晓晴         |                *\n");
		printf("*                ---------------------------------               *\n");
		printf("*                                                                *\n");
		printf("*                1)添加学生信息                                  *\n");
		printf("*                2)删除学生信息                                  *\n");
		printf("*                3)修改学生信息                                  *\n");
		printf("*                4)查找学生信息                                  *\n");
		printf("*                5)显示所有学生信息                              *\n");
		printf("*                6)按成绩排序学生信息                            *\n");
		printf("*                7)清空所有信息                                  *\n");
		printf("*                8)学生信息存盘                                  *\n");
		printf("*                9)读出存盘信息                                  *\n");
		printf("*                0)退出软件                                      *\n");
		printf("*                                                                *\n");
		printf("******************************************************************\n");
		cin>>num;
		switch (num)
		{
			case 1:{
				cout << "请输入学生信息" << endl;
				cout << "学生学号:";
				cin >> numer;
				cout << "学生姓名:";
				cin >> name;
				cout << "学生数学成绩:";
				cin >> math;
				cout << "学生语文成绩:";
				cin >> Chinese; 
				int j=1;
				for(int i = 0 ; i <10 ;i++){
					j=stu[i].getstunumber();
					if(j == 0){
						stu[i].SetStudent(numer,name,math,Chinese);
						break;
					} 
				}
				cout << "添加成功" << endl;
				system("pause");
				break;
			}
			case 2:{
				cout << "请输入要删除的学生学号" << endl;
				cin>>numer;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber=numer){
						int selected;
						cout<<"请问你是要删除学生姓名为";
						cout<<stu[i].name<<endl;
						cout<<"1.是  2.否"<<endl;
						cin>>selected;
						if(selected=1){
							stu[i].stunumber=0;
							stu[i].name="";
							stu[i].math=0;
							stu[i].Chinese=0;
							break;
						}else{
							break;
						}
					}
					
				
				}
				
				break;
			}
			case 3:{
				int number;
				int selected; 
				cout<<"请输入要修改的学生学号"<<endl;
				cin>>number;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber=number){
						number=i;
						break;
					}
				}
				cout<<"选择要修改的信息"<<endl;
				cout<<"1.修改姓名2.修改数学成绩3.修改语文成绩"<<endl; 
				cin>>selected;
				if(selected ==1){
					string name;
					cout<<"请输入姓名"<<endl;
					cin>>num;
					stu[number].name=name;
				}
				else if(selected ==2){
					int grade;
					cout<<"请输入修改后的成绩"<<endl;
					cin>>grade;
					stu[number].math=grade;
				}
				else if(selected ==3){
					int grade;
					cout<<"请输入修改后的成绩"<<endl;
					cin>>grade;
					stu[number].Chinese=grade;
				}
				break;
			}
			case 4:{
				int number;
				int selected; 
				cout<<"请输入要查找的学生学号"<<endl;
				cin>>number;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber=number){
						number=i;
						break;
					}
				}
				cout<<"学号  姓名  数学成绩  语文成绩"<<endl;
						cout<<" ";
						cout<<stu[number].stunumber;
						cout<<"      ";
						cout<<stu[number].name;
						cout<<"      ";
						cout<<stu[number].math;
						cout<<"          ";
						cout<<stu[number].Chinese;
						cout<<"      "<<endl;
						system("pause");
				break;
			}			
			case 5:{
				
				cout<<"学号  姓名  数学成绩  语文成绩"<<endl;
				for(int i = 0 ; i <10 ;i++){//遍历所有学生的类 
					if(stu[i].stunumber != 0){
						cout<<" ";
						cout<<stu[i].stunumber;
						cout<<"      ";
						cout<<stu[i].name;
						cout<<"      ";
						cout<<stu[i].math;
						cout<<"          ";
						cout<<stu[i].Chinese;
						cout<<"      "<<endl;
					}
				}
				system("pause");
				break;
			}
			case 6:{
				int j=0;
				cout<<"按总成绩排序如下"<<endl;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber != 0){
						j++;
					}
				}
				for(int i = 0 ; i <j ;i++){
					int grade;
					int grade2;
					Student temp;
					grade = stu[i].math+stu[i].Chinese;
					grade2=stu[i+1].math+stu[i+1].Chinese;
					if(grade<grade2){
						temp=stu[i];
						stu[i]=stu[i+1];
						stu[i+1]=temp;
					}
				}
				cout<<"学号  姓名  数学成绩  语文成绩"<<endl;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber != 0){
						cout<<" ";
						cout<<stu[i].stunumber;
						cout<<"      ";
						cout<<stu[i].name;
						cout<<"      ";
						cout<<stu[i].math;
						cout<<"          ";
						cout<<stu[i].Chinese;
						cout<<"      "<<endl;
					}
				}
				system("pause");
				break;
				}
			case 7:{
				for(int i = 0 ; i <10 ;i++){
					stu[i].stunumber=0;
					stu[i].name="";
					stu[i].math=0;
					stu[i].Chinese=0;
				}
				system("pause");
				cout<<"清除完毕";
				break;
			}
			case 8:{
				int j=0;
				for(int i = 0 ; i <10 ;i++){
					if(stu[i].stunumber != 0){
						j++;
					}
				}
				ofstream file("student.txt");//打开并且创建运行目录下的txt文件 
				for(int i = 0 ; i <j ;i++){
					file<<stu[i].stunumber;
					file<<",";
					file<<stu[i].name;
					file<<",";
					file<<stu[i].math;
					file<<",";
					file<<stu[i].Chinese;
					file<<endl;
				}
				system("pause");
				cout<<"存盘成功";
				break;
			}
			case 9:{
				int j =0; 
				int i =0;
				ifstream fin("student.txt"); //打开这个文本文件 
			    const int LINE_LENGTH = 100; //假设一行 有100个字符 
			    char str[LINE_LENGTH];  
			    const char *d = ",";
			    char *p;
			    while(fin.getline(str,LINE_LENGTH) ) //从txt文件里读出来一行文本数据。 
			    {    
			        cout << "从磁盘中读取数据: " << str << endl;
			    }
			   
				cout<<"读取成功";
				
				
				system("pause");
				
				break;
				
			}
			
		}//while 循环括号 

	}
	cout<<endl; 
	cout<<"已经退出系统了";
	return 0;
}

白嫖帮忙点个赞,写的不容易,新手。感谢!

  • 21
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值