人工智能之双色球预测系统---------------经过多次调试,bug已完全解决
以下为写代码的过程:
------------------初次尝试----------------------
//bug未解决!!!
这bug也太难受了,等我解决bug之后,再来回帖!!!
//bug未解决!!!
//LNK1120不需要修复此错误。 如果在生成输出中更正了所有 LNK2001 和 LNK2019 链接器错误,则此错误将消失。
#include< iostream >
#include< fstream >
#include< string >
#include<string.h>
#include<stdio.h>
#include<errno.h>
using namespace std;
#define NUM 7 //注意:不用加分号
bool statistics(const char* path, int ball_16[33]);
int main() {
string filename;
int ball_16[33] = { 0 };//一定要记得初始化
cout << "请输入文件名:\n" ;
cin >> filename;
if (statistics(filename.c_str(), ball_16),33) {
for (int i = 0;i < 33;i++) {
cout << "第" << i+1 <<"球出现次数:" << ball_16[i] << endl;
}
}
else {//统计失败
cerr << "统计出错!" << endl;
}
system("pause");
return 0;
}
bool statistics(const char* path, int ball_16[],int len) {
char result[NUM];//7
ifstream file;
int i = 0;
if (!path) {
cerr << "path is NULL." << endl;
return false;
}
file.open(path);
if (file.fail()) {
cerr << "打开输入文件出错." << strerror_s(result, 7, errno) << endl;
}
//从数据文件读取数据到数组,一行必须能读取7个
do {
i = 0;
for (i = 0;i < NUM;i++) {
file >> result[i];
if (file.eof()) {
break;
}
if (file.fail()) {
cerr << "读取文件失败,原因:" << strerror_s(result,7,errno) << endl;
}
}
if (i == 0) break;//记录正常结束
//如果到最后未满七个
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 >= 0 && index < len) {
*(ball_16 + index) += 1;
}
}
cout << endl;
} while (1);
file.close();
return true;
}
-------------------------------printf(“分割线”);--------------------------------------
问题(烦恼)解决,王者归来!!!
//bug还是未解决!(bug产生于vs2019版本,如果是其他版本,maybe没有哦!)
//bug还是未解决的原因:因为strerror_s(result, 7, errno)的功能与项目会产生矛盾.
矛盾如下:
要使用strerror_s,
= > strerror_s(result, 7, errno)
= > 里面的result必须是char * 类型的
= > 而我们需要的result必须是int类型的才能在文件中读取数据
= > 如果result使用int类型
= > 程序就会报错
图片1:
= > 如果result使用char类型
= > 程序就只能读取一位数
= > 程序可以运行, 但达不到目的
图片2:
*//虽然bug未解决,但不影响项目的实现,只要不使用strerror_s就可以了,=======>看这句话!!!*
//续上句话: strerror_s的作用是,如果打印错误,就输出打印错误的原因,
//续上句话: 既然如此,那么我们可以不使用呀,
//续上句话: 如果打印错误,我们可以使用cerr << “打开输入文件出错.” << endl;
//续上句话: 来输出 “打开输入文件出错.”
//续上句话: 那么唯一的缺点就是不知道 打印错误的原因
//LNK1120不需要修复此错误。 如果在生成输出中更正了所有 LNK2001 和 LNK2019 链接器错误,则此错误将消失。
//LNK1120出现的问题可能是 函数的声明和调用 与 定义 的不