Example TestData.txt
Aquos|5@10000
Chitato|10@9500
Chili|20@4000
Output I want(using Quick Sort) report (by subtotal):
A5-120-0|Aquos|5@10000=50000
C5-120-1|Chili|20@4000=80000
A7-504-0|Chitato|10@9500=95000
Total Price = 225000
[a] [b] - [c] - [d]
[a] Got From The Lowest Character in Name
[b] Got From The Length of Character (Take The Last Digit)
[c] Got From Factorial of The Name Length (Output First 3 Digits)
[d] Got From The Counter of The Same Number Of Factorial [c]
I already code it, but, I confused how to implement Binary Search to get The Lowest Character in Name.
Here is my Code (I confused On FindA Function):
#include <stdio.h>
#include <string.h>
#include<ctype.h>
struct sales{
char item[20];
int quantity, price,subtotal;
char a;
int b,c,d;
};
void quickSort(struct sales data[],int first, int last)
{
int pivot, i, j;
char tempItem[25];
int temp;
if(first < last){
i = first;
j = last;
pivot = first;
while(i<j)
{
while(data[i].subtotal <= data[pivot].subtotal && i<last)
{
i++;
}
while(data[j].subtotal > data[pivot].subtotal)
{
j--;
}
if(i<j)
{
temp = data[i].subtotal;
data[i].subtotal = data[j].subtotal;
data[j].subtotal = temp;
temp = data[i].quantity;
data[i].quantity = data[j].quantity;
data[j].quantity = temp;
temp = data[i].price;
data[i].price = data[j].price;
data[j].price = temp;
strcpy(tempItem,data[i].item);
strcpy(data[i].item,data[j].item);
strcpy(data[j].item,tempItem);
}
}
temp = data[pivot].subtotal;
data[pivot].subtotal = data[j].subtotal;
data[j].subtotal = temp;
temp = data[pivot].quantity;
data[pivot].quantity = data[j].quantity;
data[j].quantity = temp;
temp = data[pivot].price;
data[pivot].price = data[j].price;
data[j].price = temp;
strcpy(tempItem,data[pivot].item);
strcpy(data[pivot].item,data[j].item);
strcpy(data[j].item,tempItem);
quickSort(data,first,j-1);
quickSort(data,j+1,last);
}
}
char findA(char item[])
{
char lowest;
int i;
lowest = toupper(item[0]);
i=1;
while(item[i]!='\0')
{
if(toupper(item[i]) < lowest)
lowest = toupper(item[i]);
i++;
}
return lowest;
}
int findB(char item[])
{
int length;
length = strlen(item);
length = length%10;
return length;
}
int findC(int length)
{
int fact = 1,i;
for(i=1;i<=length;i++)
{
fact = fact * i;
}
char temp[10];
sprintf(temp,"%d",fact);
int hundreds = temp[0] - '0';
int tens = temp[1] - '0';
int ones = temp[2] - '0';
int num;
num = hundreds*100 + tens*10 + ones;
return num;
}
int findD(struct sales data[],int i)
{
int count,j,currentC;
count = 0;
j=0;
while(j<i)
{
if(data[i].c == data[j].c)
count++;
j++;
}
return count;
}
int main()
{
int count,i,j,n,total = 0;
FILE *input = fopen("TestData.txt","r");
struct sales data[100];
n = 0;
while(fscanf(input,"%[^|]|%d@%d\n", data[n].item,&data[n].quantity,&data[n].price) != EOF)
{
data[n].subtotal = data[n].quantity * data[n].price;
total = total + data[n].subtotal;
n++;
}
fclose(input);
quickSort(data,0,n-1);
for(i=0;i<n;i++)
{
data[i].a = findA(data[i].item);
data[i].b = findB(data[i].item);
data[i].c = findC(strlen(data[i].item));
data[i].d = findD(data,i);
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);
}
printf("Total Price = %d\n",total);
return 0;
}