2012-12-03 87 views
0

我是新發布的並且相對較新的C++編碼。在C++中使用堆棧評估後綴時處理十進制值

我的問題涉及: 在評估後綴使用堆棧在c + +處理十進制值。這是一項家庭作業,但我已經獲得了我的代碼以滿足基本要求。我不是在幫我做作業。我的教授希望我們強制執行這項任務,所以我避免了許多使用特殊頭文件的快捷方式,如<stack>

我已經研究過這個問題,並不滿意給出的答案。沒有其他人似乎像我一樣分割他們的代碼。我有一個問題,將檢測小數值的內部循環的值傳遞給主函數的其餘部分。代碼編譯並運行,但我的最終結果是亂碼。我發現一個帖子指出多個值不能從'return'的函數傳遞,但我確實需要從內部十進制函數返回的至少兩個值。我不明白'tuple''pair'命令。

我知道這是一個羅嗦的序曲,但我確實希望任何讀者理解我已經閱讀了前提條件並且沒有做過研究。就我個人而言,我討厭暴力方法。我明白,我的項目可能不會及時提供幫助,但我一直在爲此工作數週。我希望能夠看到這種方法的工作是令人滿意的。建議和指針非常受歡迎。

這裏是鏈接到我的代碼: link

這裏是我的代碼:

//***************************************************************************** 
// Postfix expression evaluation 
// 11/13/2012 
// by DJH 
// 
// all applicable copyrights apply 
// 
// this program evaluates an input postfix string 
// 
// created using Dev-C++ 5.2.0.3 
//***************************************************************************** 

// ----------------------------------libraries--------------------------------- 

#include <iostream>      // For cin, cout and endl 
#include <string> 
#include <cctype> 
#include <vector> 
#include <sstream> 
#include <cstdlib> 
#include <cmath> 
#include <iomanip> 

using namespace std; 
// ------------------------------ Globals ------------------------------------- 
string postfix; 

// --------------------------- stack class ------------------------------------ 
class Stack { 
public: 
    enum {MaxStack = 50}; 
    void init() {top = -1;} 
    void push(double p) { 
     if (isFull()) { 
      cerr << "Full Stack. DON'T PUSH\n"; 
      return; 
     } 
     else { 
      arr[ ++top ] = p; 
      cout << "Just pushed " << p << endl; 
      return;} 
    } 
    int pop() { 
     if (isEmpty()) { 
      cerr << "\tEmpty Stack. Don't Pop\n\n"; 
      return 1; 
     } 
     else 
     return arr[top--]; 
    } 
    bool isEmpty() {return top < 0 ? 1 : 0;} 
    bool isFull() {return top >= MaxStack -1 ? top : 0;} 
    void dump_stack() { 
     cout << "The Stack contents, from top to bottom, from a stack dump are: " << endl; 
     for (int s = top; s >= 0; s--) 
     cout << "\t\t" << arr[s] << endl; 
    } 
private: 
    int top; 
    double arr[MaxStack]; 
} pStack; 
// ------------------------------ end stack class ----------------------------- 

// -----------------------------function prototypes---------------------------- 
void evalPostfix(string); 
double decimalEvaluate(string, int, double); 
// ---------------------------------------------------------------------------- 

// -----------------------------------Main()----------------------------------- 
int main() { 
    cout << "Enter a postfix expression\n\t (without spaces - using '_' for delimiters):\n\t"; 
    cout << "For example: 8_5_3_+_*_2_/_5_+\n" << endl; 
    getline(cin, postfix); 

    // postfix = "7_2_*_5_+_2_*"; 
    cout << "You entered: " << postfix << endl; 

    int c = 0; 

    while (postfix[c] != '\0') { 
     // this loop counts the characters in the input string including 
     // whitespace 
     ++c; 
    } 

    cout << "\tThe string length is:\t" << c << endl; 
    evalPostfix(postfix); 
    int result = pStack.pop(); 
    cout << "The result of the postfix expression is: " << result << endl; 

    /* 
    stack commands: 
    Stack a_stack;     // creates new stack 
    a_stack.init();     // initializes top element 
    a_stack.pop();     // pops top of stack 
    a_stack.push(n);    // push element to top of stack 
    a_stack.dump_stack();   // displays the contents of the stack from top to bottom 
    */ 
    return 0; 
    cin.get(); 
} 

