Code:
/*=================================*/
/* */
/* Kristopher Johnson */
/* Lab 10 */
/* Bloodshed Dev-C++ 4.9.9.2 */
/* 11/27/11 */
/* */
/*=================================*/
#include <stdio.h>
#include <string.h>
#define COMPANY "Sierra Sporting Goods"
#define TRUE 1
#define FALSE 0
int quit();
int menu();
int prompt(char *msg);
int getint(int min, int max, char *item);
void add();
void report();
void title(char *name);
void message(char *msg);
void strcase(char *desc);
void getstring(char *desc);
void init_costs(double *c, int x);
void show_costs(double *c, int x);
void show(PRODUCTS rec);
double total(int q, double c);
double profit(double p, double c, int q);
double getreal(double min, double max, char *item);
char dept[6][15] = {"", "Camping ", "Tennis ", "Golf ",
"Snow Sports", "Water Sports"};
typedef struct
{
int number, type, quantity;
double cost, price, totalcost, totalprice;
char desc[20];
}PRODUCTS;
/* ========================================================================== */
main()
{
int choice, more = TRUE;
do
{
choice = menu();
switch (choice)
{
case 1: add(); break;
case 2: report(); break;
case 3: message("\n\tInvalid selection"); break;
case 4: message("\n\tInvalid selection"); break;
case 5: more = quit(); break;
}
}
while (more);
return 0;
}
/* ========================================================================== */
int menu()
{
int choice;
title(COMPANY);
printf("\t1 = Add a record\n");
printf("\t2 = Report a record\n");
printf("\t3 = Delete a record\n");
printf("\t4 = Change a record\n");
printf("\t5 = Quit\n");
choice = getint(1,5,"selection");
return choice;
}
/* ========================================================================== */
void add()
{
int choice, MINN, MAXN, MINT, MAXT, MINQ, MAXQ;
double c[6], MINC, MAXC, MINP, MAXP;
init_costs(c, 6);
PRODUCTS rec;
FILE *f;
f = fopen("limits.txt", "r");
if (f == NULL)
{
printf("\n\tError opening limits.txt!\n");
return;
}
fscanf(f, "%d%d%d%d%d%d%lf%lf%lf%lf", &MINN, &MAXN, &MINT, &MAXT, &MINQ,
&MAXQ, &MINC, &MAXC, &MINP, &MAXP) != EOF;
fclose(f);
FILE *fp;
fp = fopen("PRODUCTS.dat", "ab");
if (fp == NULL)
{
printf("\n\tError opening PRODUCTS.dat!\n");
return;
}
do
{
rec.number = getint(MINN, MAXN, "the product number");
rec.type = getint(MINT, MAXT, "the product type");
getstring(rec.desc);
rec.quantity = getint(MINQ, MAXQ, "the quantity");
rec.cost = getreal(MINC, MAXC, "the cost");
rec.price = getreal(MINP, MAXP, "the price");
rec.totalcost = total(rec.quantity, rec.cost);
rec.totalprice = total(rec.quantity, rec.price);
strcase(rec.desc);
show(rec);
fwrite(&rec, sizeof(rec), 1, fp);
c[rec.type] += rec.totalcost;
show_costs(c, 6);
choice = prompt("Continue");
}
while (choice == 'Y' || choice == 'y');
fclose(fp);
quit();
}
/* ========================================================================== */
void title (char *name)
{
printf("\n\n\t%s\n\n\n", COMPANY);
}
/* ========================================================================== */
void show(PRODUCTS rec)
{
printf("\n\n\tThe product number is -------> %04d", rec.number);
printf("\n\tThe product type is ---------> %d", rec.type);
printf("\n\tThe product description is --> %s", rec.desc);
printf("\n\tThe quantity is -------------> %d", rec.quantity);
printf("\n\tThe cost is -----------------> %.2f", rec.cost);
printf("\n\tThe price is ----------------> %.2f\n\n", rec.price);
printf("\n\tThe total cost is -----------> %.2f\n", rec.totalcost);
printf("\n\tThe total price is ----------> %.2f\n", rec.totalprice);
message("\n\tDone");
}
/* ========================================================================== */
int getint(int min, int max, char *item)
{
int x, i;
double c[6];
printf("\n\tEnter %s (%d-%d): ", item, min, max);
scanf("%d%*c", &x, *c);
while (x < min || x > max)
{
message("\n\tError, re-enter selection");
printf("\n\tEnter %s (%d-%d): ", item, min, max);
scanf("%d%*c", &x, *c);
}
return x;
}
/* ========================================================================== */
double getreal(double min, double max, char *item)
{
double y, i, c[6];
printf("\n\tEnter %s (%.2f-%.2f): ", item, min, max);
scanf("%lf%*c", &y, *c);
while (y < min || y > max)
{
message("\n\tError, re-enter selection");
printf("\n\tEnter %s (%.2f-%.2f): ", item, min, max);
scanf("%lf%*c", &y, *c);
}
return y;
}
/* ========================================================================== */
void message(char *msg)
{
printf("%s (press Enter to continue)", msg);
getchar();
}
/* ========================================================================== */
int quit()
{
int z, more = TRUE;
z = prompt("\n\tTerminate program");
if (z == 'Y' || z == 'y')
{
system("cls");
message("\n\tProgram terminated successfully");
more = FALSE;
}
return more;
}
/* ========================================================================== */
int prompt(char *msg)
{
int b;
do
{
printf("\n\t%s (Y/N)?", msg);
b = getchar();
fflush(stdin);
b = toupper(b);
if (b != 'Y' && b != 'N')
{
printf("\n\tError, try Y or N only\n");
}
}
while (b != 'Y' && b != 'N');
return (b);
}
/* ========================================================================== */
double total(int q, double c)
{
return(q * c);
}
/* ========================================================================== */
double profit(double p, double c, int q)
{
return((p - c) * q);
}
/* ========================================================================== */
void init_costs(double *c, int x)
{
int i;
for (i = 1; i < x; i++)
{
*(c+i) = 0;
}
}
/* ========================================================================== */
void show_costs(double *c, int x)
{
int i;
printf("\n\n\n\tProduct Cost by Type\n");
printf("\n\n\tType\tDepartment\tCost\n");
printf("\t------------------------------");
for (i = 1; i < x; i++)
{
printf("\n\t%d\t%s\t%.2f\n", i, *(dept+i), *(c+i));
}
printf("\n");
}
/* ========================================================================== */
void getstring(char *desc)
{
printf("\n\tEnter the product description: ");
gets(desc);
}
/* ========================================================================== */
void strcase(char *desc)
{
int x = 1, i;
for (i = 0; *(desc+i) != '\0'; i++)
{
*(desc+i) = tolower(*(desc+i));
if (*(desc+i) == ' ')
{
x = 1;
i++;
}
if (x == 1 && *(desc+i) >= 'a' && *(desc+i) <= 'z')
{
*(desc+i) = *(desc+i) - 32;
x = 0;
}
}
}
/* ========================================================================== */
void report()
{
double tprofit, gtprofit;
PRODUCTS rec;
FILE *fp;
fp = fopen("PRODUCTS.dat", "rb");
if (fp == NULL)
{
printf("\n\tError opening PRODUCTS.dat!\n");
return;
}
printf("\n\tProd#\tType\tDescription\t\tQty\tCost\tPrice\tProfit\n");
printf("\t===================================================");
while (fread(&rec, sizeof(rec), 1, fp))
{
tprofit = profit(rec.price, rec.cost, rec.quantity);
tprofit += gtprofit;
printf("\n\t%04d\t%d\t%s\t\t%d\t%.2f\t%.2f\n", rec.number,
rec.type, rec.desc, rec.quantity, rec.cost, rec.price);
}
printf("\t===================================================");
printf("\n\tTotal expected profit\t\t\t%.2f\n", gtprofit);
fclose(fp);
}
/* ========================================================================== */