#include<iostream>
#include<string>
#include<stdio.h>
#include<fstream>
#include<Windows.h>
using namespace std;
#define M 33
#define NUM 7
bool statistics(const char* fn, int ball_1_6[], int len) {
int i = 0; //定义循环变量
ifstream file;
int result[NUM] = { 0 }; //统计前七个球的出现次数
if (!fn) {
cerr << "输入结果为空" << endl;
return false;
}
file.open(fn);
if (file.fail()) {
cerr << "文件的打开失败" << strerror(errno) << endl;
return false;
}
while (1) {
for (i = 0; i < NUM; i++) {
file >> result[i];
//如果达到文件的尾部 跳出本次循环 i=0
if (file.eof()) {
break;
}
//判断文件是否读取失败
if (file.fail()) {
cerr << "文件读取失败" << strerror(errno) << endl;
break;
}
}
if (i == 0) {
//正常退出
break;
}
//如果到最后未满7个
if (i < NUM) {
cerr << "仅读到" << i << "个记录,预期读取7个" << endl; //打印出错原因
file.close();
return false;
//break;
}
for (i = 0; i < NUM; i++) { //打印
cout << " " << result[i];
}
cout << endl;
//统计
for (i = 0; i < NUM; i++) {
//存球 33-1=32
int index = result[i] - 1;
if (index >= 0 && index < len) { //合法性检查 0-1=-1
//把数字6放在a[6]的地址上,每识别一次 a[6]里的内容加1 0+6=6
(*(ball_1_6 + index))++;
}
}
}
file.close();
return true;
}
int main(void) {
string fileName;
int ball_1_6[M] = { 0 };
cout << "请输入文件名" << endl;
cin >> fileName;
if (statistics(fileName.c_str(), ball_1_6, M)) {
for (int i = 0; i < M; i++) {
cout << "数字" << i + 1 << "出现的次数为:" << ball_1_6[i] << endl;
}
cout << "统计成功" << endl;
}
else {
cout << "统计失败" << endl;
}
system("pause");
return 0;
}
12-01
3343

08-10
3216
