2015-05-08 42 views
-1

我已經嘗試使用atoi然後切換它們回到字符串推,我試圖做一個rpn計算器的類,所以推,彈出,尋求和堆棧結構是如何然後需要,但我不能得到它添加整數值。如何添加值,因爲它們是字符串?

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

struct stack 
{ 
    const char * data; 
    struct stack *bottom; 
}; 

struct stack * push(struct stack *stk,const char * x) 
{ 
    struct stack *nstk = (struct stack *)malloc(sizeof(struct stack)); 

    nstk->data = x; 
    nstk->bottom = stk; 

    return nstk; 
} 


const char * peek(struct stack *stk) 
{ 
    if(stk -> data) 
     return stk->data; 
    else 
     return("Stack is empty"); 
} 

struct stack *pop(struct stack *stk) 
{ 
    struct stack *tmp; 

    tmp = stk->bottom; 
    stk->bottom = NULL; 
    free(stk); 

    return tmp; 
} 

FILE * input_from_args(int argc,const char *argv[]) 
{ 
    if(strcmp(argv[1],"-e") != 0 && strcmp(argv[1],"-c") != 0 && strcmp(argv[1],"-g") != 0) 
    { 
     printf("Option %s is not supported \n", argv[1]); 
     exit(0); 
    } 

    else 
    { 
     return stdin; 
    } 
} 
void evaluate(struct stack * equation) 
{ 
    int op; 
    int op2; 
    int ans; 

    if(strcmp("A",equation->data) == 0) 
     { 
     op = (int)pop(equation)-> data; 
     op2 = (int)pop(equation)-> data; 
     ans = op + op2; 
     printf("%i",ans); 
     } 
} 

void convert(struct stack * equation) 
{ 
} 

void other (struct stack * equation) 
{ 

} 


int main(int argc,const char *argv[]) 
{ 
    FILE *src = input_from_args(argc, argv); 

    if (src == NULL) 
    { 
     printf("%s", "Invalid Source"); 
     exit(EXIT_FAILURE); 
    } 

    struct stack * equation = NULL; 

    int i; 
    for(i=2; i <= argc; i++) 
    { 
     equation = push(equation,argv[i]); 
    } 

    if(strcmp(argv[1],"-e") == 0) 
    { 
     evaluate(equation); 
    } 
    else if(strcmp(argv[1],"-c") == 0) 
    { 
     convert(equation); 
    } 
    else if(strcmp(argv[1],"-g") == 0) 
    { 
     other(equation); 
    } 

    return EXIT_SUCCESS; 
} 

那就是我的一切都那麼遠,如果你注意到這很好的任何其他問題,但我真正想知道的是如何評價這種數據結構的後綴式輸入的一個例子是-e 2 2 A 5 X.

+0

爲什麼你堅持把數據存儲爲字符串呢?如果這是因爲您需要能夠將運算符存儲在同一個堆棧中,請爲其添加一個結構成員。 – usr2564301

+0

我已經嘗試過了,如果我不保留push,pop,peek和struct等等,或者我會在原始帖子中提到dinged,那麼這是一個類。 – user4872257

+1

然後你*做*需要使用'atoi'和'itoa'。你提到你嘗試過,但失敗 - 呃,它很醜,但它應該工作。事實上,您當前的代碼因爲您正在嘗試添加字符串而失敗。 *將一個字符串轉換爲int是完全錯誤的想法。 – usr2564301

回答

1

我希望能對您有所幫助。

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

typedef int DataType; 

DataType cnv(const char *s){ 
    return atoi(s); 
} 

#define PRN_DATA "%d" 

typedef struct stack { 
    DataType data; 
    struct stack *bottom; 
} Stack; 

void push(Stack **stk, DataType x){ 
    struct stack *nstk = malloc(sizeof(Stack)); 

    nstk->data = x; 
    nstk->bottom = *stk; 

    *stk = nstk; 
} 

int empty(Stack *stk){ 
    return stk == NULL; 
} 

DataType pop(Stack **stk){ 
    struct stack *tmp = *stk; 
    if(empty(*stk)){ 
     printf("empty stack\n"); 
     exit(EXIT_FAILURE); 
    } 
    DataType ret = tmp->data; 

    *stk = (*stk)->bottom; 
    free(tmp); 

    return ret; 
} 

char input_from_args(int *argc, const char ***argv){ 
    if(*argc < 2 || (*argv)[1][0] != '-' || (*argv)[1][1] == '\0'){ 
     printf("Option is not specified\n"); 
     exit(EXIT_FAILURE); 
    } 
    char op = (*argv)[1][1]; 
    if((*argv)[1][2] != '\0' || op != 'e' && op != 'c' && op != 'g'){ 
     printf("Option %s is not supported \n", (*argv)[1]); 
     exit(EXIT_FAILURE); 
    } 
    *argv = &(*argv)[2]; 
    *argc -= 2; 
    return op; 
} 

void evaluate(int argc, const char **argv){ 
    struct stack *s = NULL; 
    int i; 
    DataType v1, v2; 
    for(i = 0; i < argc; ++i){ 
     switch(*argv[i]){ 
     case 'A': 
      v2 = pop(&s); 
      v1 = pop(&s); 
      push(&s, v1 + v2); 
      break; 
     case 'S': 
      v2 = pop(&s); 
      v1 = pop(&s); 
      push(&s, v1 - v2); 
      break; 
     case 'X': 
      v2 = pop(&s); 
      v1 = pop(&s); 
      push(&s, v1 * v2); 
      break; 
     case 'D': 
      v2 = pop(&s); 
      v1 = pop(&s); 
      push(&s, v1/v2); 
      break; 
     default: 
      push(&s, cnv(argv[i])); 
     } 
    } 
    printf(PRN_DATA "\n", pop(&s)); 
    if(!empty(s)){ 
     printf("data remains in the stack\n"); 
     exit(EXIT_FAILURE); 
    } 
} 

void convert(int argc, const char **argv){ 
} 

void other (int argc, const char **argv){ 
} 

int main(int argc, const char **argv){ 
    switch(input_from_args(&argc, &argv)){ 
    case 'e': 
     evaluate(argc, argv); 
     break; 
    case 'g': 
     other(argc, argv); 
     break; 
    case 'c': 
     convert(argc, argv); 
     break; 
    } 

    return EXIT_SUCCESS; 
} 
相關問題