双色球预测

#include <string>
#include <iostream>
#include <fstream>
#pragma warning(disable : 4996)
#define NUM 7
#define BALL_NUM 33

using namespace std;

bool statistics(const char* path, int ballNum[BALL_NUM]) {
	int res[NUM];
	ifstream file;
	int readCount = 0;

	//判断文件path是否为空
	if (!path) {
		cerr << "path is NULL" << endl;
		return false;
	}

	file.open(path);//打开文件

	//判断打开文件是否失败
	if (file.fail()) {
		cerr << "open path is fail:code" << strerror(errno) << endl;
		return false;
	}

	//从文件中读数据到数组,一次读取7个
	do
	{
		for (readCount = 0; readCount < NUM; readCount++) {
			file >> res[readCount];
			
			if (file.eof()) {//判断读取文件是否结束
				break;
			}
			if (file.fail()) {//判断文件读取是否失败
				cerr << "read file is fail:code" << strerror(errno) << endl;
				break;
			}
		}

		if (readCount == 0) {//读取文件正常结束
			break;
		}

		if (readCount < NUM) {//未读取7个数据
			cerr << "only " << readCount << " records were read." << endl;
			file.close();
			return false;
		}

		for (int i = 0; i < NUM; i++) {
			cout << " " << res[i];
		}
		cout << endl;

		for (int i = 0; i < NUM-1; i++) {
			int index = *(res + i) - 1;
			if (index >= 0 && index < BALL_NUM) {
				*(ballNum + index) += 1;
			}
		}
	} while (true);

	file.close();
	return true;
}

int main(void) {
	string filename;
	int ballNum[BALL_NUM] = { 0 };
	cout << "Please enter file name:";
	cin >> filename;

	if (statistics(filename.c_str(), ballNum)) {
		for (int i = 0; i < BALL_NUM; i++) {
			cout << "The " << i+1 << "st ball appeared " << ballNum[i] << " times" << endl;
		}
	}
	else {
		cerr << "statistical error" << endl;
	}

	system("pause");
	return 0;

}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值