双色球数据统计

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值