Code:
#include <stdio.h>
#include <conio.h>
void main () {
FILE *indata=fopen("c:\\cs10e\\input3.txt", "r");
FILE *outdata=fopen("C:\\cs10e\\output3.txt", "w");
int lines=0, prt_num, prc_brk_quant, quant_sold, peakprt=9999, peaksale=0;
float init_amt, unit_prc, brk_discount, grand_discount, post_brk_discount, net_pay, total_discount;
clrscr();
if (indata==NULL) {
printf ("No data file found!");
getch();
exit();
}
fscanf (indata,"%d", &prt_num);
printf ("===============================================================================\n");
printf ("Part_No. Unit_Price Price_Break_Quant Quant_Sold Initial_Amt Discount Net_Pay\n");
printf ("===============================================================================\n");
fprintf (outdata,"===============================================================================\n");
fprintf (outdata,"Part_No. Unit_Price Price_Break_Quant Quant_Sold Initial_Amt Discount Net_Pay\n");
fprintf (outdata,"===============================================================================\n");
while (prt_num!=9999) {
fscanf (indata,"%f", &unit_prc);
fscanf (indata,"%d", &prc_brk_quant);
fscanf (indata,"%d", &quant_sold);
if (quant_sold>peaksale) {
peakprt=prt_num;
peaksale=quant_sold;}
init_amt=unit_prc*quant_sold;
if (quant_sold>=prc_brk_quant) {
brk_discount=0.1*init_amt;
post_brk_discount=init_amt-brk_discount; }
else {
brk_discount=0.00;
post_brk_discount=init_amt-brk_discount; }
if (post_brk_discount>1000.00) {
grand_discount=.05*post_brk_discount;
net_pay=post_brk_discount-grand_discount;}
else {
grand_discount=0.00;
net_pay=post_brk_discount-grand_discount;}
total_discount=brk_discount+grand_discount;
fprintf (outdata,"%7d %11.2f %17d %10d %9.2f %9.2f %10.2f\n",prt_num, unit_prc, prc_brk_quant, quant_sold, init_amt, total_discount, net_pay );
printf ("%7d %11.2f %17d %10d %9.2f %9.2f %10.2f\n",prt_num, unit_prc, prc_brk_quant, quant_sold, init_amt, total_discount, net_pay );
lines++;
fscanf (indata,"%d", &prt_num); }
printf ("-------------------------------------------------------------------------------\n\n");
printf ("Total number of data lines = %d\n", lines);
printf ("The highest quantity sold is Part No. %d, with %d sells\n", peakprt, peaksale);
fprintf (outdata,"-------------------------------------------------------------------------------\n\n");
fprintf (outdata,"Total number of data lines = %d\n", lines);
fprintf (outdata,"The highest quantity sold is Part No. %d, with %d sells\n", peakprt, peaksale);
fclose(indata);
fclose(outdata);
}
It's suppose to read some data (4 numbers) from a file for each "part" and list them and do some basic calculations, but I think I might have to add another column and there isn't space on the screen.