简体   繁体   English

如何在 C 中格式化命令行输入

[英]How to format command line input in C

I'm trying to do the parentheses balance problem and adjust the following code to work with command-line input (argc/argv), but I can't figure out how to properly push variables to the stack and do comparisons between the stack and parentheses.我正在尝试解决括号平衡问题并调整以下代码以使用命令行输入(argc/argv),但我无法弄清楚如何正确地将变量推送到堆栈并在堆栈之间进行比较和括号。

balance.c: In function 'push':
balance.c:16:18: error: expected expression before 'char'
     len = strlen(char[i]);
                  ^~~~
balance.c:19:9: error: expected expression before 'char'
         char[i]=other[i];
         ^~~~
balance.c:25:22: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
         s.stk[s.top] = other;
                      ^
balance.c: In function 'main':
balance.c:55:33: warning: comparison between pointer and integer
                 if(s.stk[s.top] == "(")

I was hoping to get advice on how to properly format cmd input so I would be able to make comparisons.我希望获得有关如何正确格式化 cmd 输入的建议,以便能够进行比较。 I've adjusted some of them by using strcmp, but am not sure how to move forward.我已经使用 strcmp 调整了其中的一些,但不确定如何继续前进。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20

struct stack
{
    char stk[MAX];
    int top;
}s;

void push(char* item)
{
    int len;
    int i;
    len = strlen(char[i]);
    char other [len];
    for(i=0;i<len;i++)
        char[i]=other[i];
    if (s.top == (MAX - 1))
        printf ("Stack is Full\n");
    else
    {
        s.top = s.top + 1; // Push the char and increment top
        s.stk[s.top] = other;
    }}

void pop()
{
    if (s.top == - 1)
    {
        printf ("Stack is Empty\n");
    }
    else
    {
        s.top = s.top - 1; // Pop the char and decrement top
    }}

int main(int argc, char* argv[])
{
    int i = 0;
    s.top = -1;
    for(i = 0;i < argc;i++)
    {
        if((strcmp(argv[i],"(")==0) || (strcmp(argv[i],"[")==0) || (strcmp(argv[i],"{")==0))
        {
            push(argv[i]); // Push the open bracket
            continue;
        }
        else if((strcmp(argv[i],")")==0) || (strcmp(argv[i],"]")==0) || (strcmp(argv[i],"}")==0)) // If a closed bracket is encountered
        {
            if((strcmp(argv[i],")")==0))
            {
                if(s.stk[s.top] == "(")
                {
                    pop(); // Pop the stack until closed bracket is found
                }
                else
                {
                    printf("\nUNBALANCED EXPRESSION\n");
                    break;
                }}
            if((strcmp(argv[i],"]")==0))
            {
                if(s.stk[s.top] == "[")
                {
                    pop(); // Pop the stack until closed bracket is found
                }
                else
                {
                    printf("\nUNBALANCED EXPRESSION\n");
                    break;
                }}
            if((strcmp(argv[i],"}")==0))
            {
                if(s.stk[s.top] == "{")
                {
                    pop(); // Pop the stack until closed bracket is found
                }
                else
                {
                    printf("\nUNBALANCED EXPRESSION\n");
                    break;
                }}}}
    if(s.top == -1)
    {
        printf("\nBALANCED EXPRESSION\n"); // Finally if the stack is empty, display that the expression is balanced
    }}

Problems:问题:

len = strlen(char[i]);
Does not mean nothing.并不意味着什么。 If you want to get the size of item , you must write:如果要获取item的大小,则必须编写:
len = strlen(item);


char other [len];
for(i=0;i<len;i++)
    char[i]=other[i];

You try to copy the uninitiated data of other into... item ?您尝试将other未启动的数据复制到... item In that case, you should have written在那种情况下,你应该写

char other [len+1];
for(i=0;i<len;i++)
    other[i] = item[i];
other[len] = '\0'; // do not forget the final character of a NULL terminated string.

or或者

char other [len+1];
strcpy(other, item);

But , since other will be use in the stack, so outside of push , it can't be internal to push , you have to reserve some memory with malloc or friend functions.但是,由于other将在堆栈中使用,因此在push之外,它不能在内部push ,您必须保留一些 memory 与malloc或朋友功能。 In that case, you don't need len and a simple在这种情况下,你不需要len和一个简单的

char *other = strdup(item); //( ~ malloc + strcpy in one function)

whould be enough.就足够了。

(re) But given the stack and the code using it, it seems you only stack single characters. (重新)但是考虑到堆栈和使用它的代码,您似乎只堆叠单个字符。 In that case, you could simplify push to:在这种情况下,您可以push送简化为:

void push(char* item)
{
    if (s.top == (MAX - 1))
        printf ("Stack is Full\n");
    else
    {
        s.top = s.top + 1; // Push the char and increment top
        s.stk[s.top] = item[0]; 
    }
}

On one other side, your main function seems complicated using strcmp where simple char comparison would suffice:另一方面,您的主要 function 使用strcmp似乎很复杂,其中简单的char比较就足够了:

(strcmp(argv[i],"(")==0)

can be written可以写

( argv[i][0] == '(' )

And you stack character checking is wrong:你堆栈字符检查是错误的:

            if(s.stk[s.top] == "(")

should be writteng应该写成

            if(s.stk[s.top] == '(')

Moreover, your code could have a real interest using switch/case :此外,您的代码可能会对使用switch/case产生真正的兴趣:

int main(int argc, char* argv[])
{
    int i = 0;
    s.top = -1;
    for(i = 0;i < argc;i++)
    {
        switch(argv[i][0]) {
            case '(':
            case '[':
                push(argv[i]); // Push the open bracket
                break;
            case ')':
                if(s.stk[s.top] == '(') // note what happend in s.top is -1?
                    pop()
                else
                    printf("\nUNBALANCED EXPRESSION\n");
                    return;
            case ']':
                if(s.stk[s.top] == ']') // note what happend in s.top is -1?
                    pop()
                else
                    printf("\nUNBALANCED EXPRESSION\n");
                    return 0;

        }
    }
       
    if(s.top == -1)
    {
        printf("\nBALANCED EXPRESSION\n"); // Finally if the stack is empty, display that the expression is balanced
    }
    return 0;
}

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

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