繁体   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