首页 > 代码库 > 【一个C++程序】小课设

【一个C++程序】小课设

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//学生类
class student
{
private:
    string name;        //姓名
    int age;            //年龄
    string code;        //学号
    int nianji;         //年纪
    int grade;          //成绩
public:
    //构造函数
    student(){}
    student(string na, int a, string c, int n, int g):name(na), age(a), code(c), nianji(n), grade(g) {}
    string getName() { return name; }
    void setName(string na) { this->name = na; }
    int getAge() {return age;}
    void setAge(int age) { this->age = age; }
    string getCode() { return code; }
    void setCode(string c) { this->code = c;}
    int getNianji() { return nianji; }
    void setNianji(int nj) { this->nianji = nj; }
    int getGrade() { return grade; }
    void setGrade(int g) { this->grade = g; }
	/*
    student& operator = (const student& s)
    {
        name = s.name;        //姓名
        age = s.age;            //年龄
        code = s.code;        //学号
        nianji = s.nianji;         //年纪
        grade = s.grade;          //成绩

        return *this;
    }
	*/
};


//教师类
class teacher
{
private:
    string name;        //姓名
    int age;            //年龄
    int gongHao;         //工号
    int classTime;       //课时
    int money;          //课时费
public:
    //构造函数
    teacher(){}
    teacher(string na, int a, int gh, int ct, int m):name(na), age(a), gongHao(gh), classTime(ct), money(m) {}
    string getName() { return name; }
    void setName(string na) { this->name = na; }
    int getAge() {return age;}
    void setAge(int age) { this->age = age; }
    int getGongHao() { return gongHao; }
    void setGongHao(int gh) { this->gongHao = gh; }
    int getClassTime() { return classTime; }
    void setClassTime(int ct) { this->classTime = ct; }
    int getMoney() { return money; }
    void setMoney(int m) { this->money = m; }
};


//员工类
class employee
{
private:
    string name;        //姓名
    int age;            //年龄
    int gongHao;         //工号
    int money;          //月薪
public:
    //构造函数
    employee(){}
    employee(string na, int a, int gh, int m):name(na), age(a), gongHao(gh), money(m) {}
    string getName() { return name; }
    void setName(string na) { this->name = na; }
    int getAge() {return age;}
    void setAge(int age) { this->age = age; }
    int getGongHao() { return gongHao; }
    void setGongHao(int gh) { this->gongHao = gh; }
    int getMoney() { return money; }
    void setMoney(int m) { this->money = m; }
};

void MaoPao(vector<student> vs, vector<student> &vgai)
{
    student temp1, temp2;
    //根据分数进行排序,然后放入到vector中去,依次找到最小的然后去掉它就可以了
    for(int i = 0 ; vs.size() > 0  ; )
    {
        //每次都取当前的第一个,因为我会一直删里面的类
        temp1=vs[i];
        //和后面的比较
        for(auto j=vs.begin()+1 ; j != vs.end() ; ++j)
        {
            temp2=*j;

            if(temp1.getGrade()<temp2.getGrade())
            {
                //如果第一个是小的,那么就交换,一直到找到最大的
                temp1=temp2;
            }
        }
        //第一层循环完成,找到当前最大的,放入到vgai的第一个
        //cout<<temp1.getGrade()<<" xx "<<endl;
        vgai.push_back(temp1);
        //吧当前的temp1从vs去掉,直到vs被搬空为止
		vector<student>::iterator itr = vs.begin();
		while (itr != vs.end())
		{
			if (itr->getName() == temp1.getName())
			{
				vs.erase(itr);//删除值为3的元素
				break;
			}
			++itr;
		}
    }
}

