我编写了一个程序,将银行及其客户的数据存储在两个列表中,当被要求打印有关给定客户的所有帐户信息时,请注意,单个帐户可能在多个银行中拥有多个帐户。存储是可以的,也是搜索帐户名称的第一次迭代,但随后客户列表被删除......
我做错了什么?
这些是输入文件:
在1.txt
sanpaolo rome dddd aaaaaa in2.txt
unicredit rome dddd aaaaaa in3.txt
在2.txt
rossi mario xxxxx 1000
rossi luigi zzzzz 2000
scipio hannibal aaads 10
toad peach ssss 1000
toad peach ddwd 100000
rossi mario 222x2 100000
in3.txt
rossi mario aaaaxx 1000
toad princess ccczzz 2000
panerai watch xxxads 10
pavlov dog saaasss 1000
froid freud ddcwd 100000
froid freud 222x2 100000
这是输出
的示例
enter name to search end to terminate:
toad
unicredit rome dddd aaaaaa
toad princess ccczzz 2000
sanpaolo rome dddd aaaaaa
toad peach ddwd 100000
toad peach ssss 1000
enter name to search end to terminate:
froid
unicredit rome dddd aaaaaa
-
sanpaolo rome dddd aaaaaa
-
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 50
struct bank {
char name[LEN];
char branch[LEN];
char abi[LEN];
char cab[LEN];
struct bank *next;
struct client *link;
};
struct client {
char lastname[LEN];
char firstname[LEN];
char iban[LEN];
long int deposit;
struct client *next;
};
void cleanUp(struct bank *headP, struct client *headP2);
static void printclientinfo(struct client *client);
static void printbankinfo(struct bank *bank);
static void searchperson(struct bank *bank, char *name);
struct bank *pushbank(struct bank *bank,char *tname, char *tbranch,
char *tabi, char *tcab, char *filename);
struct client *pushclient(struct client *client, char *last, char *first,
char *iban, long int deposit);
int main(int argc, const char *argv[]) {
char name[LEN], branch[LEN], abi[LEN], cab[LEN], filename[LEN], string[LEN];
struct bank *mybank = NULL, *t = NULL;
FILE *in1;
in1 = fopen("in1.txt", "r");
while (fscanf(in1,"%s %s %s %s %s", name, branch, abi, cab, filename) != EOF) {
mybank = pushbank(mybank, name, branch, abi, cab, filename);
}
while (1 == 1) {
t = mybank;
printf("enter name to search t end to terminate:n");
scanf("%s", string);
if (strcmp(string, "end") == 0) {
fclose(in1);
cleanUp(mybank, mybank->link);
return 0;
} else {
searchperson(t, string);
}
}
return 0;
}
void cleanUp(struct bank *headP, struct client *headP2) {
struct bank *tmpP, *l_Next;
struct client *tmpP2, *l_Next2;
tmpP = headP;
tmpP2 = headP2;
while (tmpP != NULL) {
l_Next = tmpP->next;
free(tmpP->link);
free(tmpP->next);
tmpP = l_Next;
}
while (tmpP2 != NULL) {
l_Next2 = tmpP2->next;
free(tmpP2->next);
tmpP2 = l_Next2;
}
free(tmpP);
free(tmpP2);
headP = NULL;
headP2 = NULL;
return;
}
static void printbankinfo(struct bank *bank) {
struct bank *t = NULL;
t = bank;
printf("%s %s %s %sn", t->name,t->branch, t->abi, t->cab);
}
static void printclientinfo(struct client *client) {
struct client *t = NULL;
t = client;
printf("t%s %s %s %ldn", t->lastname, t->firstname, t->iban, t->deposit);
}
static void searchperson(struct bank *bank, char *name) {
int flag = 0;
while (bank != NULL) {
printbankinfo(bank);
flag = 0;
while (bank->link != NULL) {
if (strcmp(bank->link->lastname, name) == 0) {
printclientinfo(bank->link);
flag = 1;
}
bank->link = bank->link->next;
}
if (flag == 0) printf("-n");
bank = bank->next;
}
}
struct bank *pushbank(struct bank *bank, char *tname, char *tbranch,
char *tabi, char *tcab, char *filename) {
struct bank *newptr = NULL;
char last[LEN], first[LEN], iban[LEN];
long int deposit;
FILE *in;
in = fopen(filename, "r");
newptr = malloc(sizeof(struct bank));
strcpy(newptr->name, tname);
strcpy(newptr->branch, tbranch);
strcpy(newptr->abi, tabi);
strcpy(newptr->cab, tcab);
newptr->link = NULL;
while (fscanf(in, "%s %s %s %ld", last, first, iban, &deposit) != EOF) {
newptr->link = pushclient(newptr->link, last, first, iban, deposit);
}
newptr->next = bank;
return newptr;
}
struct client *pushclient(struct client *client, char *last, char *first,
char *iban, long int deposit) {
struct client *newptr = NULL;
newptr = malloc(sizeof(struct client));
strcpy(newptr->lastname, last);
strcpy(newptr->firstname, first);
strcpy(newptr->iban, iban);
newptr->deposit = deposit;
newptr->next = client;
return newptr;
}
在searchPerson
你做bank->link=bank->link->next;
.这会修改原始mybank
客户端列表并损坏您发现的整个列表。
请改用游标变量,这样就不会修改原始列表。
struct client *current_client = bank->link;
while (current_client != NULL) {
if ((strcmp(current_client->lastname, name) == 0)){
printclientinfo(current_client);
flag=1;
}
current_client = curent_client->next;
}