Pagini recente » Cod sursa (job #2625260) | Cod sursa (job #368227) | Cod sursa (job #2757315) | Cod sursa (job #983156) | Cod sursa (job #2734432)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100005
typedef struct stack{
int data;
struct stack *pred;
}stack;
int isEmpty(stack *);
int top(stack *);
void push(stack **, int);
int pop(stack **);
int isDigit(char);
int priority(char);
void infixToPostfix(char[], char[][50], int *);
int main()
{
FILE *fin = fopen("evaluare.in", "r");
stack *final = NULL;
char str[MAX];
char newStr[10005][50];
int newSize = 0;
fscanf(fin, "%[^\n]", str);
infixToPostfix(str, newStr, &newSize);
for (int i = 0; i < newSize; i ++)
if (strlen(newStr[i]) == 1 && (newStr[i][0] < '0' || newStr[i][0] > '9'))
{
int a = pop(&final);
int b = pop(&final);
int c;
switch(newStr[i][0])
{
case '+':
c = b + a;
break;
case '-':
c = b - a;
break;
case '/':
c = b / a;
break;
default:
c = b * a;
};
push(&final, c);
}
else push(&final, atoi(newStr[i]));
FILE *fout = fopen("evaluare.out", "w");
fprintf(fout, "%d", pop(&final));
return 0;
}
void push(stack **root, int data)
{
stack *temp = NULL;
temp = (stack *)malloc(sizeof(stack));
temp->data = data;
temp->pred = NULL;
if (*root == NULL)
*root = temp;
else
{
temp->pred = *root;
*root = temp;
}
}
int top(stack *root)
{
return root->data;
}
int isEmpty(stack *root)
{
return !root;
}
int pop(stack **root)
{
int x = (*root)->data;
*root = (*root)->pred;
return x;
}
int isDigit(char x)
{
return x >= '0' && x <= '9';
}
int priority(char x)
{
switch (x)
{
case '-' :
case '+' :
return 1;
case '/' :
case '*' :
return 2;
}
return -1;
}
void infixToPostfix(char str[], char newStr[][50], int *newSize)
{
stack *operators = NULL;
char number[15] = {0};
for (int i = 0; str[i]; i ++)
{
if (isDigit(str[i]))
{
number[strlen(number) + 1] = '\0';
number[strlen(number)] = str[i];
if (i == strlen(str) - 1 || !isDigit(str[i + 1]))
{
strcpy(newStr[*newSize], number);
(*newSize) ++;
number[0] = '\0';
}
}
else if (str[i] == '(')
push(&operators, str[i]);
else if (str[i] == ')')
{
while(!isEmpty(operators) && top(operators) != '(')
newStr[(*newSize)++][0] = pop(&operators);
pop(&operators);
}
else
{
while(!isEmpty(operators) && priority(str[i]) <= priority(top(operators)))
newStr[(*newSize)++][0] = pop(&operators);
push(&operators, str[i]);
}
}
while(!isEmpty(operators))
newStr[(*newSize)++][0] = pop(&operators);
}