gpt4 book ai didi

c - 如何在 C 中不使用数组的情况下读取给定的 "n"输入?

转载 作者:行者123 更新时间:2023-11-30 20:58:22 26 4
gpt4 key购买 nike

我必须读取给定数量的行,重要的是不要使用数组,而且在读取一行后,我需要将每个输入保存在变量中(最多 15 个输入)。使用数组很容易,但我不允许在我的代码中使用!谢谢

最佳答案

您可以将它们读入列表中。这是这种方法的一个粗略片段。

#include <stdio.h>
typedef struct TNumber
{
int num;
struct TNumber * nxt;
}
Number;

int main()
{
const int MAX = 15;
Number * numbers = 0;
//populate numbers list
Number * nptr = 0;
int input, n =0 ;
while (n < MAX && fscanf(stdin, "%d", &input) > 0)
{
Number * new = (Number*)malloc(sizeof(Number));
new->num = input;
new->nxt = 0;
if (numbers == 0)
numbers = nptr = new;
else
{
nptr->nxt = new;
nptr = new;
}
n++;
}
//Output the numbers to check the list
nptr = numbers;
while (nptr)
{
printf("%d ", nptr->num);
nptr = nptr->nxt;
}
}

关于c - 如何在 C 中不使用数组的情况下读取给定的 "n"输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52893407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com