c++ - parse collection of single integers into floats -


so slurped in file numbers in like: 39.00 vector<std::string> , need convert groups of numbers 3 9 0 0 form 39.00

heres small sample.

3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 3 2 1 1 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 2 3 0 0 0 2 4 0 0 4 5 0 0 6 7 0 0 6 5 5 0 5 6 9 0 8 7 0 0 4 3 5 0 5 6 9 8 5 5 4 0 3 3 6 2 0 0 3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 0 3 2 1 1 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 0 0 0 2 5 0 0 3 0 9 0 4 0 0 0 5 5 5 0 2 2 3 0 0 0 2 4 0 0 4 5 0 0 6 7 0 0 6 5 5 0 5 6 9 0 8 7 0 0 4 3 5 0 5 6 9 8 5 5 4 0 3 3 6 2 0 0 3 4 5 0 1 2 5 0 3 4 0 0 3 4 9 0 7 0 0 0 8 0 0 0 9 0 0 0 6 5 0 0 3 9 0 0  

transformed 34.50 12.50 34.00....

my goal find average of floats.

of course if there way slurp file while keeping formatting using standard library cool too.

#include <iostream> #include <string> #include <fstream> #include <streambuf> #include <regex> #include <vector> #include <math.h>  void tablewriter(std::string); float employeeaverage(std::string); float employeetotal(std::string); float totalaverage(std::string);  void totalpayroll(std::string, std::vector<std::string>);   std::string getemployeename(std::string, std::string[]);   int main(int argc, const char * argv[]) {     try {     std::vector<std::string> regexcontainer;      std::ifstream t("thesales.txt");     std::string thesales;      t.seekg(0, std::ios::end);     thesales.reserve(t.tellg());     t.seekg(0, std::ios::beg);      thesales.assign((std::istreambuf_iterator<char>(t)),                std::istreambuf_iterator<char>());      //std::cout << thesales << std::endl;      totalpayroll(thesales, regexcontainer);     std::cout << std::endl << regexcontainer.empty() << std::endl;      return 0;     } catch (int w) {         std::cout << "could not open file. exiting now." << std::endl; return 0;     } }   void tablewriter(std::string){} float employeeaverage(std::string){return 0.0;} float employeetotal(std::string){return 0.0;} float totalaverage(std::string){return 0.0;}    void totalpayroll(std::string thesales, std::vector<std::string> regexcontainer) {      std::string matches;     std::regex pattern ("\\d");      const std::sregex_token_iterator end;     (std::sregex_token_iterator i(thesales.cbegin(), thesales.cend(), pattern);          != end;          ++i)     {         regexcontainer.push_back(*i);         (std::vector<std::string>::const_iterator = regexcontainer.begin(); != regexcontainer.end(); ++i)             std::cout << *i << ' ';     }   } 

this data:

2.40 5.30 6.30 65.34 65.34 3.40 7.80 3.20 65.34 65.34 3.40 5.20 8.20 23.54 12.34 2.42 5.30 6.30 5.00  65.34 3.44 7.80 3.20 34.55 65.34 3.45 5.20 8.20 65.34 65.34 

functions such fscanf able read file , return correctly formed float number. should more efficient trying reconstruct them stream of char...


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -