2017-01-12 15 views
-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(); 
    } 
} 

好吧,我知道我的代码有点混乱,有一些问题缩进代码的某些部分。希望你仍然可以理解。所以我的问题是如何从文件default.txt中获取字符并将其传递给我的postfix函数? 在我READ_FILE功能我设法打印使用while循环中的字符(CH)。我的目标是存储字符串,所以我的后缀函数可以对它执行一些计算,因为我试图将中缀转换为后缀。如何将字符串从文件传递给函数?将中缀转换为后缀

如果你想知道,这个程序得到用户选择是否通过文件或手动输入输入一个表达式。表达式(这是一个中缀)然后转换为后缀。

感谢

+0

你为什么要关闭',而()'循环内的文件? – Barmar

+0

'龟etc()'返回时到达文件的末尾'EOF',你需要测试这一特别。它返回'int',而不是'char',你需要改变'ch'的声明。任何C教程都应该显示如何正确执行此操作。 – Barmar

+0

'character = ch'是什么意思?你从来没有宣布任何变量。 – Barmar

回答

0

这样

#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); 
}