0

I am getting error for no operator "==" matches these operands -- operand types are: info == charC/C++(349)

I guess this is due to the fact im using couston data type

I making program to solve the reverse polish eqaution.I thoght I will store in polish eqation in stack and then sort the inputs into two segments. In two diffrent stacksbut it turns out it is not possible.

using namespace std;

union info
{
    char ch;
    float num;
};
struct node{
info data;
node *link;
node(info x){
    data=x;
    link= NULL;
}
}*st;

struct rst{
    float data;
    rst *link;
    rst(float x){
        data=x;
        link= NULL;
    }
}*rs;



void insertxp(info num,struct node *head){
    node *new_node;
    new_node=new node(num);
    new_node->link=head->link;
    head->link=new_node;
}

void insertrst(float x,rst *head){
    rst *new_p;
    new_p = new rst(x);
    new_p->link=head->link;
    head->link=new_p;
}

void del(rst *head){
    rst *temp;
    temp = head;
    head = head->link;
    delete temp;
}

bool checkop(info ch){
        if (ch == '+' ||ch=='-'||ch=='*'||ch=='/'||ch=='^')
        return true;
    else
        return false;
    
}

float eval(float a,float b,char ch){
switch (ch)
    {
    case '+':
        return a+b ;
        break;
    case '-':
        return a-b;
        break;
    case '*':
        return a*b;
        break;
    case '/':
        return a/b;
        break;
    case '^':
        double c,d;
        c=(int)a;
        d=(int)b;
        return pow(c,d);
        break;
    default:
        break;
    }
}

float calc(node *head,rst *rsthead){
insertxp(')',*head);
node *ptr = head;
info curr;
float a=0,b=0,num=0;
    for (int i = 0;; i++)
    {   curr = ptr->data;
        
        
        if(checkop(curr)==true)
        {   if (curr == ')')
            {
                break;
            }
        
            a=rsthead->data;
            b=rsthead->link->data;
            num=eval(a,b);
            del(rsthead);
            del(rsthead);
            insertrst(num,rsthead);  
        }
        else
        {
            insertrst(curr);
        }
    }
    if (rsthead->link->data=!NULL)
    {
        cout << "Invalid Polish Expression!!!\n";
    }
    return rsthead->data;
}

int main(){
info x;
cout << "Enter your Exoression and press B when you completly entred your post fix expression \n";
    for (int i = 0; i < 100; i++)
    {
        cin >> x;
        if (x == 'B')
        {
            break;
        }
        insertxp(x,st);
    }

cout << "Expression Succfully Entred \n";

}```
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    This problem is a lot simpler than you seem to think. You don't have to handle parentheses in RPN. You don't have to sort anything or segment anything or use two stacks. You just need a stack. One. However you need to know that the operands come off the stack in the wrong order for subtraction and division, and program accordingly. What [tag:dsa] has to do with it remains a mystery. – user207421 Aug 24 '21 at 10:39
  • 1
    Don't invent the wheel. The Standard Library already has list and stack types. And a `union` doesn't keep track of what it stores; you want a `std::variant` for that. – MSalters Aug 24 '21 at 10:58
  • And why you are using a linked list *instead* of the stack(s) you keep talking about is another mystery. – user207421 Aug 25 '21 at 06:52

1 Answers1

0

The error message is telling you that you can't directly compare a union info with a char.

What you can do, though, is compare the field ch of a union info, which is of type char, with another char.

Hence, replace:

  • curr == ')' with curr.ch == ')';
  • ch == '+' ||ch=='-'||ch=='*'||ch=='/'||ch=='^' with ch.ch == '+' ||ch.ch == '-'||ch.ch == '*'||ch.ch =='/'||ch.ch=='^';
  • cin >> x with cin >> x.ch.

I also strongly discourage you from writing using namespace std at the beginning of your file. About this you can read: Why is “using namespace std;” considered bad practice?

Stef
  • 13,242
  • 2
  • 17
  • 28
  • but what if input if a number as it the cin >> x.ch will give an error – Agrim Kaundal Aug 24 '21 at 11:01
  • I suggest splitting that into two steps: (1) Read input into a `std::string` variable; (2) determine if input was a number or a symbol and act accordingly. Related question: [Determining input to be int or char in C++](https://stackoverflow.com/questions/18525109/determining-input-to-be-int-or-char-in-c) – Stef Aug 24 '21 at 12:17