Code:
// Header Files
#include <stdio.h>
#include <stdlib.h>
//prototypes
int menu(void);
int strcmp( const char *str1, const char *str2 );
void read(char place[11][11], int dist[10][10]);
void display(char place[11][11], int dist[10][10]);
int main (void)
{
char place[11][11];
int dist[10][10];
int row, col, i, numstops, end, wrong, flag, option;
double price;
int stops[3], point[2];
char pos[2][16];
char stopname[3][16];
read(place,dist);
while(end !=1)
{
option = menu(); // calls upon the menu
switch(option) // using a switch statement for the menu option
{
case 1: display(place,dist);
break;
case 2:
for(i=0;i<2;i++) // loops round twice allowing the input of 2 points
{
if(wrong!=1)
{
if(i==0){printf("\n Please enter your Start point: ");} // input first point
if(i==1){printf("\n Please enter your Finishing point: ");} // input first point
}
wrong=0;
scanf("%16s", pos[i]);
size_t x;
flag = 0;
for(x = 0; x < sizeof(place) / sizeof(*place); x ++)
{
if(strcmp(place[x+1], pos[i])==0)
{
point[i] = x;
flag=1;
}
}
if(flag==0)
{
printf("\n Invalid place name please re enter: ");
i--;
wrong=1;
}
}
printf("\n Please Enter the number of stops 0-3: ");
scanf("%5d",&numstops);
while(numstops >3 || numstops <0)
{
printf("\n Invalid number of stops please re enter: ");
scanf("%5d",&numstops);
}
if(numstops==0)
{
printf("\n From %s To %s is %d miles",pos[0],pos[1],dist[point[0]][point[1]]);
printf("\n Total distance : %d miles", dist[point[0]][point[0]]+dist[point[0]][point[1]]); // Adds the 2 points togteher to work out the distance
printf("\n\n\n");
}
if(numstops>0)
{
for(i=0;i<numstops;i++) //loops round depeneding on how many stops was enterd
{
if(wrong !=1)
{
printf("\n Please enter the name for Stop %-1d: ",i+1);
}
wrong=0;
scanf("%16s",stopname[i]);
size_t x;
flag =0;
for(x = 0; x < sizeof(place) / sizeof(*place); x ++)
{
if(strcmp(place[x+1], stopname[i])==0)
{
stops[i] = x;
flag=1;
}
}
if(flag==0)
{
printf("\n Invalid place name please re enter: ");
i--;
wrong=1;
}
}
//will print out the distances depending on how many stops was enterd
if(numstops==1)
{
printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
printf("\n From %s To %s is %d miles",stopname[0],pos[1],dist[point[1]][stops[0]]);
printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[point[1]][stops[0]]);
printf("\n\n\n");
}
else if(numstops==2)
{
printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
printf("\n From %s To %s is %d miles",stopname[0],stopname[1],dist[stops[0]][stops[1]]);
printf("\n From %s To %s is %d miles",stopname[1],pos[1],dist[point[1]][stops[1]]);
printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[stops[0]][stops[1]]+dist[point[1]][stops[1]]);
printf("\n\n\n");
}
else if(numstops==3)
{
printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
printf("\n From %s To %s is %d miles",stopname[0],stopname[1],dist[stops[0]][stops[1]]);
printf("\n From %s To %s is %d miles",stopname[1],stopname[2],dist[stops[1]][stops[2]]);
printf("\n From %s To %s is %d miles",stopname[2],pos[1],dist[point[1]][stops[2]]);
printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[stops[0]][stops[1]]+dist[stops[1]][stops[2]]+dist[point[1]][stops[2]]);
printf("\n\n\n");
}
}
break;
case 3:
for(i=0;i<2;i++) // loops round twice allowing the input of 2 points
{
if(wrong!=1)
{
if(i==0){printf("\n Please enter your Start point: ");} // input first point
if(i==1){printf("\n Please enter your Finishing point: ");} // input first point
}
wrong=0;
scanf("%16s", pos[i]);
size_t x;
flag = 0;
for(x = 0; x < sizeof(place) / sizeof(*place); x ++)
{
if(strcmp(place[x+1], pos[i])==0)
{
point[i] = x;
flag=1;
}
}
if(flag==0)
{
printf("\n Invalid place name please re enter: ");
i--;
wrong=1;
}
}
printf("\n From %s To %s is %d miles",pos[0],pos[1],dist[point[0]][point[1]]);
if(dist[point[0]][point[0]]+dist[point[0]][point[1]] <=100)
{
price = 0.4;
printf("\n Your Journey is Greatert than 100 miles, Price per mile = %lf",price);
}
else
{
price =0.3;
printf("\n Your Journey is less than 100 miles, Price per mile = %lf",price);
}
printf("\n The price of your journey is : %5lf", dist[point[0]][point[0]]+dist[point[0]][point[1]]*price); // Adds the 2 points togteher and multiplys by 0.4 to get the ppm
printf("\n\n\n");
break;
case 4: printf("\n Thankyou and goodbye");
exit(0);
break;
}
}
}
int menu(void) // menu funcion
{
int option;
printf("\n ============================================");
printf("\n M E N U ");
printf("\n ================++++++++++++================");
printf("\n 1. Display a complete mileage place ");
printf("\n 2. Calculate the distance of your journey ");
printf("\n 3. Calculate the cost of your journey ");
printf("\n 4. Exit ");
printf("\n --------------------------------------------");
printf("\n\n Please input your choice from the menu: ");
scanf("%d",&option);
while(option<0 || option>4)
{
printf("\n Invalid menu input please re enter: ");
scanf("%d",&option);
}
printf("\n\n");
return option;
}
void read(char place[11][11], int dist[10][10])
{
int i, j;
char filename[21], p1, p2;
FILE *in_file;
printf("\nEnter the name of the input file, name must be 20 charecters or less: ");
scanf("%s", &filename);
in_file = fopen(filename,"r");
if (in_file == NULL)
{
printf("\nCannot open %s\n", &filename);
exit(0);
}
else if (in_file != NULL)
{
for(i=0;i<11;i++)
{
fscanf(in_file, "%s ", &place[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
fscanf(in_file, "%d ", &dist[i][j]);
}
}
printf("%s Opened Successfully", &filename);
printf("\n\n\n");
}
fclose(in_file);
}
void display(char place[11][11], int dist[10][10])
{
int row, col;
printf("\n------------------------------------<<]]+++|||||MILEAGE CHART|||||+++[[>>-------------------------------------------\n\n");
for (row=0; row<11;row++)
{
printf("%-11s",place[row]);
}
printf("\n");
for (row=0; row<10;row++)
{
printf("%-11s",place[row+1]);
for (col=0;col<10;col++)
printf("%-11d", dist[row][col]);
printf("\n");
}
printf("\n--------------------------------------------------------------------------------------------------------------------\n\n");
}