main.c
#include <stdio.h>
#include <string.h>
#include "rpn.h"
int main(void){
char my_input[101];
printf("Enter an expression: ");
gets(&my_input);
printf("%.2f\n", algtorpn(my_input));
return 0;
}
algtorpn.c
#include <stdio.h>
#include <stdlib.h>
#include "cstack.h"
#include "rpn.h"
#include "algtorpn.h"
int precedence(char c) {
switch(c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
void algtorpn(char input[],char output[]){
cstack top;
int i=0, o=0;
init_cstack(&top);
int state=0;
char c;
init_stack(&top);
while(token != NULL){
switch(state){
case 0:
i++;
if(isdigit(input[i]))
state = 1;
else if((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/'))
state = 2;
else if(input[i] == '(')
state = 3;
else if(input[i] == ')')
state =4;
else if(input[i] == '\0')
state = 5;
else
state = 6;
break;
case 1:
top = output[o];
o++;
i++;
while(isdigit(input[i])){
c = output[o];
o++;
i++;
}
output[o] = ' ';
o++;
i--;
state=0;
break;
case 2:
while((!is_cempty(&top)) && (top->data == ((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/'))) && (precedence(&top)=<data->top)){
output[o] = pop(&top);
o++;
output[o] = ' ';
o++;
}
if(!is_cfull())
cpush(top);
else{
print("Stack is full");
exit(0);
}
state = 0;
break;
case 3:
if(!is_cfull())
cpush(current);
else
{
print("Stack is full");
exit(0);
}
state=0;
break;
case 4:
while(!is_cempty(top)){
pop(&top);
o++;
output[o] = ' ';
o++;
}
if(!is_cempty(top))
pop(&top);
else{
print("mismatched parenthesis");
exit(0);
}
state = 0;
break;
case 5:
while(!is_cempty(top) && (top->data == ((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/')))){
pop(&top);
o++;
output[o] = ' ';
o++;
if(!is_cempty(top){
printf("Error! Left over parenthesis");
exit(0);
}
else
return rpneval(output[o]);
break;
case 6:
printf("Error! Invalid input\n");
exit(0);
}
}
}}
algtorpn.h
#ifndef ALGTORPN_H
#define ALGTORPN_H
#include "cstack.h"
void algtorpn(char input[], char output[]);
#endif
error messgae
algtorpn.c: In function ‘algtorpn’:
algtorpn.c:27:12: warning: passing argument 1 of ‘init_stack’ from incompatible pointer type [-Wincompatible-pointer-types]
init_stack(&top);
^
In file included from rpn.h:4:0,
from algtorpn.c:4:
fstack.h:13:6: note: expected ‘struct stacknode **’ but argument is of type ‘struct cstacknode **’
void init_stack(fstack *);
^~~~~~~~~~
algtorpn.c:28:11: error: ‘token’ undeclared (first use in this function); did you mean ‘popen’?
while(token != NULL){
^~~~~
popen
algtorpn.c:28:11: note: each undeclared identifier is reported only once for each function it appears in
algtorpn.c:32:9: warning: implicit declaration of function ‘isdigit’ [-Wimplicit-function-declaration]
if(isdigit(input[i]))
^~~~~~~
algtorpn.c:47:10: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
top = output[o];
^
algtorpn.c:61:20: warning: passing argument 1 of ‘is_cempty’ from incompatible pointer type [-Wincompatible-pointer-types]
while((!is_cempty(&top)) && (top->data == ((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/'))) && (precedence(&top)=<data->top)){
^
In file included from algtorpn.c:3:0:
cstack.h:12:9: note: expected ‘cstack {aka struct cstacknode *}’ but argument is of type ‘struct cstacknode **’
boolean is_cempty(cstack);
^~~~~~~~~
algtorpn.c:61:141: warning: passing argument 1 of ‘precedence’ makes integer from pointer without a cast [-Wint-conversion]
p->data == ((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/'))) && (precedence(&top)=<data->top)){
^
algtorpn.c:7:5: note: expected ‘char’ but argument is of type ‘struct cstacknode **’
int precedence(char c) {
^~~~~~~~~~
algtorpn.c:61:147: error: expected expression before ‘<’ token
a == ((input[i] == '+') || (input[i] == '-') ||(input[i] == '*') ||(input[i] == '/'))) && (precedence(&top)=<data->top)){
^
algtorpn.c:62:21: warning: passing argument 1 of ‘pop’ from incompatible pointer type [-Wincompatible-pointer-types]
output[o] = pop(&top);
^
In file included from rpn.h:4:0,
from algtorpn.c:4:
fstack.h:17:7: note: expected ‘struct stacknode **’ but argument is of type ‘struct cstacknode **’
float pop(fstack *);
^~~
algtorpn.c:68:10: warning: passing argument 1 of ‘cpush’ from incompatible pointer type [-Wincompatible-pointer-types]
cpush(top);
^~~
In file included from algtorpn.c:3:0:
cstack.h:14:6: note: expected ‘struct cstacknode **’ but argument is of type ‘cstack {aka struct cstacknode *}’
void cpush(cstack *,char);
^~~~~
algtorpn.c:68:4: error: too few arguments to function ‘cpush’
cpush(top);
^~~~~
In file included from algtorpn.c:3:0:
cstack.h:14:6: note: declared here
void cpush(cstack *,char);
^~~~~
algtorpn.c:70:4: warning: implicit declaration of function ‘print’; did you mean ‘printf’? [-Wimplicit-function-declaration]
print("Stack is full");
^~~~~
printf
algtorpn.c:77:11: error: ‘current’ undeclared (first use in this function); did you mean ‘clearenv’?
cpush(current);
^~~~~~~
clearenv
algtorpn.c:77:5: error: too few arguments to function ‘cpush’
cpush(current);
^~~~~
In file included from algtorpn.c:3:0:
cstack.h:14:6: note: declared here
void cpush(cstack *,char);
^~~~~
algtorpn.c:87:8: warning: passing argument 1 of ‘pop’ from incompatible pointer type [-Wincompatible-pointer-types]
pop(&top);
^
In file included from rpn.h:4:0,
from algtorpn.c:4:
fstack.h:17:7: note: expected ‘struct stacknode **’ but argument is of type ‘struct cstacknode **’
float pop(fstack *);
^~~
algtorpn.c:93:8: warning: passing argument 1 of ‘pop’ from incompatible pointer type [-Wincompatible-pointer-types]
pop(&top);
^
In file included from rpn.h:4:0,
from algtorpn.c:4:
fstack.h:17:7: note: expected ‘struct stacknode **’ but argument is of type ‘struct cstacknode **’
float pop(fstack *);
^~~
algtorpn.c:102:16: warning: passing argument 1 of ‘pop’ from incompatible pointer type [-Wincompatible-pointer-types]
pop(&top);
^
In file included from rpn.h:4:0,
from algtorpn.c:4:
fstack.h:17:7: note: expected ‘struct stacknode **’ but argument is of type ‘struct cstacknode **’
float pop(fstack *);
^~~
algtorpn.c:106:23: error: expected ‘)’ before ‘{’ token
if(!is_cempty(top){
^
algtorpn.c:117:4: error: expected expression before ‘}’ token
}
^
What I have tried:
this is a Dijkstra's Shunting-Yard Algorithm (NOTE: Modified to include only numbers, operators, and parentheses.). I have been working on this for several days but I can not seem to figure out how to get the code to work and really need some help. the file rpn.c is a functional program that I wrote and does not need correcting but I am supposed to use it to write the algtorpn.c file.
pseudocode
State 0:
Increment index string index
If current input is number, next state is 1
If current input is operator, next state is 2
If current input is left parenthesis, next state is 3
If current input is right parenthesis, next state is 4
If current input is end of string, next state is 5
If current input is garbage, next state is 6
State 1: (found number)
Copy current character to output string
Increment output string index
Increment input string index
While character in input string is digit or decimal point
o Copy character to output string
o Increment output string index
o Increment input string index
Put space in output string
Increment output string index
Decrement input string index
Next state is 0
State 2: (found operator)
While the stack is not empty and the character at top of stack is an operator and the precedence
of the operator that is current character in input string is less than or equal to that of the
operator on top of the stack
o Pop operator off top of stack and put in output string
o Increment output string index
o Put space in output string
o Increment output string index
If stack is not full, Push the operator that is current character in input string on stack
Otherwise, print error and exit
Next state is 0
State 3: (found left parenthesis)
If stack is not full, push left parenthesis found in current input character on stack
Otherwise, display error message and exit
Next state is 0
State 4: (found right parenthesis)
While stack is not empty and item at top of stack is not left parenthesis
o Pop operator off top of stack and put in output string
o Increment output string index
o Put space in output string
o Increment output string index
If stack is not empty, pop left parenthesis off stop of stack but do not put in output string
Otherwise, display error message (mismatched parenthesis) and exit
Next state is 0
State 5: (End of input)
While stack is not empty and character at top of stack is an operator
o Pop operator off stack and put in output string
o Increment output string index
o Put space in output string
o Increment output string index
If stack is not empty, display error message (leftover parenthesis) and exit
Otherwise, put end of string marker in output string and send output string to RPN Evaluator
Note: return rpneval(output_string);
State 6: (garbage)
Display error message and exit