【问题标题】:Code blocks, adding and storing integers through a function代码块,通过函数添加和存储整数
【发布时间】:2016-02-18 03:49:14
【问题描述】:

所以我试图通过要求用户输入 1-100 之间的数字并让程序存储输入的所有内容然后告诉用户所有数字以及加起来是多少来让一个简单的程序工作.

现在我只想知道如何存储变量并让程序能够告诉我输入了多少个数字。

我应该在 main 之外创建一个函数来处理存储和添加吗?

#include <stdio.h>




int main () {

int number, even, odd;

char name;


printf("Enter your name")
scanf(%d, &number);
scanf (%c, &char)

printf("Enter numbers within 1-100")
printf("Enter 0 to quit")

while (number != 0) {
    if (number%2 == 1) {
        //This is where I don't know how to store the odd numbers
    }
    else {

        //And the even numbers here as well
    }



}



printf("%c,the numbers you have entered are broken down as follows:\n",name);
printf("You entered %d even numbers with a total value of \n", even);
printf("You entered %d odd numbers with a total value of \n", odd);




return 0;

}

【问题讨论】:

  • 我还需要为总创建一个变量,并在最后反映输出,但我现在还不担心
  • 使用整数或双精度来累积值,但您似乎也没有在程序中获得用户输入。 number来自哪里?
  • 您需要存储所有内容还是只存储输入的项目数和运行总和?我不太明白你的问题 - 好像你基本上是在说“我不理解变量”......
  • 对不起,要号码后忘记添加scanf了。你说对了一半,我不知道变量的全部功能。我希望程序计算输入的偶数类别和奇数类别的数字,然后计算这两个类别中所有数字的总和。基本上,我如何让程序知道如何存储所有添加的不同条目,我猜在我发现后将所有条目加在一起很容易得到。
  • 您似乎缺乏基本的 C 知识。我建议您在尝试解决此问题之前阅读一本好书/教程。

标签: c function integer


【解决方案1】:

这是一个示例程序。按照上一次更新中的建议按需增强。

#include <stdio.h>
#include <stdlib.h>
int
main ()
{

  int number, even, odd;
  int *evenarr, *oddarr;
  int evencount = 0, oddcount = 0;
  int i;

  char name;

  evenarr = (int *) malloc(100 * sizeof(int));
  oddarr = (int *) malloc(100 * sizeof(int));

  printf ("Enter numbers within 1-100");
  printf ("Enter 0 to quit");
  do
    {

      scanf ("%d", &number);

      if (number != 0)
        {
          if (number < 1 || number > 100)
            continue;
          if (number % 2 == 1)
            {
              //This is where I don't know how to store the odd numbers
              printf ("odd\n");
              oddarr[oddcount] = number;
              oddcount++;
              /* Realloc the arr if size exceed 100 */
            }
          else
            {

              //And the even numbers here as well
              printf ("even\n");
              evenarr[evencount] = number;
              evencount++;
              /* Realloc the arr if size exceed 100 */
            }
        }
    }
  while (number != 0);

  for(i=0; i<oddcount; i++)
    printf("odd : %d\n", oddarr[i]);
  for(i=0; i<evencount; i++)
    printf("even : %d\n", evenarr[i]);

}

【讨论】:

  • 好的。我知道了。你要求最后释放。没关系。
【解决方案2】:

您的问题有点模棱两可,但在我看来,您可能需要动态分配内存以进行存储,因为您不知道在运行时将输入多少个数字。

这可以通过多种方式完成。第一种方法涉及使用 malloc() 和 realloc() 函数。我会让你研究这些函数,但基本上 malloc() 在运行时在堆上分配内存,而 realloc() 允许你调整分配给你的内存大小。

我认为最好的方法是实现链表数据结构。这样,您可以在用户输入数字时存储这些数字,然后再遍历列表并计算有多少数字是奇数或偶数。同样,您也可以计算它们的总数。

关于链表数据结构的更多信息:

这些只是我用来学习链表的一些资源。 Google 上有大量关于链表的更多信息。

【讨论】:

  • 你的答案很有效,但根据 OP 的知识,我认为他无法实现链表。
【解决方案3】:

您可以使用malloc()。它在堆上分配内存。此外,使用realloc() 允许您调整使用malloc() 分配的内存大小。

mallocfree 上查看this 教程。

@Umamahesh 给出的代码是正确的,只是他没有释放程序分配的内存,这可能是致命的。

所以你只需free 指针。 @Umamahesh 答案中的更正代码是:

#include <stdio.h>
#include <stdlib.h>
int
main ()
{

  int number, even, odd;
  int *evenarr, *oddarr;
  int evencount = 0, oddcount = 0;
  int i;

  char name;

  evenarr = (int *) malloc(100 * sizeof(int));
  oddarr = (int *) malloc(100 * sizeof(int));

  printf ("Enter numbers within 1-100");
  printf ("Enter 0 to quit");
  do
    {

      scanf ("%d", &number);

      if (number != 0)
        {
          if (number < 1 || number > 100)
            continue;
          if (number % 2 == 1)
            {
              //This is where I don't know how to store the odd numbers
              printf ("odd\n");
              oddarr[oddcount] = number;
              oddcount++;
              /* Realloc the arr if size exceed 100 */
            }
          else
            {

              //And the even numbers here as well
              printf ("even\n");
              evenarr[evencount] = number;
              evencount++;
              /* Realloc the arr if size exceed 100 */
            }
        }
    }
  while (number != 0);

  for(i=0; i<oddcount; i++)
    printf("odd : %d\n", oddarr[i]);
  for(i=0; i<evencount; i++)
    printf("even : %d\n", evenarr[i]);
  free (evenarr);
  free (oddarr);

}

另见:How do free and malloc work in C?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2018-09-26
    • 2020-07-09
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多