简体   繁体   English

以下C语言代码段是做什么的?

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

The following code snippet is a part of the program to evaluate a postfix expression. 以下代码段是评估后缀表达式的程序的一部分。 I am not too much experienced with C programming, so forgive me for my lesser knowledge. 我对C编程没有太多的经验,所以请原谅我的较少知识。 I do not understand what the highlighted part of the code is doing. 我不明白代码中突出显示的部分在做什么。

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;
}

It is converting the number in the string str to an actual number, digit per digit (or character per character if you will). 它将字符串str的数字转换为实际数字,即每位数一位(如果可以的话,则为每个字符)。

The line 线

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

takes the number "so far" and adds the new digit to it, by multiplying the number by 10 and then adding the value of str[i] to it. 通过将数字乘以10,然后将str[i]的值添加到“到目前为止”的数字并将其添加到新数字上。 The character str[i] is in the range '0' .. '9' and by subtracting 48 of it -- the ASCII value of '0' -- you get the value of the digit. 字符str[i]的范围为'0'..'9',并减去48(ASCII值“ 0”),即可得到数字值。

So if data is 95; 因此,如果data为95; for instance, and str[i] is '3', then data becomes 950 + ASCII code of '3' - ASCII code of '0', so data becomes 950 + 3 = 953. 例如,如果str[i]为'3',则data变为950 + ASCII代码为'3'-ASCII代码为'0',因此data变为950 + 3 = 953。

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

That line converts the value of str[i] to integer value and converts whole number (hence the multiplication by 10). 该行将str[i]值转换为整数值并转换整数(因此乘以10)。 Eg 例如

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

It assumes ASCII representation and hence uses 48 . 它采用ASCII表示,因此使用48 A more portable way would be to use '0' instead: 一种更可移植的方式是改为使用'0'

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

According to your code snippet 根据您的代码片段

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

this line will change your string to integer format 此行会将您的字符串更改为整数格式

for example like you are giving input 235 例如像您输入235

then ASCII code of 2 is 50 and when you subtract with 48 then it will come 2. now multiply you previous no (which is 0) by 10 and add 2. then it will become 2 next 3 will come which is having 51 ASCII and after subtract 48 will become 3 now multiply you previous no (which is 2) by 10 and add 3. then it will become 23 and so on. 然后2的ASCII码是50,当您用48减去时,它将变成2。现在将您之前的编号(为0)乘以10,再加上2。然后它将变成2,下一个3将是具有51个ASCII的,减去48后将变为3,然后将您之前的编号(即2)乘以10,再加上3。则它将变为23,依此类推。

like this you are converting string to an integer no. 像这样,您正在将字符串转换为整数no。

For the better understanding print your values that is been generated at the intermediate instance since your not too much experienced in C Program have a printf statement so that you can understand the logic. 为了更好地理解,请打印您在中间实例处生成的值,因为您对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;
  }

output: 输出: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM