Problem statement: Given an input file (cityInfo.txt) with the following format:
ID: 3736230 • Letter: P
Question
Problem statement: Given an input file (cityInfo.txt) with the following format: <int> <double> <double> <c-string> i.e.: 52 58.18 134.24 Juneau, Alaska 53 39.06 94.35 Kansas City, Mo. 54 24.33 81.48 Key West, Fla. Where each row ( entry ) represents the city ID, followed by x and y coordinates, and a string label for the city name. The number of entries is NOT fixed (i.e. different files, may have different number of rows/entries) a) Write a function to count the number of entries in the input file (6 points) b) Define a structure to hold the information of a single entry i. int id, double xcoor, double ycoord and string cityname (5points) ii.Write a function member to display the struct data. (5 points) c) Using the number of entries (n) from a), and allocate memory for all of them using the structure in b) (5 points) d) Display a menu to: 1)Sort by city id (10 points) 2)Find the linear distance between two cities(enter city ids) (10 points) 3)Find the linear distance between two cities(enter city names) (20 points) 4)Max and Min distance (5 points) 5)Exit Notice that: For option 2 and 3 Make sure the two different cities Option 4, given one city name or id, it computes the distance to all the other cities, and finds the closest and farthest cities(min and max distance) and displays their respective information Hints: you can use strcmp for string comparison.
Explanation / Answer
here is your program :------------------>>>>>>>>>>
#include<iostream>
#include<string>
#include<fstream>
#include<cmath>
using namespace std;
typedef struct CityInfoType{
string name;
int id;
double x;
double y;
}CityInfo;
int countEntry(string filename){
ifstream ifs;
int count = 0;
ifs.open(filename.c_str());
if(ifs.is_open()){
char c;
while(!ifs.eof()){
ifs>>c;
if(c == ','){
count++;
}
}
ifs.close();
return count-1;
}else{
cout<<" File Opening Error !!";
exit(-1);
}
}
int read(CityInfo **info,string file){
int n = countEntry(file);
*info = new CityInfo[n];
ifstream ifs;
char c = ' ';
string line;
ifs.open(file.c_str());
if(ifs.is_open()){
for(int i = 0;i<n;i++){
ifs>>(*info)[i].id>>(*info)[i].x>>(*info)[i].y;
line.clear();
ifs.get(c);
while(c != ','){
line.push_back(c);
ifs.get(c);
}
(*info)[i].name = line;
ifs>>line;
(*info)[i].name += ", ";
(*info)[i].name += line;
}
ifs.close();
}else{
cout<<" File opening Error !!!";
exit(0);
}
return n;
}
void display(CityInfo *info,int count){
system("cls");
for(int i = 0;i<count;i++){
cout<<" ID : "<<info[i].id<<" Name : "<<info[i].name<<" Coord : ("<<info[i].x<<", "<<info[i].y<<")";
}
}
void sort(CityInfo **info,int count){
CityInfo temp;
for(int i = 0;i<count;i++){
for(int j = i;j>0;j--){
if((*info)[j].id < (*info)[j-1].id){
temp.id = (*info)[j].id;
temp.name = (*info)[j].name;
temp.x = (*info)[j].x;
temp.y = (*info)[j].y;
(*info)[j].id = (*info)[j-1].id;
(*info)[j].name = (*info)[j-1].name;
(*info)[j].x = (*info)[j-1].x;
(*info)[j].y = (*info)[j-1].y;
(*info)[j-1].id = temp.id;
(*info)[j-1].name = temp.name;
(*info)[j-1].x = temp.x;
(*info)[j-1].y = temp.y;
}
}
}
}
double linearDistance(string name1,string name2,CityInfo *info,int count){
double x1,y1,x2,y2;
int st = 0;
for(int i = 0;i<count;i++){
if(info[i].name == name1){
x1 = info[i].x;
y1 = info[i].y;
st++;
}
if(info[i].name == name2){
x2 = info[i].x;
y2 = info[i].y;
st++;
}
if(st == 2){
break;
}
}
if(st != 2){
cout<<" Names given is not correct";
return -1;
}
return (double)sqrt(abs(x1-x2)*abs(x1-x2)+abs(y1-y2)*abs(y1-y2));
}
double linearDistance(int id1,int id2,CityInfo *info,int count){
double x1,y1,x2,y2;
int st = 0;
for(int i = 0;i<count;i++){
if(info[i].id == id1){
x1 = info[i].x;
y1 = info[i].y;
st++;
}
if(info[i].id == id2){
x2 = info[i].x;
y2 = info[i].y;
st++;
}
if(st == 2){
break;
}
}
if(st != 2){
cout<<" Ids given is not correct";
return -1;
}
return (double)sqrt(abs(x1-x2)*abs(x1-x2)+abs(y1-y2)*abs(y1-y2));
}
void maxMin(int id1,CityInfo *info,int count){
double max = 0;
double min = linearDistance(id1,info[0].id,info,count);
if(min == 0){
min = 100;
}
double temp;
int mi = 0;
int mni = 0;
for(int i = 0;i<count;i++){
if(id1 == info[i].id){
continue;
}
temp = linearDistance(id1,info[i].id,info,count);
if(temp > max){
max = temp;
mi = i;
}
if(temp < min){
min = temp;
mni = i;
}
}
cout<<" Farthest City : "<<" Name : "<<info[mi].name<<" Distance : "<<max;
cout<<" Nearest City : "<<" Name : "<<info[mni].name<<" Distance : "<<min;
}
void menu(){
system("cls");
cout<<" 1. Sort By id";
cout<<" 2. Find the linear distance by id's ";
cout<<" 3. Find the linear distance by names ";
cout<<" 4. Max And Min of the city by id";
cout<<" 5. Exit";
cout<<" choose : ";
}
int main(){
CityInfo *info;
int count;
string file = "cityInfo.txt";
count = read(&info,file);
display(info,count);
cin.get();
cin.ignore();
int ch = 0;
while(ch != 5){
menu();
cin>>ch;
switch(ch){
case 1:
{
sort(&info,count);
display(info,count);
break;
}
case 2:
{
int id1,id2;
cout<<" Enter id of first city ";
cin>>id1;
cout<<" Enter id of second city ";
cin>>id2;
double dis = linearDistance(id1,id2,info,count);
cout<<" Distance : "<<dis;
break;
}
case 3:
{
string id1,id2;
cout<<" Enter name of first city ";
cin>>id1;
cout<<" Enter name of second city ";
cin>>id2;
double dis = linearDistance(id1,id2,info,count);
cout<<" Distance : "<<dis;
break;
}
case 4:
{
int id;
cout<<" Enter city id to calculate max and min distance : ";
cin>>id;
maxMin(id,info,count);
break;
}
}
cin.get();
cin.ignore();
cin.clear();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.