2015-12-08 78 views
0

我正在嘗試使用動態編程來查找哪些項目要在一定的權重容量下從文本文件中選擇,同時使值最大化。該文件的格式如下:從文件中讀取C++ Ifstream

item1, weight, value 
item2, weight, value 

可變數量的項目。我在讀取主要方法中的文件時遇到問題,我試圖錯誤地檢查錯誤的輸入。我的問題來自於我檢查重量是否丟失,或者是字符串而不是int。我無法弄清楚如何單獨檢查一個字符串,或者如果在那個地方沒有任何東西。我應該輸出一個不同的錯誤,這取決於它是哪一個。謝謝。

int main(int argc, char * const argv[]) 
{ 
    std::vector<Item> items; 
    if (argc != 3) 
    { 
     std::cerr << "Usage: ./knapsack <capacity> <filename>"; 
     return -1; 
    } 

    int capacity; 
    std::stringstream iss(argv[1]); 

    if (!(iss >> capacity) || (capacity < 0)) 
    { 
     std::cerr << "Error: Bad value '" << argv[1] << "' for capacity." << std::endl; 
     return -1; 
    } 

    std::ifstream ifs(argv[2]); 
    if (ifs.is_open()) 
    { 
     std::string description; 
     unsigned int weight; 
     unsigned int value; 
     int line = 1; 
     while (!ifs.eof()) 
     { 
      if (!(ifs >> description)) 
      { 
       std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl; 
       return -1; 
      } 

      if (!(ifs >> weight)) 
      { 
       if (ifs.eof()) 
        std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl; 
       else 
        std::cerr << "Error: Invalid weight '" << ifs << "' on line number " << line << "." << std::endl; 
       return -1; 
      } 

      else if (!(ifs >> weight) || (weight < 0)) 
      { 
       std::cerr << "Error: Invalid weight '" << ifs << "' on line number " << line << "." << std::endl; 
       return -1; 
      } 

      if (!(ifs >> value)) 
      { 
       if (ifs.eof()) 
        std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl; 
       else 
        std::cerr << "Error: Invalid value '" << ifs << "' on line number " << line << "." << std::endl; 
       return -1; 
      } 

      else if (!(ifs >> value) || (value < 0)) 
      { 
       std::cerr << "Error: Invalid value '" << ifs << "' on line number " << line << "." << std::endl; 
       return -1; 
      } 

      Item item = Item(line, weight, value, description); 
      items.push_back(item); 
      line++; 
     } 

     ifs.close(); 
     knapsack(capacity, items, items.size()); 
    } 

    else 
    { 
     std::cerr << "Error: Cannot open file '" << argv[2] << "'." << std::endl; 
     return -1; 
    } 

    return 0; 
} 

回答

0

我認爲更好的方法將使用getline函數,通過這個函數,你將採取一整行,然後你可以嘗試將它分成三個字符串。如果沒有找到三個部分或三個部分中的任何一個格式不正確,則可能會輸出錯誤。

下面的示例代碼給,

#include<fstream> 
#include<iostream> 
#include<vector> 
#include <sstream> 
#include<string> 

using namespace std; 

void split(const std::string &s, std::vector<std::string> &elems, char delim) { 
    elems.clear(); 
    std::stringstream ss(s); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
    elems.push_back(item); 
    } 
} 

int main() 
{ 
    vector<string> elems; 
    ifstream fi("YOUR\\PATH"); 

    string inp; 
    while(getline(fi, inp)) 
    { 
    split(inp, elems, ' '); 

    if(elems.size() != 3) 
    { 
     cout<<"error\n"; 
     continue; 
    } 

    int value; 
    bool isint = istringstream(elems[1])>>value; 
    if(!isint) 
    { 
     cout << "error\n"; 
     continue; 
    } 

    cout<<"value was : " << value << endl; 
    } 
    return 0; 
} 
+1

您使用的'.EOF()'作爲循環終止是錯誤的。使用'std :: getline(fi,inp);'代替。 (提升''std :: string inp;'退出循環。) – Casey

+0

謝謝@Casey。儘管如此,我仍然展示了主要想法。 – Ultraviolet

+0

謝謝你這篇文章@Md.SumsuddinShojib但是我仍然有一個問題,因爲delim是「,」而不是簡單的''字符。 – spicelord

0

您可以一次讀取整行,並使用正則表達式檢查其格式。