#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct hgd
{
char tench[30];
char duongp[30];
char phuong[30];
char tpho[30];
}ch[100],tam;
int n,i,chon;
int menu(void);
void nhap(void);
void xem(void);
void sx(void);
main()
{
for(;;)
{
chon=menu();
switch(chon)
{
case 1: nhap();
break;
case 2: xem();
break;
case 3 : sx();
break;
case 4 : exit(0);
}
}
}
int menu(void)
{
int c;
char s[10];
printf("1. nhap danh sach\n");
printf("2. xem danh sach\n");
printf("3. sap xep danh sach\n");
printf("4. thoat\n");
do
{
printf("moi ban chon 1/2/3/4 : ");gets(s);
c=atoi(s);
}while(c<1||c>4);
return c;
}
void nhap()
{
char c;
printf("nhap so chu ho :");
scanf("%d",&n);
while(getchar()!='\n');
i=1;
while(i<=n)
{
printf("\nchu ho thu %d",i);
printf("\nten chu ho :");gets(ch[i].tench);
printf("\nduong pho :");gets(ch[i].duongp);
printf("\nphuong :");gets(ch[i].phuong);
printf("\nthanh pho :");gets(ch[i].tpho);
// while(getchar()!='\n');
i=i+1;
}
}
void xem(void)
{
char c;
i=1;
printf("STT HO va TEN DUONG PHO PHUONG THANH PHO \n");
while(i<=n)
{
printf("%d",i);
printf("%20s",ch[i].tench);
printf("%20s",ch[i].duongp);
printf("%20s",ch[i].phuong);
printf("%18s",ch[i].tpho);
printf("\n");
i++;
}
getch();
}
void sx(void)
{
int j;
for(i=0;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(strcmp(ch[i].tench,ch[j].tench)>0)
{
tam=ch[i];
ch[i]=ch[j];
ch[j]=tam;
}
printf("\nda sap xep xong bam 2 de xem nhe !");
getch();
}
Tags:
lập trình c
