تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[سؤال] كتابة كود
#2
السلام عليكم ورحمة الله

أختي زهرة

هذا الحل موجود في الرابط التالي: Infix to Postfix expression

قمت بتجربته بواسطة ++Dev-C وهو عمل بشكل صحيح
PHP كود :
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;

int priority(char a) {
    
int temp;
    if (
== '^')
        
temp 1;
    else  if (
== '*' || == '/')
        
temp 2;
    else  if (
== '+' || == '-')
        
temp 3;
    return 
temp;
}

int main() {
    
string infix;
    
cout << "Enter an arithmetic expression: " << endl;
    
getline(cininfix);

    
stack<charoperator_stack;

    
stringstream output;

    for (
unsigned i 0infix.length(); i++) {
        if (
infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
            while (!
operator_stack.empty() && priority(operator_stack.top()) <= priority(infix[i])) {
                
output << operator_stack.top();
                
operator_stack.pop();
            }
            
operator_stack.push(infix[i]);
        } else if (
infix[i] == '(') {
            
operator_stack.push(infix[i]);
        } else if (
infix[i] == ')') {
            while (
operator_stack.top() != '(') {
                
output << operator_stack.top();
                
operator_stack.pop();
            }
            
operator_stack.pop();
        } else {
            
output << infix[i];
        }
    }

    while (!
operator_stack.empty()) {
        
output << operator_stack.top();
        
operator_stack.pop();
    }

    
cout << output.str() << endl;

    
cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 
0;

الرد }}}


الردود في هذا الموضوع
كتابة كود - بواسطة زهرة نيسان - 13-11-15, 02:04 PM
RE: كتابة كود - بواسطة مهموم - 13-11-15, 05:14 PM
RE: كتابة كود - بواسطة Mustafa Ahmed zz - 27-09-17, 07:30 PM

التنقل السريع :


يقوم بقرائة الموضوع: