2014-03-26 28 views
0

這是一個兩部分的問題,我希望我能讓自己明白。我將根據需要進行編輯!我試圖編寫一個程序,它將從輸入文件中繼續進行計算。該文本文件將是這個樣子:從輸入文件到輸出文件的算術?

int + 25 10 
double/5.5 8.5 
... 

每個實例與類型如int,雙,浮法,等開始,然後計算類型(加,減,等等),然後兩個數字。我希望能夠連續閱讀並輸出總和,產品等到輸出文件。舉例來說,如果我們使用上面的第一個例子中,該文件中的輸出將是:

int 25 10 = 35 

我有代碼會做如下計算:

void doAddition(ifstream &inFile) { 
int num1, num2; 
inFile >> num1 >> num2; 
cout << num1 << num2 << " = "<< (num1+num2) << '\n'; } 

與此唯一的問題是,我不知道如何添加變量(我一直在使用字符串嘗試,但它似乎不工作)的類型,例如「INT」或「雙」,所以我得到:

25 10 = 35 

相反作者:

int 25 10 = 35 

我的第二個問題,你可能會看到,我正在使用「cout」在屏幕上顯示信息,當我真的想將它添加到outfile。這裏的一些信息:

我用的移動到下一行:我評論

ifstream inFile; 
//ofstream outFile; 
char ch; 
int num1, num2; 

inFile.open("infile.txt"); 
//outFile.open("outfile.txt"); 

if (inFile.is_open()){ 

    inFile >> ch; 
    while (!inFile.eof()) 
    { 
     switch (ch) 
     { 
      case '+': 
       doAddition(inFile); 
       break; 
... 

正如你可以看到外面ofstream的一部分,因爲我:

void readToNextLine(ifstream& inFile) { 
string t1, t2; 
inFile >> t1 >> t2; } 

代碼在我的主無法讓它正常工作。有什麼建議麼?我現在打開大約10個窗口和兩個C++書籍,試圖將它們放在一起!

編輯:我不確定開關是否是最好的方法。我需要程序看到「int」並意識到這是一個詞。如果我使用了4種變量類型,如int,double,float和long,也許我可以檢查每個變量的第一個字母:i,d,f,l,然後一旦知道它可以進入+, - 等等檢查。感覺就像這樣做邏輯上它只是花更多的時間,當我可以使用一系列的類,但我只是不知道從哪裏開始。

+0

如果有需要的任何其他信息,或者說我不會讓自己清楚,請讓我知道,我會相應地編輯自己的帖子! – Hydlide

+0

我建議將其分解爲單獨的簡明問題。它會看起來更吸引SO用戶回答,並且會迫使你分解它。 – Andy

回答

1

我真的不理解所有這些麻煩閱讀文件。在Stackoverflow和Web上有太多的例子。也許是人們不搜索,或者他們要求一個與他們的確切代碼相匹配的例子。

試試這個:

struct Input_Record 
{ 
    std::string data_type_as_string; 
    std::string operation; 
    std::string value1_as_string; 
    std::string value2_as_string; 

    friend std::istream& operator>>(std::istream& inp, Input_Record& r); 
}; 

std::istream& operator>>(std::istream& inp, Input_Record& r) 
{ 
    inp >> r.data_type_as_string; 
    inp >> r.operation; 
    inp >> r.value1_as_string; 
    std::getline(inp, r.value2_as_string); // Use getline to eat the line ending. 
} 

// ... 
Input_Record r; 
while (input_file >> r) 
{ 
    // Do stuff with r 
}; 

如果你真的想有一些樂趣,你可以使用一個父基類和工廠模式一般創建基於輸入對象:

class Binary_Operation // Base class for factory pattern. 
{ 
    public: 
    //! Evaluate the object and return the result as a string 
    // e.g. numbers as text 
    virtual std::string evaluate(void) const = 0; 
}; 
class Binary_Integer_Operation : public Binary_Operation 
{ 
    public: 
    std::string evaluate(void) const 
    { 
     // Convert values to integers than perform the operation. 
     // Convert result to string using std::istringstream. 
    }; 
}; 
class Binary_Double_Operation : public Binary_Operation 
{ 
    // See Binary_Integer_Operation above. 
}; 

這允許你做類似的事情:

Binary_Operation * factory_create(const Input_Record& r) 
{ 
    Binary_Operation * p_obj = nullptr; 
    if (r.data_type == "int") 
    { 
    p_obj = new Binary_Integer_Operation; 
    // Initialize fields 
    } 
    if (r.data_type == "double") 
    { 
    p_obj = new Binary_Double_Operation; 
    // Initialize fields; 
    } 
    return p_obj; 
} 

你的處理循環看起來像這樣:

Input_Record r; 
while (input_file >> r) 
{ 
    Binary_Operation * p_operation = factory_create(r); 
    std::string result = p_operation->evaluate(); 
    cout << "result = " << result << "\n"; 
    delete p_operation; 
} 
+0

這是完美的,很多不同的東西來修補。非常感謝! – Hydlide

1

讓我們開始喜歡你提供的一個例子:

int + 25 10 

類型的「類型」和算術運算符分別是簡單,std::stringchar

std::ifstream in("infile.txt"); 
std::string type; char op; 

if (in >> type >> op) 
{ 
    // ... 
} 

對於其他兩個值,還必須將它們提取到的字符串,因爲你首先要找出type值,然後才能將它們轉換:

if (in >> type >> op >> a >> b) // a and b are strings 

現在使用的功能,檢查type並轉換ab正確類型:

void convertTo(std::string const& typeName, std::string const& a, std::string const& b, char op) 
{ 
    if (typeName == "int") 
    { 
     int a1 = std::stoi(a), 
      b2 = std::stoi(b); 

     doOperation(op, a1, b2) 
    } else if (typeName == "double") { 
     double a1 = std::stod(a), 
       b2 = std::stod(b); 

     doOperation(op, a1, b2); 
    } else if (typeName == "float") { 
     // std::stof() 
    } 
} 

doOperation()是模板化,並執行這樣的:

template<typename T> 
struct F; 

template<> struct F<int> { static const std::string value = "int"; }; 
template<> struct F<double> { static const std::string value = "double"; }; 
template<> struct F<float> { static const std::string value = "float"; }; 

template<typename U, std::string name = F<U>::value> 
void doOperation(char op, U a, U b) 
{ 
    std::ofstream out("outfile.txt"); 
    switch (op) 
    { 
     case '+': 
      out << name << " " << op << " " << (a + b); 
     case '-': 
      out << name << " " << op << " " << (a - b); 
     case '/': 
      // ... 
     // ... 
    } 
} 
+0

非常感謝你,這些都是很好的例子,並且會有很大的幫助! – Hydlide