Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中格式化命令行输入_C_Argv - Fatal编程技术网

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

如何在C中格式化命令行输入,c,argv,C,Argv,我试图解决括号平衡问题,并调整代码以使用命令行输入(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 expr

我试图解决括号平衡问题,并调整代码以使用命令行输入(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] == "(")
我希望得到关于如何正确格式化cmd输入的建议,以便能够进行比较。我已经通过使用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
    }}
#包括
#包括
#包括
#定义最大值20
结构堆栈
{
字符stk[MAX];
int top;
}s;
无效推送(字符*项)
{
内伦;
int i;
len=strlen(char[i]);
其他[长];
对于(i=0;i问题:

len=strlen(char[i]);

并不意味着什么。如果您想获得
项目的大小,您必须写:
len=strlen(项目);


但是,因为堆栈中将使用
其他
,所以在
推送
之外,它不能是
推送
的内部,您必须使用
malloc
或友元函数保留一些内存。在这种情况下,您不需要
len
和简单的

char *other = strdup(item); //( ~ malloc + strcpy in one function)
那就足够了

(re)但是鉴于堆栈和使用它的代码,您似乎只堆栈单个字符。在这种情况下,您可以简化
推送

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

另一方面,您的主要功能似乎很复杂,使用strcmp

(strcmp(argv[i],"(")==0)
可以写

( argv[i][0] == '(' )
而堆栈字符检查是错误的:

            if(s.stk[s.top] == "(")
应该写

            if(s.stk[s.top] == '(')
此外,使用
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;
}
intmain(intargc,char*argv[])
{
int i=0;
s、 top=-1;
对于(i=0;i
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;
}