首页 > 其他分享 >cpp: Struct Simple

cpp: Struct Simple

时间:2023-05-08 23:25:15浏览次数:26  
标签:Struct Simple 老师 int vector cpp include fin id

 

// DuStudent.h : 此文件包含 "DuStudent" 类。策略模式  Strategy Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 

#pragma once
#ifndef DUSTUDENT_H 
#define DUSTUDENT_H 

#include <cstring>
#include <stdbool.h>
#include <stdlib.h>
#include <iostream>
#include <malloc.h>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <string.h>
#include <list>


using namespace std;

namespace DuStructSimple
{

    /// <summary>
    /// 学生
    /// </summary>
    struct duStudent
    {
        /// <summary>
        /// ID
        /// </summary>
        //int id;
        /// <summary>
        /// 學號  主键
        /// </summary>
        int number;
        /// <summary>
        /// 姓名
        /// </summary>
        string name;
        /// <summary>
        /// 总学分數
        /// </summary>
        float score;
        /// <summary>
        /// 
        /// </summary>
       // duStudent() = default;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="num"></param>
        /// <param name="scor"></param>
        //duStudent(int snum, string sname, float scor) :number(snum), name(sname), score(scor){}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stuObj"></param>
        /// <returns></returns>
        bool operator<(const duStudent& stuObj) const
        {
            return score < stuObj.score;
        }


    };
    /// <summary>
    /// 老师
    /// </summary>
    struct  Teacher
    {

        /// <summary>
        /// ID  主键
        /// </summary>
        int id;
        /// <summary>
        /// 老師姓名
        /// </summary>
        string  name;

        /// <summary>
        /// 學生
        /// </summary>
        //dustudent tstu;
    };
    /// <summary>
    /// 课程
    /// </summary>
    struct Course
    {
        /// <summary>
        /// ID 主键
        /// </summary>
        int id;
        /// <summary>
        /// 课程名稱
        /// </summary>
        string name;

        /// <summary>
        /// 课程老師
        /// </summary>
        //teacher teach;
        /// <summary>
        /// 学生所报此课程
        /// </summary>
        //dustudent costu;
    };


    /// <summary>
    /// 课程授课老师
    /// </summary>
    struct  CreditCourse
    {
        /// <summary>
        /// 课程ID 外键
        /// </summary>
        int courseId;
        /// <summary>
        /// 课程老师外健
        /// </summary>
        int teacherId;

    };

    /// <summary>
    /// 班级
    /// </summary>
    struct Class
    {
        /// <summary>
        /// 主键
        /// </summary>
        int id;
        /// <summary>
        /// 班级名称
        /// </summary>
        string  name;
        /// <summary>
        /// 班主任老师 ID 外键 其实这个还可以另外优化,组建另一个对象
        /// </summary>
        //int teachId;

        /// <summary>
        /// 班主任老师
        /// </summary>
        //teacher teach;
        /// <summary>
        /// 學生
        /// </summary>
        duStudent stu;
        /// <summary>
        /// 
        /// </summary>
        list<duStudent> stus;
        /// <summary>
        /// 
        /// </summary>
        vector<duStudent> listu;
        /// <summary>
        /// 
        /// </summary>
        Class* next;
        //只能指同类型的指针 不然会"爆"  //为什么next要放这里  因为next所指的东西在Class里面
    };

    /// <summary>
    /// 班主任表
    /// </summary>
    struct ClassHead
    {
        /// <summary>
        /// 
        /// </summary>
        int classId;
        /// <summary>
        /// 
        /// </summary>
        int teachId;
    };


    /// <summary>
    /// 班级学生表
    /// </summary>
    struct ClassMember
    {
        /// <summary>
        ///班级  外键
        /// </summary>
        int classId;

        /// <summary>
        /// 学生ID  外键
        /// </summary>
        int clstuId;

        
    };


    /// <summary>
    /// 学生成绩单
    /// </summary>
    struct StudentReport
    {

        /// <summary>
        /// 学生编号 外键
        /// </summary>
        int stuNumber;
        /// <summary>
        /// 课程编号 外键
        /// </summary>
        int courseId;

        /// <summary>
        /// 课程成绩
        /// </summary>
        float courseScore;
        /// <summary>
        /// 学生
        /// </summary>
        //dustudent rstu;

        /// <summary>
        /// 课程
        /// </summary>
        //course rcourse;
    };


 



}
#endif


// ManageStudent.h: 此文件包含 "ManageStudent" 类。当作视图式结构体 策略模式  Strategy Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 

#pragma once
#ifndef MANAGESTUDENT_H 
#define MANAGESTUDENT_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <array>
#include <functional>
#include <list>


using namespace std;

namespace DuStructSimple
{

	/// <summary>
	/// 班主任老师表 ClassHeadTeacher
	/// 班级ID ,班级名称,班主任老师名称
	/// </summary>
	struct ClassHeadTeacher
	{

		/// <summary>
		/// 
		/// </summary>
		int teacherId;

		/// <summary>
		/// 
		/// </summary>
		string className;

		/// <summary>
		/// 
		/// </summary>
		string teacherName;

