如何在C中格式化命令行输入
我试图解决括号平衡问题,并调整代码以使用命令行输入(argc/argv),但我不知道如何正确地将变量推送到堆栈中,以及如何在堆栈和括号之间进行比较如何在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
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
似乎您应该在push()函数中使用函数。len=strlen(char[i]);
=>len=strlen(item);
?char[i]=other[i];
=>other[i]=item[i]
?…@MikeCAT谢谢,我会试试的!@Mathieu哎呀。抱歉,发帖前错过了。谢谢@Mathieu,这是非常彻底的!我对C和编程非常陌生,所以这是非常有价值的建议
if(s.stk[s.top] == "(")
if(s.stk[s.top] == '(')
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;
}