2012-09-18 49 views
-2

下面的代碼是我的子程序做反向波蘭符號計算......基本上+, - ,*和/。除了當我試圖添加2.5和2.5的程序給我4.0 ...我認爲我有一個想法爲什麼,但我不知道如何解決它...現在我正在閱讀所有數字和運算符從這個任務所需的命令行,然後採取該字符串,並使用sscanf從中獲取數字...我想,包含三個字符'2','。',和'5',並沒有被完全轉換爲浮點數,而是我認爲只是'2'。有人可以看看我的代碼,並確認或否認這一點,並可能告訴我如何解決它,以便我得到正確的答案?預先感謝您的任何幫助!float addition 2.5 + 2.5 = 4.0? RPN

float 
fsm (char mystring[]) 
{ 
    int i = -1, j, k = 0, state = 0; 
    float num1, num2, ans; 
    char temp[10]; 
    c_stack top; 
    c_init_stack (&top); 
    while (1) 
    { 
     switch (state) 
    { 
    case 0: 
     i++; 
     if ((mystring[i]) == ' ') 
     { 
      state = 0; 
     } 
     else if ((isdigit (mystring[i])) || (mystring[i] == '.')) 
     { 
      state = 1; 
     } 
     else if ((mystring[i]) == '\0') 
     { 
      state = 3; 
     } 
    else 
    { 
     state = 4; 
    } 
    break; 
case 1: 
    temp[k] = mystring[i]; 
    k++; 
    i++; 
    if ((isdigit (mystring[i])) || (mystring[i] == '.')) 
    { 
     state = 1; 
    } 
    else 
    { 
     state = 2; 
    } 
    break; 
case 2: 
    temp[k] = '\0'; 
    sscanf (temp, "%f", &num1); 
    c_push (&top, num1); 
    i--; 
    k = 0; 
    state = 0; 
    break; 
case 3: 
    ans = c_pop (&top); 
    if (c_is_empty (top)) 
    return ans; 
    else 
    { 
     printf ("There are still items on the stack\n"); 
     exit (0); 
case 4: 
     num2 = c_pop (&top); 
     num1 = c_pop (&top); 
     if (mystring[i] == '+'){ 
      ans = num1 + num2; 
     return ans; 
    } 
     else if (mystring[i] == '-'){ 
    ans = num1 - num2; 
    return ans; 
    } 
     else if (mystring[i] == '*'){ 
    ans = num1 * num2; 
    return ans; 
    } 
     else if (mystring[i] == '/'){ 
      if (num2){ 
      ans = num1/num2; 
      return ans; 
     } 
     else{ 
     printf ("Error: cannot divide by 0\n"); 
     exit (0); 
      } 
    } 
     c_push (&top, ans); 
     state = 0; 
     break; 
    } 
} 
} 
} 

這裏是我的主要程序:

#include <stdio.h> 
#include <stdlib.h> 
#include "boolean.h" 
#include "c_stack.h" 
#include <string.h> 

int main(int argc, char *argv[]) 
{ 
    char mystring[100]; 
    int i; 
    sscanf("", "%s", mystring); 
    for (i=1; i<argc; i++){ 
    strcat(mystring, argv[i]); 
    strcat(mystring, " "); 
    } 
    printf("%.2f\n", fsm(mystring)); 
    } 

,這裏是與原型的頭文件和定義c_stack:響應sixlettervariables

#include "boolean.h" 

#ifndef CSTACK_H 
#define CSTACK_H 

    typedef struct c_stacknode{ 
    char data; 
    struct c_stacknode *next; 
    } *c_stack; 

#endif 

void c_init_stack(c_stack *); 
boolean c_is_full(void); 
boolean c_is_empty(c_stack); 
void c_push(c_stack *,char); 
char c_pop(c_stack *); 
void print_c_stack(c_stack); 
boolean is_open(char); 
boolean is_brother(char, char); 
float fsm(char[]); 

部分任務是在數組中使用數字和小數的現有字符串並使用它們來創建一個浮動這是我在做什麼在這行代碼:

case 1: 
temp[k] = mystring[i]; 
k++; 
i++; 
if ((isdigit (mystring[i])) || (mystring[i] == '.')) { 
state = 1; } else { 
    state = 2; 
} 
break; 
case 2: 
temp[k] = '\0'; 
sscanf (temp, "%f", &num1); 
c_push (&top, num1); 
i--; 
k = 0; 
state = 0; 
break; 
+0

小細節:爲什麼從'state == 0'到'state = 1'時不要設置'k = 0'? – mvds

+0

我認爲這是C? – Ryan

+0

把它放在一個調試器中,然後遍歷代碼。 – abelenky

回答

1

是否也能分享c_stack的定義是什麼?它看起來像是一個整數類型的堆棧,將任何浮點輸入向下舍入。那樣,2.5 + 2.5 == 4

0

如果你定義變量作爲

int x=2.5; 

int y=2.5; 

一個後來又使像

if((x+y)==4.0) 

這將是真實的。

引擎會將浮點數轉換爲整數。我猜你正在做這樣的事情。

查找變量定義。

4

你的堆棧存儲char數據:

typedef struct c_stacknode{ 
char data; 
struct c_stacknode *next; 
} *c_stack; 

char,顧名思義,不存儲浮點數據。相反,當您撥打c_push時,發生整數轉換,將2.5截斷爲2

您需要更新struct c_stacknode及其系列相關方法的定義,以支持float數據。