【双色球预测】统计指定文件格式中数字出现次数

数据文件(输入): 最近一段时间出现的球的号码

统计结果(输出): 统计各号球出现次数, 存入数组

输入文件:

ball.txt

 8 11 17 23 32 33		10
 4  5  7 10 12 22       16
 3 13 15 18 21 33       16
 4  8  9 13 28 33        4
 9 15 19 21 23 29       15
 9 11 15 22 24 26        3
 1  5  7  9 10 20       16
 2 10 13 16 23 32        8
 1  7 12 14 18 25       10
 9 12 21 27 29 30        5

lottery_prediction.cpp

#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

#define NUM 33   //球号
#define NBALL 7  //一次6+1
bool statistics(const char* path, int ball[]) {
	ifstream file;
	int result[NUM];//数组可以用指针表示其值,如:*(result+i)==result[i]

	if (!path) {
		cerr << "path is NULL" << endl;
		return false;//传递空指针
	}

	file.open(path);
	if (file.fail()) {
		cerr << "打开输入文件出错, 原因:" << strerror(errno) << endl;
		return false;
	}//文件打开成功,开始传值进入result
	while (1) {//一行一行执行,行数未知故while循环,遇i==0时结束
		int i = 0;
		for (; i < NBALL; i++) {
			file >> result[i];
			if (file.eof()) {
				break;//文末下一位存有eof
			}
			if (file.fail()) {
				cerr << "读取文件失败, 原因:" << strerror(errno) << endl;
				break;
			}
		}
		//行末结束,继续录入下一行;若未达到行尾-??如何判断是否是行尾??file>>如何区分空格和回车??
		//无法判断达到行尾,无法区分回车和空格,但若全部取得数不为7的倍数,认为输入文件出错
		//if(i == NBALL) {
		//	continue;
		//}

		//打印这行数字并将出现数字累积到ball中
		for (i = 0; i < NBALL; i++) {
			cout << setw(3) << result[i];
			int index = *(result + i) - 1;
			if (index >= 0 && index <= NUM) {//判断-防御式编程
				(*(ball + index))++;
			}
		}cout << endl;
		
		if (file.eof()) break;//文末正常结束
		if (i + 1 < NBALL) {
			cerr << "仅读到" << i + 1 << "个,预期读取" << NBALL << "个" << endl;
			file.close();
			return false;
		}
	}
	file.close();
	return true;
}

int main() {
    int ball[NUM] = { 0 };
	//string filename;
	//cout << "请输入文件名:";
	//cin >> filename;
	//statistics(filename.c_str(), ball);
	if (statistics("ball.txt", ball)) {
		//打印ball中存储的各号球出现次数
		for (int i = 0; i < NUM; i++) {
			cout << "第" << setw(2) << i + 1 << "号球出现次数:" << *(ball + i) << endl;
		}
	}
	else
		cerr << "统计出错!" << endl;

	system("pause");
	return 0;
}

输出结果:

要使用Python3处理Excel文件(假设为`.xlsx`格式),统计红球出现次数,并绘制棒棒糖图(lollipop chart),最后将结果写入HTML文件并实现拖拽功能,你可以按照以下步骤进行: 1. 使用`openpyxl`库来读取`.xlsx`文件,这个库专门用于读写Excel 2010 xlsx/xlsm/xltx/xltm文件。 2. 统计每个红球的出现次数。假设红球的数字位于某一行的前六个单元格。 3. 使用`matplotlib`库来绘制棒棒糖图。 4. 使用`mpld3`库将绘制好的棒棒糖图转换为HTML格式,并添加拖拽功能。 5. 将生成的HTML保存到文件。 以下是一个简化的示例代码,用于实现上述步骤: ```python import openpyxl from collections import Counter import matplotlib.pyplot as plt import mpld3 # 读取Excel文件 wb = openpyxl.load_workbook('双色球数据.xlsx') sheet = wb.active # 假设红球的数字从第2列到第7列(索引1到6) red_balls = [cell.value for cell in sheet[1][1:7]] # 统计红球出现次数 red_ball_counts = Counter(red_balls) # 绘制棒棒糖图 fig, ax = plt.subplots() ax.bar(red_ball_counts.keys(), red_ball_counts.values()) for i, count in enumerate(red_ball_counts.values()): ax.plot([i + 1, i + 1], [0, count], 'k-') ax.text(i + 1, count + 1, str(count), ha='center', va='bottom') # 设置图表标题和坐标轴标签 ax.set_title('红球出现次数') ax.set_xlabel('红球号码') ax.set_ylabel('出现次数') # 使用mpld3添加拖拽功能并保存为HTML html = mpld3.fig_to_html(fig) with open('red_ball_lollipop.html', 'w') as f: f.write(html) # 关闭Excel工作簿 wb.close() ``` 在上面的代码,请确保你已经安装了`openpyxl`, `matplotlib`, `mpld3`这些库。如果尚未安装,可以使用pip安装它们: ```shell pip install openpyxl matplotlib mpld3 ``` 执行上述代码后,你会得到一个名为`red_ball_lollipop.html`的文件,该文件的图表可以拖拽查看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值