Странный результат вычисления строки в обратной польской записи - C (СИ)
Формулировка задачи:
Ввожу строку, преобразовывает ок, но считает как-то криво
Например 100+2 = 200, а 123+3 = 446
хотя в основном считает нормально, отслеживая пошагово, то действия выполняются правильно
Не могу найти что не так.
Заранее спасибо!
Листинг программы
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string.h>
- #include <conio.h>
- struct list {
- int data;
- struct list * next;
- };
- typedef struct stack
- {
- struct list *top;
- } Stack;
- void makenull (Stack *S)
- {
- struct list *p;
- while (S->top)
- {
- p = S->top;
- S->top = p->next;
- delete(p);
- }
- }
- void create (Stack *S)
- {
- S->top = NULL;
- }
- int top (Stack *S)
- {
- if (S->top)
- return (S->top->data);
- else
- return 0;
- }
- int pop(Stack *S)
- {
- int a;
- struct list *p;
- p = S->top;
- a = p->data;
- S-> top = p->next;
- delete(p);
- return a;
- }
- void push(int a, Stack *S)
- {
- struct list *p;
- p = new struct list;
- p->data = a;
- p->next = S-> top;
- S->top = p ;
- }
- int empty (Stack *S)
- {
- return (S->top == NULL);
- }
- int major(char o)
- {
- if ((o=='+') || (o=='-'))
- return 2;
- else if ((o=='(') || (o==')'))
- return 1;
- else if ((o=='*') || (o=='/'))
- return 3;
- }
- int major(Stack *o)
- {
- if ((o->top->data=='+') || (o->top->data=='-'))
- return 2;
- else if ((o->top->data=='(') || (o->top->data==')'))
- return 1;
- else if ((o->top->data=='*') || (o->top->data=='/'))
- return 3;
- }
- void main()
- {
- Stack * S = new Stack;
- create(S);
- int k=0;
- char x;
- char s_in[256]={0}, s_out[256]={0};
- scanf("%s", &s_in);
- int len = strlen(s_in);
- for (int i=0; i<len; i++)
- {
- x=s_in[i];
- if ((x!='+')&&(x!='-')&&(x!='*')&&(x!='/')&&(x!='(')&&(x!=')')&&(x!=' '))
- {
- s_out[k++]=x;
- if ((s_in[i+1]=='+')||(s_in[i+1]=='-')||(s_in[i+1]=='*')||(s_in[i+1]=='/')||(s_in[i+1]=='(')||(s_in[i+1]==')')||(s_in[i+1]==' '))
- {
- s_out[k++]=' ';
- }
- }
- else if (x=='(')
- push(x, S);
- else if ((x=='+')||(x=='-')||(x=='*')||(x=='/')&&(x!='(')&&(x!=')'))
- {
- while ((empty(S)!=1)&&(major(S)>=major(x)))
- s_out[k++]=pop(S);
- push(x,S);
- }
- else if (x==')')
- {
- while (S->top->data!='(')
- s_out[k++] = pop(S);
- pop(S);
- }
- }
- if (empty(S)!=1)
- {
- while (empty(S)!=1)
- s_out[k++]=pop(S);
- s_out[k++]=' ';
- }
- std::cout<<s_out;
- makenull(S);
- char b[256]={0}; int l=0;
- int len2 = strlen(s_out);
- for (int i=0; i<len2; i++)
- {
- x = s_out[i];
- if ((x!='+')&&(x!='-')&&(x!='*')&&(x!='/')&&(x!=' '))
- {
- b[l++]=x;
- if ((s_out[i+1]=='+')||(s_out[i+1]=='-')||(s_out[i+1]=='*')||(s_out[i+1]=='/')||(s_out[i+1]==' ')||(s_out[i+1]==0))
- {
- push(atoi(b), S);
- *(b)=0; l=0;
- }
- }
- else if ((x=='+')||(x=='-')||(x=='*')||(x=='/'))
- {
- int m=0;
- switch (x)
- {
- case '+': m=pop(S)+pop(S);
- break;
- case '-': m=pop(S)-pop(S);
- break;
- case '/': m=pop(S)/pop(S);
- break;
- case '*': m=pop(S)*pop(S);
- break;
- }
- push(m, S);
- }
- }
- if (empty(S)!=1)
- {
- std::cout <<"="<<S->top->data;
- }
- makenull(S);
- getch();
- }
Листинг программы
- char b[256]={0}; int l=0;
- int len2 = strlen(s_out);
- for (int i=0; i<len2; i++)
- {
- x = s_out[i];
- if ((x!='+')&&(x!='-')&&(x!='*')&&(x!='/')&&(x!=' '))
- {
- b[l++]=x;
- if ((s_out[i+1]=='+')||(s_out[i+1]=='-')||(s_out[i+1]=='*')||(s_out[i+1]=='/')||(s_out[i+1]==' ')||(s_out[i+1]==0))
- {
- push(atoi(b), S);
- *(b)=0; l=0;
- }
- }
- else if ((x=='+')||(x=='-')||(x=='*')||(x=='/'))
- {
- int m=0;
- switch (x)
- {
- case '+': m=pop(S)+pop(S);
- break;
- case '-': m=pop(S)-pop(S);
- break;
- case '/': m=pop(S)/pop(S);
- break;
- case '*': m=pop(S)*pop(S);
- break;
- }
- push(m, S);
- }
- }
- if (empty(S)!=1)
- {
- std::cout <<"="<<S->top->data;
- }
Решение задачи: «Странный результат вычисления строки в обратной польской записи»
textual
Листинг программы
- case 'cos':
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д