我正在嘗試撰寫一個程式,該程式最多讀取 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;
}
uj5u.com熱心網友回復:
看這里
// function for calculating the average
float sum(account* temp) {
float average = 1.0, sum = 0.0;
int i;
account* start; <<<==== a random value
temp = start; <<<=== over write the parameter of this function with a random value
for (i = 0; i < t; i ) {
sum = temp->total;
temp = temp->nextptr;
}
average = sum / t;
return average;
}
不確定您在這里要做什么-但這肯定是錯誤的
uj5u.com熱心網友回復:
您的代碼存在很多問題:
您說您最多需要 8
account秒,但是代碼沒有施加這樣的限制。在
sum()(命名錯誤,順便說一句)中,您沒有正確回圈節點,因為temp = start;分配是向后的。start未初始化,然后temp在回圈內部使用,從而呼叫未定義的行為。您實際上根本不需要該start變數,因為您可以簡單地增加account* temp引數。在 中
main(),您最初指向ptr8 s 的陣列account,但是如果用戶1在您的選單上輸入,并且ptr是NULL(除非初始失敗,否則不會是malloc()),那么您指向ptr的是單個account. 如果你的目標是讓用戶輸入任意數量的accounts,那么你的串列管理就全錯了。更糟糕的是,每次用戶進入
1您的選單時,您都是在陣列中addaccount()的第一個呼叫,并且只是用資料填充指定的。因此,您實際上并沒有創建一個新的并將其添加到串列中。您只是一遍又一遍地將第一個鏈接回自身。accountaddaccount()accountaccountaccount如果用戶在您的選單上輸入,
2您將呼叫最后一個由. 如果尚未創建 no,您的代碼將崩潰,因為此時未初始化。您需要先呼叫該函式,以便它可以迭代整個串列。sum()account1accounttempaccount如果用戶
3在您的選單上輸入,代碼會嘗試free()單獨account的 s,但您實際上并沒有從malloc()單獨account的 s 開始。您正在嘗試將accounts 存盤在陣列中,因此您只需要改為free()陣列。此外,您的回圈正在檢查
selection != 4,但您的選單沒有選項4。你應該檢查selection != 3一下。
話雖如此,嘗試更像這樣的東西:
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467394.html
標籤:C
上一篇:如何獲得總數?
