gpt4 book ai didi

为银行记录创建数据库

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:42 27 4
gpt4 key购买 nike

这是我正在从事的项目的代码,我的目标是创建一个数据库,我可以在其中添加编辑和删除记录。该程序正在编译,但非常缓慢并且经常崩溃。我只是一个初学者,我不明白为什么会这样。也许有人可以帮助我改进代码或为我指明正确的方向?

enter code here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void clearInput(void);
void listAll (void);
void deleteAccount(void);
void addNewAccount(void);
void modifyAccount(void);
int prompt(void);

struct account{
int number;
char lastname[15];
char firstname[15];
float balance;
struct account*next;
};

struct account *firsta, *currenta, *newa;
int anum = 0;

int main()
{
FILE *datafile;
char *filename="studentdatabase.txt";
char ch;
firsta=NULL;
datafile=fopen(filename, "r");
if (datafile) /*assume doesnt exist otherwise*/
{
firsta=(struct account*)malloc(sizeof(struct account));
currenta=firsta;
while (1)
{
newa=(struct account*)malloc(sizeof(struct account));
fread(currenta, sizeof(struct account),1,datafile);
if (currenta->next==NULL)
break;
currenta->next=newa;
}
fclose(datafile);
anum=currenta->number;
}
do
{
clearInput();
puts("\nA - Add a new account");
puts("D - Delete account");
puts("L - List all accounts");
puts("M - Modify account");
puts("Q - Quit this program\n");
printf("\tYour Choice:");
ch=getchar();
ch=toupper(ch);
switch(ch)
{
case 'A':
puts("Add new account\n");
clearInput();
addNewAccount();
break;

case 'D':
puts("Delete account\n");
deleteAccount();
break;

case 'L':
puts("List all accounts\n");
listAll();
break;

case 'M':
puts("Modify an account\n");
modifyAccount();
break;

case 'Q':
puts ("Quit\n");
default:
break;
}
}
while(ch!='Q');
/*Save the records to disc*/
currenta=firsta;
if(currenta==NULL)
return(0); /*No data to write - End of Program*/
datafile=fopen(filename,"w");

if (datafile=NULL)
{
printf("Error writing to %s\n", filename);
return(1);
}
/*write each record to disc*/

while(currenta!=NULL)
{
fwrite(currenta, sizeof(struct account),1,datafile);
currenta=currenta->next;
}
fclose(datafile);
return(0);
}
/*This function clears any text from the input stream*/
void clearInput(void)
{
fflush(stdin);
}
void addNewAccount(void)
{
newa=(struct account*)malloc(sizeof(struct account));
/*Check to see if this is the first record, if so then
itialize all the pointers to this, first ftrusture in
the database*/
if(firsta==NULL)
firsta=currenta=newa;
/*Otherwise you must find the end of the structure list
(easily spotted by the NULL pointer) and add on the new
structure you just allocated memory for*/
else
{
currenta=firsta; /*makes the first current*/

/*loop throught all records*/
while(currenta->next!=NULL)
currenta=currenta->next;
/*last record found*/
currenta->next=newa; /*save the address of new*/
currenta=newa; /*makes current new*/
}
/*now you just fill in the new structure*/
anum++;
printf("%27s:%5i\n","Account number", anum);
currenta->number=anum;
printf("%27s:","Enter customer's lastname");
gets(currenta->lastname);
printf("%27s:","Enter firstname");
gets(currenta->firstname);
printf("%27f:€","Enter account balance");
scanf("%f", &currenta->balance);
/*Finally cap the new record with a NULL pointer so
that you know its the last record*/
currenta->next=NULL;
}

void listAll(void)
{
if (firsta==NULL)
puts("There are no records to print out");
else
{
printf("%6s %-15s %-15s €%8.2f\n",
currenta->number,
currenta->lastname,
currenta->firstname,
currenta->balance);
}
while ((currenta=currenta->next) !=NULL);
}
void deleteAccount(void)
{
int record;
struct account *previousa;
if(firsta==NULL)
{
puts("There are no records to delete");
return;
}
listAll();
/*Shows all record first*/
printf("Enter account number to delete: ");
scanf("%d",&record);

currenta=firsta;
while(currenta!=NULL)
{
{
if(currenta->number==record)
{
if(currenta==firsta) /*special condition*/
firsta=currenta->next;
else
previousa->next=currenta->next;
free(currenta);
printf("Account %d deleted! \n", -record);
return;
}
previousa=currenta;
currenta=currenta->next;
}
}
printf("Account %d was not found!\n", record);
puts("Nothing deleted.");
}
void modifyAccount(void)
{
int record;
if (firsta==NULL)
{
puts("There are no records to modify!");
return;
}
listAll(); /*Show all records first*/
printf("Enter account number to modify or change: ");
scanf("%d",&record);
currenta=firsta;
while (currenta!=NULL)
{
if(currenta->number==record)
{
printf("Account €%d:\n", currenta->number);
printf("Last name: %s\n", currenta->lastname);
if (prompt())
gets (currenta->lastname);
printf("firstname %s \n", currenta->firstname);
if (prompt())
gets(currenta->firstname);
printf("Balance %8.2f\n", currenta->balance);
if (prompt())
scanf("%f", &currenta->balance);
return;
}
else
{
currenta=currenta->next;
}
}
printf("Account %d was not found!\n", record);
}
int prompt(void)
{
char ch;
clearInput();
printf("Update?");
ch=getchar();
ch=toupper(ch);
clearInput();
if(ch=='Y')
{
printf("Enter new. ");
return(1);
}
else return(0);
}

最佳答案

一开始你的输入循环对我来说有点奇怪。

while (1)
{
newa=(struct account*)malloc(sizeof(struct account));
fread(currenta, sizeof(struct account),1,datafile);
if (currenta->next==NULL)
break;
currenta->next=newa;
}

我知道你依赖于这样一个事实,即当你写出列表时,最后编写的结构应该将“下一个”字段设置为 NULL 作为你的结束标记,但我建议你可能想找到一种更简洁的方法检查结束。这也会有一个小的内存泄漏,因为你分配了 newa 但在最后一次读取时中断,所以最后的 newa 永远不会被使用。

此外,加载例程导致列表中的第一条记录为空记录,因为您分配了 firsta,为它设置了 currenta,然后分配了 newa 用于第一次读取并将其设置为空 currentanext

加载后,虽然您将只有 1 条记录。请注意,您不是在阅读 newa,而是在阅读 currenta

fread(currenta, sizeof(struct account),1,datafile);

您保存数据的尝试也有问题,您有一个典型的拼写错误:

datafile=fopen(filename,"w");
if (datafile=NULL)
^^^

您正在将 NULL 重新分配给 datafile 变量,您需要在此处使用 ==

您似乎在整个代码中都假设全局的 currenta 处于已知状态。例如,在 listAll 函数中,您的代码在打印记录之前没有设置 currenta。另请注意,listAll 循环并打印所有记录,您只打印任何 currentA 然后将该变量移至 NULL。

我建议您只将 firsta 保留为全局变量以定位列表的头部,但在其他任何地方都使用局部变量并正确设置它们。

如前所述,您的 printf 语句需要匹配它们的数据类型。这可能会导致失败。

关于为银行记录创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8534149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com