Advertisement
rootUser

structure insert , display, sort

Jan 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.  
  5. struct GraphicsCard
  6. {
  7.     char Brand[20];
  8.     char Model[20];
  9.     int Price;
  10.     int RAM;
  11.     int Speed;
  12.  
  13.     GraphicsCard()
  14.     {
  15.         strcpy(Brand, "");
  16.         strcpy(Model, "");
  17.         Price = 0;
  18.         RAM = 0;
  19.         Speed = 0;
  20.     }
  21.  
  22.     GraphicsCard(char _Brand[], char _Model[], int _Price, int _RAM, int _Speed)
  23.     {
  24.         strcpy(Brand, _Brand);
  25.         strcpy(Model, _Model);
  26.         Price = _Price;
  27.         RAM = _RAM;
  28.         Speed = _Speed;
  29.     }
  30. };
  31.  
  32. void sort_GraphicsCard_speed(struct GraphicsCard* gpu[], int n)
  33. {
  34.     struct GraphicsCard now, next;
  35.     struct GraphicsCard* temp;
  36.     int i, j;
  37.     for(i=0; i<n-1; i++)
  38.     {
  39.         for(j=0; j<n-1; j++)
  40.         {
  41.             now = *gpu[j];
  42.             next = *gpu[j+1];
  43.             if(now.Speed < next.Speed)
  44.             {
  45.                 temp = gpu[j];
  46.                 gpu[j] = gpu[j+1];
  47.                 gpu[j+1] = temp;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     char Brand[20], Model[20];
  56.     int Price, RAM, Speed, i, n;
  57.     struct GraphicsCard* gpu[100];
  58.  
  59.     scanf("%d", &n);
  60.     for(i=0; i<n; i++)
  61.     {
  62.         scanf("%s %s %d %d %d", &Brand, &Model, &Price, &RAM, &Speed);
  63.         gpu[i] = new GraphicsCard(Brand, Model, Price, RAM, Speed);
  64.     }
  65.  
  66.     sort_GraphicsCard_speed(gpu, n);
  67.  
  68.     for(i=0; i<n; i++)
  69.     {
  70.         struct GraphicsCard temp = *gpu[i];
  71.         printf("%s %s %d %d %d\n", temp.Brand, temp.Model, temp.Price, temp.RAM, temp.Speed);
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement