Guest User

Untitled

a guest
Dec 21st, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include<ctype.h>
  4.  
  5. struct sales{
  6.     char item[20];
  7.     int quantity, price,subtotal;
  8.     char a;
  9.     int b,c,d;
  10. };
  11.  
  12. void quickSort(struct sales data[],int first, int last)
  13. {
  14.     int pivot, i, j;
  15.     char tempItem[25];
  16.     int temp;
  17.     if(first < last){
  18.         i = first;
  19.         j = last;
  20.         pivot = first;
  21.     while(i<j)
  22.     {
  23.         while(data[i].subtotal <= data[pivot].subtotal && i<last)
  24.         {
  25.             i++;
  26.         }      
  27.         while(data[j].subtotal > data[pivot].subtotal)
  28.         {
  29.             j--;
  30.         }
  31.         if(i<j)
  32.         {
  33.             temp = data[i].subtotal;
  34.             data[i].subtotal = data[j].subtotal;
  35.             data[j].subtotal = temp;
  36.             temp = data[i].quantity;
  37.             data[i].quantity = data[j].quantity;
  38.             data[j].quantity = temp;
  39.             temp = data[i].price;
  40.             data[i].price = data[j].price;
  41.             data[j].price = temp;
  42.             strcpy(tempItem,data[i].item);
  43.             strcpy(data[i].item,data[j].item);
  44.             strcpy(data[j].item,tempItem);
  45.         }
  46.     }
  47.         temp = data[pivot].subtotal;
  48.         data[pivot].subtotal = data[j].subtotal;
  49.         data[j].subtotal = temp;
  50.         temp = data[pivot].quantity;
  51.         data[pivot].quantity = data[j].quantity;
  52.         data[j].quantity = temp;
  53.         temp = data[pivot].price;
  54.         data[pivot].price = data[j].price;
  55.         data[j].price = temp;
  56.         strcpy(tempItem,data[pivot].item);
  57.         strcpy(data[pivot].item,data[j].item);
  58.         strcpy(data[j].item,tempItem);
  59.  
  60.         quickSort(data,first,j-1);
  61.         quickSort(data,j+1,last);
  62.     }
  63. }
  64.  
  65. char findA(char item[])
  66. {
  67.     char lowest;
  68.     int i;
  69.     lowest = toupper(item[0]);
  70.     i=1;
  71.     while(item[i]!='\0')
  72.     {
  73.         if(toupper(item[i]) < lowest)
  74.             lowest = toupper(item[i]);
  75.         i++;
  76.     }
  77.     return lowest;
  78. }
  79.  
  80. int findB(char item[])
  81. {
  82.     int length;
  83.     length = strlen(item);
  84.     length = length%10;
  85.     return length;
  86. }
  87.  
  88. int findC(int length)
  89. {
  90.     int fact = 1,i;
  91.     for(i=1;i<=length;i++)
  92.     {
  93.         fact = fact * i;
  94.     }
  95.     char temp[10];
  96.     sprintf(temp,"%d",fact);
  97.     int hundreds = temp[0] - '0';
  98.     int tens = temp[1] - '0';
  99.     int ones = temp[2] - '0';
  100.     int num;
  101.     num = hundreds*100 + tens*10 + ones;
  102.     return num;
  103. }
  104.  
  105. int findD(struct sales data[],int i)
  106. {
  107.     int count,j,currentC;
  108.        count = 0;
  109.         j=0;
  110.         while(j<i)
  111.         {
  112.             if(data[i].c == data[j].c)
  113.                 count++;
  114.             j++;
  115.         }
  116.         return count;
  117. }
  118.  
  119. int main()
  120. {
  121.     int count,i,j,n,total = 0;
  122.     FILE *input = fopen("TestData.txt","r");
  123.     struct sales data[100];
  124.     n = 0;
  125.     while(fscanf(input,"%[^|]|%d@%d\n", data[n].item,&data[n].quantity,&data[n].price) != EOF)
  126.     {
  127.         data[n].subtotal = data[n].quantity * data[n].price;
  128.         total = total + data[n].subtotal;
  129.         n++;
  130.     }
  131.     fclose(input);
  132.  
  133.     quickSort(data,0,n-1);
  134.    
  135.     for(i=0;i<n;i++)
  136.     {
  137.         data[i].a = findA(data[i].item);      
  138.         data[i].b = findB(data[i].item);      
  139.         data[i].c = findC(strlen(data[i].item));
  140.         data[i].d = findD(data,i);      
  141.         printf("%c%d-%d-%d|%s|%d@%d=%d\n",data[i].a,data[i].b,data[i].c,data[i].d,data[i].item,data[i].quantity,data[i].price,data[i].subtotal);
  142.     }
  143.     printf("Total Price = %d\n",total);
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment