【问题标题】:How do I ignore spaces in a string input?如何忽略字符串输入中的空格?
【发布时间】:2020-07-20 18:15:06
【问题描述】:

我正在创建一个程序来评估包含在单行文本文件中的后缀表达式。我在处理扫描文件中的空格时遇到了一些麻烦。到目前为止,我所做的是将文件中的单行扫描到缓冲区中,然后一次处理一个字符的字符串。将行读入字符串后,如何忽略空格?例如:

2 4 3 * + 

这是完整的程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int stack[1000];
int top = -1;
void push(int x);
int pop();

bool isOperator(char ch);
int performOperation(int op1, int op2, char op);

int main()
{
    char exp[1000], buffer[15]; 
    int i, num, op1, op2, len, j, x;
    int stack[1000];
    char fileName[20];
    FILE *inFile;
    char *e;
    printf("Please enter text file:");
    scanf("%s", fileName);
    inFile = fopen(fileName, "r");
    if (inFile == NULL) {
        printf("Error\n");
        return -1;
    }
    
    int N = 0; i = 0, temp;
    while (!feof(inFile)) {
        fgets(buffer, 15, inFile);
        N++;
    }

    printf("Postfix expression:\n");
    printf("%s", buffer);
    e = buffer;
    while (*e != '\0') {
        if (isdigit(*e)) {
            num = *e - 48;
            push(num);
        } else {
            op1 = pop();
            op2 = pop();
            if (isOperator(*e)) {
                int ans;
                ans = performOperation(op1, op2, *e);
            }
            push(ans);
        }
        e++;
    }
    printf(" The value of the expression is %d\n", ans);
}

void push(int x)
{
    stack[++top] = x;
}
 
int pop()
{
    return stack[top--];
}

bool isOperator(char ch) {
    if (ch == '+' || ch == '-' || ch == '*'|| ch == '/')
        return true;
    else 
        return false;
}
   
int performOperation(int op1, int op2, char op) {
    int ans;
    switch (op) {
    case '+':
        ans = op2 + op1;
        break;
    case '-':
        ans = op2 - op1;
        break;
    case '*':
        ans = op2 * op1;
        break;
    case '/':
        ans = op2 / op1;
        break;
    }
    return ans; 
}

感谢任何帮助。

我忘了说我们只处理一位数字。

【问题讨论】:

标签: c file-io postfix-notation


【解决方案1】:

您可以删除空格,这样您就不必处理它们了

void remove_spaces(char* s) {
    const char* d = s;
    do {
        while (*d == ' ') {
            ++d;
        }
    } while (*s++ = *d++);
}

【讨论】:

  • 剥离空格不合适:2 4 3 * + 变为 243*+,这是一个语法错误。
  • 并拆分它们并传递一个数组槽
【解决方案2】:

如果我理解正确,问题在于处理嵌入的空格。

要解决此问题,您至少可以通过以下方式重写 while 循环

int ans = 0;
for ( e = buffer; *e != '\0'; ++e ) {
    if ( !isspace( ( unsigned char )*e ) )
    {
        if ( isdigit( ( unsigned char )*e ) ) {
            num = *e - '0';
            push(num);
        } else if ( isOperator(*e) ) {
            op1 = pop();
            op2 = pop();
            ans = performOperation(op1, op2, *e);
            push(ans);
        }
    }
}

ans = pop();
printf(" The value of the expression is %d\n", ans);

在printf调用之前注意这个语句

ans = pop();

您必须在打印之前从堆栈中弹出答案。此外,您应该处理无效字符并检查堆栈是否为空。

也是这个循环

int N = 0; i = 0, temp;
while (!feof(inFile)) {
    fgets(buffer, 15, inFile);
    N++;
}

接缝没有意义,循环的条件不正确。例如,对于空文件,变量缓冲区将不包含有效数据。

【讨论】:

  • 嗨@Vlad,好久不见...您可能想提一下代码只处理一位数字。
  • @chqrlie 我认为该程序是专门为处理正的一位数而编写的。否则对于初学者来说太复杂了。
  • 对于初学者来说确实是一个复杂的程序,并且 OP 证实了您的假设,但我不明白为什么有人会否决您的答案。
  • @chqrlie 这不重要。:)
【解决方案3】:

您的代码中存在多个问题:

  • 循环while (!feof(inFile)) 不正确。你应该改用:

      while (fgets(buffer, sizeof buffer, inFile)) {
          /* handle the string expression in buffer */
    
  • 遇到数字时不要立即推送数字,应该解析可能有多个数字的数字。

  • 正确解析数字后,您可以丢弃解析器中遇到的任何空白。

这是修改后的版本:

#include <stdio.h>

bool isOperator(char ch) {
    if (ch == '+' || ch == '-' || ch == '*'|| ch == '/')
        return true;
    else 
        return false;
}
   
int performOperation(int op1, int op2, char op) {
    int ans;
    switch(op) {
    case '+':
        ans = op2 + op1;
        break;
    case '-':
        ans = op2 - op1;
        break;
    case '*':
        ans = op2 * op1;
        break;
    case '/':
        ans = op2 / op1;
        break;
    }
    return ans; 
}

int main() {
    char fileName[100];
    char buffer[100];
    FILE *inFile;

    printf("Please enter text file:");
    if (scanf("%99s", fileName) != 1) {
        printf("No input\n");
        return 1;
    }
    inFile = fopen(fileName, "r");
    if (inFile == NULL) {
        printf("Error\n");
        return -1;
    }
    
    while (fgets(buffer, sizeof buffer, inFile);
        printf("Postfix expression:\n");
        printf("%s", buffer);
        char *e = buffer;
        while (*e != '\0') {
            if (isdigit((unsigned char)*e)) {
                int num = 0;
                while (isdigit((unsigned char)*e)) {
                    num = num * 10 + *e++ - '0';
                }
                push(num);
            } else
            if (isspace((unsigned char)*e) {
                e++; // ignore white space
            } else
            if (isOperator(*e)) {
                int op1 = pop();
                int op2 = pop();
                int ans = performOperation(op1, op2, *e++);
                push(ans);
            } else {
                printf("Invalid character in expression: %c\n", *e++);
            }
        }
        int ans = pop();
        printf(" The value of the expression is %d\n", ans);
    }
    fclose(inFile);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-17
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多