[英]How to format command line input in C
我正在尝试解决括号平衡问题并调整以下代码以使用命令行输入(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
}}
问题:
len = strlen(char[i]);
并不意味着什么。 如果要获取item
的大小,则必须编写:
len = strlen(item);
char other [len];
for(i=0;i<len;i++)
char[i]=other[i];
您尝试将other
未启动的数据复制到... item
? 在那种情况下,你应该写
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.
或者
char other [len+1];
strcpy(other, item);
但是,由于other
将在堆栈中使用,因此在push
之外,它不能在内部push
,您必须保留一些 memory 与malloc
或朋友功能。 在这种情况下,你不需要len
和一个简单的
char *other = strdup(item); //( ~ malloc + strcpy in one function)
就足够了。
(重新)但是考虑到堆栈和使用它的代码,您似乎只堆叠单个字符。 在这种情况下,您可以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];
}
}
另一方面,您的主要 function 使用strcmp
似乎很复杂,其中简单的char
比较就足够了:
(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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.