#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;
}
双色球预测
于 2024-01-05 17:30:46 首次发布