C90:While 循环函数条件总是满足

问题描述 投票:0回答:1

文件set_my.c中的第一题主程序 您必须编写一个程序,该程序接受 int 类型的整数值列表作为输入。 从这个值列表中,您必须构建一个组。 程序必须从用户接收值列表,从中构建组,最后按到达顺序打印组成员。可以假设输入是正态的,但可能有值that repeat more than once in the input list 对于这样的值(在输入中出现不止一次),它的第一次出现决定了它在打印输出中的位置。组中的成员数量不受限制,并且格式化 realloc 函数必须被使用。输入中值的个数也没有限制。输入可以是多行,每行可以出现不限数量的条目。

输入的结束将由标准库中的函数返回的EOF值来标识,通过该函数执行输入(在键盘输入中,可以通过在新行的开头键入d-ctrl来插入EOF) . 输入示例:

13 13 13 45 -9 -9 -9 18 18 18 3 4 55 45 66 13 66

对于这个输入,输出将是:

13 45 -9 18 3 4 55 66

输出只包含输入中出现的唯一值,按照它们第一次出现的顺序。h

删除重复值。

你被要求写两个函数:

set_get - 此函数读取用户的输入并将其存储在一个集合中。

set_print - 此函数以所需的顺序和格式打印集合的元素。

这将是我到目前为止所做的代码。

setSize setArr;
char* input; /*problem with pointing char pointer type directly*/
int prev, size, inputN, count;
int* set;
bool checkMinus;
checkMinus= true;
prev = DEFULT;/*initialze to DEFULT */
set  = NULL; /* initialize set to NULL */
size = 0; /* size of set*/
count = 0;/* number of elements in set*/
inputN; /* transformed from char type to integar type*/
input = malloc(sizeof(char)*2);
input[1] = '\0';
set = (int *) malloc(sizeof(int));

/* prompt user for input */
printf("Enter integers to add to set, one at a time or space between each integers.\n");
printf("End input with Ctrl-D (EOF).\n");
/* loop to read input until EOF is reached */
while (1)
{
    *input = getchar();
    if (feof(stdin)) {
        break;
    }
    
    if (input == ' ' && !checkMinus) { /* initializing check back to true conditons*/
        checkMinus = true; 
    }
                    
    if (!checkMinus && input == '-'){/* check for correction of minus or pluses */
        printf("Please insert valid input requested "
               "(not allowed to have plus or twice in the same number minus).\n");
        exit(1);
    }
                
    if ((*input <'0' || *input > '9') && *input != ' ' && *input != '-'  && *input != '\n' && *input != EOF ) {
        printf("Please insert valid input requested( phrase plus sign was included ).\n");
        exit(1);
    }
                    
    inputN = atoi(input);/*transforming to integar*/

    if (inputN != prev) {
        prev = inputN;/* we will assign new value to prev */
        set = (int *) realloc(set,SIZE_ENLARGE(size++)); /*resize set*/

        if (set == NULL) { /*check if realloc failed*/
            printf("Error: could not allocate memory.\n");
            exit(1);
        }
                        
        set[count++] = inputN; /* add input to set */
    }
}

调试试图添加更多的条件并没有取得什么成就,但是在调试时我发现无论如何总是满足条件。我有一个完全不同的代码,但我不知道如何修复它并且从头开始一个新的,但现在我有一个奇怪的错误,其中一个条件

if  ((*input <'0' || *input > '9') && *input != ' ' && *input != '-'  && *input != '\n' && *input != EOF )

总是遇到,我不知道如何解决它,它在 while 循环中我真的不知道是什么原因造成的或如何解决它,请帮助我,我一直在努力解决这个问题。

我试过调试,发现我无法通过那个条件 (*input <'0' || *input > '9') && *input != ' ' && *input != '-' && *input != ' ' && *输入 != EOF )

c c89
1个回答
0
投票

您发布了您的作业和一段有问题的代码:

  1. 你应该指出问题描述中的“无限制”是有缺陷的。您受到虚拟内存或至少磁盘大小的限制。根据定义,集合也是未排序的,所以他们应该称之为其他东西。
  2. 头文件丢失
  3. 代码片段甚至不在函数中
  4. setSize
    未定义
  5. DEFAULT
    未定义
  6. inputN;
    什么都不做;应该是初始化?
  7. input
    在用于存储来自
    int
    的数据时应该是
    getchar()
  8. 不知道为什么当它是大小为 2 的固定数组时动态分配
    input()
    (实际上你只需要一个
    int
    )。
  9. input == ' '
    将指针与整数常量进行比较,这不是您想要的。
  10. realloc()
    的值分配给临时变量,否则会在失败时泄漏内存。在下面的代码中,调用者应该处理 NULL 返回值。
  11. 不要投
    void *
    .
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

struct set {
    int *data;
    size_t n;
};

void set_init(struct set *s) {
    *s = (struct set) { NULL, 0 };
}

struct set *set_add(struct set *s, int v) {
    for(size_t i = 0; i < s->n; i++)
        if(s->data[i] == v)
            return s;
    int *tmp = realloc(s->data, sizeof(s->data) * (s->n + 1));
    if(!tmp)
        return NULL;
    s->data = tmp;
    s->data[s->n++] = v;
    return s;
}

void set_print(const struct set *s) {
    for(size_t i = 0; i < s->n; i++) {
        printf("%d%s", s->data[i], i + 1 == s->n ? "\n" : " ");
    }
}

struct set *set_get(struct set *s) {
    set_init(s);
    for(;;) {
        int v;
        if(scanf("%d", &v) != 1)
            break;
        set_add(s, v);
    }
    return s;
}

int main() {
    struct set s;
    set_get(&s);
    set_print(&s);
}

和示例会话:

13 13 13 45 -9 -9 -9 18 18 18 3 4 55 45 66 13 66
13 45 -9 18 3 4 55 66
© www.soinside.com 2019 - 2024. All rights reserved.