Code:
#include<stdio.h>
#include"dos.h"
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<iostream.h>
#include<fstream.h>
struct TYPEIC
{ char TenIC[15];
long gia;
int Sl;
};
struct MUAIC
{ char TenIC[15];
long gia,tong;
int sl;
};
class dsic
{
protected:
TYPEIC ic[300];
int n,ictim;
public:
void nhap(TYPEIC ic[]);
void inds(TYPEIC ic[]);
void timic(TYPEIC ic[]);
void muaic(TYPEIC ic[]);
void suatt(TYPEIC ic[]);
void ghi(TYPEIC ic[]);
void taothem(TYPEIC ic[]);
void in(TYPEIC ic[]);
};
void dsic::nhap(TYPEIC ic[])
{ int i;
TYPEIC tam;
FILE *f;
f=fopen ("c:\\ds.txt","r");
if (f==NULL)
cout<<"file ko ton tai,de nghi sua lai ten file la ds.txt, va dat trong o c";
else
{
while(fread(&tam,sizeof(TYPEIC),1,f)>0)
{ i++;
ic[i]=tam;
}
n=i;}
fclose(f);
}
void dsic::inds(TYPEIC ic[])
{ int i;
cout<< " Dang sach IC trong quan";
cout<< "STT"<<" "<<" Ten IC "<<" "<<" Gia "<<" "<<"So Luong";
for (i=1;i<=n;i++)
cout<<" "<<i<<" "<<ic[i].TenIC<<" "<<ic[i].gia<<" "<<ic[i].Sl;
getch();
};
void dsic::timic(TYPEIC ic[])
{
char a[15],b[15];
int i,k,y;
ictim=0;
cout<<"Ten IC can tim : ";
gets(a);
for(i=1;i<=n;i++)
{
strcpy (b,ic[i].TenIC);
k=strcmpi (a,b);
if (k==0) break;
};
k=i;
ictim=i;
if (k>n) cout<< "Khong co hang";
else
{ cout<< "IC can tim";
y++;
gotoxy(1,y); cout<<"STT";
gotoxy(5,y); cout<<"IC";
gotoxy(21,y); cout<<"Gia";
gotoxy(28,y);cout<<"SL";
y++;
y++;
gotoxy(1,y); cout<<" "<<k;
gotoxy(5,y); cout<<ic[k].TenIC;
gotoxy(21,y); cout<<ic[k].gia;
gotoxy(28,y);cout<<ic[k].Sl;
}
getch();
};
void dsic::muaic(TYPEIC ic[])
{
char /*a[15]*/flag='y';
int i=1,m,k,slmua;
long tt=0;
dsic tam;
MUAIC icmua[100];
while (flag=='y')
{
while (ictim ==0)
{
tam.timic(ic);
k=ictim;
cout<< "Dung con ban muon mua chu (1:co;0:ko)";
cin>>ictim;
}
cout<<" Ban muon mua bao nhieu ";
cin>>slmua;
strcpy(icmua[i].TenIC,ic[k].TenIC);
icmua[i].sl=slmua;
icmua[i].gia=ic[i].gia;
icmua[i].tong=icmua[i].gia*icmua[i].sl;
m=i;i++;
cout<<"ban co muon mua nua ko?";
cin>>flag;
}
for (i=1;i<=m;i++)
tt = tt + icmua[i].tong;
int y=0;
cout<< "Danh Sach IC da mua";
for (i=1;i<=m;i++)
{ y++;
gotoxy(1,y); cout<<"STT";
gotoxy(5,y); cout<<"IC";
gotoxy(21,y); cout<<"Gia";
gotoxy(28,y);cout<<"SL";
gotoxy(35,y);cout<<"Tong";
y++;
y++;
gotoxy(1,y); cout<<" "<<k;
gotoxy(5,y); cout<<icmua[k].TenIC;
gotoxy(21,y); cout<<icmua[k].gia;
gotoxy(28,y);cout<<icmua[k].sl;
gotoxy(35,y);cout<<icmua[k].tong;
}
cout<<" Tong gia tri hoa don "<<tt;
getch();
};
void dsic::suatt(TYPEIC ic[])
{
char a[15],b[15],flag1='y',flag2='y';
int i,k,t;
while (flag1=='y')
{
cout<<"IC can thay doi ";
gets(a);
for (i=0;i<=n;i++)
{
strcpy (b,ic[i].TenIC);
k=strcmpi (a,b);
if (k==0) break;
}
k=i;
if (k>n) cout<< "Khong co hang";
else
{
int y=0;
cout<< "IC can tim";
y++;
gotoxy(1,y); cout<<"STT";
gotoxy(5,y); cout<<"IC";
gotoxy(21,y); cout<<"Gia";
gotoxy(28,y);cout<<"SL";
y++;
y++;
gotoxy(1,y); cout<<" "<<k;
gotoxy(5,y); cout<<ic[k].TenIC;
gotoxy(21,y); cout<<ic[k].gia;
gotoxy(28,y);cout<<ic[k].Sl;
}
while (flag2=='y')
{
cout<<"Ban can sua thong tin nao ";
cout<<"Nhan 1 : sua Ten";
cout<<"Nhan 2 : sua Don Gia";
cout<<"Nhan 3 : sua So Luong";
cin>>t;
if (t==1)
{
cout<<"Ten moi ";
gets(ic[k].TenIC);
}
else if (t==2)
{
cout<<"Gia moi ";
cin>>ic[k].gia;
}
else if (t==3)
{
cout<<"So luong ";
cin>>ic[k].Sl;
}
cout<<"Ban co muon sua them thong tin trong IC nay ko ? y/n";
cin>>flag2;
getch();
}
cout<<"Ban muon sua thong tin cua IC nao ko? y/n";
cin>>flag1;
getch();
}
getch();
}
void dsic::taothem(TYPEIC ic[])
{ int k,i;
cout<<"ban muon tao them bao nhieu ";
cin>>k;
for (i=n;i<=(n+k);i++)
{ cout<<" Ten IC ";
gets(ic[i].TenIC);
cout<<" Don gia ";
cin>>ic[i].gia;
cout<<"So luong ";
cin>>ic[i].Sl;
}
}
void dsic::ghi(TYPEIC ic[])
{
int i;
FILE *f = fopen("c:\\ds.txt","ab");
for (i=1;i<=n;i++) fwrite(&ic[i],sizeof(TYPEIC),1,f);
fclose(f);
};
void main()
{
dsic ic1;
TYPEIC ic[100];
char ten[30],fl;
int lc;
clrcrs();
ic1.nhap(ic);
cout<< "\n Chao Mung Da Den Voi Cua Hang Bo Duc";
cout<< "\n Ban Ten gi ";
gets(ten);
while (fl!='y')
{
cout<< "\nBan muon lam gi";
cout<< "\n1: Tim IC";
cout<< "\n2: Mua IC";
cout<< "\n3: Sua Thong Tin";
cout<< "\n4: Tao Them IC";
cout<< "\n5: In danh sach IC";
cout<< "\n6: Thoat";
cin>>lc;
if (lc == 1) ic1.timic(ic);
else if (lc == 2) ic1.muaic( ic);
else if (lc == 3) ic1.suatt( ic);
else if (lc == 4) ic1.taothem(ic);
else if (lc == 5) ic1.inds(ic);
else { cout << "Ban that su muon thoat y/n";
cin>>fl;}
}
getch();
}