Untitled
unknown
plain_text
a year ago
5.1 kB
15
Indexable
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 20
int isp(char item);
void output(char item);
void push(char item);
char pop(void);
void generateQuadruples(void);
char expression[MAX];
char postfix[MAX];
char stack[MAX];
int top = -1;
int postfixIndex = 0;
int expressionLength;
int main() {
printf("Enter the infix expression: ");
fgets(expression, MAX, stdin);
expression[strcspn(expression, "\n")] = '\0';
expressionLength = strlen(expression);
push('#');
for (int i = 0; i < expressionLength; ++i) {
char item = expression[i];
if (isalpha(item)) {
output(item);
} else if (item == '+' || item == '-' || item == '*' || item == '/' || item == '^') {
while (top != -1 && isp(stack[top]) >= isp(item)) {
output(pop());
}
push(item);
} else if (item == '(') {
push(item);
} else if (item == ')') {
while (top != -1 && stack[top] != '(') {
output(pop());
}
pop();
}
}
while (top != -1 && stack[top] != '#') {
output(pop());
}
printf("Postfix expression: ");
puts(postfix);
generateQuadruples();
return 0;
}
int isp(char item) {
switch (item) {
case '+': case '-': return 1;
case '*': case '/': return 2;
case '^': return 3;
default: return 0;
}
}
void output(char item) {
postfix[postfixIndex++] = item;
}
void push(char item) {
stack[++top] = item;
}
char pop(void) {
return stack[top--];
}
void generateQuadruples(void) {
int tempCounter = 0;
char tempVar[3];
char operand1[3], operand2[3];
printf("\noperator\top1\t\top2\t\tresult\n");
printf("---------------------------------------\n");
for (int i = 0; i < postfixIndex; ++i) {
char item = postfix[i];
if (isalnum(item)) {
push(item);
} else {
char op1 = pop();
char op2 = pop();
snprintf(tempVar, sizeof(tempVar), "t%d", ++tempCounter);
printf("%c\t\t%c\t%c\t%s\n", item, op2, op1, tempVar);
push(tempVar[0]);
}
}
}
exp13
----
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
typedef struct {
int top;
int items[MAX];
} IntStack;
void initStack(IntStack* s) {
s->top = -1;
}
int isEmpty(IntStack* s) {
return s->top == -1;
}
void push(IntStack* s, int value) {
if (s->top < (MAX - 1)) {
s->items[++(s->top)] = value;
} else {
printf("Stack overflow\n");
exit(1);
}
}
int pop(IntStack* s) {
if (!isEmpty(s)) {
return s->items[(s->top)--];
}
printf("Stack underflow\n");
exit(1);
}
int peek(IntStack* s) {
if (!isEmpty(s)) {
return s->items[s->top];
}
printf("Stack is empty\n");
exit(1);
}
int precedence(char op) {
switch(op) {
case '+':
case '-': return 1;
case '*':
case '/': return 2;
}
return 0;
}
int applyOp(int a, int b, char op) {
switch(op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
void evaluateExpression(const char* expr) {
IntStack values;
IntStack ops;
initStack(&values);
initStack(&ops);
for (int i = 0; i < strlen(expr); i++) {
if (isspace(expr[i])) continue;
if (isdigit(expr[i])) {
int val = 0;
while (i < strlen(expr) && isdigit(expr[i])) {
val = (val * 10) + (expr[i] - '0');
i++;
}
i--;
push(&values, val);
} else if (expr[i] == '(') {
push(&ops, '(');
} else if (expr[i] == ')') {
while (!isEmpty(&ops) && peek(&ops) != '(') {
int val2 = pop(&values);
int val1 = pop(&values);
char op = (char)pop(&ops);
push(&values, applyOp(val1, val2, op));
}
pop(&ops);
} else {
while (!isEmpty(&ops) && precedence(peek(&ops)) >= precedence(expr[i])) {
int val2 = pop(&values);
int val1 = pop(&values);
char op = (char)pop(&ops);
push(&values, applyOp(val1, val2, op));
}
push(&ops, expr[i]);
}
}
while (!isEmpty(&ops)) {
int val2 = pop(&values);
int val1 = pop(&values);
char op = (char)pop(&ops);
push(&values, applyOp(val1, val2, op));
}
printf("Result: %d\n", peek(&values));
}
int main() {
char expression[MAX];
printf("Enter the expression: ");
fgets(expression, MAX, stdin);
expression[strcspn(expression, "\n")] = '\0';
evaluateExpression(expression);
return 0;
}
Editor is loading...
Leave a Comment