void print(vector<teacher> vt)
{
	const string NAME1 = "高代老师名字", NAME2 = "数分老师名字", NAME3 = "英语老师名字";

	cout << "各科教师的信息是:" << endl;
	cout << "姓名\t\t年龄\t工号\t课时\t课时费\t" << endl;
	for (auto it = vt.begin(); it != vt.end(); ++it)
	{
		teacher t = *it;
		string s = t.getName();
		if (s == NAME1 || s == NAME2 || s == NAME3)
		{
			cout << t.getName() << "\t"
				<< t.getAge() << "\t"
				<< t.getGongHao() << "\t"
				<< t.getClassTime() << "\t"
				<< t.getMoney() << endl;
		}
	}
}

int main()
{
    vector<student> vs, vgai;
    student s1("myName1", 19, "20130901001", 2, 98);        //自己是第一个,内定了
    student s2("myName2", 19, "20130901002", 2, 94);
    student s3("myName3", 19, "20130901003", 2, 33);
    student s4("myName4", 19, "20130901004", 2, 45);
    student s5("myName5", 19, "20130901005", 2, 75);
    student s6("myName6", 19, "20130901006", 2, 12);
    student s7("myName7", 19, "20130901007", 2, 77);
    student s8("myName8", 19, "20130901008", 2, 33);
    student s9("myName9", 19, "20130901009", 2, 55);
    student s10("myName10", 19, "20130901010", 2, 78);
    student s11("myName11", 19, "20130901011", 2, 28);
    student s12("myName12", 19, "20130901012", 2, 58);
    student s13("myName13", 19, "20130901013", 2, 8);
    student s14("myName14", 19, "20130901014", 2, 58);
	//1-----------------------------------------------------------------------
    cout<<"我的信息是:"<<endl;
    cout<<"姓名\t\t年龄\t学号\t\t年级\t成绩\t"<<endl;
    cout<<s1.getName()<<"\t\t"
        <<s1.getAge()<<"\t"
        <<s1.getCode()<<"\t"
        <<s1.getNianji()<<"\t"
        <<s1.getGrade()<<endl;


    //输出最近的10位,不会数据库只能用这种蠢方法
    vs.push_back(s1); vs.push_back(s2); vs.push_back(s3); vs.push_back(s4);
    vs.push_back(s5); vs.push_back(s6); vs.push_back(s7); vs.push_back(s8);
    vs.push_back(s9); vs.push_back(s10); vs.push_back(s11); vs.push_back(s12);
    vs.push_back(s13); vs.push_back(s14);
    vector<student>::iterator it = vs.begin();
    int count = 1;  //计数10个
	//2-----------------------------------------------------------------------
    cout<<"后面10位的信息"<<endl;
    while(it != vs.end() && count <= 11)
    {
        cout<<it->getName()<<"        "
        <<it->getAge()<<"\t"
        <<it->getCode()<<"\t"
        <<it->getNianji()<<"\t"
        <<it->getGrade()<<endl;
        ++it;
        ++count;
    }

	//3-----------------------------------------------------------------------
    MaoPao(vs, vgai);
    cout<<endl;
        cout<<"排序后:"<<endl;
    vector<student>::iterator it2 = vgai.begin();
    //int count2 = 1;  //计数10个

    while(it2 != vgai.end())
    {
        cout<<it2->getName()<<"        "
        <<it2->getAge()<<"\t"
        <<it2->getCode()<<"\t"
        <<it2->getNianji()<<"\t"
        <<it2->getGrade()<<endl;
        ++it2;
    }

	//4-----------------------------------------------------------------------
	vector<teacher> vt;
	teacher t1("高代老师名字", 60, 001, 30, 7000), t2("数分老师名字", 60, 002, 30, 9980),
		t3("英语老师名字", 998, 003, 2, 700), t4("牛逼老师名字", 60, 004, 30, 7000);

	vt.push_back(t1), vt.push_back(t2), vt.push_back(t3), vt.push_back(t4);

	print(vt);

	char c;
	cin>>c;

    return 0;
}



一个小题,同学叫我帮忙,我就很快的写了一些,确实很乱,不过要是连上数据库还是有点意思的

【一个C++程序】小课设