اذهب إلى المحتوى
  • 0

مساعده في logic

Mari Carmen

السؤال

#include<iostream>
#include<string>
#include<stack>
using namespace std;

bool isOperator(char ch) {
    if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '>' || ch == '<' || ch == '<=' || ch == '>=' || ch == '==' || ch == '!=' || ch == '&&' || ch == '||' || ch == '(')
        return true;
    else return false;

}
int precedence(char op) {
    if (op == '*' || op == '/' || op == '%')
        return 100;
    else if (op == '(')
        return 0;
    else if (op == '+' || op == '-')
        return 50;
    else if (op == '>' || op == '<' || op == '<=' || op == '>=')
        return 40;
    else if (op == '==' || op == '!=')
        return 30;
    else if (op == '&&')
        return 20;
    else if (op == '||')
        return 10;
}
double evaluate(double x, double y, char op) {
    if (op == '+')
        return y + x;
    else if (op == '-')
        return y - x;
    else if (op == '*')
        return y * x;
    else if (op == '/')
        if (x != 0)
            return y / x;
        else {
            cout << "divbyzero";
            return 0;
        }
    else {
        cout << "invalid operator";
        return -1;

    }


}
void main() {
    string exp; // infix
    cout << "input exp" << endl;
    getline(cin, exp);
    string postfix = "";
    int j = 0;
    stack <char> s;

    for (int i = 0; i < exp.length(); i++) {
        if (exp[i] == '(')
            s.push(exp[i]);
        else if (exp[i] == ')') {
            while (!s.empty() && s.top() != '(') {
                postfix += s.top();
                s.pop();
            } //end while 
            s.pop(); // to remove ( from stack 

        }
        else if (exp[i] >= '0' && exp[i] <= '9') {

            postfix += exp[i];

        }
        else if (isOperator(exp[i]) && s.empty()) {
            s.push(exp[i]);

        }
        else if (isOperator(exp[i]) && !s.empty()) {
            while (!s.empty() && precedence(exp[i]) <= precedence(s.top())) {
                postfix += s.top();
                s.pop();
            }
            s.push(exp[i]);
        }
    }
    while (!s.empty()) {
        postfix += s.top();
        s.pop();

    }
    cout << "postfix= " << postfix << endl;
    // Evaluation part
    stack <double> d;
    for (int i = 0; i < postfix.length(); i++) {
        if (!isOperator(postfix[i]) - 0)

            d.push(postfix[i]-'0');
        else {
            double x = d.top(); d.pop();
            double y = d.top(); d.pop();
            double z = evaluate(x, y, postfix[i]);
            d.push(z);

        }

    }// end loop postfix
    double res = d.top(); d.pop();
    cout << "Final result=" << res << endl;
    if (!d.empty())
        cout << " Bad result" << endl;
}
 

ممكن مساعده كيف اعمل logic expiration

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

لا توجد أي إجابات على هذا السؤال بعد

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...