我是靠谱客的博主 单身皮皮虾,最近开发中收集的这篇文章主要介绍C++/OOP/8-2 Students II (10分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C++/OOP/8-2 Students II (10分)

Write a CLI program that reads scores and names of students, and prints out a summary sheet (in the same format with the last homework “Students I”). The user can input as many students as possible. One student can have as many courses as possible. One course consists the name of the course and the scores the students got. Please note that a student won’t choose every available course in general.

PTA作业,写死我了,还是有bug

//student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <vector>
 
using namespace std;
extern vector<string> title;//抬头也就是标题栏,课程信息
extern vector<int> maxi;//声明全局变量,最大值,最小值,该门课程总分,该门课程选课数量
extern vector<int> mini;
extern vector<int> sum;
extern vector<int> cnt;

class student{
	int num;
	string name;
	struct course{
		string name;
		int score;
	};
	vector<course> courses;
	float average;
public:
	void setNum(int i);//编号
	void setName(string str);
	void setScore(string name, int score);
	void getAverage();
	void printResult();
};

#endif
//student.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include "student.h"
using namespace std;

vector<string> title;
vector<int> maxi;
vector<int> mini;
vector<int> sum;
vector<int> cnt;


void student::setNum(int i)
{
	num = i;
}

void student::setName(string str)
{
	name = str;
}

void student::setScore(string name, int score)
{
	course course;
	course.name = name;
	course.score = score;
	courses.push_back(course);
}

void student::getAverage()
{
	int sum = 0, i;
	for(i=0; i<courses.size(); i++)
		sum += courses[i].score;
	average = 1.0*sum/courses.size();
}

void student::printResult()
{
	int i,j,index=2;
	
	cout << left << setw(10) << num;
	cout << left << setw(10) << name;

	for (i=0; i<courses.size(); i++){
		for(j=2; j<title.size(); j++)
			if(courses[i].name==title[j])
				break;//定位找到和标题名称相同的课程

		if(j<title.size()){
			cout<< setw(10*(j-index)) << "";//j-index即偏移量
			cout<< setw(10)<< courses[i].score;
			index = j+1;

			if(cnt[j-2]==0)
				maxi[j-2] = courses[i].score;
			else if(maxi[j-2]<courses[i].score)
				maxi[j-2] = courses[i].score;

			if(cnt[j-2]==0)
				mini[j-2] = courses[i].score;
			else if(mini[j-2]>courses[i].score)
				mini[j-2] = courses[i].score;

			sum[j-2] += courses[i].score;

			cnt[j-2] +=1;
		}
	}
	cout << left << setw(10*(title.size()-index-1))<<"";
	cout << left << setw(10) << average << endl;
}
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include "student.h"
using namespace std;

vector<string> split_str(string str, string delim);//字符串分割函数
void init();//用来初始化课程信息向量

int main()
{
	vector<student *> stu;
	string str, name;
	vector<string> s;
	int i = 1, j, score, k;
	title.push_back("num");
	title.push_back("name");


	ifstream file;
	file.open("studentRecord.txt");

	while(getline(file, str)){
		student *pstu = new student;
		s = split_str(str, "t");//分割字符串
		pstu->setNum(i);
		pstu->setName(s[0]);
		for(j=1; j<s.size(); j+=2){
			name = s[j];//课程+成绩一定是偶数
			score = stoi(s[j+1], 0, 10);//强制类型转换
			pstu->setScore(name, score);
			for(k=0; k<title.size(); k++){
				if(title[k]==name)
					break;
			}
			if(k>=title.size())
				title.push_back(name);//如果扫描中改课程未在title中便增加
		}
		pstu->getAverage();
		stu.push_back(pstu);//存进记录
		i++;
	}
	title.push_back("average");

	for(i=0; i<title.size(); i++){
		cout << left << setw(10) << title[i];
	}
	cout << endl;//输出标题

	init();//初始化

	for(i=0; i<stu.size(); i++){
		stu[i]->printResult();//输出记录
		delete stu[i];
	}

	cout<< setw(10)<<"";
	cout<< setw(10)<<"max";
	for(i=0; i<maxi.size(); i++){
		cout<< setw(10)<<maxi[i];
	}
	cout<<endl;

	cout<< setw(10)<<"";
	cout<< setw(10)<<"min";
	for(i=0; i<mini.size(); i++){
		cout<< setw(10)<<mini[i];
	}
	cout<<endl;

	cout<< setw(10)<<"";
	cout<< setw(10)<<"average";
	for(i=0; i<sum.size(); i++){
		cout<< setw(10)<<(float)1.0*sum[i]/cnt[i];
	}
	cout<<endl;


	return 0;
}

/*分割字符串函数*/
vector<string> split_str(string str, string delim)
{
    string::size_type pos1, pos2 = 0;
    vector<string> result;

	str += delim;
    while(pos2!=str.npos)
    {
        pos1 = str.find_first_not_of(delim, pos2);
        if(pos1==str.npos) break;
		pos2 = str.find_first_of(delim, pos1);
		result.push_back(str.substr(pos1, pos2-pos1));
    }
    return result;
}

void init()
{
	for(int i=0; i<title.size()-3; i++){
		maxi.push_back(0);
		mini.push_back(0);
		sum.push_back(0);
		cnt.push_back(0);
	}
}

这是运行结果
运行结果如图所示

最后

以上就是单身皮皮虾为你收集整理的C++/OOP/8-2 Students II (10分)的全部内容,希望文章能够帮你解决C++/OOP/8-2 Students II (10分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部