Guest User

Untitled

a guest
Jul 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #ifndef _H_READFILE
  2. #define _H_READFILE
  3.  
  4. #include <string>
  5. #include <fstream> //Read File
  6. #include <math.h> //pow(x,y)
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. /*
  12.     0: 48
  13.     9: 57
  14.     -: 45
  15. */
  16.  
  17. // Converts a string into a number
  18. int stringtoint(string input, bool &error) {   
  19.  
  20.     int number = 0; //our converted number
  21.     bool sign = false; //positive or negative number
  22.  
  23.     if((int)input[0] == 10) { //Zeilumbruchzeichen rauslöschen
  24.         string new_input = "";
  25.  
  26.         for(unsigned int i = 0; i < input.size() - 1; i++) {
  27.             new_input += input[i + 1];
  28.         }
  29.  
  30.         input = new_input;
  31.     }
  32.  
  33.     if((int)input[0] == 45) { //Vorzeichen einbeziehen
  34.         number *= -1;
  35.  
  36.         //Vorzeichen aus dem String löschen
  37.         string new_input = "";
  38.  
  39.         for(unsigned int i = 0; i < input.size() - 1; i++) {
  40.             new_input += input[i + 1];
  41.         }
  42.  
  43.         input = new_input;
  44.     }
  45.  
  46.     for(unsigned int i = 1; i <= input.size(); i++) {
  47.         if(((int)input[i - 1] < 48) || ((int)input[i - 1] > 57)) { //Number not found
  48.             error = true;
  49.             break; //leave for-loop because wrong character found
  50.         } else {
  51.             number += ((int)input[i - 1] - 48) * pow(10, input.size() - i);
  52.         }  
  53.     }
  54.  
  55.     if(sign) {
  56.         number *= -1;
  57.     }
  58.  
  59.     return number;
  60. }
  61.  
  62. //Liest Datei ein, schreibt den Inhalt in die Matrizen rein und gibt zurück ob alles geklappt hat oder nicht
  63. bool ReadFile(string filename, vector<vector<int> > &mat_pref, vector<vector<int> > &mat_cost) {
  64.  
  65.     ifstream file;
  66.     string line;
  67.  
  68.     bool error = false; //readerror
  69.    
  70.     unsigned int nodes = 0;
  71.  
  72.     file.open(filename.c_str(), ios::in);
  73.  
  74.     if(file.good()) {
  75.  
  76.         getline(file, line);
  77.  
  78.         nodes = stringtoint(line, error);
  79.  
  80.         if(error) {
  81.             cout << "Unknown number found in input file: " << line << endl;
  82.             cout << "Aborting prograrm" << endl;
  83.  
  84.             return false;
  85.         }
  86.  
  87.         cout << nodes << endl;
  88.         vector<int> temp(nodes, 0);
  89.  
  90.         for(unsigned int i = 0; i < nodes; i++) {
  91.             //"nodes" Zahlen die mit Leerzeichen separiert sind einlesen
  92.             for(unsigned int j = 0; j < nodes; j++) {
  93.                 getline(file, line, ' ');
  94.                 temp[j] = stringtoint(line, error);
  95.  
  96.                 if(error) {
  97.                     cout << "Unknown number found in input file: " << line << endl;
  98.                     cout << "Aborting prograrm" << endl;
  99.                    
  100.                     return false;
  101.                 }
  102.             }  
  103.             mat_pref.push_back(temp); //"Array"(Zeile) in die "Matrix" einfügen
  104.         }
  105.        
  106.         //"nodes" Zeilen einlesen
  107.         for(unsigned int i = 0; i < nodes; i++) {
  108.             //"nodes" Zahlen die mit Leerzeichen separiert sind einlesen
  109.             for(unsigned int j = 0; j < nodes; j++) {
  110.                 getline(file, line, ' ');
  111.                 temp[j] = stringtoint(line, error);
  112.                 if(error) {
  113.                     cout << "Unknown number found in input file: " << line << endl;
  114.                     cout << "Aborting prograrm" << endl;
  115.                    
  116.                     return false;
  117.                 }
  118.             }  
  119.  
  120.             mat_cost.push_back(temp); //"Array"(Zeile) in die "Matrix" einfügen
  121.         }
  122.        
  123.         file.close();
  124.  
  125.         return true; //Alles hat geklappt
  126.     } else { //Datei konnte nicht zum Lesen geöffnet werden
  127.         cout << "File not found or broken!" << endl;
  128.         file.close();
  129.         return false;
  130.     }
  131. }
  132. #endif
Add Comment
Please, Sign In to add comment