Guest User

Untitled

a guest
Apr 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. //#include<conio.h>
  5. typedef struct sanpham
  6. {
  7.   char tensp[20];
  8.   int giasp,masp,sosp;
  9. }sanpham;
  10. typedef struct node
  11. {
  12.   sanpham sp;
  13.   struct node *next;
  14. }node;
  15.  
  16. node *root=NULL; node *cur=NULL;
  17.  
  18. void nhapdl(void)
  19. {
  20.     FILE *f;
  21.     if((f=fopen("sanpham1.txt","w"))==NULL){
  22.       printf("khong the mo file\n");exit(-1);}
  23.     else{
  24.     int i=-50000;
  25.     sanpham a;
  26.     printf("nhap du lieu\n");
  27.     while(1){
  28.     printf("ma san pham:");
  29.     scanf("%d",&a.masp);
  30.     if(a.masp==0) break;
  31.     while(i>=a.masp)
  32.     {
  33.         printf("loi nhap ma san pham! nhap lai ma san pham:");
  34.     scanf("%d",&a.masp);
  35.     }
  36.     i=a.masp;
  37.     printf("gia:");
  38.     scanf("%d",&a.giasp);
  39.     while(0>=a.giasp){
  40.     printf("loi nhap gia! nhap lai:");scanf("%d",&a.giasp);
  41.     }
  42.     printf("so luong:");
  43.     scanf("%d",&a.sosp);
  44.     while(0>a.sosp){
  45.     printf("loi nhap so luong ! nhap lai:");scanf("%d",&a.sosp);
  46.     }
  47.     printf("ten san pham:");
  48.     while(getchar()!='\n');
  49.     gets(a.tensp);
  50.     //while(getchar()!='\n');
  51.     fprintf(f,"%d %d %d ",a.masp,a.giasp,a.sosp);
  52.     fprintf(f,"%s\n",a.tensp);
  53.     }
  54.     }
  55.     fclose(f);
  56. }
  57. void taonut(sanpham dl)
  58. {
  59.     node *new1;
  60.     new1=(node *)malloc(sizeof(node));
  61.     new1->sp=dl;
  62.     new1->next=NULL;
  63.     if(root==NULL){
  64.     root=new1;
  65.     cur=root;
  66.     }
  67.     else{
  68.         cur->next=new1;
  69.         cur=cur->next;
  70.     }
  71. }
  72. void taods(void)
  73. {
  74.     sanpham a;
  75.     FILE *f;
  76.     f=fopen("sanpham1.txt","r");
  77.     do //while(!feof (f))
  78.     {
  79.       //fscanf(f,"%d %d %d",&a.masp,&a.giasp,&a.sosp);
  80.       fscanf (f,"%d",&a.masp);
  81.       fscanf (f,"%d",&a.giasp);
  82.       fscanf (f,"%d",&a.sosp);
  83.       fgets(a.tensp,20,f);
  84.       a.tensp[strlen(a.tensp)-1]='\0';
  85.       taonut(a);
  86.     }while(!feof (f));
  87.     fclose(f);
  88. }
  89. void in(void)
  90. {
  91.     node *duyet;
  92.     for(duyet=root;duyet!=NULL;duyet=duyet->next)
  93.     printf(" %d %d %d %s\n",duyet->sp.masp,duyet->sp.giasp,duyet->sp.sosp,duyet->sp.tensp);
  94. }
  95. void sapxep(void)
  96. {
  97.     node *duyet1,*duyet2;
  98.     sanpham tem;
  99.     for(duyet1=root;duyet1!=NULL;duyet1=duyet1->next)
  100.         for(duyet2=duyet1->next;duyet2!=NULL;duyet2=duyet2->next)
  101.             if(duyet1->sp.giasp>duyet2->sp.giasp)
  102.             {
  103.                 tem=duyet1->sp;
  104.                 duyet1->sp=duyet2->sp;
  105.                 duyet2->sp=tem;
  106.             }
  107.     in();
  108. }
  109. void tongtien(void)
  110. {
  111.     int tong=0;
  112.     node *duyet;
  113.     for(duyet=root;duyet!=NULL;duyet=duyet->next)
  114.     tong+=(duyet->sp.sosp)*(duyet->sp.giasp);
  115.     printf("tong tien la:%d\n",tong);
  116. }
  117. main(void)
  118. {
  119.     nhapdl();
  120.     printf("nhap thanh cong\n");
  121.     taods();
  122.     in();
  123.     printf("tao thanh cong\n");
  124.     //getch();
  125.     sapxep();
  126.     printf("da sap xep\n");
  127.     tongtien();
  128.  
  129. }
Add Comment
Please, Sign In to add comment