		bool operator<(const ClassHeadTeacher& stuObj) const
		{
			return teacherId < stuObj.teacherId;
		}
			
	};
	/// <summary>
	/// 授老师课表程
	/// 老师名称,课程名称,
	/// </summary>
	struct CreditCourseTeacher
	{
		/// <summary>
		/// 
		/// </summary>
		int teacherId;
		/// <summary>
		/// 
		/// </summary>
		int courceId;
		/// <summary>
		/// 
		/// </summary>
		string teacherName;
		/// <summary>
		/// 
		/// </summary>
		string CourceName;

		bool operator<(const CreditCourseTeacher& stuObj) const
		{
			return teacherId < stuObj.teacherId;
		}

	};
	/// <summary>
	/// 授课老师学生成绩表
	/// 学生ID,学生姓名,班级名称,相关课程名称(分数),老师名称
	/// </summary>
	struct CreditTeacherStudent
	{

		/// <summary>
		/// 学生ID
		/// </summary>
		int studentId;
		/// <summary>
		/// 学生姓名
		/// </summary>
		string studentName;
		/// <summary>
		/// 班级名称
		/// </summary>
		string className;
		/// <summary>
		/// 课程ID
		/// </summary>
		int courceId;
		/// <summary>
		/// 相关课程名称
		/// </summary>
		string CourceName;
		/// <summary>
		/// 相关课程分数
		/// </summary>
		float CourceScroe;
		/// <summary>
		/// 老师名称
		/// </summary>
		string teacherName;


		bool operator<(const CreditTeacherStudent& stuObj) const
		{
			return CourceScroe < stuObj.CourceScroe;
		}

	};
	/// <summary>
	/// 学生成绩单 详情分数和总分数课目 老师课表
	/// 学生ID,学生姓名,班级名称,语文(分数),数学,英语,总分数
	/// </summary>
	struct StudentScore
	{
		/// <summary>
		/// 学生ID
		/// </summary>
		int studentId;
		/// <summary>
		/// 学生姓名
		/// </summary>
		string studentName;
		/// <summary>
		/// 班级名称
		/// </summary>
		string className;
		/// <summary>
		/// 语文
		/// </summary>
		float Chinese;
		/// <summary>
		/// 语文授老师
		/// </summary>
		string ChineseTeacher;
		/// <summary>
		/// 数学
		/// </summary>
		float math;
		/// <summary>
		/// 数学授老师
		/// </summary>
		string mathTeacher;
		/// <summary>
		/// 英语
		/// </summary>
		float English;
		/// <summary>
		/// 英语授课老师
		/// </summary>
		string EnglishTeacher;
		/// <summary>
		/// 总分数
		/// </summary>
		float totalScore;

		int rank=1;

		bool operator<(const StudentScore& stuObj) const
		{
			return totalScore < stuObj.totalScore;
		}

	};



}
#endif

  

 

// GeovinDu.h : 此文件包含 "GeovinDu" 类。策略模式  Strategy Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 

#pragma once
#ifndef GEOVINDU_H 
#define GEOVINDU_H 



#include <iostream>
#include <sstream>
#include <vector>
#include "DuStudent.h"
#include "ManageStudent.h"


using namespace std;

namespace DuStructSimple
{




	/// <summary>
	/// 
	/// </summary>
	class GeovinDu
	{

	private:


	public:
		/// <summary>
		/// 
		/// </summary>
		void displayStudent();
		/// <summary>
		/// 
		/// </summary>
		void dispalyStructDemo();
		/// <summary>
		/// 
		/// </summary>
		void dsipalyFuntion();
		/// <summary>
		/// 
		/// </summary>
		void dipslayStructCustomer();
		/// <summary>
		/// 统计分数
		/// </summary>
		/// <param name="stuArry"></param>
		/// <param name="reportArry"></param>
		vector<duStudent> getSumScore(vector<duStudent> stuArry, vector<StudentReport> reportArry);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="a"></param>
		/// <param name="b"></param>
		/// <returns></returns>
		//bool compareTwoStudents(StudentScore a, StudentScore b);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="a"></param>
		/// <param name="n"></param>
		//void computeRanks(StudentScore a[], int n);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="a"></param>
		/// <param name="n"></param>
		void computeRanksVector(vector<StudentScore> a, int n);
		/// <summary>
		/// 班主任老师表
		/// </summary>
		/// <param name="calssArry"></param>
		/// <param name="headArry"></param>
		/// <param name="teachArry"></param>
		/// <returns></returns>
		vector<ClassHeadTeacher> getClassHeadTeacher(vector<Class> calssArry, vector<ClassHead> headArry, vector<Teacher> teachArry);
		/// <summary>
		/// 授课老师表
		/// </summary>
		/// <param name="courseArry"></param>
		/// <param name="creditArry"></param>
		/// <param name="teachArry"></param>
		/// <returns></returns>
		vector<CreditCourseTeacher> getCreditCourseTeacher(vector<Course> courseArry, vector<CreditCourse> creditArry, vector<Teacher> teachArry);


