Hi
In my previos post I have asked about how to search for number form one file into
another , the 1st file I have contains number and state , 2nd file contains number and value
If the number from first file is found the program take 6 values before and 6 values after the found number and assigns (1) to them , if there is no match number value is saved with (-1) into .csv file .
the problem is :
1. before finding a match it will assign (-1) to the number value pair in second file , but when it finds the match it will again
assign (1) to the same number value that assigned (-1) before .
2. if there numbers in the first file like (380,381), the program will mix the +6,-6 number value for (380,386) and it will be like this :
374 0.3 -1
375 0.288462 -1
376 0.288462 -1
377 0.289614 -1
378 0.307465 -1
379 0.20198 -1
374 0.3 1
375 0.288462 1
376 0.288462 1
377 0.289614 1
378 0.307465 1
379 0.20198 1
380 0.166522 1
381 0.166522 1
382 0.16 1
383 0.24 1
384 0.249815 1
385 0.270398 1
386 0.269032 1
375 0.288462 1
376 0.288462 1
377 0.289614 1
378 0.307465 1
379 0.20198 1
380 0.166522 1
381 0.166522 1
382 0.16 1
383 0.24 1
384 0.249815 1
385 0.270398 1
386 0.269032 1
387 0.252771 1
while it should be like this :
374 0.3 1
375 0.288462 1
376 0.288462 1
377 0.289614 1
378 0.307465 1
379 0.20198 1
380 0.166522 1
381 0.166522 1
382 0.16 1
383 0.24 1
384 0.249815 1
385 0.270398 1
386 0.269032 1
387 0.252771 1
the two files here :
file1 :
https://drive.google.com/file/d/0B03WnxRSaChVUWlHWFlfZmgycUk/view?usp=sharing
file 2:
https://drive.google.com/file/d/0B03WnxRSaChVUjdONXJGRGdNOTg/view?usp=sharing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
int main ()
{
int number = 0;
double value = 0;
std::string state;
int key = 0;
std::map<int, std::string> map_f1;
std::map<int, double> map_f2;
std::map<int,double>::iterator it2, it3;
std::ifstream file1 ("file1.txt");
std::ifstream file2 ("file2.txt");
std::ofstream out;
std::vector<double> vec_f2;
out.open("13_EAR_RIGHT_v1.csv");
if ( file1.is_open() and file2.is_open() )
{
while ( file1 >> number >> state )
map_f1[number] = state;
file1.close();
int nextnumber = 0;
while ( file2 >> number >> value )
{
// fill gaps with -1.0
if (number > nextnumber)
{
vec_f2.insert(vec_f2.end(), number - nextnumber ,-1.0);
nextnumber = number;
}
if (number == nextnumber)
{
vec_f2.push_back(value);
nextnumber++;
}
}
file2.close();
int maxnum = (int)vec_f2.size();
for(auto it2 = vec_f2.begin()+1; it2 != vec_f2.end(); it2++)
{
int number = it2-vec_f2.begin();
auto it1 = map_f1.find( number);
if (it1 != map_f1.end())
{
//out << it1 -> first << " \tclose\t " << *it2 << '\n';
// Display previous 6 and following 6
key = number;
for( int i =std::max(1, key - 6); i < std::min(key + 7, maxnum); i++)
{
out << i << " , " <<vec_f2[i]<<","<< "1"<<'\n';
}
}
else
{
out << number << " , "<< *it2 <<","<<"-1"<<'\n';
}
}
}
else
std::cout << "Unable to open file1 and/or file2\n";
return 0;
}
|