Jump to content



Photo

C - Database program

c#

  • This topic is locked This topic is locked
1 reply to this topic

#1 atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 17 June 2014 - 08:10

This programming assignment entails reading data from two text files and storing it in two arrays. One array contains information about a product (product name, product description, product number, product price), and the other array contains sales information for the various products in the various company divisions. 
Your assignment is to create a program to handle this problem.

Your program should include functions that carry out the following operations:

  1. void readItemInfo(), which reads the file that contains information about the various products that are sold by the company, and stores the information in an array. Each element in this array will be a struct that contains the product title (a string), the product description (a string), the product number (an int), and the unit price (a double).
  2. void readSalesInfo(), which reads the file that contains sales information by company division for the various products, and stores this information in an array. Each element in this array will be a struct that contains the product number (an int), and sales information for the product from the four geographic divisions of the company (each is an int).
  3. double totalSales(), which returns the total revenue from sales of all items from all four company divisions.
  4. int maxSalesItem(), which returns the product number whose sales revenue is the highest.
  5. int maxSalesDivision(), which answers the question "Which company division has the highest revenues for all products?"

Communication from main() to each of the functions listed above should be by way of parameters that are passed to the functions. Notice that the product number field appears in both files: this is the key field that ties both files together and ties both arrays together.

The two text files that your program uses will be made available on Canvas.

 

This is the code that i created but is it possible for you guys to help me to shorten this or to make this code to be much neater or more organize in a way or possibly is there any mistake that you guys spotted?

#define MAX_LENGTH 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Product {
	char title[MAX_LENGTH];
	char type[MAX_LENGTH];
	int id;
	double price;
} Product;

typedef struct Sale {
	int productId;
	int north;
	int south;
	int east;
	int west;
} Sale;

int countProducts(FILE * stream)
{
	const short LINES_PER_PRODUCT = 4;
	char * fgets_retval;
	int count = 0;
	int i;
	char tempStr[MAX_LENGTH];

	do
	{
		fgets_retval = fgets(tempStr, sizeof(tempStr), stream);

		if (fgets_retval)
		{
	
			count++;

			for (i = 0; i < LINES_PER_PRODUCT; i++)
				fgets(tempStr, sizeof(tempStr), stream);
		}
	} while (fgets_retval);

	return count;
}

void readItemInfo(FILE * stream, Product * products, int productCount)
{
	int i = 0;
	char tempStr[MAX_LENGTH];

	rewind(stream);

	for (i = 0; i < productCount; i++)
	{
		// Read product title.
		fgets(tempStr, sizeof(tempStr), stream);
		strcpy_s(products[i].title, sizeof(products[i].title), tempStr);

		// Read product type.
		fgets(tempStr, sizeof(tempStr), stream);
		strcpy_s(products[i].type, sizeof(products[i].type), tempStr);

		// Read product number.
		fgets(tempStr, sizeof(tempStr), stream);
		products[i].id = atoi(tempStr);

		// Read product price.
		fgets(tempStr, sizeof(tempStr), stream);
		products[i].price = atof(tempStr);

		// Read and discard blank line.
		fgets(tempStr, sizeof(tempStr), stream);
	}
}

void readSaleInfo(FILE * stream, Sale * sales, int productCount)
{
	int i = 0;

	for (i = 0; i < productCount; i++)
	{
		fscanf_s(stream, "%d", &sales[i].productId, 1);
		fscanf_s(stream, "%d", &sales[i].north, 1);
		fscanf_s(stream, "%d", &sales[i].south, 1);
		fscanf_s(stream, "%d", &sales[i].east, 1);
		fscanf_s(stream, "%d", &sales[i].west, 1);
	}
}

void printItemInfo(Product * products, int productCount)
{
	int i = 0;

	for(i = 0; i < productCount; i++)
	{
		printf("\nTitle:\t\t%s", products[i].title);
		printf("Item Type:\t%s", products[i].type);
		printf("Product Number:\t%d\n", products[i].id);
		printf("List Price:\t%.2lf\n\n", products[i].price);
	}
}

double totalSale(Product * products, Sale * sales, int productCount)
{
	double total = 0, subtotal = 0;
	int i;

	for (i=0; i < productCount; i ++)
	{
		total= (sales[i].north + sales[i].south + sales[i].east + sales[i].west) * (products[i].price);
		subtotal += total;
	}
	return subtotal;
}

int maxSaleItem(Product * products, Sale * sales, int productCount)
{
	int i,a;
	double highestSale = 0, total = 0;

	for (i=0; i < productCount; i ++)
	{
		total= (sales[i].north + sales[i].south + sales[i].east + sales[i].west) * (products[i].price);
		if (total>highestSale) 
		{
			highestSale = total;
			a = i;
		}
	}
	return sales[a].productId;
}

int maxSaleDivision(Product * products, Sale * sales, int productCount)
{
	int i, a;
	double north = 0, totalNorth = 0, south = 0, totalSouth = 0,
		east = 0, totalEast = 0, west = 0, totalWest = 0,
		maxDivision = 0;

	for (i=0; i < productCount; i ++)
	{
		north = (sales[i].north)*(products[i].price);
		totalNorth += north;
		if (totalNorth > maxDivision) 
		{
			maxDivision = totalNorth;
			a=1;
		}

		south = (sales[i].south)*(products[i].price);
		totalSouth += south;
		if (totalSouth > maxDivision)
		{
			maxDivision = totalSouth;
			a=2;
		}

		east = (sales[i].east)*(products[i].price);
		totalEast += east;
		if (totalEast > maxDivision)
		{
			maxDivision = totalEast;
			a = 3;
		}
		west = (sales[i].west)*(products[i].price);
		totalWest += west;
		if (totalWest > maxDivision)
		{
			maxDivision = totalWest;
			a = 4;
		}
	}
	return a;
}
void maxDivision(Product * products, Sale * sales, int productCount)
{
	int division;
	division = maxSaleDivision(products, sales, productCount);
	switch(division)
	{
	case 1 : printf("North division had the highest sales.");
		break;
	case 2 : printf("South division had the highest sales.");
		break;
	case 3 : printf("East division had the highest sales.");
		break;
	case 4 : printf("West division had the highest sales.");
		break;
	}
}

int main(void)
{
	FILE * stream = NULL;
	Product * products = NULL;
	Sale * sales = NULL;
	int productCount = 0;
	int i = 0;

	if (fopen_s(&stream, "products.txt", "r"))
	{
		printf("Could not open products file.\n");
		exit(1);
	}

	productCount = countProducts(stream);
	if (productCount == 0)
	{
		printf("No data was found.");
		fclose(stream);
		exit(2);
	}

	products = (Product *)malloc(sizeof(Product) * productCount);

	if (!products)
	{
		printf("Could not allocate heap storage for the products array.");
		fclose(stream);
		exit(3);
	}

	readItemInfo(stream, products, productCount);

	fclose(stream);
	stream = NULL;

	if (fopen_s(&stream, "productsales.txt", "r"))
	{
		printf("Could not open product sales file.\n");
		exit(1);
	}

	sales = (Sale *)malloc(sizeof(Sale) * productCount);

	if (!sales)
	{
		printf("Could not allocate heap storage for the products array.");
		fclose(stream);
		exit(3);
	}

	
	readSaleInfo(stream, sales, productCount);

	fclose(stream);

	printItemInfo(products, productCount);

	printf("Total Sale is %.2lf\n", totalSale(products, sales, productCount));
	printf("Item with highest revenue is %d\n", maxSaleItem(products, sales, productCount));
	maxDivision(products, sales, productCount);

	// Free memory.
	free(products);
	free(sales);

	getchar();
	return EXIT_SUCCESS;
}

Attached Files




#2 Eric

Eric

    Neowinian Senior

  • Tech Issues Solved: 13
  • Joined: 02-August 06
  • Location: Greenville, SC

Posted 17 June 2014 - 16:55

[Thread closed]

 

Please see this about asking for help with assignments.