		/// <summary>
		/// 授课老师学生成绩表
		/// </summary>
		/// <param name="creditArry"></param>
		/// <param name="stuArry"></param>
		/// <param name="reportArry"></param>
		/// <param name="calssArry"></param>
		/// <param name="memberArry"></param>
		/// <param name="courseName"></param>
		/// <returns></returns>
		vector<CreditTeacherStudent> getCreditTeacherStudent(vector<CreditCourseTeacher> creditArry, vector<duStudent> stuArry, vector<StudentReport> reportArry, vector<Class> calssArry, vector<ClassMember> memberArry, string courseName);


		/// <summary>
		/// 学生成绩单 详情分数和总分数课目
		/// </summary>
		/// <param name="stuArry"></param>
		/// <param name="reportArry"></param>
		/// <param name="calssArry"></param>
		/// <param name="memberArry"></param>
		/// <param name="courseArry"></param>
		/// <param name="creditArry"></param>
		/// <returns></returns>
		vector<StudentScore> getStudentScore(vector<duStudent> stuArry, vector<StudentReport> reportArry, vector<Class> calssArry, vector<ClassMember> memberArry, vector<CreditCourseTeacher> creditArry);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="stuid"></param>
		/// <param name="courseid"></param>
		/// <param name="reportArry"></param>
		/// <returns></returns>
		float  getScore(int stuid, int courseid, vector<StudentReport> reportArry);

		/// <summary>
		/// 得到课程老师
		/// </summary>
		/// <param name="courseid"></param>
		/// <param name="creditArry"></param>
		/// <returns></returns>
		string getTeacher(int courseid, vector<CreditCourseTeacher> creditArry);
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		vector<duStudent> ReadFileStu();
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		vector<Class> ReadFileClass();

		/// <summary>
		/// 老师
		/// </summary>
		/// <returns></returns>
		vector<Teacher> ReadFileTeach();



		/// <summary>
		/// 课程
		/// </summary>
		/// <returns></returns>
		vector<Course> ReadFileCourse();

		/// <summary>
		/// 老师课程表
		/// </summary>
		/// <returns></returns>
		vector<CreditCourse> ReadFileCreditCourse();

		/// <summary>
		/// 班主任
		/// </summary>
		/// <returns></returns>
		vector<ClassHead> ReadFileClassHead();

		/// <summary>
		/// 班级学生表
		/// </summary>
		/// <returns></returns>
		vector<ClassMember> ReadFileClassMember();

		/// <summary>
		 /// 学生成绩表
		 /// </summary>
		 /// <returns></returns>
		vector<StudentReport> ReadFileStudentReport();


	};

}
#endif

// GeovinDu.h : 此文件包含 "GeovinDu" 类。策略模式  Strategy Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 
/**
 * 一 函数
1.函数免的命名规范-标识符
2.函数调用-函数名()
3.函数传参-形参 实参 
传参方式:值传递:形参无法修饰实参
          指针传递:形参可以修饰实参
4.函数的样式:void( )/int ()/void(**)/int (**)
5.声明可以多次,定义只能一次
6.头文件.h 。Cpp
二指针
1.指针编号
2.地址编号计算
3.变量地址 &变量名
4.指针赋值:两种方式 变量的编号/指针 int * p=&
5.指针使用 *p p &p区别。
6.Cost 指针 const int *p
7.Void * 
8.指针数组 *p=arr 第一个元素。
三结构体

关键字struct 结构体名 变量
定义
struct 结构体名
{
列表
};
创建结构体2
struct 结构体名
{
列表
}创建结构体;

结构体名 变量

变量.列表
 * 
 * .
 */


#include "GeovinDu.h"


#include <iostream>
#include <fstream>//文件操作
#include <sstream>//int转string
#include <iomanip>//cout格式化输出 setw()

#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <stdlib.h>
#include "student.h"
#include "node.h"
#include "list.h"
#include "DuOperation.h"
#include "Dufunction.h"

using namespace std;


#define stuPath "stuArrys.txt"
#define claPath "claArry.txt"
#define teachPath "teachArry.txt"
#define coutPath "couArry.txt"
#define crePath "creArry.txt"
#define headPath "headArry.txt"
#define memberPath "memberAryy.txt"
#define reportPath "reportArrys.txt"


namespace DuStructSimple
{






    /// <summary>
    /// 
    /// </summary>
    void GeovinDu::dipslayStructCustomer()
    {

        //学生
        //duStudent stuArry[5] = { {100,"小李",0},{101,"王二",0},{102,"赵三",0},{103,"张五",0},{104,"何六",0}};
        //stu.name = "小李";
        //stu.number = 100;
        //vector<duStudent> stuArrys= { {100,"小李",0},{101,"王二",0},{102,"赵三",0},{103,"张五",0},{104,"何六",0} };
        vector<duStudent> stuArrys = ReadFileStu();


        //班级
        //Class claArry[2] = { {1,"一年级一班"},{2,"一年级二班"}};
        //vector<Class> claArry = { {1,"一年级一班"},{2,"一年级二班"} };

        vector<Class> claArry = ReadFileClass();
        //cla.id = 1;
        //cla.name = "一年级一班";

        //老师
        //Teacher teachArry[3]={ {1,"吕老师"},{2,"吴老师"},{3,"林老师"}};
        //vector<Teacher> teachArry = { {1,"吕老师"},{2,"吴老师"},{3,"林老师"} };

        vector<Teacher> teachArry = ReadFileTeach();

        //teach.id = 1;
        //teach.name = "李老师";

        //课程
        //Course couArry[3]= { {1,"语文"},{2,"数学"},{3,"英语"} };
        //vector<Course> couArry = { {1,"语文"},{2,"数学"},{3,"英语"} };

        vector<Course> couArry = ReadFileCourse();

        //cou.id = 1;
        //cou.name = "语文";

        //授课表(三个老师)
        //CreditCourse creArry[3] = { {1,2},{2,1},{3,3} };

         //vector<CreditCourse> creArry = { {1,2},{2,1},{3,3} };

        vector<CreditCourse> creArry = ReadFileCreditCourse();

        //班主任,这只有一个班
        //ClassHead headArry[2] = { { 1,3 },{2,5} };
       // vector<ClassHead> headArry = { { 1,3 },{2,5} };
        vector<ClassHead> headArry = ReadFileClassHead();

        //head->classId = 1;
        //head->teachId = 3;

        //学生班级表
        //ClassMember memberAryy[5] = { {1,100},{1,101},{1,102},{1,103},{1,104} };

        //vector<ClassMember> memberAryy = { {1,100},{1,101},{1,102},{1,103},{1,104} };
        vector<ClassMember> memberAryy = ReadFileClassMember();


        //学生成绩表
        //StudentReport reportArry[15] = { {100,1,50},{100,2,80},{100,3,95},{101,1,78},{101,2,99},{101,3,100} ,{102,1,55},{102,2,60},{102,3,78},{103,1,92},{103,2,90},{103,3,99},{104,1,78},{104,2,65},{104,3,50} };
        

       // vector<StudentReport> reportArrys= { {100,1,50},{100,2,80},{100,3,95},{101,1,78},{101,2,99},{101,3,100} ,{102,1,55},{102,2,60},{102,3,78},{103,1,92},{103,2,90},{103,3,99},{104,1,78},{104,2,65},{104,3,50} };
        
        
       vector<StudentReport> reportArrys = ReadFileStudentReport();
    
        //for (auto sr : reportArry)
        //{
        //    reportArrys.push_back(sr);

        //}
        vector<duStudent> newstu=getSumScore(stuArrys, reportArrys);
        //排序
        sort(newstu.begin(), newstu.end());
        cout << "\t学号" << "\t姓名" << "\t总分数" << endl;
        for (auto stu : newstu)
        {
            cout<<"\t"<<stu.number << "\t" << stu.name << "\t" << stu.score << endl;
        }
        vector<ClassHeadTeacher> newhead = getClassHeadTeacher(claArry, headArry, teachArry);  
        cout << "\t班级名称" << "\t老师ID" << "\t班主任姓名" << endl;
        for (auto head : newhead)
        {
            cout << "\t" << head.className << "\t" << head.teacherId << "\t" << head.teacherName << endl;
        }

        vector<CreditCourseTeacher> newcourseteach = getCreditCourseTeacher(couArry, creArry, teachArry);
        cout << "\t课程名称" << "\t老师Id" << "\t老师姓名" << endl;
        for (auto cousreteach : newcourseteach)
        {
            cout << "\t" << cousreteach.CourceName << "\t\t" << cousreteach.teacherId << "\t" << cousreteach.teacherName << endl;
        }

        string courcename = "吕老师";//语文
        vector<CreditTeacherStudent> newcoursestu = getCreditTeacherStudent(newcourseteach, stuArrys, reportArrys, claArry, memberAryy, courcename);
        sort(newcoursestu.begin(), newcoursestu.end());
        cout << "\t年级班级" << "\t课程名称" << "\t学分" << "\t学生姓名" << "\t学号" << "\t老师" << endl;
        for (auto newstu : newcoursestu)
        {
            cout << "\t" << newstu.className << "\t" << newstu.CourceName << "   \t\t"<< newstu.CourceScroe << "\t" << newstu.studentName << "\t\t" << newstu.studentId << "\t" << newstu.teacherName << endl;
        }

        courcename = "吴老师";//数学
        newcoursestu = getCreditTeacherStudent(newcourseteach, stuArrys, reportArrys, claArry, memberAryy, courcename);
        sort(newcoursestu.begin(), newcoursestu.end());
        cout << "\t年级班级" << "\t课程名称" << "\t学分" << "\t学生姓名" << "\t学号" << "\t老师" << endl;
        for (auto newstu : newcoursestu)
        {
            cout << "\t" << newstu.className << "\t" << newstu.CourceName << "   \t\t" << newstu.CourceScroe << "\t" << newstu.studentName << "\t\t" << newstu.studentId << "\t" << newstu.teacherName << endl;
        }

        courcename = "林老师"; //英语
        newcoursestu = getCreditTeacherStudent(newcourseteach, stuArrys, reportArrys, claArry, memberAryy, courcename);
        sort(newcoursestu.begin(), newcoursestu.end());
        cout << "\t年级班级" << "\t课程名称" << "\t学分" << "\t学生姓名" << "\t学号" << "\t老师" << endl;
        for (auto newstu : newcoursestu)
        {
            cout << "\t" << newstu.className << "\t" << newstu.CourceName << "   \t\t" << newstu.CourceScroe << "\t" << newstu.studentName << "\t\t" << newstu.studentId << "\t" << newstu.teacherName << endl;
        }

        //
        vector<StudentScore> stuscore = getStudentScore(stuArrys, reportArrys, claArry, memberAryy, newcourseteach);
        //computeRanksVector(stuscore, 5);
        sort(stuscore.begin(), stuscore.end());

        cout << "\t学生编号" << "\t学生姓名" << "\t年级班号" << "\t语文分数" << "\t数学分数" << "\t英语分数" << "\t总分数" << "\t 语文老师" << "\t 数学老师" << "\t 英语老师" << "" << endl;
        for (auto dustu : stuscore)
        {
            cout << "\t" << dustu.studentId << "     \t" << dustu.studentName << "       \t" << dustu.className << " \t" << dustu.Chinese << "     \t         " << dustu.math << "    \t        " << dustu.English << "  \t        " << dustu.totalScore << "  \t " << dustu.ChineseTeacher<<"  \t  "<<dustu.mathTeacher<<"   \t "<<dustu.EnglishTeacher << endl;
        }

    }
    /// <summary>
    /// 学生
    /// </summary>
    /// <returns></returns>
    vector<duStudent> GeovinDu::ReadFileStu()
    {
        vector<duStudent> stus;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id=-1;
        /// <summary>
        /// 姓名
        /// </summary>
        string name;
        /// <summary>
        /// 总学分數
        /// </summary>
        float score;
        fin.open(stuPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> name >> score;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                duStudent dstu;
                dstu.number=id;
                dstu.name=name;
                dstu.score = score;
              
                stus.push_back(dstu);//添加
            }
            beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return stus;

    }

    /// <summary>
    /// 班级
    /// </summary>
    /// <returns></returns>
    vector<Class> GeovinDu::ReadFileClass()
    {
        vector<Class>  clas;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;
 
        fin.open(claPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> name;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                Class cls;
                cls.id = id;
                cls.name = name;     
                clas.push_back(cls);//添加
            }
            beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return clas;

    }


    /// <summary>
    /// 老师
    /// </summary>
    /// <returns></returns>
    vector<Teacher> GeovinDu::ReadFileTeach()
    {
        vector<Teacher>  teachs;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(teachPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> name;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                Teacher tea;
                tea.id = id;
                tea.name = name;
                teachs.push_back(tea);//添加
            }
            beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return teachs;

    }

    /// <summary>
    /// 课程
    /// </summary>
    /// <returns></returns>
    vector<Course> GeovinDu::ReadFileCourse()
    {
        vector<Course>  cours;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(coutPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> name;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                Course cou;
                cou.id = id;
                cou.name = name;
                cours.push_back(cou);//添加
            }
            beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return cours;

    }
    /// <summary>
    /// 老师课程表
    /// </summary>
    /// <returns></returns>
    vector<CreditCourse> GeovinDu::ReadFileCreditCourse()
    {
        vector<CreditCourse>  creeditcours;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;

        int teaid = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(crePath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> teaid;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                CreditCourse credit;
                credit.courseId = id;
                credit.teacherId = teaid;
                creeditcours.push_back(credit);//添加
            }
            //beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return creeditcours;

    }

    /// <summary>
    /// 班主任
    /// </summary>
    /// <returns></returns>
    vector<ClassHead> GeovinDu::ReadFileClassHead()
    {
        vector<ClassHead>  heads;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        int teaid = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(headPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> teaid;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                ClassHead head;
                head.classId = id;
                head.teachId = teaid;
                heads.push_back(head);//添加
            }
            beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return heads;

    }

    /// <summary>
    /// 班级学生表
    /// </summary>
    /// <returns></returns>
    vector<ClassMember> GeovinDu::ReadFileClassMember()
    {
        vector<ClassMember>  members;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        int stuid = -1;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(memberPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> stuid;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                ClassMember member;
                member.classId = id;
                member.clstuId = stuid;
                members.push_back(member);//添加
            }
           // beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return members;

    }

