当前位置:网站首页>Realization of readable and writable files in library management system with C language bidirectional linked list
Realization of readable and writable files in library management system with C language bidirectional linked list
2022-06-22 07:54:00 【Day-3】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int nCount = 0;
// Book structure
typedef struct Node {
struct Node * Blink;
struct Node * Flink;
char BookName[50];
float BookPrice;
int BookNumber;
};
struct Node * ndHeaderNode = NULL;
// Add book functions
struct Node * AppendNode(struct Node * CurrentNode, char * BookName, int BookNumber, float BookPrice)
{
struct Node * pNewNode = NULL;
struct Node * pTempNode = NULL;
struct Node * pHeadNode = CurrentNode;
pNewNode = (struct Node *)malloc(sizeof(struct Node));
if (pNewNode == NULL)
{
printf("memory malloc failed!\n");
return pNewNode;
}
if (CurrentNode == NULL)
{
CurrentNode = pNewNode;
CurrentNode->Blink = NULL;
CurrentNode->Flink = NULL;
}
else
{
while (pHeadNode->Flink != NULL)
{
pTempNode = pHeadNode;
pHeadNode = pHeadNode->Flink;
}
pHeadNode->Flink = pNewNode;
pHeadNode->Blink = pTempNode;
}
strcpy(pNewNode->BookName, BookName);
pNewNode->BookPrice = BookPrice;
pNewNode->BookNumber = BookNumber;
pNewNode->Flink = NULL;
nCount++;
return CurrentNode;
}
// Functions for querying books
void QueryNode(struct Node * HeaderNode, char * BookName)
{
if (HeaderNode == NULL)
{
printf("ERROR:HeaderNode == NULL\n");
return;
}
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
printf(" Title :%s\n", HeaderNode->BookName);
printf(" pricing :%f\n", HeaderNode->BookPrice);
printf(" Book number :%d\n", HeaderNode->BookNumber);
return;
}
while (HeaderNode->Flink != NULL)
{
HeaderNode = HeaderNode->Flink;
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
printf(" Title :%s\n", HeaderNode->BookName);
printf(" pricing :%f\n", HeaderNode->BookPrice);
printf(" Book number :%d\n", HeaderNode->BookNumber);
return;
}
}
printf("Can Not Found!\n");
}
// Modify book information
void ModifyNode(struct Node * HeaderNode, char * BookName, float BookPrice)
{
if (HeaderNode == NULL)
{
printf("ERROR:HeaderNode == NULL\n");
return;
}
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
HeaderNode->BookPrice = BookPrice;
printf("ModifyNode Success!\n");
return;
}
while (HeaderNode->Flink != NULL)
{
HeaderNode = HeaderNode->Flink;
if (strcmp(HeaderNode->BookName, BookName) == 0)
{
HeaderNode->BookPrice = BookPrice;
printf("ModifyNode Success!\n");
return;
}
}
printf("ModifyNode Failed!\n");
return;
}
// Delete book functions
void DeleteNode(struct Node * HeaderNode, char * BookName)
{
struct Node * pNode = NULL;
pNode = HeaderNode;
for (size_t i = 0; i < nCount; i++)
{
if (strcmp(pNode->BookName, BookName) == 0)
{
if (pNode == HeaderNode)
{
pNode = HeaderNode->Flink;
free(HeaderNode);
ndHeaderNode = pNode;
HeaderNode = ndHeaderNode;
nCount--;
return;
}
if (pNode->Flink == NULL)
{
pNode->Blink->Flink = NULL;
free(pNode);
nCount--;
printf("Delete Success!\n");
return;
}
pNode->Blink->Flink = pNode->Flink;
pNode->Flink->Blink = pNode->Blink;
free(pNode);
nCount--;
printf("Delete Success!\n");
return;
}
}
}
// Write the contents of the book to a file
void WriteFile(struct Node * HeaderNode, char * szFilePath)
{
struct Node *p = HeaderNode;
FILE * w = fopen(szFilePath, "wb");
if (w == NULL)
{
printf("open file failed!\n");
system("pause");
fclose(w);
exit(0);//0 The normal exit 1 Abnormal exit
}
while (p)
{
// Output linked list node data to the screen
printf("%s ", p->BookName);
printf("%f ", p->BookPrice);
printf("%d ", p->BookNumber);
printf("\n");
// Output linked list node data to file output.txt
fprintf(w, "%s ", p->BookName);
fprintf(w, "%f ", p->BookPrice);
fprintf(w, "%d", p->BookNumber);
fprintf(w, "\n");
p = p->Flink;
}
fprintf(w, "\n");
fclose(w);
return;
}
// Read file contents
struct Node * ReadFile(char * szFilePath)
{
struct Node * head = (struct Node *)malloc(sizeof(struct Node));
head->Blink = NULL;
head->Flink = NULL;
char name[50];
memset(name, 0, 50);
float price;
int number;
struct Node * p;
struct Node * q;
p = q = head;
FILE * r = fopen(szFilePath, "rb");
if (r == NULL)
{
printf("open file failed!\n");
system("pause");
fclose(r);
exit(0);//0 The normal exit 1 Abnormal exit
}
while (fscanf(r, "%s", name) != EOF)
{
q = (struct Node *)malloc(sizeof(struct Node));
fscanf(r, "%f", &price);
fscanf(r, "%d", &number);
strcpy(q->BookName, name);
q->BookPrice = price;
q->BookNumber = number;
q->Blink = p;
p->Flink = q;
p = q;
}
p->Flink = NULL;
return head->Flink;
}
// Show all book information
void show(struct Node * HeaderNode)
{
printf(" Title :%s\n", HeaderNode->BookName);
printf(" pricing :%f\n", HeaderNode->BookPrice);
printf(" Book number :%d\n", HeaderNode->BookNumber);
while (HeaderNode->Flink != NULL)
{
HeaderNode = HeaderNode->Flink;
printf(" Title :%s\n", HeaderNode->BookName);
printf(" pricing :%f\n", HeaderNode->BookPrice);
printf(" Book number :%d\n", HeaderNode->BookNumber);
}
}
void Start()
{
while (1)
{
int nFlag = 0;
int ReadFlag = 0;
char szBookName[50];
float fBookPrice = 0;
float fNewBookPrice = 0;
int nBookNumber = 0;
printf(" Please enter the function you want to use :\n");
printf("1. Add book information \n");
printf("2. Search for book information \n");
printf("3. Modify book information \n");
printf("4. Delete book information \n");
printf("5. Store book information in a file \n");
printf("6. Read book information from file \n");
printf("7. Show all book information \n");
printf("8. sign out \n");
scanf("%d", &ReadFlag);
switch (ReadFlag)
{
case 1:
//memset(szBookName, 0 ,50);
printf(" Please enter the title of the book :");
scanf("%s", szBookName);
printf(" Please enter the pricing :");
scanf("%f", &fBookPrice);
printf(" Please enter the book number :");
scanf("%d", &nBookNumber);
// New function
ndHeaderNode = AppendNode(ndHeaderNode, szBookName, nBookNumber, fBookPrice);
break;
case 2:
printf(" Please enter the title of the book :");
scanf("%s", szBookName);
// Query function
QueryNode(ndHeaderNode, szBookName);
break;
case 3:
printf(" Please enter the title of the book :");
scanf("%s", szBookName);
printf(" Please enter the new pricing :");
scanf("%f", &fNewBookPrice);
//code Modified function
ModifyNode(ndHeaderNode, szBookName, fNewBookPrice);
break;
case 4:
printf(" Please enter the title of the book :");
scanf("%s", szBookName);
// Delete function
DeleteNode(ndHeaderNode, szBookName);
break;
case 5:
//C:\Users\rkvir\Documents\Visual Studio 2015\Projects
WriteFile(ndHeaderNode, "information.txt");
break;
case 6:
ndHeaderNode = ReadFile("information.txt");
break;
case 7:
show(ndHeaderNode);
break;
case 8:
nFlag = 1;
break;
}
if (nFlag)
{
break;
}
}
}
int main()
{
Start();
return 0;
}
边栏推荐
- Open version - order delivery
- FFMPEG坑
- How to import and upload a CSV generated by a third-party platform to a Taobao store
- Detailed explanation of subnet mask
- Open version - inventory description
- Error e: unable to locate package sudo
- Get through version 4.3 mind map
- ASP. Net core development experience
- 7、 Picker component
- LR 2022 ultra detailed installation tutorial "latest"
猜你喜欢

