-1
// Function push 
void push(char x){  
    stack[++top] = x;
}

//Function pop
char pop(){
    if(top == -1)
    return -1;
else
    return stack[top--];
}

//Arithmetic operator precedence
int priority(char x){   

    if(x == '(')
        return 0;
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 2;
    else
        return -1;
}

//Function to convert infix to postfix
char postfix(){

    char *e, x = '\0';
    char exps[20];

    e = exps;
    printf("\nEnter an expression: \n");
    scanf("%s",exps);

    while(*e != '\0')         //While loop to arrange stack
    {
        if(isalnum(*e))      //isalnum convert   character to ASCII code
             printf("%c",*e);
        else if(*e == '(')
             push(*e);
        else if(*e == ')')
     {
        while((x = pop()) != '(')
            printf("%c", x);
     }
        else
     {
        while(priority(stack[top]) >= priority(*e))
            printf("%c",pop());
        push(*e);
    }
    e++;
}
while(top != -1)
{
    printf("%c",pop());
}
exit(0);
return 0;
}

//Function to read file called default input 
char read_file(){

    char file_location[100];
    int user_option=1;
    FILE *fp;
    character =ch;
    while (user_option == 1) {

        printf("Enter the location of the file:\n\n");
        getchar();
        gets(file_location);

        fp = fopen(file_location,"r"); //read file

        if( fp == NULL )
        {
            perror("Error while opening the file, \n\n");
            exit(EXIT_FAILURE);
        }

        printf("The contents of the %s file are :\n\n" , file_location);

        while( ( *ch = fgetc(fp) !=EOF))
            printf("%s" ,ch);
            fclose(fp);
            postfix();
        break;
    }
    return 0;
}



int manual_input() {

    int choice=0;

    while(choice == 0)
    {
        printf("\n\t\t\t\tMENU");
        printf("\n\t------------------------------");
        printf("\n\n\t 1. Postfix");
        printf("\n\t 2. Prefix");
        printf("\n\t 3. Both");
        printf("\n\t 4. Exit");
        printf("\n\tWould you like to convert it to: ");
        scanf( "%d", &choice );

         switch(choice)
        {
            case 1:
                printf("\nYOU SELECTED OPTION 1 %c",1);
                break;
            case 2:
                printf("\nYOU SELECTED OPTION 2 %c",2);
                break;
            case 3:
                printf("\nYOU SELECTED OPTION 3 %c",3);
                break;
            default:
                printf("\nYOU SELECTED OPTION 4 %c",4);
                exit(0);
        }
        postfix();
    }
     return 0;
 }

int main(){

    printf("\nHi ,how would you like to input expression? \n");
    printf("1.Get from file\n");
    printf("2.Input own expression\n");
    scanf("%d",&option);

    if (option == 1) {
        read_file();
    } else {
        manual_input();
    }
 }

Alright so I know my codes a little messy, had some problems indenting certain parts of code. Hopefully you can still understand. So my question is how do I get the characters from the file default.txt and pass it to my postfix function? In my read_file function I manage to print the characters (ch) using a while loop. My goal here is to store the string so my postfix function can perform some calculation on it since I am trying to convert infix to postfix.

If you're wondering, this program gets user to choose whether to enter an expression through a file or manual input. The expression (which is an infix) is then converted to postfix.

Thanks

  • Why are you closing the file inside the `while()` loop? – Barmar Jan 12 '17 at 20:06
  • `fgetc()` returns `EOF` when it reaches the end of the file, you need to test for this specifically. And it returns `int`, not `char`, you need to change the declaration of `ch`. Any C tutorial should show how to do this correctly. – Barmar Jan 12 '17 at 20:08
  • What is `character =ch;` supposed to mean? You've never declared either variable. – Barmar Jan 12 '17 at 20:09
  • Note that [`gets()` is too dangerous to be used — ever](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Jan 13 '17 at 03:03

1 Answers1

0

like this

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_EXP_LEN 256
//Stringification
#define S_(n) #n
#define S(n) S_(n)

int top = -1;
char stack[MAX_EXP_LEN];

void push(char x){  
    stack[++top] = x;
}

char pop(void){
    if(top == -1)
        return -1;
    else
        return stack[top--];
}

int priority(char x){
    if(x == '(')
        return 0;
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 2;
    else
        return -1;
}

void postfix(const char *exps){//Use the input expression as an argument
    const char *e = exps;
    char x = '\0';

    while(*e != '\0'){
        if(isalnum(*e))
             printf("%c",*e);
        else if(*e == '(')
             push(*e);
        else if(*e == ')'){
            while((x = pop()) != '(')
                printf("%c", x);
        } else {
            while(priority(stack[top]) >= priority(*e))
                printf("%c", pop());
            push(*e);
        }
        e++;
    }
    while(top != -1){
        printf("%c", pop());
    }
    puts("");
}

void read_file(char exps[MAX_EXP_LEN + 1]){
    char file_location[FILENAME_MAX+1] = "";
    FILE *fp;

    printf("Enter the location of the file:\n\n");
    scanf("%" S(FILENAME_MAX) "[^\n]%*c", file_location);

    fp = fopen(file_location, "r");
    if( fp == NULL ){
        perror("Error while opening the file.\n\n");
        exit(EXIT_FAILURE);
    }
    fscanf(fp, "%" S(MAX_EXP_LEN) "s", exps);
    fclose(fp);
}

void manual_input(char exps[MAX_EXP_LEN + 1]){
    printf("Input expression\n");
    scanf("%" S(MAX_EXP_LEN) "s", exps);
}

int main(void){
    char exps[MAX_EXP_LEN + 1] = "";
    int option;

    printf("\nHi ,how would you like to input expression? \n");
    printf("1.Get from file\n");
    printf("2.Input own expression\n");
    scanf("%d", &option);
    scanf("%*[^\n]");scanf("%*c");//clear stdin
    if (option == 1)
        read_file(exps);//exps pass to function
    else
        manual_input(exps);
    postfix(exps);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70