C PROGRAMMING LANGUAGE (FILE PROCESSING, SORTING, AND SEARCHING)
Annisa want to make a Inventory Sales Report, she have the input with the format below (Input From TestData.txt) :
Aquos|5@10000
Chitato|10@9500
Chili|20@4000
Then she want to make a sorted (USING QUICK SORT) report (by subtotal) break down like this :
A5-120-0|Aquos|5@10000=50000
C5-120-1|Chili|20@4000=80000
A7-504-0|Chitato|10@9500=95000
Total Price = 225000
The unique transaction format is created by some rules with format:
[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]
PLEASE USE QUICK SORT AND BINARY SEARCH!!!
ONLY USE STDIO.H and STRING.H LIBRARY!!!
CODE WITH EXPLANATION IN COMMENTS
#include <stdio.h>
#include <string.h>
//let us use a struct to store the data read from file
struct sales
{
char item[20];
int quantity, price,subtotal;
//data to be written to file for each record
char a;
int b,c,d;
};
//Implementing quick sort, based on subtotal
void quickSort(struct sales data[],int first, int last)
{
int pivot, i, j;
char tempItem[25];
int temp;
//if first and last positions has not crossed each other due to repeated increment and decrement
if(first < last)
{
//let the first element be pivot
i = first;
j = last;
pivot = first;
//while i<j
//increment i till element at i < pivot position (only if it is less than last position)
//decrement j while element at j > pivot position
while(i<j)
{
while(data[i].subtotal <= data[pivot].subtotal && i<last)
{
i++;
}
while(data[j].subtotal > data[pivot].subtotal)
{
j--;
}
//and if i and j has not crossed each other, swap the lements at i and j position
if(i<j)
{
temp = data[i].subtotal;
data[i].subtotal = data[j].subtotal;
data[j].subtotal = temp;
//dont forget to swap the other values (name, quantity, price) to maintain data correctness
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;
//use strcpy for char *
strcpy(tempItem,data[i].item);
strcpy(data[i].item,data[j].item);
strcpy(data[j].item,tempItem);
}
}
//swap the elements at j and pivot position to change the pivot
temp = data[pivot].subtotal;
data[pivot].subtotal = data[j].subtotal;
data[j].subtotal = temp;
//dont forget to swap the other values (name, quantity, price) to maintain data correctness
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;
//use strcpy for char *
strcpy(tempItem,data[pivot].item);
strcpy(data[pivot].item,data[j].item);
strcpy(data[j].item,tempItem);
//call the quick sort repeatedly by creating partitin array
quickSort(data,first,j-1);
quickSort(data,j+1,last);
}
}
//Find the value of [a]
char findA(char item[])
{
char lowest;
int i;
//set firts character as lowest
lowest = toupper(item[0]);
//start comparing from second character
i=1;
//check till end of string
while(item[i]!='\0')
{
//if current value is less than lowest value (< compares the ascii value of characters) so converting to upper case during comparison
if(toupper(item[i]) < lowest)
lowest = toupper(item[i]);
i++;
}
//finally return lowest
return lowest;
}
int findB(char item[])
{
int length;
//strlen returns the length of string
length = strlen(item);
//% returns the last digit
length = length%10;
return length;
}
int findC(int length)
{
int fact = 1,i;
//multiply i with fact till length
for(i=1;i<=length;i++)
{
fact = fact * i;
}
//get first 3 digits of factorial
//first convert the factorial to char[] (to access firts 3 digits easily)
char temp[10];
sprintf(temp,"%d",fact);
//0 th position holds the 100s value of our number, 1st posiiton holds the tens digit and 2nd position holds the 3rd digit
int hundreds = temp[0] - '0';
int tens = temp[1] - '0';
int ones = temp[2] - '0';
int num;
//multiply the respective digits with their positon value and add them
num = hundreds*100 + tens*10 + ones;
return num;
}
int findD(struct sales data[],int i)
{
int count,j,currentC;
//to find data[i].d - NORMAL WAY -
//- start a loop till index i and increment count if that factorial number (c) has already occured before.
//else count remains 0
count = 0;
j=0;
while(j<i)
{
if(data[i].c == data[j].c)
count++;
j++;
}
//return the value of count
return count;
}
int main()
{
//declare required variables
int count,i,j,n,total = 0;
//open file to read data
FILE *input = fopen("TestData.txt","r");
//declare a struct of max size . example - 100
struct sales data[100];
//now, until eof is reached, read the data from File
n = 0;
/******"%[^|]|%d@%d\n"
* - %[^|]| - read till | is encountered and ^| to discard | into frst parameter (data[n].item)
* - %d@ - read an integer til @ into second argument (data[n].quantity) no need to include ^@ since %d anyways would not read a character and hence @ will be ignored
* - %d\n - reads rest of integer into lsat parameter until \n
********/
while(fscanf(input,"%[^|]|%d@%d\n", data[n].item,&data[n].quantity,&data[n].price) != EOF)
{
//To check data correctness
//printf("\n%s %d %d",data[n].item,data[n].quantity,data[n].price);
//calculate subtotal
data[n].subtotal = data[n].quantity * data[n].price;
//find total simulataneously
total = total + data[n].subtotal;
//in the end n holds the number of records
n++;
}
//close the file
fclose(input);
//call quick sort to sort the structure
quickSort(data,0,n-1);
//Now that the data are sorted, lets find the values of a,b,c and d to write to file
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);
//for checking purpose
//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);
}
//open a file to write data
FILE *output = fopen("Output.txt","w");
//loop and write the data in given format using fprintf
for(i=0;i<n;i++)
{
fprintf(output,"%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);
}
//and finally write the total
fprintf(output,"Total Price = %d", total);
//close the file
fclose(output);
printf("The report has been written to file.");
return 0;
}
TestData.txt
Aquos|5@10000
Chitato|10@9500
Chili|20@4000
Output.txt
A5-120-0|Aquos|5@10000=50000
C5-120-1|Chili|20@4000=80000
A7-504-0|Chitato|10@9500=95000
Total Price = 225000


CODE SCREENSHOT:










PS: SORTED using quick sort. Binary search has not been used here since we are not performing search of sorted items here. Please reach out if you have any queries and please don’t forget to leave your valuable feedback.
C PROGRAMMING LANGUAGE (FILE PROCESSING, SORTING, AND SEARCHING) Annisa want to make a Inventory Sales Report,...