繁体   English   中英

function 和 C 中的 while 循环出现问题:找不到无法退出循环的原因

[英]Problem with function and while loop in C: could not find reason why I cannot exit the loop

我有如下代码:(运行良好)

#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            // divide the given polynomial expression with "()"
        func = malloc(sizeof(int));
        scanf("%[^\n]s", func);
        char *cal[20] = { NULL, };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        return 0;
}

我得到了结果(当我输入 (3x**2+4x**1+4)*(2x**2+3))

(3x**2+4x**1+4)
*
(2x**2+3)
Complete 1

因此,我尝试通过在上面的代码下添加更多 while 循环来进行更多类型的操作:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            // divide the given polynomial expression with "()"
        func = malloc(sizeof(int));
        scanf("%[^\n]s", func);
        char *cal[20] = { NULL, };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        int j = 0;           // Change + to +- in the divided expression
        char *num[20] = {NULL, };
        char ptr2 = strtok(cal, "+");
        while (2j - 2 < i){
                for(int k = 0; k< strlen(cal[2j-2]); k++){
                        if ((cal[2j-2] + k) == '-'){
                                char s[] = "+";
                                memmove(cal[2j - 2] + k-1, s, strlen(s));
                        }
                }

        }
        printf("Complete 2");
        return 0;
}

但是,在这种情况下,我得到了类似的结果

(3x**2+4x**1+4)
*
(2x**2+3)

function 还没有结束。

我想我陷入了第一个 while 循环,因为没有打印出“完成 1”。

为什么会出现这种情况?

感谢您的阅读,任何帮助将不胜感激。

PS 我使用 gcc 命令在 linux 环境中运行此代码。

您的代码有几个主要问题,在我尝试编译它时导致了很多警告甚至错误,正如您在此处看到的那样。

显然,您忽略或禁用了编译器警告,这似乎是问题的根源。 以下是更多信息的链接:

为什么我应该总是启用编译器警告?


我不明白你的算法背后的逻辑,所以我不能给你一个可靠的答案,所以这应该被更多地视为扩展评论而不是仅仅为了让你更进一步的答案。

尽管如此,这里的代码至少修复了所有语法问题。 我无法详细解释它们,因为它们太多了。 将代码与您的代码进行比较,您会看到:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            
        func = malloc(sizeof(*func) * 8);
        scanf("%[^\n]", func);
        char *cal[20] = { NULL };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        int j = 0;           
        char *num[20] = { NULL };
        char* ptr2 = strtok(*cal, "+");
        char s[2] = "+";
        while (j - 2 < i){
                for(int k = 0; k < strlen(cal[j-2]); k++){
                        if ((*(cal[j-2] + k)) == '-'){

                             memmove(cal[j - 2] + (k-1), s, strlen(s));
                        }
                }
        }
        printf("Complete 2");
        return 0;
}

不保证这将以所需的方式工作。 它应该只为您提供正确方向的线索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM