các pác nào càn code thì cứ liên hệ nhé
đây là một số code cơ bản làm menu :
code
Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct cata
{char ten[20];
int ngs;
int ths;
int ns;
float lg;
int sl;
};
void swap(cata &a,cata &b)
{cata tam;
tam=a;a=b;b=tam;
};
class aa
{cata ng[30];
public:
aa(cata *);
void menu();
void load();
void save();
void input();
void list();
void append();
void edit();
void search();
void display(int);
void sort();
};
aa::aa(cata *p)
{for(int i=0;i<30;i++)
ng[i]=p[i];
};
void aa::menu()
{int choice;
char tenf;
do{cout<<"\t\t\t ---MENU---\n";
cout<<"\t\t\t| 1.input |\n";
cout<<"\t\t\t| 2.save |\n";
cout<<"\t\t\t| 3.list |\n";
cout<<"\t\t\t| 4.append |\n";
cout<<"\t\t\t| 5.edit |\n";
cout<<"\t\t\t| 6.search |\n";
cout<<"\t\t\t| 7.sort |\n";
cout<<"\t\t\t| 8.quit |\n";
cout<<"\t\t\t ----------\n";
do{cout<<"\n Vao cac lua chon:";
cin>>choice;
if(choice<1 || choice>8) cout<<"\n Khong hop le_hay vao lai\n";
}while(choice<1 || choice>8);
switch(choice)
{case 1:{clrscr();input();};break;
case 2:{clrscr();save();};break;
case 3:{clrscr();list();};break;
case 4:{clrscr();append();};break;
case 5:{clrscr();edit();};break;
case 6:{clrscr();search();};break;
case 7:{clrscr();sort();};break;
};
}while(choice!=8);
};
void aa::load()
{FILE *fp;
if((fp=fopen("khkt","r"))==NULL)
{cout<<"\n Khong ton tai:\n";
exit(1);
};
fread(&ng[0].sl,sizeof(ng[0].sl),1,fp);
fread(ng,sizeof ng,1,fp);
fclose(fp);
};
void aa::save()
{FILE *fp;
if((fp=fopen("khkt","w"))==NULL)
{cout<<"\n Khong ton tai file nay!\n";
exit(1);
};
fwrite(&ng[0].sl,sizeof ng[0].sl,1,fp); // CHU Y N PHAI DUOC KHAI BAO LA BIEN;
fwrite(ng,sizeof ng,1,fp);
fclose(fp);
};
void aa::input()
{int i=0;
char hoi;
do{cout<<"\n Co nhap khong (y,n?)";
cin>>hoi;
if(hoi=='y' || hoi=='Y')
{cout<<"\n Vao ten:";cin>>ng[i].ten;
cout<<"\n Vao luong:";cin>>ng[i].lg;
cout<<"\n Vao nam:";cin>>ng[i].ns;
cout<<"\n Vao thang:";
do
{cin>>ng[i].ths;
if(ng[i].ths<0 || ng[i].ths>12) cout<<"\n Khong thoa man-Hay vao lai\n";
}while(ng[i].ths<0 || ng[i].ths>12);
cout<<"\n Vao ngay:";
if(ng[i].ths==1 || ng[i].ths==3 || ng[i].ths==5 || ng[i].ths==7 || ng[i].ths==8 || ng[i].ths==10)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>31) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>31);
if(ng[i].ths==4 || ng[i].ths==6 || ng[i].ths==9 || ng[i].ths==11 || ng[i].ths==12)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>30) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>30);
if(ng[i].ths==2)
{if(ng[i].ns%4==0)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>29) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>29);
if(ng[i].ns%4!=0)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>28) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>28);
};
i++;
};
}while(hoi!='n' && hoi!='N');
for(int j=0;j<i+1;j++)
ng[j].sl=i;
};
void aa::append()
{load();
int i=ng[0].sl;
char hoi;
do{cout<<"\n Co them khong (y,n?)";
cin>>hoi;
if(hoi=='y' || hoi=='Y')
{cout<<"\n Vao ten:";cin>>ng[i].ten;
cout<<"\n Vao luong:";cin>>ng[i].lg;
cout<<"\n Vao nam:";cin>>ng[i].ns;
cout<<"\n Vao thang:";
do
{cin>>ng[i].ths;
if(ng[i].ths<0 || ng[i].ths>12) cout<<"\n Khong thoa man-Hay vao lai\n";
}while(ng[i].ths<0 || ng[i].ths>12);
cout<<"\n Vao ngay:";
if(ng[i].ths==1 || ng[i].ths==3 || ng[i].ths==5 || ng[i].ths==7 || ng[i].ths==8 || ng[i].ths==10)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>31) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>31);
if(ng[i].ths==4 || ng[i].ths==6 || ng[i].ths==9 || ng[i].ths==11 || ng[i].ths==12)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>30) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>30);
if(ng[i].ths==2)
{if(ng[i].ns%4==0)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>29) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>29);
if(ng[i].ns%4!=0)
do{cin>>ng[i].ngs;
if(ng[i].ngs<1 || ng[i].ngs>28) cout<<"\n Khong thoa man-Hay vao lai ngay\n";
}while(ng[i].ngs<1 || ng[i].ngs>28);
};
i++;
};
}while(hoi!='n' && hoi!='N');
for(int j=0;j<i+1;j++)
ng[j].sl=i;
};
void aa::edit()
{load();
int k;
char ten1[20];
cout<<"\n Vao ban ghi can sua chua:";cin>>k;
cout<<"\n Vao ten moi:";cin>>ten1;
for(int i=0;i<20;i++)
{ng[k-1].ten[i]=ten1[i];
save();
}
};
void aa::search()
{char ten1[20];
int found;
cout<<"\n Vao ten can tim:";
cin>>ten1;
load();
for(int i=0;i<ng[0].sl;i++)
if(!strcmp(ten1,ng[i].ten))
{display(i);
found=1;
};
if(found!=1) cout<<"\n Khong thay!";
};
void aa::sort()
{load();
clrscr();
for(int i=0;i<ng[0].sl-1;i++)
for(int j=i+1;j<ng[i].sl;j++)
if(strcmp(ng[i].ten,ng[j].ten)>0)
swap(ng[i],ng[j]);save();
};
void aa::list()
{load();
for(int i=0;i<ng[0].sl;i++)
cout<<"\t"<<i+1<<"."<<ng[i].ten<<"\t"<<ng[i].ngs<<"/"<<ng[i].ths<<"/"<<ng[i].ns<<"\t"<<ng[i].lg<<"\n";
};
void aa::display(int i)
{load();
cout<<"\t"<<i<<"."<<ng[i].ten<<"\t"<<ng[i].ngs<<"/"<<ng[i].ths<<"/"<<ng[i].ns<<"\t"<<ng[i].lg<<"\n";
};
main()
{clrscr();
cata *p;
aa m(p);
m.menu();
getch();
return 0;
};
Vui lòng đọc Nội quy trước khi gửi bài viết. Yêu cầu để code trong tag code