我正试图编写一个程序,读取最多8个银行账户信息,并将其动态存储在链接列表中.我编写了一个函数来计算所有输入的银行账户余额的平均值,但当我试图调用该函数时,不会产生任何输出.你能帮我找出问题出在哪里吗?

#include <stdio.h>

#include <stdlib.h>

//node structure
typedef struct node {
   int no;
   char name[20];
   float total;
   struct node * nextptr;
}
account;

//int for division of total to find the average
static int t;

//adding accounts function
account * addaccount(account * temp) {
   fflush(stdin);
   printf("Enter the account name: \n");
   gets(temp -> name);
   printf("Enter the account number: \n");
   scanf("%d", & temp -> no);
   printf("Enter the account balance: \n");
   scanf("%f", & temp -> total);
   t++; // used for finding the average 
   return temp;
}

// function for calculating the average
float sum(account * temp) {
   float average = 1.0, sum = 0.0;
   int i;
   account * start;
   temp = start;
   for (i = 0; i < t; i++) {
      sum += temp -> total;
      temp = temp -> nextptr;
   }
   average = sum / t;
   return average;
}

int main() {
   int selection;
   account * start = NULL;
   account * save, * temp;
   account * ptr;
   ptr = (account * ) malloc(sizeof(account) * 8);
   do {
      //Menu
      printf("\n1.Adding account\n");
      printf("2.Find the average of the added accounts' balances\n");
      printf("3.Exit\n");
      scanf("%d", & selection);
      switch (selection) {
      case 1: {
         if (ptr == NULL)
            ptr = (account * ) realloc(ptr, sizeof(account));
         save = addaccount(ptr);
         if (start == NULL) {
            start = save;
            start -> nextptr = NULL;
         } else {
            temp = start;
            while (temp -> nextptr != NULL)
               temp = temp -> nextptr;
            temp -> nextptr = save;
            save -> nextptr = NULL;
         }
         break;
      }
      case 2: {
         float avg;
         avg = sum(temp);
         printf("%f", avg);
         break;
      }
      case 3: {
         temp = start;
         while (temp != NULL) {
            free(temp);
            temp = temp -> nextptr;
         }
         break;
      }
      }
   } while (selection != 4);
   return 0;
}

推荐答案

你的代码有很多问题:

  • 你说你想要8个account中的maximum,但法规没有规定这样的限制.

  • sum()中(顺便说一句,这是错误命名的),您没有正确地循环通过 node ,因为temp = start;赋值是向后的.startuninitialized,然后在循环中使用temp,从而调用undefined behavior.实际上根本不需要start变量,因为可以简单地增加account* temp参数.

  • main()中,您最初将ptr指向一个8 accounts的数组,但如果用户在菜单上输入1,并且ptrNULL(除非初始malloc()失败),那么您将ptr指向一个account.如果你的目标是允许用户输入任意数量的account,那么你的列表管理就完全错了.

    更糟糕的是,每次用户在菜单上输入1,你就在数组的第一个account上调用addaccount(),而addaccount()只是用数据填充指定的account.所以,实际上你根本没有创建一个新的account并将其添加到列表中.你只是一次又一次地将前account个链接回自己.

  • 如果用户在菜单上输入2,则您正在拨打1创建的last account.如果还没有创建account,代码将崩溃,因为此时tempuninitialized.您需要调用first account上的函数,这样它就可以迭代整个列表.

  • 如果用户在您的菜单上输入3,代码将try 输入free()个单独的account,但实际上您并没有从malloc()个单独的account开始.您正在try 将account存储在一个数组中,因此只需将数组存储为free()即可.

    此外,你的循环判断selection != 4,但你的菜单没有选项4.你应该判断selection != 3.

话虽如此,不妨try 以下方式:

#include <stdio.h>
#include <stdlib.h>

//node structure
typedef struct node {
   int no;
   char name[20];
   float total;
   struct node * nextptr;
}
account;

//adding accounts function
account* addaccount() {
   account *temp = malloc(sizeof(account));
   if (temp == NULL) {
      printf("Unable to create new account\n");
      return NULL;
   }
   fflush(stdin);
   printf("Enter the account name: \n");
   gets(temp->name);
   printf("Enter the account number: \n");
   scanf("%d", &temp->no);
   printf("Enter the account balance: \n");
   scanf("%f", &temp->total);
   temp->nextptr = NULL;
   return temp;
}

// function for calculating the average
float average(account* start) {
   if (start == NULL) return 0.0;
   float sum = 0.0;
   int t = 0;
   do {
      sum += start->total;
      start = start->nextptr;
      ++t;
   }
   while (start != NULL);
   return sum / t;
}

int main() {
   int selection;
   account *start = NULL, *last = NULL, *temp;

   do {
      //Menu
      printf("1.Adding account\n");
      printf("2.Find the average of the added accounts' balances\n");
      printf("3.Exit\n");
      scanf("%d", &selection);
      switch (selection) {
         case 1: {
            if ((temp = addaccount()) == NULL) break;
            if (start == NULL)
               start = temp;
            else
               last->nextptr = temp;
            last = temp;
            break;
         }
         case 2: {
            printf("%f\n", average(start));
            break;
         }
      }
   } while (selection != 3);

   temp = start;
   while (temp != NULL) {
      free(temp);
      temp = temp -> nextptr;
   }

   return 0;
}

C++相关问答推荐

ATmega328P USART发送字符重复打印

我应该如何解决我自己为iOS编译的xmlsec1库的问题?转换Ctx.first在xmlSecTransformCtxPrepare()之后为空

正在try 将文件/文件夹名从目录 struct 存储到链接列表

为什么内核使用扩展到前后相同的宏定义?

C是否用0填充多维数组的其余部分?

使用scanf在C中读取和存储文件中的值

GTK3按钮信号错误

在创建动态泛型数组时,通过realloc对故障进行分段

如何在提取的索引中分配空值?

C代码在字符串中删除不区分大小写的子字符串的问题

C整型和_泛型.哪些类型是兼容的?

在C程序中使用Beaglebone Black UART的问题

从不兼容的指针类型返回&&警告,但我看不出原因

我可以创建适用于不同endian的 colored颜色 struct 吗?

将非连续物理内存映射到用户空间

C23标准是否向后兼容?

在文件描述符上设置FD_CLOEXEC与将其传递给POSIX_SPOWN_FILE_ACTIONS_ADCLOSE有区别吗?

将不同类型的指针传递给函数(C)

execve 不给出which命令的输出

C 预处理器中的标记分隔符列表