2014-02-11 31 views
0

昨晚我問了一個關於計算機科學課程的問題,其中我必須將中綴轉換爲後綴符號並進行評估。我能夠調試它並使其工作(有點),但是,我仍然爲某些表達式輸出奇怪的輸出。C++中綴到Postfix適用於某些輸入,但不適用於其他輸入?

它適用於像7 + 7,3 + 2等基本輸入的第一次迭代......但是,一旦你添加括號或其他表達式,它就會走出怪誕,我不知道爲什麼。

代碼如下,以及我正在獲取的文本文件/輸出。

頭文件

#ifndef STACK_H 
#define STACK_H 
///////////////////////////////////////////////////// 
////Includes///////////////////////////////////////// 
///////////////////////////////////////////////////// 
#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <stdlib.h> 
#include <iomanip> 
#include <sstream> 
#include <stdio.h> 
///////////////////////////////////////////////////// 
using namespace std; 

/*-------------------------------------------------------------------------------------------------------------*/ 
template <typename Object> 
class Stack 
{ 
private: 
    class stackListNode 
     { 
     public: 
      Object data; 
      stackListNode *next; 
     private: 
      //Nothing to declare-->placeholder since class 
      //sets data members to private initially 
     }; 

    stackListNode *top; 

public: 
    ///////////////////////////////////////////////// 
    //Constructor Function////////////////////////// 
    Stack() {top = NULL;} 

     ///////////////////////////////////////////////// 
     //Rest of functions defined inline for simplicity 

     void push(char token) // Push token onto the stack and create new node for top of stack 
      { 
       stackListNode *newNode = new stackListNode; 
       newNode->data = token; 
       newNode->next = top; 
       top = newNode; 
      } 

     char pop() 
      { 
       if(empty()) 
       { 
        cout << "Stack is empty!\n" << endl; 
        return NULL;     
       } 

       stackListNode *temp = top; 
       top = temp->next; 
       return temp->data; 
      } 

     char peek() 
      { 
       if(empty()) 
       { 
        cout << "Stack is empty!\n" << endl; 
        //exit(1); 
        return NULL;     
       } 
       return top->data; 
      } 

     int empty() 
      { 
       return top == NULL; 
      } 
}; 

#endif 

司機:

///////////////////////////////////////////////////// 
////Includes///////////////////////////////////////// 
///////////////////////////////////////////////////// 
#include <iostream> 
#include <fstream> 
#include <string> 
#include "Stack.h" 
///////////////////////////////////////////////////// 
using namespace std; 

int precendence(char stack_beginning); //overloaded for character sitting at the front of the stack 
void InFixToPostfix(ifstream& in_file); 
//double EvaluatePostfix(double first_operand, double second_operand, char*myArray); 

int main() 
{ 
////VARS///////////////////////////////////////////// 

    string absolutePath; 

    cout << endl; 
    cout << "Please type in the name of the file you would to open \n"; 
    cin >> absolutePath; 

    ifstream in_file; 
    in_file.open(absolutePath.c_str()); 
    if(!in_file) 
    { 
     cout << "failed to open input file\n" ; 
     return 1 ; 
    } 
    else 
    { 
     InFixToPostfix(in_file); //kicks off program 
    } 

} 

void InFixToPostfix(ifstream& in_file) 
{ 
    string infix; 
    int right_parentheses = 0; 
    int left_parentheses = 0; 

    while(getline(in_file, infix)) 
    { 
     char myArray[infix.length()]; 
     strcpy(myArray, infix.c_str()); 


     ////////Declares a STRING Stack//////////////////////////////// 
     Stack<char> stack_string; 
     ////////Declares an Int Stack///////////////////////////////// 
     Stack<double> stack_int; 
     ////////////////////////////////////////////////////////////// 
     int j = 0; 
     char *postfix = new char[infix.length()]; 
     for(int i = 0; i < sizeof(myArray); i++) 
     { 
      int number = myArray[i] - '0'; 
      if(number > 0) 
      { 
       postfix[j] = myArray[i]; 
       j++; 
       //outputs the number b/c it is an operand 
      } 
      else if(myArray[i] == ')') 
      { 
       while(stack_string.peek() != '(') 
       { 
        cout << stack_string.peek() << " "; 
        stack_string.pop(); //pops to the peek 
       } 
      stack_string.pop(); // if there is a), pops to the peek 
      } 
      else if(myArray[i] == '+' || myArray[i] == '-' || myArray[i] == '/' || myArray[i] == '*' || myArray[i] == '(') 
      { 
       if(!stack_string.empty()) 
        { 
         char stack_beginning = stack_string.peek(); 
         int stack_top = precendence(stack_beginning); 
         //int stack_top = precendence(stack_string.peek()); 
         int operatorHierarchy = precendence(myArray[i]); 
         //must be declared here because i will have been interated through array 
         while(operatorHierarchy >= stack_top) 
         { 
          stack_string.pop(); 
          postfix[j] = myArray[i]; 
          j++; 
          stack_top = precendence(stack_beginning); 
          operatorHierarchy = precendence(myArray[i]); 
         } 
        } 
       stack_string.push(myArray[i]); 
      } 
     } 
     while(!stack_string.empty()) 
     { 
      char c = (char)stack_string.pop(); 
      postfix[j] = c; 
      j++; 

     } 

     //////////Evaluate Section///////////////////////////// 
     cout << postfix <<endl; 
     for(int i = 0; i < j; i++) 
     { 
      //cout<<myArray[i]<<endl; 
      cout << postfix[i] <<endl; 
      int number = postfix[i] - '0'; 
      if(number > 0) //this is a number 
      { 
       stack_int.push(number); 
       cout << stack_int.peek(); 
      } 
      else if(postfix[i] == '*' || postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '/') 
      { 
       double first_operand; 
       first_operand = stack_int.peek(); //fetches first operand on the stack_int 
       stack_int.pop(); 
       ////////////////// 
       double second_operand; 
       second_operand = stack_int.peek(); 
       stack_int.pop(); 
       ////////////////// 
       if(postfix[i] == '+') 
       { 
        stack_int.push(second_operand + first_operand); 
       } 
       else if(postfix[i] == '-') 
       { 
        stack_int.push(second_operand - first_operand); 
       } 
       else if(postfix[i] == '*') 
       { 
        stack_int.push(second_operand * first_operand); 
       } 
       else if(postfix[i] == '/') 
       { 
        stack_int.push(second_operand/first_operand); 
       } 
      } 
     } 
    cout << (double)stack_int.pop() << endl; 
    } 
} 

int precendence(char stack_beginning) 
{ 
    int precendence; 
    if(stack_beginning == '(') 
    { 
     precendence = 3; 
     return precendence; 
    } 
    else if(stack_beginning == '*' || stack_beginning == '/') 
    { 
     precendence = 2; 
     return precendence; 
    } 
    //by making it 2, the precendence is dubbed less than +/- 
    else if(stack_beginning == '+' || stack_beginning == '-') 
    { 
     precendence = 1; 
     return precendence; 
    } 
    else 
    { 
     return 0; 
    } 
} //by making it 1, the precendence is dubbed greater than */"/" 

文本文件:

9+5 
(4+2) 
(3+8)/2 

輸出:

Please type in the name of the file you would to open 
f.txt 
95+ 
9 
    5 
+ 
14 
+ 42 
4 
2 
2 
+ 382/ 
3 
8 
2 
/
4 

回答

1

這裏有一些問題我已經確定:

C風格的字符串或std ::字符串,但不能同時
您正在使用strcpystd::string::c_str()。 堅持std::string
您可以通過視爲數組來訪問單個字符,字符串[5]返回第6個字符。

炭堆從串堆疊
您的意見說字符串堆棧不同,但尚未聲明Stack<char>這是字符的堆棧,而不是一個字符串。
同樣,堆棧不是一堆整數。
當您應該使用std::string的變量時,您正在動態分配一個數組的字符。

使用庫函數。
參見isdigittouppertolower

使用調試器
現在是使用調試器的好時機。單步執行程序並查看變量。

使用print語句
如果你拒絕使用調試器,與行號代碼的地方「打印」語句讓你的程序的行爲的快照。

相關問題