Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Chèn dữ liệu từ file.txt vào Danh Sách Liên Kết
07:29 07-11-2021
610 lượt xem
0 bình luận
10:43 07-11-2021
Lỗi load dữ liệu từ file txt vào danh sách liên kết.
Code
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
//DANH SACH LIEN KET
struct Data{
int id;
int pin;
string hoten;
unsigned long int sodu;
};
struct Node{
Data data;
Node* pNext;
};
struct List{
Node* pHead;
Node* pTail;
};
Node* CreateNode(Data init_data);
void CreateList(List& l);
void ADDNODE(List& l, Node* node);
void PrintListID(List l);
void PrintListTheTu(List l);
int GetSizeList(List l);
void Xoa_Dau(List &l);
void Xoa_Cuoi(List &l);
int RemoveID(List& l, int x);
bool IsEmpty(List lst);
void Xuat_Node(List l, int id);
bool GetID(List l,int id);
bool GetPIN(List l,int pin);
//KHAI BAO HAM
void Menu_chinh(List l);
void Dang_Nhap_Admin(List &l);
void Menu_Admin(List &l);
void Dang_Nhap_User(List l);
void Menu_User(List &l,int &id);
void Tao_Tai_Khoan_User(List &l);
void Nhap_Thong_Tin_User(Data &data);
void Xuat_Thong_Tin_User(Data data);
void Rut_tien(List l,int id);
void Chuyen_Tien(List &l,int id);
void Doi_PIN(List &l, int id);
void Tro_lai_Menu_Admin(List &l);
void Tro_lai_Menu_User(List &l,int id);
void PrintListFile(List l);
void InputListFile(List l);
int main(){
List List_User;
InputListFile(List_User);
CreateList(List_User);
Menu_chinh(List_User);
return 0;
}
//DINH NGHIA HAM//
void Menu_chinh(List l){
system("cls");
int option=0;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"| ATM by 0_SUN_5 |"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"[1] DANG NHAP ADMIN\n";
cout<<"[2] DANG NHAP USER\n";
cout<<"[3] DONG CHUONG TRINH\n";
cout<<"Nhap lua chon: ";
cin>>option;
switch (option) {
case 1:
Dang_Nhap_Admin(l);
break;
case 2:
Dang_Nhap_User(l);
break;
case 3:
break;
default:
system("cls");
cout<<"Lua chon khong hop le. Vui long chon lai\n";
Menu_chinh(l);
break;
}
}
void Dang_Nhap_Admin(List &l){
system("cls");
string user;
string pass;
int bien_dem=1;
cout<<"-------------------------------------------------------------"<<endl;
cout<<" DANG NHAP ADMIN "<<endl;
cout<<"============================================================="<<endl;
while (bien_dem){
cout<<"User: ";cin>>user;
cout<<"Mat Khau: ";cin>>pass;
if ((user=="admin"&&pass=="000000")||
(user=="admin1"&&pass=="000000")||
(user=="admin2"&&pass=="000000")){
Menu_Admin(l); break;
}
else{
system("cls");
cout<<"Sai User hoac Mat Khau. Vui long nhap lai!\n";
}
bien_dem++;
}
}
void Menu_Admin(List &l){
system("cls");
int option;
cout<<"MENU ADMIN"<<endl;
cout<<"-----------\n";
cout<<"[1] Xem danh sach tai khoan\n";
cout<<"[2] Them tai khoan\n";
cout<<"[3] Xoa tai khoan\n";
cout<<"[4] Mo khoa tai khoan\n";
cout<<"[5] Thoat\n";
cout<<"Nhap lua chon: ";
cin>>option;
switch (option) {
case 1:
system("cls");
PrintListTheTu(l);
Tro_lai_Menu_Admin(l);
break;
case 2:
system("cls");
Tao_Tai_Khoan_User(l);
PrintListFile(l);
Tro_lai_Menu_Admin(l);
break;
case 3:
system("cls");
PrintListTheTu(l);
cout<<"Nhap ID can xoa: ";
int x;
cin>>x;
RemoveID(l,x);
system("cls");
if (RemoveID(l,x)==1){
cout<<"Xoa thanh cong ID: "<<x<<endl;;
}else cout<<"Qua trinh xoa ID: "<<x<<" That bai!"<<endl;
Tro_lai_Menu_Admin(l);
break;
case 4:
system("cls");
cout<<"Chua hoan thien!\n";
Tro_lai_Menu_Admin(l);
break;
case 5:
Menu_chinh(l);
break;
default:
Menu_Admin(l);
break;
}
}
void Dang_Nhap_User(List l){
system("cls");
cout<<"-------------------------------------------------------------"<<endl;
cout<<" DANG NHAP USER "<<endl;
cout<<"============================================================="<<endl;
int id;
cout<<"ID: ";cin>>id;
if (GetID(l, id)){
int pin;
cout<<"PIN: ";cin>>pin;
if( GetPIN(l, pin)) Menu_User(l, id);
else{
system("cls");
int option;
cout<<"Sai Pin! \n";
cout<<"Bam [1] de Dang Nhap lai \n";
cout<<"Bam [0} de tro ve Menu Chinh\n";
cout<<"Nhap lua chon: ";
cin>>option;
switch (option) {
case 1:
Dang_Nhap_User(l);
return;
default:
Menu_chinh(l);
return;
}
}
}else{
system("cls");
int option;
cout<<"ID khong hop le! ";
cout<<"Bam [1] de Dang Nhap lai \n";
cout<<"Bam [0} de tro ve Menu Chinh\n";
cout<<"Nhap lua chon: ";
cin>>option;
switch (option) {
case 1:
Dang_Nhap_User(l);
return;
default:
Menu_chinh(l);
return;
}
}
}
void Menu_User(List &l,int &id){
system("cls");
int option;
cout<<"MENU USER"<<endl;
cout<<"-----------\n";
cout<<"[1] Xem thong tin tai khoan\n";
cout<<"[2] Rut tien\n";
cout<<"[3] Chuyen tien\n";
//cout<<"[4] Xem noi dung giao dich\n";
cout<<"[4] Doi PIN\n";
cout<<"[5] Thoat\n";
cout<<"Nhap lua chon: ";
cin>>option;
switch (option) {
case 1:
system("cls");
Xuat_Node(l, id);
Tro_lai_Menu_User(l,id);
break;
case 2:
system("cls");
Rut_tien(l,id);
Tro_lai_Menu_User(l,id);
break;
case 3:
system("cls");
Chuyen_Tien(l,id);
Tro_lai_Menu_User(l,id);
break;
case 4:
system("cls");
Doi_PIN(l, id);
Tro_lai_Menu_User(l,id);
break;
case 5:
Menu_chinh(l);
break;
default:
Menu_chinh(l);
break;
}
}
void Tao_Tai_Khoan_User(List &l){
Data data;
Nhap_Thong_Tin_User(data);
system("cls");
Node *Node_user;
Node_user=CreateNode(data);
ADDNODE(l, Node_user);
cout<<"Tao thanh cong tai khoan: "<<data.id<<endl;
}
void Nhap_Thong_Tin_User(Data &data){
cout<<"Dien thong tin tai khoan! \n";
cout<<"ID: "; cin>>data.id;
cout<<"PIN: "; cin>>data.pin;
cout<<"Ho Ten ( khong chua khoang trang): "; cin>>data.hoten;
cout<<"So du tai khoan: "; cin>>data.sodu;
}
void Xuat_Thong_Tin_User(Data data){
cout<<"ID: "<<data.id<<endl;
cout<<"PIN: "<<data.pin<<endl;
cout<<"Ho Ten: "<<data.hoten<<endl;
cout<<"So du: "<<data.sodu<<endl;
}
void Rut_tien(List l,int id){
int tien_rut=0;
cout<<"Nhap so tien can rut: ";
while(cin>>tien_rut){
if(tien_rut>=50&&tien_rut%50==0){
cout<<"Nhan [1] de rut: "<<tien_rut<<endl;
cout<<"Nhan [0] de huy!\n";
cout<<"Nhap lua chon: ";
int option; cin>>option;
if (option==1){
for(Node*k=l.pHead;k!=NULL;k=k->pNext){
if (k->data.id==id) {
k->data.sodu=k->data.sodu-tien_rut;
cout<<"Da rut :"<< tien_rut<<endl;
// for (int i=0;i<10;i++){
// if (k->data.lich_su[i].giaodich!="0"){
// k->data.lich_su[i].giaodich="Rut Tien";
// k->data.lich_su[i].tien=tien_rut;
// }
//}
break;
}
}
}else cout<<"Da huy!\n"; break;
}else cout<<"So tien da nhap khong hop le!\nSo tien phai lon hon 50 va la boi so cua 50\nVui long nhap lai: ";
}
}
void Chuyen_Tien(List &l,int id){
int id_nhan;
cout<<"Nhap ID nhan tien: ";
cin>>id_nhan;
Node *k=l.pHead;
while(k!=NULL){
if (k->data.id==id_nhan){
int tien_chuyen;
cout<<"Nhap so tien can chuyen : ";
while(cin>>tien_chuyen){
if(tien_chuyen>=50){
cout<<"Nhan [1] de chuyen: "<<tien_chuyen<<endl;
cout<<"Nhan [0] de huy!\n";
cout<<"Nhap lua chon: ";
int option; cin>>option;
if (option==1){
k->data.sodu=k->data.sodu+tien_chuyen;
for(Node*h=l.pHead;h!=NULL;h=h->pNext){
if (h->data.id==id) h->data.sodu=h->data.sodu-tien_chuyen;
}
cout<<"Da chuyen: "<< tien_chuyen<<endl; return;
}
else{
cout<<"Da huy!\n"; return;
}
}else cout<<"So tien da nhap khong hop le! So tien phai lon hon 50\nVui long nhap lai: ";
}
}
k=k->pNext;
}
cout<<"ID Nguoi nhan khong ton tai!\n"; return;
}
void Tro_lai_Menu_Admin(List &l){
int option ;
cout<<"BAM [0] de tro lai MENU ADMIN: ";
while (cin>>option) {
if (option==0) {
Menu_Admin(l);
break;
}else{
cout<<"Khong hop le! vui long nhap lai: ";
}
}
}
void Tro_lai_Menu_User(List &l,int id){
int option ;
cout<<"BAM [0] de tro lai MENU USER: ";
while (cin>>option) {
if (option==0) {
Menu_User(l,id);
break;
}else{
cout<<"Khong hop le! vui long nhap lai: ";
}
}
}
void Doi_PIN(List &l, int id){
int pin_moi;
cout<<"Nhap PIN can doi: ";cin>>pin_moi;
Node*h=l.pHead;
while(h!=NULL){
if (h->data.id==id) {
h->data.pin=pin_moi;
cout<<"Doi PIN Thanh Cong! \n";break;
}
h=h->pNext;
}
}
bool GetID(List l,int id){
Node*node=l.pHead;
while(node!=NULL){
if (id==node->data.id){
return true;
}
node=node->pNext;
}return false;
}
bool GetPIN(List l,int pin){
Node*node=l.pHead;
while(node!=NULL){
if (pin==node->data.pin){
return true;
}
node=node->pNext;
}return false;
}
Node* CreateNode(Data init_data){
Node* node = new Node;
if (node==NULL) return NULL;
node->data.id = init_data.id;
node->data.pin= init_data.pin;
node->data.hoten=init_data.hoten;
node->data.sodu=init_data.sodu;
//for (int i=0;i<100;i++){
//node->data.lich_su[i].giaodich="0";
//}
node->pNext = NULL; // node vừa tạo chưa thêm vào danh sách nên chưa liên kết với phần tử nào cả nên phần liên kết gán bằng NULL
return node;
}
void CreateList(List& l){
l.pHead = NULL;
l.pTail = NULL;
}
bool IsEmpty(List lst){
if (lst.pHead==NULL){
return true;
}
return false;
}
void ADDNODE(List &l, Node* node){
if (l.pHead == NULL)
{
l.pHead = node;
l.pTail = node;
}
else
{
l.pTail->pNext = node;
l.pTail = node;
}
}
void Xuat_Node(List l, int id){
Node*k = l.pHead;
while (k!=NULL){
if (k->data.id==id){
Xuat_Thong_Tin_User(k->data);
return;
}
k=k->pNext;
}
cout<<"Xuat Thong Tin Khong thanh cong! \n";
}
void PrintListID(List l){
if (!IsEmpty(l)){
Node* node = l.pHead;
while (node != NULL)
{
Xuat_Thong_Tin_User(node->data);
cout<<endl;
node = node->pNext; // Chuyển sang node tiếp theo
}
cout<<"So luong ID: "<<GetSizeList(l)<<endl;
}else cout<<"Danh sach trong!\n";
}
void PrintListTheTu(List l){
if (!IsEmpty(l))
{
Node* node = l.pHead;
while (node != NULL)
{
cout<<"ID: "<<node->data.id<<endl;
cout<<"PIN: "<<node->data.pin<<endl;
cout<<endl;
node = node->pNext; // Chuyển sang node tiếp theo
}
cout<<"So luong Tai Khoan: "<<GetSizeList(l)<<endl;
}else cout<<"Danh sach The trong!\n";
}
int GetSizeList(List l){
if (IsEmpty(l)){
return 0;
}
else{
Node* node=l.pHead;
int nCount=0;
while (node!=NULL) {
nCount++;
node=node->pNext;
}
return nCount;
}
}
void Xoa_Dau(List &l){
Node *node=l.pHead;
l.pHead=l.pHead->pNext;
delete node;
}
void Xoa_Cuoi(List &l){
Node *p = nullptr;
for (Node *k=l.pHead;k!=NULL;k=k->pNext){
if (k==l.pTail){
l.pTail=p;
l.pTail->pNext=NULL;
delete k;
return;
}p=k;
}
}
void XoaSauMotNode(List &l, Node*q){
Node*p;
for( Node *k=l.pHead;k!=NULL;k=k->pNext){
if (k->data.id==q->data.id){
p=k->pNext;
k->pNext=p->pNext;
delete p;
return;
}
}
}
int RemoveID(List& l, int x){
if (l.pHead->data.id==x){
Xoa_Dau(l);return 1;
}
if (l.pTail->data.id==x){
Xoa_Cuoi(l);return 1;
}
Node *q = nullptr;
for (Node *k=l.pHead;k!=NULL;k=k->pNext){
if (k->data.id==x){
XoaSauMotNode(l, q);
}q=k;
return 1;
}
return 0;
}
//File .txt
void PrintListFile(List l){
ofstream ID("/Users/nguyenminhtam/Desktop/Code/ATM/File Txt/[ID].txt");
if (l.pHead != NULL)
{
Node* node = l.pHead;
while (node != NULL)
{
ID<<node->data.id<<endl;
ID<<node->data.pin<<endl;
ID<<node->data.hoten<<endl;
ID<<node->data.sodu<<endl;
ID<<endl;
node = node->pNext; // Chuyển sang node tiếp theo
}
}
ID.close();
ofstream TheTu("/Users/nguyenminhtam/Desktop/Code/ATM/File Txt/TheTu.txt");
if (l.pHead != NULL)
{
Node* node = l.pHead;
while (node != NULL)
{
TheTu<<"ID: "<<node->data.id<<endl;
TheTu<<"PIN: "<<node->data.pin<<endl;
TheTu<<endl;
node = node->pNext; // Chuyển sang node tiếp theo
}
}
TheTu.close();
}
void InputListFile(List l){
ifstream file("/Users/nguyenminhtam/Desktop/Code/ATM/File Txt/[ID].txt");
if(file.fail()){
cout<<"Inout List to File Failed\n";
return;
}
while(!file.eof()){
Data data;
file>>data.id;
file>>data.pin;
file>>data.hoten;
file>>data.sodu;
Node *Node_user;
Node_user=CreateNode(data);
ADDNODE(l, Node_user);
}
file.close();
return;
}
Nội dung file txt
ID: 11111111
PIN: 0
Ho Ten: Nguyen_Minh_Tam
So du: 99999
ID: 2222222
PIN: 0
Ho Ten: Loc
So du: 999999
Ảnh lỗi
Em bị thông báo lỗi như này ạ mong anh chị giúp đỡ