2013-12-10 28 views
-1

以下代碼片段是評估後綴表達式的程序的一部分。我對C編程沒有太多經驗,所以請原諒我的知識。我不明白代碼中突出顯示的部分在做什麼。以下是C語言代碼片斷在做什麼?

char str[100]; 

int i, data = -1, operand1, operand2, result; 

/* Get the postfix expression from the user */ 

printf("Enter ur postfix expression:"); 

fgets(str, 100, stdin); 

for (i = 0; i < strlen(str); i++) 
{ 

    if (isdigit(str[i])) 
    { 
      /* 
      * if the i/p char is digit, parse 
      * character by character to get 
      * complete operand 
      */ 
      data = (data == -1) ? 0 : data; 

      data = (data * 10) + (str[i] - 48); //What is happening here 

      continue; 
} 
+4

調試你的代碼,你就知道了。 – Maroun

+1

@MarounMaroun以下是完整的代碼供您參考http://see-programming.blogspot的鏈接。in/2013/02/evaluate-postfix-expression-using.html – pacman7845421

+0

您確定代碼中沒有多餘的星號? – moeCake

回答

1

根據您的代碼段

**數據=(數據* 10)+(STR [1] - 48);

這條線會改變你的字符串整數格式

例如像你給輸入235

隨後的2 ASCII碼爲50,當你用48減去那麼它會來2. 現在乘以你以前的否(這是0)乘以10並加2.然後它變成2 接下來的3將會有51個ASCII碼,減去48後將變成3 現在乘以你以前的否(它是2)10並加3,那麼它會變成23等等。

像這樣,你正在將字符串轉換爲整數no。

2

它(如果你願意或字符,每個字符)轉換數字字符串str中的實際數量,每個數字位數。

data = (data * 10) + (str[i] - 48); 

取數「爲止」,並將該新的數字給它,由10數相乘,然後加入的str[i]值到它。字符str[i]在'0'..'9'範圍內,並通過減去其中的48個 - ASCII值爲'0' - 您可以獲得數字的值。

因此,如果data是95;例如,和str[i]是 '3',則data成爲 '3' 950 + ASCII碼 - 的 '0' 的ASCII碼,所以data變爲950 + 3 = 953

+0

好的,謝謝你的回答。我還有一個問題,這是從數組中提取數字的必要方法嗎? – pacman7845421

+0

@AkashRana:不,最好使用標準庫函數,比如strtol(string to long),請參閱http://en.cppreference.com/w/c/string/byte/strtol –

+0

非常感謝! – pacman7845421

2
data = (data * 10) + (str[i] - 48); 

即線將值轉換str[i]爲整數值並轉換整數(因此乘以10)。 例如

'0' - > 0 '1' - > 1 例如 「100」 - > 100

它假設ASCII表示,因此使用48。更便攜的方式將是使用'0'代替:

data = (data * 10) + (str[i] - '0'); 
+0

這裏我很挑剔,但理論上'0'不會提高可移植性,因爲它假設字符集數字'0' - '9'是按照升序排列。它們可能在任何有史以來設計的系統中,但無論如何... – user694733

+1

@ user694733 C標準要求'0'到'9'是連續的並且按升序排列。如果要符合C標準,那麼最常用的ASCII和不太常用的EBCDIC榮譽以及C使用的任何表示都需要這樣做。 –

+0

我不知道它實際上是在標準中提到的。很高興知道。 – user694733

0

爲了更好地理解,打印在中間實例中生成的值,因爲您在C程序中沒有太多經驗可以使用printf語句,以便您可以理解邏輯。

#include <stdio.h> 
#include <string.h> 
#include<ctype.h> 
#include<conio.h> 
    int top = -1; 
    int stack[100]; 

    /* push the given data into the stack */ 
    void push (int data) { 
    stack[++top] = data; 
    } 

    /* Pop the top element from the stack */ 
    int pop() { 
    int data; 
    if (top == -1) 
     return -1; 
    data = stack[top]; 
    stack[top] = 0; 
    top--; 
    return (data); 
    } 

    int main() { 
    char str[100]; 
    int i, data = -1, operand1, operand2, result; 
    /* Get the postfix expression from the user */ 
    printf("Enter ur postfix expression:"); 
    fgets(str, 100, stdin); 
    for (i = 0; i < strlen(str); i++) { 
     if (isdigit(str[i])) { 
      /* 
      * if the i/p char is digit, parse 
      * character by character to get 
      * complete operand 
      */ 
      data = (data == -1) ? 0 : data; 
      printf("%d value of str[i] ",str[i]); // returns the ascii value 
      data = (data * 10) + (str[i] - 48); //multiplies with ten and substracts with 48 so thst u get ur input number 
      printf("%d\n",data);  
      continue; 
     } 

     if (data != -1) { 
      /* if the i/p is operand, push it into the stack */ 
      push(data); 
     } 

     if (str[i] == '+' || str[i] == '-' 
      || str[i] == '*' || str[i] == '/') { 
      /* 
      * if the i/p is an operator, pop 2 elements 
      * from the stack and apply the operator 
      */ 
      operand2 = pop(); 
      operand1 = pop(); 
      if (operand1 == -1 || operand2 == -1) 
       break; 
      switch (str[i]) { 
       case '+': 
        result = operand1 + operand2; 
        /* push the result into the stack */ 
        push(result); 
        break; 
       case '-': 
        result = operand1 - operand2; 
        push(result); 
        break; 
       case '*': 
        result = operand1 * operand2; 
        push(result); 
        break; 
       case '/': 
        result = operand1/operand2; 
        push(result); 
        break; 
      } 
     } 
     data = -1; 
    } 
    if (top == 0) 
     printf("Output:%d\n", stack[top]); 
    else 
     printf("u have given wrong postfix expression\n"); 
    getch(); 
    return 0; 
    } 

輸出:enter image description here