// --------------------------------end of Main()------------------------------- 

// ------------------------------functions follow------------------------------ 
void evalPostfix(string) { 
    double ch = 0, dc = 0, b = 0, a = 0, d = 0; 
    double tempDC = 0; 
    double tempDCD = 0; 
    char op; 
    int i = 0, j = 0, k = 0, m = 0, n = 0, q = 0; 
    while (postfix[i] != '\0') { 

     if (postfix[i] == '_') { 
      i++; 
     } else if (isdigit(postfix[i])) { 
      ch = postfix[i] - '0';   // for numbers only 
      j = i + 1; 
      while (postfix[j] != '_') { 
       if (isdigit(postfix[j])) { 
        ch = ch * 10 + (postfix[j] - '0'); 
        k = j + 1; 
        // this accounts for decimals by skipping the '.' and 
        // conducting operations on trailing numbers 
        if (postfix[k] == '.') { 
         dc = 0; 
         decimalEvaluate(postfix, k, dc); 
         dc = tempDC/tempDCD; 
         d = ch + dc; 
         k++; 
        } 
        j = k - 1; 
        j++; 
       } 
      } 
      cout << "Post decimal function k: " << k << endl; 
      cout << "Post decimal function dc: " << setprecision(12) << dc 
        << endl; 
      cout << "Post decimal function d: " << d << endl; 
      pStack.push(d); 
      i = j - 1; 
      i++; 
     } else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' 
       || postfix[i] == '/' || postfix[i] == '^') { 
      b = pStack.pop(); 
      a = pStack.pop(); 
      op = postfix[i];   // for operators only 
      switch (op) { 
      case '+': 
       pStack.push(a + b); 
       break; 
      case '-': 
       pStack.push(a - b); 
       break; 
      case '*': 
       pStack.push(a * b); 
       break; 
      case '^': 
       pStack.push(pow(a, b)); 
       break; 
      case '/': 
       if (b == 0) { 
        cout << "Division by zero not allowed!" << endl; 
        cin.get(); 
        exit(0); 
       } 
       pStack.push(a/b); 
      default: 
       cout << "Invalid Operation" << endl; 
      } 
      i++; 
     } 

    } 
} 
// ---------------------------------------------------------------------------- 

double decimalEvaluate(string postfix, int k, double dc) { 
    dc = 0; 
    double tempDC = 0; 
    double tempDCD = 0; 
    int n = 0, m = 0, lenDC = 0; 
    n = k; 

    while (postfix[n] != '_') { 
     if ((isdigit(postfix[n])) == false) { 
      n++; 
     } 
     cout << "This step (1) n: " << n << endl; 
     if (isdigit(postfix[n])) { 
      m = n; 
      // assumes characters between a '.' and '_' are all digits 
      // (may need to check) 
      while (postfix[m] != '_') { 
       lenDC++; 
       // this loop counts the digits in the input trailing a decimal 
       // point 
       m++; 
      } 

      cout << "This step (2) m: " << m << endl; 
      cout << "This step (2) lenDC: " << lenDC << endl; 

      while ((postfix[n]) != '_') { 
       tempDC = tempDC * 10 + (postfix[n]) - '0'; 
       n++; 
      } 

      cout << "This step (3) n: " << n << endl; 
      cout << "This step (3) tempDC: " << tempDC << endl; 
     } 

     k = n; 
     tempDCD = pow(10, lenDC); 
     dc = tempDC/tempDCD; 

     cout << "This step (4) k: " << k << endl; 
     cout << "This step (4) tempDCD: " << tempDCD << endl; 
     cout << "This step (4) tempDC: " << tempDC << endl; 
     cout << "This step (4) dc: " << dc << endl; 
    } 

    return dc, k, tempDC, tempDCD; 
} 
+1

歡迎堆棧溢出!與您可能使用的某些其他論壇不同,堆棧溢出是**問題**和**答案**網站。這不是一個問題和解決方案網站,也不是一個討論網站。我無法在您的帖子中找到問題。 *你的問題是什麼?* –

+0

哎呀!道歉。我想知道如何通過參考。我會嘗試Henrik的建議。 – user1872965

回答

0

按引用傳遞變量:

void decimalEvaluate (string postfix, int& k, double& dc, double& tempDC, double& tempDCD) 
{ 
    tempDC = 0.0; 
    //... 
}