Originally Posted by
Salem
Perhaps you should post a complete program which replicates the problem. Talk about vague and unhelpful.
Works for me.
Code:
//#include "arithmeticEvaluator.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define CLOSING_PARENTHESIS ')'
char getToken( char **p ) {
char result = **p;
(*p)++;
return result;
}
void convertToPostfix(char *infix){
int a = 1;
printf("%d", a);
fflush(stdout);
//printCharacterStack(operatorStack);
char token;
while((token = getToken(&infix)) != '\0'){
putchar(token);
sleep(1);
// if(isdigit(token))
// {
// pushNum(&operandStack, token);
// }
// else if(token == CLOSING_PARENTHESIS)
// {
// /*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
// }
// else if(!isdigit(token))
// {
// pushChar(&operatorStack, token);
// }
}
}
int main(void)
{
char a[] = "ja;ogtujw3";
convertToPostfix(a);
return 0;
}
main.c
Code:
#include "arithmeticEvaluator.h"#include <stdio.h>
int main(){
char* postfix;
char* infixedUserInput = getKeyboardInput();
void convertToPostfix(infixedUserInput);
//printCharacterStack(operatorStack);
return 0;
}
inputAndPrintinUtilities.c
Code:
#include <stdio.h>#include "arithmeticEvaluator.h"
#define MEMORY_ALLOCATE 20
const char* withinArraySeperator = " ";
void printCharacterArray(char* arrayToBePrinted){
for(int i = 0; arrayToBePrinted[i] != '\0'; i++)
{
printf("%c%s", arrayToBePrinted[i], withinArraySeperator);
}
}
void printCharacterStack(struct CharStack stackToBePrinted){
printf("Hello!");
// int sizeOfStack = stackToBePrinted.stackPointer;
// char characterArrayToBePrinted[MAX_STACK_SIZE];
// int i = 0;
// for(i; i < sizeOfStack; i++)
// {
// characterArrayToBePrinted[i] = stackToBePrinted.stackValue[i];
// }
// characterArrayToBePrinted[i] = '\0';
// printCharacterArray(characterArrayToBePrinted);
}
char* getKeyboardInput(void){
char* input = malloc(sizeof(char) * MEMORY_ALLOCATE); //Still have to free memory
if (input == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(-1);
}
int i;
char inputCharacter;
for(i = 0; i < MEMORY_ALLOCATE; i++){
inputCharacter = getchar();
if(inputCharacter == '\n'){
break;
}
input[i] = inputCharacter;
}
input[i] = '\0';
return input;
}
char getToken(char **inputString){
char returnCharacter = **inputString;
*inputString = *inputString + 1; //Still have to free memory
return returnCharacter;
}
stackUtilities.c
Code:
#include <stdio.h>
#include "arithmeticEvaluator.h"
#define MAX_STACK_SIZE 100
struct CharStack operatorStack = {.stackPointer = 5, .stackValue = {'7','2','3','4','5'}};
struct NumStack operandStack = {.stackPointer = 0};
void pushChar(struct CharStack *stack, char pushedValue){
if (stack -> stackPointer < MAX_STACK_SIZE) {
stack -> stackValue[stack -> stackPointer++] = pushedValue;
}
else {
fprintf(stderr, "Character Stack full, can't push %g\n", pushedValue);
exit(-1);
}
}
char popChar(struct CharStack stack){
if (stack.stackPointer > 0){
return stack.stackValue[--stack.stackPointer];
}
else {
fprintf(stderr, "Character Stack empty\n");
exit(-1);
}
}
void pushNum(struct NumStack *stack, double pushedValue){
if (stack -> stackPointer < MAX_STACK_SIZE) {
stack -> stackValue[stack -> stackPointer++] = pushedValue;
}
else {
fprintf(stderr, "Number Stack full, can't push %g\n", pushedValue);
exit(-1);
}
}
double popNum(struct NumStack stack){
if (stack.stackPointer > 0){
return stack.stackValue[--stack.stackPointer];
}
else {
fprintf(stderr, "Number Stack empty\n");
exit(-1);
}
}
convertToPostfix.c
Code:
#include "arithmeticEvaluator.h"#include <stdio.h>
#include <stdlib.h>
#define CLOSING_PARENTHESIS ')'
void convertToPostfix(char *infix){
int a = 1;
fflush(stdout);
printf("%d", a);
//printCharacterStack(operatorStack);
char token;
while((token = getToken(&infix)) != '\0'){
if(isdigit(token))
{
pushNum(&operandStack, token);
}
else if(token == CLOSING_PARENTHESIS)
{
/*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
}
else if(!isdigit(token))
{
pushChar(&operatorStack, token);
}
}
}
arithmeticEvaluaator.h
Code:
#ifndef ARITHMETICEVALUATOR_H#define ARITHMETICEVALUATOR_H
#define MAX_STACK_SIZE 100
extern struct CharStack operatorStack;
extern struct NumStack operandStack;
struct CharStack{
char stackValue[MAX_STACK_SIZE];
int stackPointer;
};
struct NumStack{
double stackValue[MAX_STACK_SIZE];
int stackPointer;
};
void pushChar(struct CharStack *, char pushedValue);
char popChar(struct CharStack);
void printCharacterArray(char*);
void printCharacterStack(struct CharStack);
char* getKeyboardInput(void);
char getToken(char **);
void convertToPostfix(char *);
#endif