2017-01-12 30 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); 
} 
相關問題