Current account savings management system of data structure linked list (C + +)
Current savings account management system
1 question content and purpose requirements
(1) Structural background
Linked list is a data structure of linked storage. It uses a group of storage units with arbitrary address to store the data elements in the linear list. The data in the linked list is represented by nodes. The of each node is composed of elements + pointers. Among them, elements are the storage units for storing data, and pointers are the address data linking each node.
(2) To achieve the purpose, insert, delete and search the linked list through the pointer. The specific topics are as follows:
This program mainly simulates the bank current savings account management system and realizes the following functions:
1. To open an account, the city of opening the account needs to record the savings account (including account number, name and balance) and set the balance to 0
2. Cancel the account, find the account information, and delete the depositor information if the balance is 0
3. Deposit into an account, find the account information, modify the account balance (original balance + deposit amount), and generate a transaction record (including transaction time, deposit and withdrawal flag and transaction amount)
4. Withdraw money from an account, find the account information, modify the account balance (original balance - withdrawal amount), and generate a transaction record (including transaction time, deposit and withdrawal flag and transaction amount)
5. Display all account information and all account information (account number, name, balance, etc.)
6. Query an account balance, find the account to be searched, and display the account balance
7. Query the transaction records of an account, find the account to be queried, and display all the transaction records of the account
8. If the existing records are stored in the file, the relevant information is read from the file when the program runs, and the information in the file is updated when the program runs
9. If it is required to add the function of calculating interest, how should it be carried out (not successfully implemented)
This topic is the current savings account management system. The information to be stored consists of two parts: one is the account information and the other is the deposit and withdrawal records. A pointer field is added to the single linked list, that is, the whole storage structure is stored by two linked lists, in which the horizontal linked list stores the access records and other information of a user, The vertical linked list stores the user's account ID and other information.
Figure 1 storage structure
The data structure is defined as follows:`
typedef struct node_log
{
string DataTime;// Trading time
char W_D;// Used to mark transaction type
float balance;
struct node_log *next;
} node_log,*p_mode_log;
typedef struct node_account
{
string ID;// User ID
string Name;// User name
float balance;// Account balance
struct node_log *first_node_log;
struct node_account *next;
int num;
} node_account,*p_node_account;``
##2.2.3 function prototype and function introduction
main ( )
Function: main function. The main function is to call various functions, generate data sets and complete human-computer interaction. The program execution always starts from the main function. If there are other functions, it will return to the main function after calling other functions. Finally, the main function will end the whole program. When executing the program, the system calls the main function [1]. The main function is called after the initialization of the non local object with static storage period is completed in the program startup.
get_time ( )
Function: print the system time, which is used to record the deposit and withdrawal time, put the time in tmp, and then store tmp in node_ DataTime field of log storage structure;
add( )
Function: node in storage structure_ Add new nodes in order in account, store the data entered by the user from the keyboard in the new node memory, and set node_ The pointer to log, which makes the original point to node_ The pointer of account becomes to point to node_ Pointer to log.
Delete ( )
Function: node in storage structure_ Delete the original or newly added node in the account, let the user enter the ID to match the original stored ID, and check whether the balance in the ID account is 0. If it is 0, delete the account and put the account in the storage structure node_ Delete in account. If the check balance is not 0, return to the main interface.
get_money ( )
Function: realize the withdrawal function, first in the storage structure node_ Find a matching ID in the account. First compare the money to be withdrawn with the original account. If the money withdrawn is less than node_ The amount in the balance field in the account will match the money the user wants to withdraw to the node_ Subtract the balance in the account, mark the withdrawal symbol, print the withdrawal time by calling the time() function, create a new node, and delete the previous node_ The log pointer points to the newly created node. If the money withdrawn is greater than node_ If the amount in the balance field in account is insufficient, it will be printed and returned.
store_money( )
Function: realize the function of saving money, first in the storage structure node_ Find the matching ID in the account and transfer the money saved by the user to the node_ Add the balance in the account, mark the withdrawal symbol, print the withdrawal time by calling the time() function, create a new node, and delete the previous node_ The log pointer points to the newly created node.
void show( )
Function: realize the function of displaying all users. Loop through the entire node with while()_ The account is traversed. If it is found that no node is created_ The account structure first creates the structure, and then matches the ID entered by the user. If it matches, the output is stored in node_ For the ID, Name and balance data stored in the account, use the for() loop to the node_log, where the number of output results is equal to node_ The table length of the log linked list. The output results include transaction time (DataTime), deposit and withdrawal (W_D) and balance.
void search1( )
Function: realize the function of querying account balance. First check whether L has been created. If it is found that node has not been created_ The account structure is created first by using the if () statement in the node_ In the account linked list, the ID matching with the ID entered by the user is found in sequence. If the matching is successful, the node is checked_ Traverse the account and output node_ User ID, user Name and balance in account.
search2( )
Function: the function of querying account transaction records. First check whether L has been created. If it is found that node has not been created_ The account structure is created first by using the if () statement in the node_ In the account linked list, the ID matching the ID entered by the user is found in sequence. If the matching is successful, the node is checked_ Traverse the account and output node_ User ID, user Name and balance in account. Then use the for () loop to the node_log and match with the deposit / withdrawal mark, print "deposit" / "withdrawal", and output the transaction time (DataTime), deposit / withdrawal (W_D) and balance.
exit(0)
Function: exit(0) means to exit in normal state. exit() is exit. The parameter passed in is the status code when the program exits. 0 indicates normal exit, and others indicate abnormal exit. Exit the program. 0 in parentheses indicates the exit return code of the program, which has no practical significance. Exit () is exit, 0 is the returned parameter, or 1 - 1. You can use it to judge whether the function returns correctly.
C + + generation implementation (Xcode)
This code can also be implemented in Code::blocks 20.03
// main.cpp // Current savings account management system // // Created by Tony's Macbook pro on 2021/6/16. // #include <iostream> #include <fstream> #include <string.h> #include <time.h> #include <stdlib.h> using namespace std; typedef struct node_log { string DataTime; char W_D; float balance; struct node_log *next; } node_log,*p_mode_log; typedef struct node_account { string ID; string Name; float balance; struct node_log *first_node_log; struct node_account *next; int num; } node_account,*p_node_account; p_node_account L; string get_time() { time_t t = time(0); char tmp[64]; strftime(tmp,sizeof(tmp),"%Y/%m/%d/%X",localtime(&t)); return tmp; } void add() { if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp = new node_account; cout<<"Please enter the card number:"; cin>>Lp->ID; cout<<"Please enter your name:"; cin>>Lp->Name; Lp->balance=0; Lp->num=0; Lp->first_node_log=new node_log; Lp->next=L->next; L->next = Lp; cout<<"Added successfully"<<endl; } void Delete() { string ID; if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp=L; cout << "Card number to be cancelled:"; cin >> ID; while(Lp) { if (Lp->next->ID == ID) { if(Lp->balance==0) { Lp->next = Lp->next->next; cout << "Logout succeeded!"; } else { cout<<"Balance is not zero"<<endl; } return; } Lp=Lp->next; } } void get_money() { string ID; if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp=L->next; cout << "Card No.:"; cin >> ID; while(Lp) { if (Lp->ID == ID) { float money; cout << "Withdrawal amount:"; cin>>money; if(Lp->balance>=money) { Lp->balance-=money; Lp->num+=1; p_mode_log p=new node_log; p->W_D='W'; p->DataTime=get_time(); p->balance=money; p->next=Lp->first_node_log->next; Lp->first_node_log->next=p; } else { cout<<"Sorry, your credit is running low"<<endl; } return; } Lp=Lp->next; } } void store_money() { string ID; if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp=L->next; cout << "Card No.:"; cin >> ID; while(Lp) { if (Lp->ID == ID) { float money; cout << "Deposit amount:"; cin>>money; Lp->balance+=money; Lp->num+=1; p_mode_log p=new node_log; p->W_D='W'; p->DataTime=get_time(); p->balance=money; p->next=Lp->first_node_log->next; Lp->first_node_log->next=p; return; } Lp=Lp->next; } } /*void save() { ofstream out("orderForm.txt"); if (!out) { cout << "cannot open the file\n"; return; } p_node_account Lp = L->next; while(Lp) { out<<Lp->ID<<' '<<Lp->Name<<' '<<Lp->balance<<endl; out<<Lp->num<<endl; p_mode_log p=Lp->first_node_log->next; while(p) { out<<p->DataTime<<' '<<p->W_D<<' '<<p->balance; p=p->next; if(p) out<<endl; } Lp =Lp->next; } out.close(); cout << "Write orderform Txt file succeeded! " << endl; } */ void show() { p_node_account Lp = L->next; while(Lp) { cout<<Lp->ID<<' '<<Lp->Name<<' '<<Lp->balance<<endl; p_mode_log p=Lp->first_node_log->next; cout.setf(ios::left); cout.width(32); cout<<"Trading time"; cout.width(32); cout<<"Transaction type"; cout.width(32); cout<<"Transaction amount"<<endl; for(int i=0; i<Lp->num; i++) { cout.width(32); cout<<p->DataTime; if(p->W_D=='w'||p->W_D=='W') { cout.width(32); cout<<"withdraw money"; } else { cout.width(32); cout<<"deposit"; } cout.width(32); cout<<p->balance<<endl; p=p->next; } Lp =Lp->next; } } void search1() { string ID; if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp=L->next; cout << "Card No.:"; cin >> ID; while(Lp) { if (Lp->ID == ID) { cout<<Lp->ID<<' '<<Lp->Name<<' '<<Lp->balance<<endl; break; } Lp =Lp->next; } } void search2() { string ID; if(L==NULL) { L=new node_account; L->next=NULL; } p_node_account Lp=L->next; cout << "Card No.:"; cin >> ID; while(Lp) { if (Lp->ID == ID) { cout<<Lp->ID<<' '<<Lp->Name<<' '<<Lp->balance<<endl; p_mode_log p=Lp->first_node_log->next; cout.setf(ios::left); cout.width(32); cout<<"Trading time"<<endl; cout.width(32); cout<<"Transaction type"<<endl; cout.width(32); cout<<"Transaction amount"<<endl; for(int i=0; i<Lp->num; i++) { cout.width(32); cout<<p->DataTime; if(p->W_D=='w'||p->W_D=='W') { cout.width(32); cout<<"withdraw money"; } else { cout.width(32); cout<<"deposit"; } cout.width(32); cout<<p->balance<<endl; p=p->next; } break; } Lp =Lp->next; } } /*void load() { ifstream in("orderForm.txt"); if (!in) { cout << "cannot open the file\n"; return; } while(!in.eof()) { p_node_account Lp = new node_account; Lp->first_node_log=new node_log; in>>Lp->ID>>Lp->Name>>Lp->balance; in>>Lp->num; Lp->next=L->next; L->next=Lp; for(int i=0; i<Lp->num; i++) { p_mode_log p = new node_log; in>>p->DataTime>>p->W_D>>p->balance; p->next=Lp->first_node_log->next; Lp->first_node_log->next=p; } } in.close(); } */ int main() { int choice; L = new node_account; L->first_node_log=NULL; L->next=NULL; //load(); while (choice=1) { cout << "*********************************************" << endl; cout << " 1.Registered account " << endl; cout << " 2.Cancellation of account " << endl; cout << " 3.deposit " << endl; cout << " 4.withdraw money " << endl; cout << " 5.Show all users " << endl; cout << " 6.Query account balance " << endl; cout << " 7.Query account transactions " << endl; cout << " 0.sign out " << endl; cout << " " << endl; cout << "*********************************************" << endl; cout << "Please enter your choice:"; int choice; cin >> choice; //system("cls"); switch (choice) { case 1: add(); break; case 2: Delete(); break; case 3: store_money(); break; case 4: get_money(); break; case 5: show(); break; case 6: search1(); break; case 7: search2(); break; case 0: exit(0); break; default: break; } //system("pause"); //system("cls"); //save(); } return 0; }
Appendix I experimental environment
Windows 10
Code::block 20.03
Appendix II user manual
Executable file bit "2 07 LAN tianzhe current savings account management system. cbp".
(1) When "please enter a selection:", enter the service to be selected on the keyboard.
(2) After each input, press enter to proceed to the next step.
(3) If you enter a number that does not meet the requirements (such as the account number that has been cancelled, the money withdrawn is greater than the balance, etc.), it will automatically return to the main page and reselect the service.
(4) When service 0 is selected, "Press any key to continue." will appear on the screen Indicates that the program has been exited.
reference
[1] Yan Weimin, Li Dongmei, WU Weimin, data structure (C language version), Beijing: People's Posts and Telecommunications Publishing House, 1st edition, February 2011
[2] Li Chunbao, computer experiment guidance of data structure course (5th Edition), Beijing: Tsinghua University Press, 1st edition, January 2013
[3]Stanley B.Lippman, translated by Hou Jie, Chinese version of Essential C + +, Beijing: Electronic Industry Press, 1st edition, August 2013