Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by sanh ( 6 years ago )
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
 
struct mavung
{
	char tinh[15];
	char id[5];
	struct mavung *next;
};typedef mavung MAVUNG;
MAVUNG *first;
 
struct data
{
	char tentinh[15];
	char tenDV[20];
	char diachi[20];
	char sdt[15];
};typedef data DATA;
 
struct danhba
{	
	char tentinh[15];
	char tenDV[20];
	char diachi[20];
	char sdt[15];
	struct danhba *next;
};
typedef danhba DANHBA;
DANHBA *head;

void add(DATA &a)
{
	DANHBA *p,*right;
	p = (DANHBA*)malloc(sizeof(DANHBA)); if(p==NULL){printf("ERROR!!!");exit(1);}
	
	strcpy(p->tentinh,a.tentinh); strcpy(p->tenDV,a.tenDV); strcpy(p->diachi,a.diachi);strcpy(p->sdt,a.sdt);
	
	p->next = NULL;//khoi tao danh sach
	
	right = head;
	if (right==NULL)
    {
        head=p;
    }
    else
    {
        while(right->next != NULL)//
        	right=right->next;
       	
		right->next = p;
    }
}

void show(DANHBA *sh)
{
	sh = head;
	int count = 0;
	
	if( sh==NULL )
	{
		return;
	}
	printf("\n\t\t\t\t\t Tinh\t\tDon Vi\t   Dia Chi      SDT\n");// \t = 8 space
	printf("\t\t\t\t   ______________________________________________________");
	while (sh!=NULL)
	{
		count ++;
		printf("\n\t\t\t\t %d | %10s | %15s | %5s | %10s |",count,sh->tentinh,sh->tenDV,sh->diachi,sh->sdt);
		printf("\n\t\t\t\t   ------------------------------------------------------");
		sh = sh->next;
	}
	printf("\n");
}

 void appendFile()
{
	char line[68];
	char s[] = " ";
	DATA d;
	FILE *f = fopen("E:\\gg.txt","r"); if (f==NULL) {printf("FAIL OPENING FILE!");}
	
	while (fgets(line,sizeof(line),f)!=NULL)
	{
		if( sscanf( line, "%s %s %s %s", d.tentinh, d.tenDV, d.diachi, d.sdt) != 4)//scan tung dong trong file sau do tach tung phan tu
		{
		    fprintf( stderr, "failed to extract tentinh, tenDV, diachi, sdt from input line\n" );  
		    fclose( f );
		    exit( EXIT_FAILURE );
		}
		else 
		{
			add(d);//them data tu d vao linked list DANHBA
		}
	}
	fclose(f);
	show(head);
}
void del1(DANHBA *p, DANHBA *q)
{
	if(p==q) q=p->next; else q->next = p->next; 
	if(p->next==NULL) q->next=NULL;
	free(p);
}
void duplicate()
{
	char ch;
	int found = 0,count1 = 0,count2 = 1,d;
	DANHBA *p1,*p2,*dup,*q,*q2;
	p1 = head;
	q=p1;
	
	while (p1!= NULL) 
{	
	p2=p1->next; q2=p1;
	while(p2!= NULL)
	{
		if(strcmp(p1->sdt,p2->sdt)==0)
		{   found=1;
			printf("\n Trong tinh [%s] co so dien thoai cua [%s] trung voi [%s]",p1->tentinh,p1->tenDV,p2->tenDV);
			printf("\n nhan 1 de xoa so cua [%s], 2 de xoa so cua [%s], N de bo qua\n",p1->tenDV,p2->tenDV);
			scanf("%c",&ch);
			if(ch=='1') del1(p1,q);
			if(ch=='2') {q2->next=p2->next;	free(p2);	}
			return;
		}	
	
		 q2=p2;
		 p2=p2->next;
	
	}	
	q=p1;
	p1=p1->next;
}
if (found==0) printf("Khong co sdt trung lap");
}
void swap(DANHBA *p1, DANHBA *p2)
{
	DANHBA temp;
	temp = *p1;
	*p1 = *p2;
	*p2 = temp;
	p2->next = p1->next;
	p1->next = temp.next;
}

void substring(char s[], char sub[], int p, int l) 
{
   int c = 0;
   
   while (c < l) {
      sub[c] = s[p+c-1];
      c++;
   }
   sub[c] = '\0';
}

