簡體   English   中英

以下C語言代碼段是做什么的?

[英]What is this following C language code snippet doing?

以下代碼段是評估后綴表達式的程序的一部分。 我對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;
}

它將字符串str的數字轉換為實際數字,即每位數一位(如果可以的話,則為每個字符)。

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

通過將數字乘以10,然后將str[i]的值添加到“到目前為止”的數字並將其添加到新數字上。 字符str[i]的范圍為'0'..'9',並減去48(ASCII值“ 0”),即可得到數字值。

因此,如果data為95; 例如,如果str[i]為'3',則data變為950 + ASCII代碼為'3'-ASCII代碼為'0',因此data變為950 + 3 = 953。

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

該行將str[i]值轉換為整數值並轉換整數(因此乘以10)。 例如

'0'-> 0'1'-> 1例如“ 100”-> 100

它采用ASCII表示,因此使用48 一種更可移植的方式是改為使用'0'

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

根據您的代碼片段

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

此行會將您的字符串更改為整數格式

例如像您輸入235

然后2的ASCII碼是50,當您用48減去時,它將變成2。現在將您之前的編號(為0)乘以10,再加上2。然后它將變成2,下一個3將是具有51個ASCII的,減去48后將變為3,然后將您之前的編號(即2)乘以10,再加上3。則它將變為23,依此類推。

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

為了更好地理解,請打印您在中間實例處生成的值,因為您對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;
  }

輸出: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM