我写了一个程序来存储银行及其客户的两个列表数据,当被要求打印关于给定客户的所有帐户信息时,请注意单个可以在多个银行中拥有多个帐户。存储是可以的,也是第一次搜索帐户名称的迭代,但随后客户列表被删除...
我做错了什么?
这些是输入文件:
< / p>
in1.txt
sanpaolo rome dddd aaaaaa in2.txt
unicredit rome dddd aaaaaa in3.txt
in2.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 %s\n", 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 %ld\n", 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;
}
答案 0 :(得分:0)
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;
}
客户端列表并破坏您找到的整个列表。
而是使用游标变量,这样就不会修改原始列表。
$('.checkbox-class-name').click();