Code:
/*******************************************************************
* File: proj3.c
* Created by: Chris
* Created on: 04/07/2004
* Class: CMSC201
* Section: 0204
* Email: [email protected]
*
* Description: Main file of Project 3, a train report program.
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "trains.h"
#include <strings.h>
/* Maximum string length for structure */
#define LENGTH 12
int main()
{
int numCars;
typedef struct car
{
int carNum;
char cargo[LENGTH];
char origin[LENGTH];
char destination[LENGTH];
int weight;
int miles;
float cost;
}CAR;
CAR *train;
PrintGreeting ();
numCars = ReadNumCars ();
train = (CAR *) malloc(numCars * sizeof(CAR));
FillArray ( train, numCars, &costPtr, &wtPtr );
FindTotals ( train, numCars, &costPtr, &wtPtr );
return 0;
}
/*******************************************************************
* File: train.c
* Created by: Chris
* Created on: 04/06/2004
* Class: CMSC201
* Section: 0204
* Email: [email protected]
*
* Description: Library file of Project Three containing all
* of the functions used within the program.
*
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "trains.h"
#include <string.h>
#define LENGTH 12
/* Display greeting message */
/* name: PrintGreeting */
/* inputs: none */
/* returned value: none */
void PrintGreeting ()
{
printf( "\nThank you for using Train Report! This program will");
printf( "\nread information from the data file, and print out a");
printf( "\ntrain report on each of those cars, and statistics");
printf( "\nabout the whole train, followed by a graphical");
printf( "\nrepresentation of the train. Enjoy!\n\n");
}
/* Reads number of cars in train */
/* name: ReadNumCars */
/* inputs: none */
/* returned value: none */
int ReadNumCars ()
{
int cars = 0;
scanf( "%d", &cars);
return cars;
}
/* Fills the array with items from the data file */
/* name: FillArray */
/* inputs: number of cars */
/* returned value: none */
void FillArray (CAR* train, int numCars, float* costPtr, int* wtPtr)
{
int i;
char str1[LENGTH] = "coal";
char str2[LENGTH] = "cattle";
char str3[LENGTH] = "hogs";
char str4[LENGTH] = "eggs";
char str5[LENGTH] = "autos";
for ( i = 0; i < numCars; i++ )
{
scanf("%d", &train[i].carNum);
scanf("%s", train[i].cargo);
scanf("%s", train[i].origin);
scanf("%s", train[i].destination);
scanf("%d", &train[i].weight);
scanf("%d", &train[i].miles);
if( strcmp(train[i].cargo, str1) == 0 )
{
train[i].cost = 29.1;
}
if( strcmp(train[i].cargo, str2) == 0 )
{
train[i].cost = 52.5;
}
if( strcmp(train[i].cargo, str3) == 0 )
{
train[i].cost = 63.1;
}
if( strcmp(train[i].cargo, str4) == 0 )
{
train[i].cost = 75.9;
}
if( strcmp(train[i].cargo, str5) == 0 )
{
train[i].cost = 16.5;
}
}
}
/* Finds the total weight and total cost of the train */
/* name: FindTotals */
/* inputs: array of structures, number of cars, cost and weight pointers */
/* returned values: none */
void FindTotals (CAR* train, int numCars, float* costPtr, int* wtPtr)
{
int i;
int increment;
int weight = 0;
float cost = 0;
costPtr = &cost;
wtPtr = &weight;
for ( i = 0; i < numCars; i++ )
{
increment = train[i].cost * train[i].miles * train[i].weight;
cost = cost + increment;
}
i = 0;
for ( i = 0; i < numCars; i++ )
{
weight = weight + train[i].weight;
}
}
/* Prints the train report */
/* name: PrintReport */
/* inputs: array of structures, number of cars, weight and cost pointer */
/* returned values: none */
void PrintReport (CAR* train, int numCars, float* costPtr, int* wtPtr)
{
int i;
printf( "\n\nThere are %d cars in this train.\n", numCars);
printf( "The data for each car is as follows:\n\n" );
for ( i = 0; i < numCars; i++ )
{
printf( "\n\nCar number : %d", train[i].carNum );
printf( "\nCargo : %s", train[i].cargo );
printf( "\nOrigin : %s", train[i].origin );
printf( "\nDestination : %s", train[i].destination );
printf( "\nWeight : %d pounds", train[i].weight );
printf( "\nDistance : %d miles", train[i].miles );
printf( "\nCost : $ %f", train[i].cost );
}
printf( "\n\n-------------------------------" );
printf( "\n-------------------------------\n");
printf( "\nNumber of Cars : %d", numCars);
printf( "\nTotal Weight : %d tons", *wtPtr);
printf( "\nTotal Cost : $ %f", *costPtr);
}