//SOME BODY PLEASE WRITE THIS IN C++ LANGUAGE//
//THANK YOU//
# include<stdio.h>
# include<conio.h>
/*program to evaluate the given postfix expression*/
typedef struct {
    int a[100];
    int top;
} STACK;
void push(STACK *s,int x)
{
    if(s->top==99)
        printf("STACK OVERFLOW\n");
    else
        s->a[++s->top]=x;
}

int pop(STACK *s)
{
    int x;
    if(s->top<0)
        printf("STACK UNDERFLOW\n");
    else {
        x=s->a[s->top--];
        return x;
    }
}
int operation(int p1,int p2,char op)
{
    switch(op) {
    case '+':
        return p1+p2;
    case '*':
        return p1*p2;
    case '-':
        return p1-p2;
    case '/':
        return p1/p2;
    }
}
int evaluate(char pos[])
{
    STACK s1;
    int p1,p2,result,i;
    s1.top=-1;
    for(i=0; pos[i]!='\0'; i++)
        if(isdigit(pos[i]))
            push(&s1,pos[i]-'0');/*use to find the integer value of it*/
        else {
            p2=pop(&s1);
            p1=pop(&s1);
            result=operation(p1,p2,pos[i]);
            push(&s1,result);
        }/*end of for loop*/
    return pop(&s1);
}
void main()
{
    char postfix[100];
    clrscr();
    printf("Please Enter the VALID POSTFIX string\n\n Operands are SINGLE DIGIT\n\n");
    gets(postfix);
    printf("The Result is==>%d",evaluate(postfix));
    getch();
}/*end of main*/

Dani AI

Generated

Short, practical guidance for converting this C program to C++ and answers to the two questions from (and echoing ’s point that most C is valid C++):

The printf line: that call evaluates evaluate(postfix) (which returns an int) and prints it with the %d format. In modern C++ replace C I/O with streams: std::cout << "Result is " << evaluatePostfix(postfix) << '\n';.

The typedef struct { ... } STACK; line is a C idiom. In C++ simply declare struct Stack { ... }; and use Stack s;. Even better, use the STL: std::stack<int> (or std::vector<int> for random access) and handle underflow/overflow with checks or exceptions. Also remove non-standard functions: gets, clrscr, getch, and conio.h — use std::getline for input and standard headers (<iostream>, <stack>, <string>, <cctype>).

Example (concise, safe C++ conversion that keeps single-digit operands and adds basic error checks):

#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <stdexcept>

int apply(int a, int b, char op) {
    switch(op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': if (b==0) throw std::runtime_error("divide by zero"); return a / b;
    }
    throw std::runtime_error("unknown operator");
}

int evaluatePostfix(const std::string& s) {
    std::stack<int> st;
    for (char ch : s) {
        if (std::isdigit(static_cast<unsigned char>(ch))) st.push(ch - '0');
        else if (!std::isspace(static_cast<unsigned char>(ch))) {
            if (st.size() < 2) throw std::runtime_error("stack underflow");
            int b = st.top(); st.pop();
            int a = st.top(); st.pop();
            st.push(apply(a, b, ch));
        }
    }
    if (st.empty()) throw std::runtime_error("no result");
    return st.top();
}

int main() {
    std::string input;
    std::getline(std::cin, input);
    std::cout << "Result is " << evaluatePostfix(input) << '\n';
}

Troubleshooting notes: the example assumes single-digit operands (same as the original). To support multi-digit or negative numbers, tokenize the input (e.g., std::istringstream) and parse tokens with std::stoi. Always check std::stack::empty() before top()/pop(), and cast characters to unsigned char when calling std::isdigit/std::isspace to avoid undefined behavior.

Recommended Answers

All 2 Replies

What part of the program are you having problems with? I ask because a C program is mostly a C++ program.

What part of the program are you having problems with? I ask because a C program is mostly a C++ program.

i cannot understand this one:-
printf("The Result is==>%d",evaluate(postfix));


and not sure if the following remains the same

typedef struct {
int a[100];
int top;
} STACK;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.