Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
struct Chart
{
char Town[10][10];
int Distance[10][10];
};
Chart chart=
{
{ "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York" },
{ 0, 23, 12, 89, 456, 123, 46, 732, 345, 123 },
{ 23, 0, 46, 234, 123, 46, 89, 234, 567, 90 },
{ 12, 46, 0, 767, 456, 46, 234, 123, 732, 35 },
{ 89, 234, 767, 0, 732, 32, 48, 67, 98, 100 },
{ 456, 123, 456, 732, 0, 234, 46, 89, 89, 732 },
{ 123, 46, 46, 32, 234, 0, 123, 46, 123, 234 },
{ 46, 89, 234, 48, 46, 123, 0, 46, 89, 19 },
{ 732, 234, 123, 67, 89, 46, 46, 0, 123, 732 },
{ 345, 567, 732, 98, 89, 123, 89, 123, 0, 78 },
{ 123, 90, 35, 100, 732, 234, 19, 732, 78, 0 },
};
int ReadChart(const char Towns[],const Chart* chart )
{
FILE *fp;
fp = fopen("Towns.txt", "r");
if (fp == NULL)
return 0;
else
{
size_t res = fread(chart, sizeof(Chart), 1, fp);
fclose(fp);
return res;
}
}
int WriteChart(const char Towns[], const Chart* chart)
{
FILE *fp;
fp = fopen("Towns.txt", "w");
if (fp == NULL)
return 0;
else
{
size_t res = fwrite(chart, sizeof(Chart), 1, fp);
fclose(fp);
return res;
}
}
int main(void)
{
char Distance;
int valid_input;
int row,col;
char des1,des2,des3;
int menu; /* Holds all chices open to the user */
double total = 0.00; /* the result of the calculation */
printf("\n\nWhat would you like to do?\n\n"); /* WRITE instructions */
printf("\t1 = Display Mileage chart from text file\n");
printf("\t2 = Input of a Journey between two points via up to 3 intermediate stops\n");
printf("\t3 = Display total cost of journey at 40 ppm up to 100 miles and 30ppm for mileas in excess of this\n");
printf("\t4 = Exit program\n");
printf("\n\nPleas make your selection now:\n\t");
scanf("%d",&menu); /* READ calculation type */
valid_input = 0;
while( valid_input == 0 )
{
printf("\t Please enter first destination point\n");
printf("%s",des1);
printf("\t Please enter second destination point\n");
printtf("%s",des2);
printf("\t Please enter thrid destination point\n");
printf("%s",des3);
else
{
printf("\tError: Invalid \n");
}
}
switch (menu != 4)
{
case 1:
FILE *infile;
infile = fopen("Towns.txt", "r"); // using relative path name of file
if (infile == NULL)
{
printf("Unable to open file.");
}
for(row=1; row<11;row++) // reads in the data into the array in 2 loops
{
for(col=1;col<11;col++)
{
fscanf(infile,"%d", chart[row-1][col-1]);
}
}
for(row=0; row<10;row++)
{
printf("\n\n");
for(col=0;col<10;col++)
{
printf("%-2d ",chart[row][col]);
}
}
printf("\n\n continue?");
scanf("%d", &menu);
break;
case 2:
prinf("\t Please enter first destination point\n");
printf("%s",des1);
prinf("\t Please enter second destination point\n");
printf("%s",des2);
prinf("\t Please enter thrid destination point\n");
printf("%s",des3);
break;
case 3:
if (Distance => 100)
{
total= 0.40*100;
}
if (Distance < 100)
{
total= 0.30*100;
}
break;
case 4:
printf("Thank you for using my program");
break;
default:
printf("Invalid option selected\n");
}
int search (node *root, int key)
{
if ( root != NULL)
{
if ( key == root->data )
return 1;
else if ( key < root->data )
return search ( root->left, key );
else /* key > root->data */
return search ( root->right, key );
}
return 0;
}
}
Node.h file