जब ऑपरेंड की अमान्य संख्या में कोई त्रुटि हो तो पोस्टफ़िक्स अभिव्यक्ति को प्रिंट करने से कैसे रोकें?


निम्नलिखित परीक्षण मामलों के समान, इसे केवल त्रुटि मुद्रित करनी चाहिए न कि पोस्टफ़िक्स अभिव्यक्ति।

एक इन्फ़िक्स अभिव्यक्ति दर्ज करें: +
त्रुटि: ऑपरेंड की अमान्य संख्या
एक इन्फ़िक्स एक्सप्रेशन दर्ज करें: 1+
त्रुटि: ऑपरेंड की अमान्य संख्या
एक इन्फ़िक्स एक्सप्रेशन दर्ज करें: +1
त्रुटि: ऑपरेंड की अमान्य संख्या
एक इनफ़िक्स अभिव्यक्ति दर्ज करें: 1-+4
त्रुटि: ऑपरेंड की अमान्य संख्या

मैंने print_postfix फ़ंक्शन को संशोधित करने का प्रयास किया है ताकि यह इस शर्त के तहत अभिव्यक्ति को प्रिंट कर सके कि स्टैक खाली है। हालाँकि, मुझे एहसास हुआ कि वैध इन्फ़िक्स अभिव्यक्तियों के लिए स्टैक भी खाली होगा।

मैंने क्या प्रयास किया है:

सी++
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

const int MAX_SIZE = 100;

class Stack{
  private:
    int top;
    string stackArray[MAX_SIZE];

  public:
    Stack(){
      top = -1;
    }

    void push(string s){
      if(top >= (MAX_SIZE - 1)){
        cout << "Stack Overflow";
      }
      else{
        top++;
        stackArray[top] = s;
      }
    }

    string pop(){
      if(top < 0){
        return "";
      }
      else{
        string s = stackArray[top];
        top--;
        return s;
      }
    }

    string peek(){
      if(top < 0){
        cout << "Stack is empty";
        return "";
      }
      else{
        string s= stackArray[top];
        return s;
      }
    }

    bool isEmpty() {
      return(top < 0);
    }
};

bool isNumber(string s){
  if(s >= "0" && s <= "9"){
    return true;
  }
  else{
    return false;
  }
}

bool isOperator(string s){
  if(s == "+" || s == "-" || s == "*" || s == "/" || s == "^"){
    return true;
  }
  else{
    return false;
  }
}

bool isValidCharacter(string s){
  if(isNumber(s) || isOperator(s)){
    return true;
  }
  else if(s == "(" || s == ")"){
    return true;
  }
  else if(s == "{" || s == "}"){
    return true;
  }
  else if(s == "[" || s == "]"){
    return true;
  }
  else{
    return false;
  }
}

int precedence(string op){
  if(op == "^"){
    return 3;
  }
  else if(op == "*" || op == "/"){
    return 2;
  }
  else if(op == "+" || op == "-"){
    return 1;
  }
  else{
    return -1;
  }
}

bool isMatchingPair(string s1, string s2){
  if(s1 == "(" && s2 == ")"){
    return true;
  }
  else if(s1 == "{" && s2 == "}"){
    return true;
  }
  else if(s1 == "[" && s2 == "]"){
    return true;
  }
  else{
    return false;
  }
}

bool areParenthesesBalanced(string exp){
  Stack s;
  for(int i = 0; i < exp.size(); i++){
    if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){
      s.push(string(1, exp[i]));
    }
    if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){
      if(s.isEmpty() || !isMatchingPair(s.pop(), string(1, exp[i]))){
        return false;
      }
    }
  }
  return s.isEmpty();
}

string to_postfix(string infix){
  Stack s;
  string postfix = "";
  if(!areParenthesesBalanced(infix)) {
    cout << "Error: Invalid parentheses" << endl;
    return "";
  }
  for (int i = 0; i < infix.size(); i++) {
    string infixString(1, infix[i]);
    if (infix[i] == ' '){
      cout << "Error: Invalid number of operators" << endl;
      return "";
    }
    if (isValidCharacter(infixString)){
      if (isNumber(infixString)) {
        postfix += infixString;
      }
      else if (infixString == "(" || infixString == "[" || infixString == "{") {
        s.push(infixString);
      }
      else if (infixString == ")" || infixString == "]" || infixString == "}") {
        while (!s.isEmpty() && s.peek() != "(" && s.peek() != "[" && s.peek() != "{") {
          postfix += s.pop();
        }
        if (s.isEmpty()) {
          cout << "Error: Invalid parentheses" << endl;
          return "";
        }
        else {
          s.pop();
        }
      }
      else {
        while (!s.isEmpty() && precedence(infixString) <= precedence(s.peek())) {
          postfix += s.pop();
        }
        s.push(infixString);
      }
    }
    else{
      cout << "Invalid character '" << infix[i] << "' in the expression" << endl;
      return "";
    }
  }
  while (!s.isEmpty()) {
    postfix += s.pop();
  }
  return postfix;
}

void print_postfix(string postfix){
  if(postfix != ""){
    cout << "Postfix expression: ";
    for(int i = 0; i < postfix.size(); i++){
      cout << postfix[i] << " ";
    }
    cout << endl;
  }
}

double evaluatePostfix(string postfix){
  Stack s;
  for(int i = 0; i < postfix.size(); i++){
    string postfixString(1, postfix[i]);
    if(isOperator(postfixString)){
      if(s.isEmpty()){
        cout << "Error: Invalid number of operands";
        return 0;
      }
      int op1 = stoi(s.pop());
      if(s.isEmpty()){
        cout << "Error: Invalid number of operands";
        return 0;
      }
      int op2 = stoi(s.pop());
      double result = 0.0;

      switch(postfix[i]){
        case '+':
          result = op2 + op1;
          break;
        case '-':
          result = op2 - op1;
          break;
        case '*':
          result = op2 * op1;
          break;
        case '/':
          if(op1 == 0){
            cout << "Error: Division by zero" << endl;
          }
          result = (double)op2 / op1;
          break;
        case '^':
          result = pow(op2, op1);
          break;
        case '%':
          result = fmod(op2, op1);
          break;
        default:
          cout << "Error: Invalid operator" << endl;
      }
      s.push(to_string(result)); 
    }
    else{
      s.push(postfixString);
    }
  }
  if(!s.isEmpty()) {
    string resultString = s.pop();
    if(!resultString.empty() && isNumber(resultString)){
      return stod(resultString);
    }
  }
  return 0;
}

int main() {
  string infixExpression;
  cout << "Enter an infix expression: ";
  getline(cin, infixExpression);

  string postfixExp = to_postfix(infixExpression);

  print_postfix(postfixExp);

  double result = evaluatePostfix(postfixExp);
    if(result != 0.0){
      cout << "Result: " << fixed << setprecision(3) << result << endl;
    }
  
  return 0;
}

समाधान 1

एक वैश्विक बनाएँ bool वैरिएबल कहा जाता है AnErrorOccurred और इसे सेट करें false.
जब कोई त्रुटि आती है, तो इसे सेट करें true.
अपनी प्रिंट विधि में, वेरिएबल की जाँच करें। अगर यह है falseअभिव्यक्ति प्रिंट करें।
फिर किसी भी स्थिति में, इसे सेट करें false अगली बार के लिए फिर से.

コメント

タイトルとURLをコピーしました