Asked Mar 2nd, 9:33 AM 87 0 1
  • 87 0 1
0

Mình muốn tìm cách chuyển đổi từ tiền tố sang hậu tố

Share
  • 87 0 1

#include "stack.hpp"

using namespace std;

// Auxiliary method, you probably find it useful
// Operands are all lower case and upper case characters
bool isOperand(char c){
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

// Auxiliary method, you probably find it useful
int precedence(char c)
{
  if(c == '+' || c == '-'){
    return 0;
  }
  if(c == '*' || c == '/'){
    return 1;
  }
  if(c == '^'){
    return 2;
  }
  return -1;
}

int main(){
  freopen("input_infix2postfix.txt", "r", stdin);
  string input;
  string output; // output string
  string solution;
  string dummy;
  string here;
    int j = 0;
    int con;
  int line_counter = 0;
  while(cin >> solution){
    cin >> input;
    Stack<char> stack;
    string result;

 //The input file is in the format "expected_solution infix_expression", 
 //where expected_solution is the infix_expression in postfix format
int x = 0;
for(int i=0; i<input.length(); ++i){
  // WRITE CODE HERE to store in 'result' the postfix transformation of 'input'
if (isOperand(input[i]) )
{
    output[j++] = input[i];	
    //cout << "output: " << output[i] << endl;	
}


else if (input[i] == '(' ) 
{
    stack.push(input[i]);
}	

else if (input[i] == ')' )
{
    while (stack.peek() != '(' )
    {
        output[j++] = stack.peek();
        stack.pop();
    }
    stack.pop();	
    }
else if (input[i] == '*' || input[i] == '/' || input[i] == '+' || input[i] == '-' || input[i] == '^')
{				
    stack.push(input[i]);
    //cout << "input: " << stack.peek() << endl;
        if (precedence(stack.peek()) >= precedence(input[i]) )
        {

                output[j++] = stack.peek();
                stack.pop();		
        }
        else 
            { break; }

}			
}
// You need to do some extra stuff here to store in 'result' the postfix transformation of 'input'

result = output;

// Checking whether the result you got is correct
if(solution == result){
  cout << "line " << line_counter << ": OK [" << solution << " " << result << "]" << endl;
}else{
  cout << "line " << line_counter << ": ERROR [" << solution << " " << result << "]" << endl;
}
line_counter++;
result.clear();	
  }
}

Mình đang bị lỗi là nó không in ra đúng như solution trong test file. Bên trái là solution (hậu tố), bên phải là expression mình cần phải chuyển đổi (tiền tố). Mình đang muốn chuyển từ tiền tố sang hậu tố.

Test file của mình: input_infix2postfix.txt
        ab+	a+b
        abcd^e-fgh*+^*+i-	a+b*(c^d-e)^(f+g*h)-i
        ab+cd+*		(a+b)*(c+d)
Hoàng Đức Quân @devil_boom_129
Mar 2nd, 5:13 PM

bạn dùng cách đánh này để highlight code nhé

#include "stack.hpp"
using namespace std;

// Auxiliary method, you probably find it useful
// Operands are all lower case and upper case characters
bool isOperand(char c){
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

// Auxiliary method, you probably find it useful
int precedence(char c)
{
  if(c == '+' || c == '-'){
    return 0;
  }
  if(c == '*' || c == '/'){
    return 1;
  }
  if(c == '^'){
    return 2;
  }
  return -1;
}

int main(){
  freopen("input_infix2postfix.txt", "r", stdin);
  string input;
  string output; // output string
  string solution;
  string dummy;
  string here;
    int j = 0;
    int con;
  int line_counter = 0;
  while(cin >> solution){
    cin >> input;
    Stack<char> stack;
    string result;

 //The input file is in the format "expected_solution infix_expression", 
 //where expected_solution is the infix_expression in postfix format
int x = 0;
for(int i=0; i<input.length(); ++i){
  // WRITE CODE HERE to store in 'result' the postfix transformation of 'input'
if (isOperand(input[i]) )
{
    output[j++] = input[i];	
    //cout << "output: " << output[i] << endl;	
}


else if (input[i] == '(' ) 
{
    stack.push(input[i]);
}	

else if (input[i] == ')' )
{
    while (stack.peek() != '(' )
    {
        output[j++] = stack.peek();
        stack.pop();
    }
    stack.pop();	
    }
else if (input[i] == '*' || input[i] == '/' || input[i] == '+' || input[i] == '-' || input[i] == '^')
{				
    stack.push(input[i]);
    //cout << "input: " << stack.peek() << endl;
        if (precedence(stack.peek()) >= precedence(input[i]) )
        {

                output[j++] = stack.peek();
                stack.pop();		
        }
        else 
            { break; }

}			
}
// You need to do some extra stuff here to store in 'result' the postfix transformation of 'input'

result = output;

// Checking whether the result you got is correct
if(solution == result){
  cout << "line " << line_counter << ": OK [" << solution << " " << result << "]" << endl;
}else{
  cout << "line " << line_counter << ": ERROR [" << solution << " " << result << "]" << endl;
}
line_counter++;
result.clear();	
  }
}

Screen Shot 2019-03-03 at 12.12.43 AM.png

+1
| Reply
Share

1 ANSWERS


Answered Mar 2nd, 6:22 PM
0

Bạn thử tham khảo cách trong này xem:

https://www.geeksforgeeks.org/prefix-infix-conversion/

Share