简体   繁体   中英

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. 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).

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. 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.

So if data is 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.

  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). Eg

'0' -> 0 '1' -> 1 Eg"100" -> 100

It assumes ASCII representation and hence uses 48 . A more portable way would be to use '0' instead:

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

According to your code snippet

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

this line will change your string to integer format

for example like you are giving input 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.

like this you are converting string to an integer 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.

#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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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