void appendTinh()
{
	char line[21];
	char s[] = " ";
	MAVUNG d,*q,*right;
	FILE *f = fopen("E:\\tinh.txt","r"); if (f==NULL) {printf("FAIL OPENING FILE!");}
	while (fgets(line,sizeof(line),f)!=NULL)
	{
		if( sscanf( line,"%s %s",d.tinh,d.id) != 2)
		{
		    fprintf( stderr, "failed to extract tinh, sdt from input line\n" );  
		    fclose( f );
		    exit( EXIT_FAILURE );
		}
		else 
		{
			q = (MAVUNG*)malloc(sizeof(MAVUNG)); if(q==NULL){printf("ERROR!!!");exit(1);}
			strcpy(q->tinh,d.tinh);strcpy(q->id,d.id);
			
			q->next = NULL;
			
			right = first;
			if (right==NULL)
		    {
		        first=q;
		    }
		    else
		    {
		        while(right->next != NULL)//
		        	right=right->next;
		       	
				right->next = q;
		    }	
		}
	}
	fclose(f);
}

void sortDB()
{
	DANHBA *p1,*p2;
	p1 = head;
	while(p1!=NULL)
	{
		if (p1->next != NULL) p2 = p1->next;
		while (p2!=NULL)
		{
			if (strcmp(p1->tentinh,p2->tentinh)>0) swap(p1,p2);
			p2 = p2->next;
		}
		p1 = p1->next;
	}
}
 
void showtinh(DANHBA *sh)
{
	DANHBA *temp1;
	int s,found = 0;
	char tt[12],c;
	if (sh == NULL) printf("\n\t\t\tDANH BA RONG!!!");
	printf("Cac tinh hien co trong danh ba: \n\n");
	sortDB();//sap xep truoc khi in ten tinh
	
	sh = head;
	temp1 = sh;
	printf("\tTen tinh: %s",temp1->tentinh);
	while (sh != NULL)//tim cac tinh hien co trong danh ba va in ra
	{
		if (strcmp(sh->tentinh,temp1->tentinh)!=0)//temp1 dung de check cac tinh khac nhau roi in ra neu trung thi khong
		{
			printf("\n\tTen tinh: %s",sh->tentinh);
			temp1 = sh;//tiep tuc tim kiem (neu khac)
		}
		else
		{
			sh = sh->next;
		}
 
	}
	
	do
	{
		printf("\n\nLiet ke danh ba tung tinh: Nhan 1 --- Liet ke danh ba tat ca cac tinh: Nhan 2\nChon: ");scanf("%d",&s);
		
		if (s==1)
		{
			do
			{
				fflush(stdin);printf("\nChon ten tinh can liet ke: vidu: QuangNam\nChon: ");gets(tt);sh = head;
				printf("\n\t\t\t\t\t [%s]\n",tt);
				while (sh != NULL)
				{
					if (strcmp(sh->tentinh,tt) == 0)//check ten tinh trong file mavung
					{
						found = 1;
						printf("\n\t\t\t| %15s | %5s | %10s |",sh->tenDV,sh->diachi,sh->sdt);
						sh = sh->next;
					}
					else 
					{
						sh = sh->next;
						if(sh==NULL && found == 0)
						{
							printf("\nTen tinh ban vua nhap khong co trong danh ba hoac khong phai ten tinh cua VietNam.");
							break;
						}						
					}
 
				}
				fflush(stdin);printf("\n\nBan co muon tim tinh khac? Y(Yes)--N(No)  ");scanf("%c",&c);
			} while(c=='Y'|| c=='y');
			if (c=='n'||c=='N')
			{
				break;
			}
			else break;		
		}
		if (s==2)
		{
					sortDB();
					sh = head;
					temp1 = sh;
					char mv[4];//temp de luu ma vung
					substring(sh->sdt,mv,1,4);//ham tach mavung ra khoi sdt de in ra man hinh
					printf("\n\n\t\t(%s)\t[%s]",mv,temp1->tentinh);
					printf("\n\t\t\t------------------------------------------");//__________________________________________
					while (sh != NULL)
					{
						if (strcmp(sh->tentinh,temp1->tentinh)==0)
						{
							printf("\n\t\t\t| %15s | %5s | %10s |",sh->tenDV,sh->diachi,sh->sdt);
							printf("\n\t\t\t------------------------------------------");
							sh = sh->next;
						}
						else
						{
							temp1 = sh;
							substring(temp1->sdt,mv,1,4);
							printf("\n\n\t\t(%s)\t[%s]",mv,temp1->tentinh);
							printf("\n\t\t\t------------------------------------------");
							printf("\n\t\t\t| %15s | %5s | %10s |",sh->tenDV,sh->diachi,sh->sdt);
							printf("\n\t\t\t------------------------------------------");
							sh = sh->next;
						}
					}
			break;
		}
	} while (s==1 || s==2);
}
 
void append()
{
	MAVUNG *m = (MAVUNG*)malloc(sizeof(MAVUNG));
	DANHBA *p = (DANHBA*)malloc(sizeof(DANHBA));
	char tempsdt[7];
	int i,found = 0;
	fflush(stdin);
	
	do
	{
		printf("\nNhap ten tinh. Vidu: DaNang.\nNhap: ");gets(p->tentinh);
		m = first;
		while (m!=NULL)//lap m kieu MAVUNG
		{
			if (strcmp(m->tinh,p->tentinh) == 0)//check ten vua nhap va ten tinh trong file mavung
			{
				found = 1;//cho found = 1 neu ten tinh hop le
			}
			m = m->next;
		}
		if (found != 1)
		{
			fflush(stdin);
			printf("\nSai ten tinh hoac khong dung voi chuan ten.");
			printf("\nMoi ban nhap lai ten tinh. Vidu: DaNang.\nNhap: ");gets(p->tentinh);
		}
	} while (found != 1);
	
	printf("\nNhap ten don vi: ");gets(p->tenDV);
	printf("\nNhap dia chi: ");gets(p->diachi);
	do
	{
		fflush(stdin);printf("\nNhap so dien thoai thuoc tinh %s (gom 7 so): ",p->tentinh);gets(tempsdt);//nhap sdt (7 so) khong bao gom ma vung sau do luu vao tempsdt
		i = strlen(tempsdt);
	}while (i!=7);
	
	m = first;
	while (m!=NULL)
	{
		if (strcmp(m->tinh,p->tentinh) == 0)//tim ten tinh trong file mavung
		{
			strcpy(p->sdt,m->id);//lay id cua tinh tu file mavung
		}
		m = m->next;
	}
	
	strcat(p->sdt,tempsdt);//sau khi co mavung ta tiep tuc noi sdt vua nhap vao
	
	if (head == NULL)
	{
		head = p;
	}
	else
	{
		p->next = head->next;
		head = p;
	}
	sortDB();
	
}
 
void search(void)
{
	duplicate();
	int ch;
	char sdt[11],tdv[20],c1,c2;
	printf("\n-----------Ban vua chon chuc nang tim kiem thong tin-----------");
	printf("\n---Nhan 1 de tim theo so dien thoai, 2 de tim theo ten don vi---");
	printf("\n\nCHON: ");scanf("%d",&ch);
	switch(ch)
	{
		case 1: //tim theo sdt
			{
				do
				{
					int found = 0;
					DANHBA *p = (DANHBA*)malloc(sizeof(DANHBA)); if(p==NULL){printf("ERROR!!");exit(1);}
					fflush(stdin);printf("\nNhap so dien thoai can tim kiem thong tin: ");gets(sdt);
					p = head;
					while(p!=NULL && found==0)//lap p kieu DANHBA va check found
					{
						if (strcmp(p->sdt,sdt)==0)//kiem tra sdt vua nhap va sdt trong danhba
						{
							found = 1;
							printf("\nThong tin cua sdt ban can tim: ");
							printf("\nTen tinh: %s\nTen don vi: %s\nDia chi: %s\nSo dien thoai: %s",p->tentinh,p->tenDV,p->diachi,p->sdt);
						}
						else
						{
							p = p->next;
							if (p == NULL && found ==0)
							{
								printf("\nSo dien thoai ban vua nhap khong co trong danh ba.Ban co muon them?");
								fflush(stdin);printf("\nY de them N de thoat.\nCHON: ");scanf("%c",&c1);
								if (c1=='Y'||c1=='y')
								{
									append();
								}
								else
								{
									break;
								}
							}
						}
					}
					fflush(stdin);printf("\nTiep tuc tim kiem?  Y(Yes) -- N(No)");scanf("%s",&c2);
				} while (c2=='Y'||c2=='y');
				if (c2=='N'||c2=='n')
					{
						break;
					}
			}
		
		case 2: //tim theo ten DV
			{
				do
				{
					int found = 0;
					DANHBA *p = (DANHBA*)malloc(sizeof(DANHBA)); if(p==NULL){printf("ERROR!!");exit(1);}
					fflush(stdin); printf("\nNhap don vi can tim kiem thong tin: ");gets(tdv);
					p = head;
					while(p!=NULL && found==0)
					{
						if (strcmp(p->tenDV,tdv)==0)
						{
							found = 1;
							printf("\nThong tin cua don vi ban can tim: ");
							printf("\nTen tinh: %s\nTen don vi: %s\nDia chi: %s\nSo dien thoai: %s",p->tentinh,p->tenDV,p->diachi,p->sdt);
						}
						else
						{
							p = p->next;
							if (p == NULL && found == 0)
							{
								printf("\nDon vi ban vua nhap khong co trong danh ba.Ban co muon them?");
								fflush(stdin);printf("\nY de them N de thoat.\nCHON: ");scanf("%c",&c1);
								if (c1=='Y'||c1=='y')
								{
									append();
								}
								else
								{
									break;
								}
							}
						}
					}
					fflush(stdin);printf("\nTiep tuc tim kiem?  Y(Yes) -- N(No)");scanf("%s",&c2);
				} while (c2=='Y'||c2=='y');
				if (c2=='N'||c2=='n') break;
			}	
	}
}
 
