文件存放数据结构
原创
©著作权归作者所有:来自51CTO博客作者yandozhaoji32的原创作品,请联系作者获取转载授权,否则将追究法律责任
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
#define BUFSIZE 1024
#define LINESIZE 26
fstream tongji;
class Number{
public:
int index;
int date;
int num1;
int num2;
int num3;
int num4;
int num5;
bool compare;
int step;
Number* last;
public:
Number(){
}
bool compare2(Number * number){
if(number->num1 == num1)
return true;
return false;
}
string toString(){
string str;
char buffer[20];
sprintf(buffer, "%d ", index);
str.append(buffer);
sprintf(buffer, "%d ", date);
str.append(buffer);
sprintf(buffer, "%d ", num1);
str.append(buffer);
sprintf(buffer, "%d ", num2);
str.append(buffer);
sprintf(buffer, "%d ", num3);
str.append(buffer);
sprintf(buffer, "%d ", num4);
str.append(buffer);
sprintf(buffer, "%d ", num5);
str.append(buffer);
sprintf(buffer, "%d ", step);
str.append(buffer);
return str;
}
};
void createNumberArray(vector<Number*> *vec){
char buffer [BUFSIZE];
fstream readFile;
fstream writeFile;
readFile.open("info.txt" , ios_base::in | ios_base::out);
writeFile.open("out.txt" , ios_base::in | ios_base::out | ios_base::trunc);
while(!readFile.eof()){
readFile.getline(buffer, BUFSIZE);
string str(buffer);
int index = 0;
int tmp = 0;
while((tmp = str.find_first_of((char)9, tmp)) != -1)
str.replace(tmp, 1, " ", 2);
while((index = str.find_first_of(',' , index)) != -1)
str.replace(index, 1, " " , 2);
writeFile.write(str.c_str(), str.length());
writeFile.write("\r\n",2);
}
writeFile.seekg(0);
while(!writeFile.eof())
{
writeFile.getline(buffer, BUFSIZE);
Number* num = new Number();
string str(buffer);
int index1 = 0;
int index2 = 0;
int i = 0;
while((index1 = str.find_first_not_of(' ' , index1)) != -1)
{
if((index2 = str.find_first_of(' ', index1)) == -1)
{
index2 = str.length();
}
string substr(str.substr(index1,index2-index1));
index1 = index2;
*((int *)(num) + i) = atoi(substr.c_str());
i++;
}
num->compare = false;
num->step = 0;
num->last = NULL;
vec->push_back(num);
}
readFile.flush();
readFile.close();
writeFile.flush();
writeFile.close();
}
int main(){
tongji.open("tongji.txt" , ios_base::in | ios_base::out | ios_base::trunc);
vector<Number*> vec;
createNumberArray(&vec); //将中奖号码数据结构化
int total = vec.size();
for(unsigned int i = 0; i < vec.size(); i++)
{
bool find = false;
int last = i;
int count_number = 0;
if(vec[i]->compare == true)
continue;
else{
int count2 = 0;
for(unsigned int j = i+1; j < vec.size(); j++){
if(true == vec[i]->compare2(vec[j])){
count_number++;
find = true;
if(vec[i]->compare == false){
tongji.write(vec[i]->toString().c_str(), vec[i]->toString().length());
tongji.write("\r\n", 2);
vec[i]->compare = true;
}
vec[j]->compare = true;
vec[j]->step = vec[j]->index - vec[last]->index;
last = j;
if(vec[j]->step < 3){
count2++;
}
tongji.write(vec[j]->toString().c_str(), vec[j]->toString().length());
tongji.write("\r\n", 2);
}
}
cout<<count2<<" "<<count_number<<" "<< double(count2)/(double)count_number <<endl;
cout<<(double)count_number/(double)total<<" "<<vec[i]->num1<<endl;
if(find == true){
tongji.write("\r\n", 2);
tongji.write("\r\n", 2);
tongji.write("\r\n", 2);
}
}
}
cout<<(double)1/(double)11<<endl;
return 0;
}
上一篇:文件内存映射
下一篇:WinPcap编程环境设置
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章