Open version - inventory description

lr 2022超详细安装教程「最新」

Relative positioning, absolute positioning, fixed positioning
![[普通物理]波的能量与干涉](/img/fe/066aa9e8ed776b8f069b59b7123367.png)
[普通物理]波的能量与干涉

navicat如何查询已连接的数据库密码信息

Multimedia architecture -- Introduction to display

模电实验——实验一 晶体管共射极单管放大器

Win openfeign from simple to deep

Toyota bz4x canceled the launch conference. Even if the low-temperature charging problem does not exist, how about the product strength?

充电宝的玄机
随机推荐
【圖論常見模板題】4種最短路解法和2種最小生成樹解法
【宋红康 MySQL数据库 】【高级篇】【06】MySQL的逻辑架构
Upload your own library to pod
MySQL query group by 1055 is the simplest and most convenient way to solve the problem perfectly
setneedsdisplay layoutifneeded setNeedsLayout
The applet uses the step bar vant steps in vant
ASP. Net core development experience
Xmind 2022思维导图激活版资源?
How to import and upload a CSV generated by a third-party platform to a Taobao store
Cocoapods problem record
Crmeb mall distribution function
(8)顺序栈和链栈
Open source get through version - integral function
A training summary of Intranet penetration test
Open version - inventory description
Idea cannot connect to sqlsms
Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)
[multi thread programming] thread scheduling strategy and priority
What is distributed transaction
Relative positioning, absolute positioning, fixed positioning