Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //#include<conio.h>
- typedef struct sanpham
- {
- char tensp[20];
- int giasp,masp,sosp;
- }sanpham;
- typedef struct node
- {
- sanpham sp;
- struct node *next;
- }node;
- node *root=NULL; node *cur=NULL;
- void nhapdl(void)
- {
- FILE *f;
- if((f=fopen("sanpham1.txt","w"))==NULL){
- printf("khong the mo file\n");exit(-1);}
- else{
- int i=-50000;
- sanpham a;
- printf("nhap du lieu\n");
- while(1){
- printf("ma san pham:");
- scanf("%d",&a.masp);
- if(a.masp==0) break;
- while(i>=a.masp)
- {
- printf("loi nhap ma san pham! nhap lai ma san pham:");
- scanf("%d",&a.masp);
- }
- i=a.masp;
- printf("gia:");
- scanf("%d",&a.giasp);
- while(0>=a.giasp){
- printf("loi nhap gia! nhap lai:");scanf("%d",&a.giasp);
- }
- printf("so luong:");
- scanf("%d",&a.sosp);
- while(0>a.sosp){
- printf("loi nhap so luong ! nhap lai:");scanf("%d",&a.sosp);
- }
- printf("ten san pham:");
- while(getchar()!='\n');
- gets(a.tensp);
- //while(getchar()!='\n');
- fprintf(f,"%d %d %d ",a.masp,a.giasp,a.sosp);
- fprintf(f,"%s\n",a.tensp);
- }
- }
- fclose(f);
- }
- void taonut(sanpham dl)
- {
- node *new1;
- new1=(node *)malloc(sizeof(node));
- new1->sp=dl;
- new1->next=NULL;
- if(root==NULL){
- root=new1;
- cur=root;
- }
- else{
- cur->next=new1;
- cur=cur->next;
- }
- }
- void taods(void)
- {
- sanpham a;
- FILE *f;
- f=fopen("sanpham1.txt","r");
- do //while(!feof (f))
- {
- //fscanf(f,"%d %d %d",&a.masp,&a.giasp,&a.sosp);
- fscanf (f,"%d",&a.masp);
- fscanf (f,"%d",&a.giasp);
- fscanf (f,"%d",&a.sosp);
- fgets(a.tensp,20,f);
- a.tensp[strlen(a.tensp)-1]='\0';
- taonut(a);
- }while(!feof (f));
- fclose(f);
- }
- void in(void)
- {
- node *duyet;
- for(duyet=root;duyet!=NULL;duyet=duyet->next)
- printf(" %d %d %d %s\n",duyet->sp.masp,duyet->sp.giasp,duyet->sp.sosp,duyet->sp.tensp);
- }
- void sapxep(void)
- {
- node *duyet1,*duyet2;
- sanpham tem;
- for(duyet1=root;duyet1!=NULL;duyet1=duyet1->next)
- for(duyet2=duyet1->next;duyet2!=NULL;duyet2=duyet2->next)
- if(duyet1->sp.giasp>duyet2->sp.giasp)
- {
- tem=duyet1->sp;
- duyet1->sp=duyet2->sp;
- duyet2->sp=tem;
- }
- in();
- }
- void tongtien(void)
- {
- int tong=0;
- node *duyet;
- for(duyet=root;duyet!=NULL;duyet=duyet->next)
- tong+=(duyet->sp.sosp)*(duyet->sp.giasp);
- printf("tong tien la:%d\n",tong);
- }
- main(void)
- {
- nhapdl();
- printf("nhap thanh cong\n");
- taods();
- in();
- printf("tao thanh cong\n");
- //getch();
- sapxep();
- printf("da sap xep\n");
- tongtien();
- }
Add Comment
Please, Sign In to add comment