综合课程设计(建立一个统计某足球队在联赛中成绩的类Football)

该文描述了一个使用C++编程的类`Football`,用于统计足球队在联赛中的成绩。类包含进球和失球的动态数组,以及计算积分和净胜球的方法。程序能从文件读取数据,输出结果到屏幕和result.txt文件。此外,提供了成绩录入和统计的功能选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验要求

建立一个统计某足球队在联赛中成绩的类Football,程序中使用的数据全部放入文件中,使用C++读取文件的方式输入程序中需要的数据,最后程序运行的结果同时以屏幕显示和文件两种方式输出,文件名字为result.txt,以上3个文件全部放到以学号和姓名命名的文件夹中,供阅读和运行使用。

具体要求如下:

(1)私有数据成员

int *win, *lost, m, max:

m: 存储已经比赛的场数

max:存储最大比赛场数

win:指向存储各场进球数的动态数组(win=new int[max])

lost:指向存储各场失球数的动态数组(lost=new int[max])

(2)公有成员

Football(int maxl = 50):构造函数,用参数maxl初始化max,带有参数时用其参数值,否则用默认值50初始化max,并用max的值为win和lost动态申请内存空间,m置为0。

~Football():析构函数,解释对象中的数组所占用的内存空间。

void input(int *winb, int *lostb, int n):输入n场比赛的各场进球数、失球数(l <=n<=max),将n赋给m。

int score():计算并返回该对的积分(胜一场得3分,平一场得1分,负一场得0分)。

int winball():计算并返回该队的净胜球数。

(3)编写一个程序测试该类。

测试数据:win[10]={3, 1, 4, 2, 3, 1, 2, 3, 4, 2}

                 lost[10]={2, 2, 3, 3, 1, 3, 2, 1, 3, 2 }

计算并打印该队的积分和净胜球数。

一场比赛中胜球数大于失球数时为胜,相等是为平,小于时为负;

每场的净胜球数为该场的进球数减去失球数

实验代码

#include<iostream>
#include <fstream>
#include <windows.h>
using namespace std;
#define N 100

class football {
private:
	int *win;     //各场进球数
	int *lost;	  //各场失球数
	int m;        //存储已经比赛的场数
	int max;      //存储最大比赛场数
public:
	football(int maxl = 50) {	//构造函数
		max = maxl;
		win = new int[max];
		lost = new int[max];
		m = 0;
	}
	~football() {
		delete win;
		delete lost;
	}
	void input(int* winb, int* lostb, int n) {
		int start = m;		//储存偏移量
		m += n;
		if (m > max) m = max;
		for (int i = 0; i < n; i++) {
			if (i + start >= m) {
				cout << "\033[33m" << "Warning:已储存最大比赛场数!!" << "\033[0m" << endl;
				break;
			}
			win[i+start] = winb[i];
			lost[i+start] = lostb[i];
		}
	}
	int score() {
		int sc = 0;
		for (int i = 0; i < m; i++) {
			if (win[i] > lost[i]) {
				sc += 3;
			}
			else if (win[i] == lost[i]) {
				sc += 1;
			}
		}
		return sc;
	}
	int winball() {
		int win_all = 0;
		int lost_all = 0;
		for (int i = 0; i < m; i++) {
			win_all += win[i];
			lost_all += lost[i];
		}
		return win_all - lost_all;
	}
};

void menu();		//提前声明

bool read_txt(football *f,const string data) {		//读文件
	int n = 0;
	int win[N], lost[N];
	cout << "文件读取中..." << endl;
	ifstream inputFile;					
	inputFile.open(data.c_str(), ios::in);
	if (inputFile.is_open()) {
		inputFile >> n;
		for (int i = 0; i < n; i++) {
			inputFile >> win[i] >> lost[i];
		}
		inputFile.close();
	}
	else {
		cout << "\033[31m" << "Error:文件读取失败" << "\033[0m" << endl;
		return 0;
	}
	Sleep(2000);
	cout << "\x1b[A" << "\x1b[2K"; //清除上一行的输出
	cout << "文件读取完成" << endl << endl;
	f->input(win, lost, n);
	return 1;
}

void print(football f,string result) {		//写文件
	ofstream outFile(result.c_str(),ios::app);		
	cout << "该队的总积分为:" << f.score() << endl;
	cout << "该队的净胜球数为:" << f.winball() << endl;
	//将数据写入result.txt文件
	cout << "正在将数据写入文件..." << endl;
	outFile << "该队的总积分为:" << f.score() << endl;
	outFile << "该队的净胜球数为:" << f.winball() << endl;
	outFile.close();
	Sleep(2000);
	cout << "\x1b[A" << "\x1b[2K";
	cout << endl;
	cout << "数据写入成功!" << endl;
}


void add() {
	system("cls");
	cout << "\t*****************************************\n";
	cout << "\t*****                              ******\n";
	cout << "\t*****         成绩录入系统         ******\n";
	cout << "\t*****                              ******\n\n";
	cout << "请输入需要写入的文件名:\n";
	char name[20];
	cin >> name;
	ofstream out(name, ios::app);
	cout << "请输入总共的数据量:" << endl;
	int n;
	cin >> n;
	out << n << endl;
	cout << "请依次按照进球数、失球数输入数据:" << endl;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		out << a << " " << b << endl;
	}
	cout << "成绩录入成功" << endl;
	cout << "返回(Y)or退出(N):" ;
	char c;
	cin >> c;
	if (c == 'Y' || c == 'y') {
		menu();
		return;
	}
	else {
		return;
	}
}
void count() {
	system("cls");
	cout << "\t*****************************************\n";
	cout << "\t*****                              ******\n";
	cout << "\t*****         成绩统计系统         ******\n";
	cout << "\t*****                              ******\n\n";
	cout << "请输入需要读入数据的文件名:\n";
	char name[20];
	cin >> name;
	football f;
	bool r = read_txt(&f, name);
	while(!r) {
		cout << "请输入正确的文件名:" << endl;
		cin >> name;
		r = read_txt(&f, name);
	}
	char s;
	cout << "是否继续追加数据(Y/N):";
	cin >> s;
	while(s == 'Y'||s == 'y'){
		cout << "请输入需要读入数据的文件名:\n";
		char name1[20];
		cin >> name1;
		bool ft = read_txt(&f, name1);
		while(!ft) {
			cout << "请输入正确的文件名:" << endl;
			cin >> name1;
			ft = read_txt(&f, name1);
		}	
		cout << "是否继续追加数据(Y/N):";
		cin >> s;
	}
	cout << "正在统计成绩信息..." << endl; 
	Sleep(1000);
	cout << "\x1b[A" << "\x1b[2K" << endl;
	print(f, "result.txt");
	cout << "返回(Y)or退出(N):";
	char c;
	cin >> c;
	if (c == 'Y' || c == 'y') {
		menu();
		return;
	}
	else {
		return;
	}
}

void menu() {
	system("cls");
	cout << "\t*****************************************\n";
	cout << "\t*****                              ******\n";
	cout << "\t*****   欢迎来到足球成绩管理系统   ******\n";
	cout << "\t*****                              ******\n";
	cout << "\t*****      请选择需要进行的操作    ******\n";
	cout << "\t*****                A:录入成绩    ******\n";
	cout << "\t*****                B:统计成绩    ******\n";
	cout << "\t*****                C:退出程序    ******\n";
	cout << "\t*****                              ******\n";
	cout << "\t*****************************************\n";
	char c;
	bool b = 1;
	while(b){
		cin >> c;
		switch (c)
		{
		case 'A': 
		case 'a': 
			add();
			b=0;
			break;
		case 'B': 
		case 'b': 
			count();
			b=0;
			break;
		case 'C': 
		case 'c': 
			b=0;
			return;
		default:
			cout<<"请输入正确的操作:\n";
			b = 1;
			break;
		}
	}
	
}

int main() {
	menu();
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值