void outFile()
{
	
	FILE *f = fopen("C:\\Users\\ADMIN\\Desktop\\output.txt","w"); if (f==NULL) {printf("FAIL OPENING FILE!");}
	DANHBA *p = (DANHBA*)malloc(sizeof(DANHBA));
	DATA d;//d trong DATA lam bien trung gian
 
	p = head;
	while (p!=NULL)
	{
		strcpy(d.tentinh,p->tentinh);strcpy(d.tenDV,p->tenDV);strcpy(d.diachi,p->diachi);strcpy(d.sdt,p->sdt);
		printf("\n%s %s %s %s",d.tentinh,d.tenDV,d.diachi,d.sdt);
		fprintf(f,"%s %s %s %s\n",d.tentinh,d.tenDV,d.diachi,d.sdt);
		p = p->next;
	}
	fclose(f);
	printf("\n\nDa luu danh ba vao file");
	
}
 
void delDB()
{
	DANHBA *prev,*curr;
	curr = (DANHBA*)malloc(sizeof(DANHBA));
	char sdt[15];
	fflush(stdin);printf("\nNhap so dien thoai ban muon xoa: ");scanf("%s",sdt);
	curr = head;
	
	if (curr != NULL && strcmp(curr->sdt,sdt)==0)//neu node dau la phan tu can xoa->xoa
	{
		head = curr->next;
		free(curr);
		return;
	}	
	while (curr!=NULL && strcmp(curr->sdt,sdt)!=0)//tim phan tu can xoa
	{
		prev = curr;//bien con tro prev dung de kiem soat phan tu ngay truoc phan tu can xoa
		curr = curr->next;
	}
	if (curr == NULL)
	{
		printf("\nKhong tim thay sdt can xoa.");
		return;
	}
	prev->next = curr->next;//loai bo node can xoa (curr)
	free(curr);
	printf("\nDa xoa sdt %s. Chon chuc nang 1 hoac 2 de kiem tra.");
}
 
int main()
{
	appendTinh();
	head = NULL;
	char c;
	int ch;
	appendFile();
	printf("\nDanh ba vua duoc doc tu File.");
	duplicate();
	printf("\n\n");
	printf("\n\t\t\t------------DANH BA VIET NAM-------------");
	printf("\n\t\tMenu Chon:");
	do
	{
		printf("\n\t\t\t1. Hien thi danh ba.\n\t\t\t2. Hien thi danh ba theo tinh.\n\t\t\t3. Tim kiem thong tin.\n\t\t\t4. Them thong tin.");
		printf("\n\t\t\t5. Sap xep tang dan theo tinh. \n\t\t\t6. Xoa mot so trong danh ba.\n\t\t\t7. Tim trung lap.\n\t\t\t8. In danh ba ra file.");
		printf("\n\t\t\t9. Thoat.");
		printf("\n\nChon chuc nang ban muon su dung: ");scanf("%d",&ch);
		switch (ch)
		{
			case 1:
				printf("\n\t\t\t\t\t\t\t   DANH BA\n\n");
				show(head);
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
			
			case 2:
				showtinh(head);
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 3:
				search();
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 4:
				{
					char c;
					printf("\nMoi nhap thong tin muon them vao danh ba: \n");
					do
					{
						append();
						fflush(stdin);printf("\n Ban muon tiep tuc? Y(Yes) --- N(No)");scanf("%c",&c);
					} while (c=='Y'||c=='y');
				}
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 5:
				sortDB();
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 6:
				delDB();
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 7:
				duplicate();
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 8:
				outFile();
				fflush(stdin);printf("\nQuay lai menu chon? Y--N  ");scanf("%c",&c);
				break;
				
			case 9 :
				
				break;
				
		}
	} while ((c=='Y'||c=='y') && (ch!=9));
	return 0;
}

 

Revise this Paste

Your Name: Code Language: