簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM