c++ 实战小项目

需求:编写程序找出最近一段时间每个号码出现的次数并把结果保存到一个数组,供其他分析模块调用,往期数组保存在一个名为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

算法设计

 1.将双色球往期数据从文件读入一维数组;

2.逐行遍历一维数组的每个元素,统计前六个球在1-33范围内出现的总次数;

代码

#include<iostream>
#include<Windows.h>
#include<string.h>
#include<fstream>

using namespace std;
#define NUM 7
bool statistics(const char* path, int ball_16[],int len)//定义一个统计的函数  1到6个球,里面有33个数子
{	
	int result[NUM]={0};
	


	ifstream file;
	int i = 0;
	if (!path) { 
		cerr << "path is NULL" << endl;
		return false; 
	}
	file.open(path);
	if (file.fail()) {
		cerr << "打开输入文件错误" << strerror(errno) << endl;// strerror(errno)系统自带的功能
		return false;
	}
	//从数据文件读取数据到数组,一行必须读7个
	do
	{
		i = 0;
		for (i = 0; i < NUM; i++)
		{
			file >> result[i];
			if (file.eof())
			{
				break;//file.eof()文件尾部
			}
			if (file.fail()) 
			{

				cerr << "读取文件失败,原因" << strerror(errno) << endl;// strerror(errno)系统自带的功能
				break;
			}
		}

		if (i == 0) break;//记录正常结束
		//如果到最后未满7个
		if (i < NUM) {
			cerr << "仅读到" << i << "个记录,预期读取7个" << endl;
			file.close();
			return false;
		}
		for (i = 0; i < NUM; i++)
		{
			cout << " " << result[i];
		}
		cout << endl;
		//对读入的数据进行统计
		for (i = 0; i < NUM - 1; i++)
		{
			int index = *(result + i) - 1;//1球=》0,33球=》32
			if (index < len && index >= 0)
			{
				*(ball_16 + index) += 1;
			}
		}
	} while (true);
	file.close();
		return true;
}

int main(void)
{
	string filename;//定义一个字符串文件名
	int ball_16[33]={0};//1到6号球的数据,数组初始化D:\C++\人工智能之双色球预测系统\人工智能之双色球预测系统

	cout << "请输入文件名.\n";
	cin >> filename;
	

	if (statistics(filename.c_str(), ball_16,33)) {
		cout << "_______________________" << endl;
		for (int i = 0; i < 33; i++) {
			cout << "第" << i + 1 << "球出现的次数" << ball_16[i] << endl;
		}
	}
	else {//统计失败
		cerr << "统计出错" << endl;
	}

	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值