    /// <summary>
    /// 学生成绩表
    /// </summary>
    /// <returns></returns>
    vector<StudentReport> GeovinDu::ReadFileStudentReport()
    {
        vector<StudentReport>  reports;
        ifstream fin;
        int beforeId = -1;//上一个id
        int id = -1;
        int couseid = -1;
        int score = 0;
        /// <summary>
        /// 名
        /// </summary>
        string name;

        fin.open(reportPath, ios_base::in);//打开文件
        //if (fin == NULL)//文件不存在,直接返回
        //{
        //    return;
        //}
        if (!fin) {
            cerr << "Error opening test2.txt for reading\n";
            exit(1);
        }
        while (!fin.eof())//循环读取直到文件末尾
        {
            fin >> id >> couseid >> score;
            if (id > 0 && id != beforeId)//因为空格的原因 读取读后一个数据两次,这里使用beforeId来避免重复添加
            {
                StudentReport report;
                report.stuNumber = id;
                report.courseId = couseid;
                report.courseScore = score;               
                reports.push_back(report);//添加
            }
           // beforeId = id;//记录上一次id
        }

        fin.close();//关闭文件流
        return reports;

    }
    /// <summary>
    /// 统计总分数
    /// </summary>
    /// <param name="stuArry"></param>
    /// <param name="reportArry"></param>
    vector<duStudent> GeovinDu::getSumScore(vector<duStudent> stuArry, vector<StudentReport> reportArry)
    {
        vector<duStudent> newstu;
        //dustudent stu = stuArry;
        //StudentReport report = reportArry;      
        //int len = sizeof(reportArry)/sizeof(reportArry[0]);
        //for (int i=0;i<len;i++)
        //{
        //    int slen = sizeof(stuArry)/ sizeof(stuArry[0]);

        //    for (int j=0;j<slen;j++)
        //    {
        //        if (reportArry[i].stuNumber == stuArry[j].number)
        //        {
        //            stuArry[j].score = stuArry[j].score + reportArry[i].courseScore;
        //        }

        //    }
        //}
        for (auto stu : stuArry)
        {

            for (auto report : reportArry)
            {
                if (report.stuNumber == stu.number)
                {
                    stu.score = stu.score + report.courseScore;

                }

            }
           
            newstu.push_back(stu);
        }

        return newstu;
    }
    /// <summary>
    /// 班主任老师表
    /// </summary>
    /// <param name="calssArry"></param>
    /// <param name="headArry"></param>
    /// <param name="teachArry"></param>
    /// <returns></returns>
    vector<ClassHeadTeacher> GeovinDu::getClassHeadTeacher(vector<Class> calssArry,vector<ClassHead> headArry,vector<Teacher> teachArry)
    {
        vector<ClassHeadTeacher> headteacher;
        //int headid = 0;
        for (auto hea : headArry)
        {
           // headid = hea.classId;
            for (auto clas :calssArry)
            {

                for (auto teac : teachArry)
                {
                    if (hea.classId == clas.id && teac.id == hea.teachId)
                    {
                        ClassHeadTeacher h;
                        h.className = clas.name;
                        h.teacherId = teac.id;
                        h.teacherName = teac.name;
                        headteacher.push_back(h);
                    }
                        

                }
            }
        }
        return headteacher;
    }
    /// <summary>
    /// 授课老师表
    /// </summary>
    /// <param name="courseArry"></param>
    /// <param name="creditArry"></param>
    /// <param name="teachArry"></param>
    /// <returns></returns>
    vector<CreditCourseTeacher> GeovinDu::getCreditCourseTeacher(vector<Course> courseArry,vector<CreditCourse> creditArry,vector<Teacher> teachArry)
    {
        vector<CreditCourseTeacher> newteacher;
        for (auto credit : creditArry)
        {
            for (auto course : courseArry)
            {

                for (auto teach : teachArry)
                {
                    if (credit.courseId == course.id && credit.teacherId == teach.id)
                    {
                        CreditCourseTeacher tea;
                        tea.courceId = course.id;
                        tea.CourceName = course.name;
                        tea.teacherId = teach.id;
                        tea.teacherName = teach.name;
                        newteacher.push_back(tea);
                    }           
                    


                }
            }


        }

        return newteacher;
    }
    /// <summary>
    /// 授课老师学生成绩表
    /// </summary>
    /// <param name="creditArry"></param>
    /// <param name="stuArry"></param>
    /// <param name="reportArry"></param>
    /// <param name="calssArry"></param>
    /// <param name="memberArry"></param>
    /// <param name="courseName"></param>
    /// <returns></returns>
    vector<CreditTeacherStudent> GeovinDu::getCreditTeacherStudent(vector<CreditCourseTeacher> creditArry, vector<duStudent> stuArry, vector<StudentReport> reportArry, vector<Class> calssArry, vector<ClassMember> memberArry,string courseName)
    {
        vector<CreditTeacherStudent> newstus;
        for (auto stu : stuArry)
        {

            for (auto report : reportArry)
            {

                for (auto member : memberArry)
                {
                    for (auto cla : calssArry)
                    {
                        for (auto coursteach : creditArry)
                        {

                            if (stu.number == report.stuNumber && member.clstuId == stu.number && member.classId == cla.id && coursteach.courceId == report.courseId && (coursteach.CourceName==courseName|| coursteach.teacherName==courseName))
                            {
                                CreditTeacherStudent stude;
                                stude.className = cla.name;
                                stude.studentId = stu.number;
                                stude.studentName = stu.name;
                                stude.teacherName = coursteach.teacherName;
                                stude.CourceName = coursteach.CourceName;
                                stude.CourceScroe = report.courseScore;
                                stude.courceId = report.courseId;
                                newstus.push_back(stude);
                            }

                        }
                        

                    }
                }

            }

        }

        return newstus;
    }


