Untitled
unknown
plain_text
a year ago
3.0 kB
6
Indexable
#include <bits/stdc++.h>
using namespace std;
struct HangHoa{
string maHang;
string tenHang;
string donViTinh;
int donGia;
int soLuong;
};
struct Node{
HangHoa infor;
Node *next;
};
typedef Node *tro;
void nhapHH (HangHoa &hh){
cout << "Nhap ma\n";
getline(cin,hh.maHang);
cout << "Nhap ten hang\n";
getline(cin,hh.tenHang);
cout << "Nhap don vi tinh\n";
getline(cin,hh.donViTinh);
cout << "Nhap don gia\n";
cin >> hh.donGia;
cout << "Nhap so luong\n";
cin >> hh.soLuong;
cin.ignore();
}
void input_List(tro &L){
tro P,Q;
HangHoa x; int d = 0;
L = NULL;
for(int i = 0; i < 3;i++){
nhapHH(x);
P = new Node;
P->infor = x;
P->next = NULL;
if(L==NULL){
L = P;
}
else{
Q->next = P;
}
Q = P;
d++;
}
}
void hienThiDS(tro L){
if(L == NULL){
cout << "Danh sach rong\n";
return;
}
cout << setw(5) << "STT";
cout << setw(15) << "Ma Hang";
cout << setw(30) << "Ten Hang";
cout << setw(10) << "Don Vi Tinh" ;
cout << setw(10) << "Don Gia" ;
cout << setw(10) << "So Luong";
cout << setw(10) << "Thanh Tien" << endl;
tro Q = L;
int i = 0;
while(Q != NULL){
HangHoa x = Q->infor;
cout << setw(5) << i+1;
cout << setw(15) << x.maHang;
cout << setw(30) << x.tenHang;
cout << setw(10) << x.donViTinh;
cout << setw(10) << x.donGia;
cout << setw(10) << x.soLuong;
cout << setw(10) << x.donGia*x.soLuong << endl;
Q = Q->next;
++i;
}
}
int length(tro L){
int n = 0;
tro Q = L;
while(Q!= NULL){
Q = Q->next;
++n;
}
return n;
}
void firstAdd(tro &L, HangHoa x){
tro P;
P = new Node;
P->infor = x;
P->next = L;
L = P;
}
void removeIndex(tro &L, int k){
tro Q = L;
int d = 1;
while(d < k-1){
Q = Q->next;
++d;
}
tro M =Q->next;
Q->next = M->next;
delete(M);
}
tro timMax(tro L){
tro P = L;
tro Q = L->next;
while(Q!= NULL){
if(Q->infor.soLuong >= P->infor.soLuong){
P = Q;
}
Q = Q->next;
}
return P;
}
int main(){
tro L;
cout << "Nhap danh sach hang hoa\n";
input_List(L);
cout << "Danh sach vua nhap\n";
hienThiDS(L);
cout << "Chieu dai cua danh sach " << length(L) << endl;
HangHoa them;
cout << "Hang hoa can them\n";
nhapHH(them);
firstAdd(L,them);
cout << "Hien thi danh sach\n";
hienThiDS(L);
removeIndex(L,3);
cout << "Hien thi danh sach\n";
hienThiDS(L);
cout << "Max: " << timMax(L)->infor.soLuong << endl;
}
/*
h2003
hop but
chiec
35000
15
h2002
but chi
cai
12000
50
h2001
vo
quyen
6500
20
h2004
tay
cai
10000
50
*/
Editor is loading...
Leave a Comment