C语言-“怪异” char[] 输出中的字符

发布于 2024-12-14 18:36:58 字数 1664 浏览 4 评论 0原文

我必须构建一个 C 程序,使用 STACK 将中缀表示法转换为后缀表示法。进展顺利,并且在某种程度上发挥了作用。我上次使用 C 语言已经是很久以前的事了,所以我可能不太擅长使用 char[] 变量。

所以问题是,当我给出这样的输入时:

A+B*(C*E-D)

我的程序返回这样的:

ABCE*D-*+ĚĚĚĚĚĚĚĚĚĚĚ

正如你所看到的,我的程序很好地完成了后缀转换,但我的结果中有一堆“垃圾”字符(ĚĚĚĚĚĚĚĚĚĚ)。

这是我的代码片段(只有我认为不正确的部分,可能是 char[] 以及我如何为 postfix[] 变量赋值的方式:

int main()
{
    char infix[20], postfix[20];
    int len, tip, i, p=0;

    STACK pom;
    MAKE_NULL(&pom);

    printf ("Unesi izraz.\n");
    scanf ("%s", infix);

    len = strlen(infix);

    for(i=0; i<len; i++)
    {
        tip = nadi_tip(infix[i]);

        if (tip == Lijeva)
        {
            PUSH (infix[i], &pom);
        }

        if (tip == Operand)
        {
            postfix[p] = infix[i];
            p++;
        }

        if (tip == Desna)
        {
            while (!EMPTY(pom) && (TOP(pom)!= '('))
              {
                postfix[p++] = TOP(pom);
                POP (&pom);
              }
            POP (&pom);
        }

        if (tip == Operator)
        {
            while (!EMPTY(pom) && TOP(pom)!= '(')
             {
                if(prioritet(infix[i]) <= prioritet(TOP(pom)))
                {
                  postfix[p++] = TOP(pom);
                  POP (&pom);
                }
                else break;
             }
             PUSH(infix[i], &pom);
        }
    }
 while (EMPTY(pom) != 1)
 {
    postfix[p++] = TOP(pom);
    POP(&pom);
 }

 printf("Izlaz: %s", postfix);
 return 0;

}

infix[] 是我的输入,postfix[] 是我的输出。做了什么我做错了,为什么我有 ĚĚĚĚĚĚĚĚĚ 字符。

I had to build a C program which convert's infix notation into postfix notation using STACK. That went well and it's working in some way. It was long ago when I used last time C language so I'm probably dont use char[] variables very well.

So problem is that when I give input like this :

A+B*(C*E-D)

My program returns this :

ABCE*D-*+ĚĚĚĚĚĚĚĚĚĚĚ

So as you see my program did postfix conversion very well but I have bunch of "garbage" chars at my result (ĚĚĚĚĚĚĚĚĚĚĚ).

Here is snippet of my code (only part that I think is not correct, maybe something with char[] and way how I assing value to postfix[] variable:

int main()
{
    char infix[20], postfix[20];
    int len, tip, i, p=0;

    STACK pom;
    MAKE_NULL(&pom);

    printf ("Unesi izraz.\n");
    scanf ("%s", infix);

    len = strlen(infix);

    for(i=0; i<len; i++)
    {
        tip = nadi_tip(infix[i]);

        if (tip == Lijeva)
        {
            PUSH (infix[i], &pom);
        }

        if (tip == Operand)
        {
            postfix[p] = infix[i];
            p++;
        }

        if (tip == Desna)
        {
            while (!EMPTY(pom) && (TOP(pom)!= '('))
              {
                postfix[p++] = TOP(pom);
                POP (&pom);
              }
            POP (&pom);
        }

        if (tip == Operator)
        {
            while (!EMPTY(pom) && TOP(pom)!= '(')
             {
                if(prioritet(infix[i]) <= prioritet(TOP(pom)))
                {
                  postfix[p++] = TOP(pom);
                  POP (&pom);
                }
                else break;
             }
             PUSH(infix[i], &pom);
        }
    }
 while (EMPTY(pom) != 1)
 {
    postfix[p++] = TOP(pom);
    POP(&pom);
 }

 printf("Izlaz: %s", postfix);
 return 0;

}

infix[] is my input and postfix[] is my output. What did I do wrong I why am I having ĚĚĚĚĚĚĚĚĚĚĚ characters. Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

回忆那么伤 2024-12-21 18:37:07

我的猜测是您混淆了字符和指向字符串的指针。
POP() 和 PUSH() 的定义在哪里?什么是 ndi_tip()
为什么不在 forloop 中使用 switch,这样更容易阅读和维护。

My guess is that you are confusing characters and pointers to strings.
Where are the definitions for POP() and PUSH() ? What is ndi_tip()
Why don't you use a switch inside the forloop, that is much easier to read and maintain.

国粹 2024-12-21 18:37:06

看起来您的字符串不是以空结尾的。

It doesn't look like your string is null terminated.

2024-12-21 18:37:06

正如其他人所说,你应该初始化你的数组。

或者,在程序中的任何时候,您都可以使用,

memset (infix ,0, 20);
memset (postfix, 0, 20);

这会将数组元素的所有值设置为零。

As others have said you should initialize your array.

Or, at any time in the program, you can use,

memset (infix ,0, 20);
memset (postfix, 0, 20);

That will set all the values of array elements to zero.

舂唻埖巳落 2024-12-21 18:37:05
    char infix[20], postfix[20];

您不初始化它们,也不在算法末尾附加“\0”。

    char infix[20], postfix[20];

You do not initialize these, nor append '\0' at the end of algorithm.

强者自强 2024-12-21 18:37:04

您没有以 NULL 终止您的字符串!开玩笑。您从其他三十多个告诉您的人那里得到了这个消息吗?只是为了添加一些信息,垃圾字符试图将 char[] 末尾之外的内存中发生的任何内容解释为字符。它会抓取内存中的所有内容,直到遇到空终止字符并将其全部吐出,每次都相同的原因是您的 char[]ĚĚĚĚĚĚĚĚĚĚĚ< /code> 每次程序运行时都会被分配到彼此相邻的位置。如果您已经知道所有这些,那么我很抱歉用另一个多余的答案浪费您的时间。

YOU DIDN'T NULL TERMINATE YOUR STRINGS! Kidding. Did you get that message yet from the thirty or so other people that told you? Just to add some information on top of that, the garbage characters are an attempt to interpret whatever happens to be in memory beyond the end of your char[] as chars. It grabs all it can of whatever is in memory until it hits a null terminating character and spits it all out, and the reason it's the same every time is that your char[] and ĚĚĚĚĚĚĚĚĚĚĚ get allocated next to each other every time the program runs. If you already knew all of this, then I apologize for wasting your time with another superfluous answer.

只有一腔孤勇 2024-12-21 18:37:04

您需要以 NUL 终止 postfix

postfix[p] = 0;
printf...

一种更简单(但效率稍低)的方法是将数组初始化为 {0} 或将其 memset0

You need to NUL-terminate postfix.

postfix[p] = 0;
printf...

A simpler (but slightly less efficient) method would be to initialize your array to {0} or to memset it to 0.

静赏你的温柔 2024-12-21 18:37:02

您的 postfix 字符串上似乎没有 NUL 终止符。您可以将定义更改为 char postfix[20] = {0};,或者在 printf 之前添加 postfix[p] = '\0';

It looks like you don't have a NUL terminator on your postfix string. You could either change the definition to char postfix[20] = {0};, or you just before the printf, you could add postfix[p] = '\0';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文