    /// <summary>
    /// 学生成绩单 详情分数和总分数课目
    /// </summary>
    /// <param name="stuArry"></param>
    /// <param name="reportArry"></param>
    /// <param name="calssArry"></param>
    /// <param name="memberArry"></param>
    /// <param name="courseArry"></param>
    /// <param name="creditArry"></param>
    /// <returns></returns>
    vector<StudentScore> GeovinDu::getStudentScore(vector<duStudent> stuArry, vector<StudentReport> reportArry,vector<Class> calssArry, vector<ClassMember> memberArry,vector<CreditCourseTeacher> creditArry)
    {
        vector<StudentScore> scores;
        for (auto stu : stuArry)
        {

            for (auto member : memberArry)
            {
                for (auto cla : calssArry)
                {
                    
                    
                        if (member.clstuId == stu.number && member.classId == cla.id)
                         {
                             StudentScore stude;
                             stude.className = cla.name;
                             stude.studentId = stu.number;
                             stude.studentName = stu.name;
                             stude.Chinese = getScore(stu.number, 1, reportArry);
                             stude.math = getScore(stu.number, 2, reportArry);
                             stude.English = getScore(stu.number, 3, reportArry);
                             stude.totalScore = stude.Chinese + stude.math + stude.English;
                             stude.ChineseTeacher = getTeacher(1, creditArry);
                             stude.EnglishTeacher = getTeacher(3, creditArry);
                             stude.mathTeacher = getTeacher(2, creditArry);
                            scores.push_back(stude);
                        }
                    
                }

            }
        }
        return scores;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="stuid"></param>
    /// <param name="courseid"></param>
    /// <param name="reportArry"></param>
    /// <returns></returns>
    float  GeovinDu::getScore(int stuid, int courseid,vector<StudentReport> reportArry)
    {
        float scroe;
        for (auto report : reportArry)
        {
            if (report.courseId == courseid && report.stuNumber == stuid)
            {
                scroe = report.courseScore;
            }
        }
        return scroe;
    }
    /// <summary>
    /// 得到课程老师
    /// </summary>
    /// <param name="courseid"></param>
    /// <param name="creditArry"></param>
    /// <returns></returns>
    string GeovinDu::getTeacher(int courseid, vector<CreditCourseTeacher> creditArry)
    {
        string teacher;

        for (auto teach : creditArry)
        {
            if (teach.courceId == courseid)
            {
                teacher = teach.teacherName;
            }
        }

        return teacher;
    }

    /// <summary>
    /// Function for comparing two students according to given rules
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    bool compareTwoStudents(StudentScore a, StudentScore b)
    {
        // If total marks are not same then
        // returns true for higher total
        if (a.totalScore != b.totalScore)
            return a.totalScore > b.totalScore;

        // If marks in Maths are same then
        // returns true for higher marks
        if (a.math != b.math)
            return a.math > b.math;

        if (a.Chinese != b.Chinese)
            return a.Chinese > b.Chinese;

        return (a.English > b.English);
    }

    /// <summary>
    /// Fills total marks and ranks of all Students
    /// </summary>
    /// <param name="a"></param>
    /// <param name="n"></param>
    //void computeRanks(StudentScore a[], int n)
    //{
    //    // To calculate total marks for all Students
    //    for (int i = 0; i < n; i++)
    //        a[i].totalScore = a[i].math + a[i].Chinese + a[i].English;

    //    // Sort structure array using user defined
    //    // function compareTwoStudents()
    //    sort(a, a + n, compareTwoStudents);

    //    // Assigning ranks after sorting
    //    for (int i = 0; i < n; i++)
    //        a[i].rank = i + 1;
    //}
    /// <summary>
    /// 
    /// </summary>
    /// <param name="a"></param>
    /// <param name="n"></param>
    void GeovinDu::computeRanksVector(vector<StudentScore> a, int n)
    {
        // To calculate total marks for all Students
        //for (int i = 0; i < n; i++)
            //a[i].totalScore = a[i].math + a[i].Chinese + a[i].English;
        for (auto stu : a)
        {
            stu.totalScore = stu.Chinese + stu.math + stu.English;
        }

        // Sort structure array using user defined
        // function compareTwoStudents()
        //sort(a, a + n, compareTwoStudents);
        sort(a.begin(), a.end(), compareTwoStudents);
        // Assigning ranks after sorting
        for (int i = 0; i < n; i++)
            a[i].rank = i + 1;
    }
}

  

调用:

// ConsoleStructSimpleApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <fstream>//文件操作
#include <sstream>//int转string
#include <iomanip>//cout格式化输出 setw()
#include <stdlib.h>
#include "GeovinDu.h"


using namespace std;
using namespace DuStructSimple;


int main()
{
   // std::cout << "Hello World!涂聚文 Geovin Du\n";

	GeovinDu geovin;


	//geovin.displayStudent();
	// 无用
	//geovin.dispalyStructDemo();
	// 
	// 
	//geovin.dsipalyFuntion();

	geovin.dipslayStructCustomer();

	system("pause");
	return 0;

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

  

输出:

        学号    姓名    总分数
        101     王二    187
        102     赵三    193
        104     何六    193
        100     小李    225
        103     张五    281
        班级名称        老师ID  班主任姓名
        一年级一班      3       林老师
        课程名称        老师Id  老师姓名
        语文            2       吴老师
        数学            1       吕老师
        英语            3       林老师
        年级班级        课程名称        学分    学生姓名        学号    老师
        一年级一班      数学            60      赵三            102     吕老师
        一年级一班      数学            65      何六            104     吕老师
        一年级一班      数学            80      小李            100     吕老师
        一年级一班      数学            90      张五            103     吕老师
        一年级一班      数学            99      王二            101     吕老师
        年级班级        课程名称        学分    学生姓名        学号    老师
        一年级一班      语文            50      小李            100     吴老师
        一年级一班      语文            55      赵三            102     吴老师
        一年级一班      语文            78      王二            101     吴老师
        一年级一班      语文            78      何六            104     吴老师
        一年级一班      语文            92      张五            103     吴老师
        年级班级        课程名称        学分    学生姓名        学号    老师
        一年级一班      英语            10      王二            101     林老师
        一年级一班      英语            50      何六            104     林老师
        一年级一班      英语            78      赵三            102     林老师
        一年级一班      英语            95      小李            100     林老师
        一年级一班      英语            99      张五            103     林老师
        学生编号        学生姓名        年级班号        语文分数        数学分数        英语分数        总分数   语文老师       数学老师        英语老师
        101             王二            一年级一班      78               99             10              187      吴老师           吕老师         林老师
        102             赵三            一年级一班      55               60             78              193      吴老师           吕老师         林老师
        104             何六            一年级一班      78               65             50              193      吴老师           吕老师         林老师
        100             小李            一年级一班      50               80             95              225      吴老师           吕老师         林老师
        103             张五            一年级一班      92               90             99              281      吴老师           吕老师         林老师
请按任意键继续. . .

  

 

 

标签:Struct,Simple,老师,int,vector,cpp,include,fin,id
From: https://www.cnblogs.com/geovindu/p/17383482.html

相关文章

  • golang map key struct hash policy
     Theeasiestandmostflexiblewayistousea struct asthekeytype,includingallthedatayouwanttobepartofthekey,soinyourcase:typeKeystruct{X,Yint}Andthat'sall.Usingit:m:=map[Key]int{}m[Key{2,2}]=4m[Key{2......
  • pta_【CPP0027】以圆类Circle及立体图形类Solid为基础设计球类Sphere
    #include<iostream>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*析构函数*/voidsetX(d......
  • leetcode 101 对称二叉树 Simple
    题目给你一个二叉树的根节点root,检查它是否轴对称。输入:root=[1,2,2,3,4,4,3]输出:true输入:root=[1,2,2,null,3,null,3]输出:false题解考察二叉树的遍历,使用广度优先BFS方法.BFS的关键在于使用队列,遍历树时,读到的节点先入队,再出队,出队时读取值,放入结......
  • cpp: 内存地址
    在VisualStudio调试器中使用“内存”窗口(C#、C++、VisualBasic、F#)在调试器中查看变量的内存-VisualStudio(Windows)|MicrosoftLearn inta=10; charb; boolc; stringd; cout<<"变量a的地址:"<<&a<<endl; cout<<"变量b的地址:"......
  • ITIL信息技术基础架构库Information Technology Infrastructure Library
    ITIL是什么?|ITIL流程和框架-ManageEngineServiceDeskPlusITIL是InformationTechnologyInfrastructureLibrary的缩写,即:信息技术基础架构库。ITIL是由英国政府部门CCTA(CentralComputingandTelecommunicationsAgency)在20世纪80年代末开发的一套IT服务管理标准库,它......
  • 日期与时间【Date/SimpleDateFormat/Calendar】
    视频链接:https://www.bilibili.com/video/BV1Cv411372m?p=121&vd_source=9140dcc493e34a9f4e95ca2f8f71bbd31Data1.1Date类概述Date类的对象在java中代表的是当前所在系统的此刻日期时间。Date的构造器publicDate():创建一个Date对象,代表的是系统当前此刻日期时间。Date......
  • Web|[SWPUCTF 2018]SimplePHP
    访问是一个文件上传页面,点击查看文件页面可以发现特殊的链接,应该存在文件包含http://dfef288e-1b73-48e0-9458-a4e733c40c38.node4.buuoj.cn:81/file.php?file=查看源码发现一些文件,页面内容提示flag在f1ag.php中index.phpfile.phpupload_file.phpf1ag.php直接包含f1a......
  • Instruction does not dominate all uses! 解决
    报错现场:Instructiondoesnotdominatealluses!%59=muli32%58,%val%56=zexti32%59toi64LLVMERROR:Brokenfunctionfound,compilationaborted! 原因:指令的插入点不对。被插入的指令需要的操作数,在插入点之后才产生。无米之炊 解决:如果是使用 inser......
  • pta_【CPP0026】以点类Point及平面图形类Plane为基础设计三角形类Triangle
    #include<iostream>#include<cmath>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*......
  • 6-4 【CPP0026】以点类Point及平面图形类Plane为基础设计三角形类Triangle
    6-4【CPP0026】以点类Point及平面图形类Plane为基础设计三角形类Triangle分数 10作者 C++多态编程单位 石家庄铁道大学以平面图形类Plane为基类公有派生三角形类Triangle,main(void)函数完成对其的测试。Point类结构说明: Point类的数据成员包括:①私有......