In: Computer Science
In C++
Make changes to List.h and List.cpp, but not City.h or City.cpp.
Project includes Cities01.txt , City.h ,City.cpp , List.h , List.cpp , and Trial.cpp
Cities01.txt
Lansing 42.73 -84.55
Detroit 42.33 -83.04
Flint 43.01 -83.68
Grand-Rapids 42.96 -85.66
Jackson 42.27 -84.47
Kalamazoo 42.23 -85.55
Ann-Arbor 42.22 -83.75
Mt-Pleasant 43.60 -84.78
Clare 43.82 -84.77
Saginaw 43.42 -83.95
City.h
// City class definition
class city
{ friend class list;
public:
city(); // Constructor
bool get(istream& in); // Input name & location
void put(ostream& out); // Output data
private:
string name; // Name
float latitude,longitude; // Location
};
List.h
#define LIST_SIZE 20
#include "City.h"
class list
{ public:
list(); // Constructor - empty list
bool insert(city arg); // Add a city
void display(ostream& out); // Output data
void sort(string arg); // Sort by distance from arg
int size(); // Return number of cities
private:
int len; // Number of used cities
city map[LIST_SIZE]; // Data set
};
City.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "City.h"
/**************************************
* Constructor - no parameters
**************************************/
city::city()
{ name = "";
latitude = longitude = 0.0F;
}
/**************************************
* Get
**************************************/
bool city::get(istream &in)
{ in >> name;
in >> latitude;
in >> longitude;
return in.good();
}
/**************************************
* Put
**************************************/
void city::put(ostream &out)
{ out << left;
out << setw(14) << name;
out << right << fixed << setprecision(2);
out << setw(8) << latitude;
out << setw(8) << longitude;
}
List.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "List.h"
/*************************************
* list()
*************************************/
list::list()
{ len = 0;
}
/*************************************
* insert()
*************************************/
bool list::insert(city arg)
{
// Check to see if there is room
if(len>=LIST_SIZE) return false;
// Add to array
map[len++] = arg;
// Return success
return true;
}
/*************************************
* display()
*************************************/
void list::display(ostream &out)
{
}
/*************************************
* size()
************************************/
int list::size()
{ return len;
}
Trial.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "List.h"
/*************************************
* main()
*************************************/
void main()
{ string name;
fstream infile;
city c;
list state;
// Load from file
cout << "Enter file name: ";
cin >> name;
cout << endl;
infile.open(name,ios::in);
if(!infile.is_open()) return;
while(c.get(infile)) state.insert(c);
infile.close();
// Display
cout << state.size() << " Cities" << endl
<< endl;
state.display(cout);
/* Steps 10.6-7
// Sort by closest to here and display
cout << "Enter home city: ";
cin >> name;
cout << endl;
state.sort(name);
state.display(cout);
*/
}
Could use some help with implementing the list::display() function so the output looks like this:
Enter file name: Cities01.txt
10 Cities
City Lat Long
-------------- ------ ------
Lansing 42.73 -84.55
Detroit 42.33 -83.04
Flint 43.01 -83.68
Grand-Rapids 42.96 -85.66
Jackson 42.27 -84.47
Kalamazoo 42.23 -85.55
Ann-Arbor 42.22 -83.75
Mt-Pleasant 43.60 -84.78
Clare 43.82 -84.77
Saginaw 43.42 -83.95
Create a private function list::dist() that takes two integers src and dst, and calculates the distance between the cities at index src and dst. Hint:
Distance = Sqrt((latsrc-latdist)^2+(longsrc-longdist)^2)
Update the display() function so it calls the distance() function to display the distance between each city and the first city in the list. Uncomment the section in main() for steps 10.6-7. Implement the list::sort() function in two parts:
Output should look like this:
Enter file name: Cities01.txt
10 Cities
City Lat Long Dist
-------------- ------ ------ ------
Lansing 42.73 -84.55 0.00
Detroit 42.33 -83.04 1.56
Flint 43.01 -83.68 0.91
Grand-Rapids 42.96 -85.66 1.13
Jackson 42.27 -84.47 0.47
Kalamazoo 42.23 -85.55 1.12
Ann-Arbor 42.22 -83.75 0.95
Mt-Pleasant 43.60 -84.78 0.90
Clare 43.82 -84.77 1.11
Saginaw 43.42 -83.95 0.91
Enter home city: Lansing
City Lat Long Dist
-------------- ------ ------ ------
Lansing 42.73 -84.55 0.00
Jackson 42.27 -84.47 0.47
Mt-Pleasant 43.60 -84.78 0.90
Flint 43.01 -83.68 0.91
Saginaw 43.42 -83.95 0.91
Ann-Arbor 42.22 -83.75 0.95
Clare 43.82 -84.77 1.11
Kalamazoo 42.23 -85.55 1.12
Grand-Rapids 42.96 -85.66 1.13
Detroit 42.33 -83.04 1.56
SOLUTION -
The code written in bold is edited code.
In this code setter and getter methods are also added to set and get the value of fields as they are private
also swap function and toRadians functions are used which does the same thing as the name suggests .And also selection sort is used to sort the cities
City.h
// City class definition
class city
{ friend class list;
public:
city(); // Constructor
bool get(istream& in); // Input name & location
void put(ostream& out); // Output data
float getlat(); // get latitude
float getlong();// get longitude
string getname(); // get name
void setlat(float lat); // set latitude
void setlong(float long); // set longitude
void setname(string name); //set name
private:
string name; // Name
float latitude,longitude; // Location
};
List.h
#define LIST_SIZE 20
#include "City.h"
class list
{ public:
list(); // Constructor - empty list
bool insert(city arg); // Add a city
void dist(city src,city desc);//distance between two cities
float toRadians(float degree); //convert degree to radians
void swap(city a,city b); // swap to cities values
void display(ostream& out); //
Output data
void sort(string arg); // Sort by distance from arg
int size(); // Return number of cities
private:
int len; // Number of used cities
city map[LIST_SIZE]; // Data set
};
City.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "City.h"
/**************************************
* Constructor - no parameters
**************************************/
city::city()
{ name = "";
latitude = longitude = 0.0F;
}
/**************************************
* Get
**************************************/
bool city::get(istream &in)
{ in >> name;
in >> latitude;
in >> longitude;
return in.good();
}
void city::setlat(float lat) //setter method for latitude
{
latitude=lat;
}
void city::setlong(float long ) //setter method for longitude
{
longitude=long;
}
void city:: setname(string n) //setter method for name
{
name=n;
}
float city::getlat() // getter method for latitude
{
return lat;
}
float city:: getlong() //getter method for longitude
{
return long;
}
srtring city:: getname() //getter method for name
{
return name;
}
/**************************************
* Put
**************************************/
void city::put(ostream &out)
{ out << left;
out << setw(14) << name;
out << right << fixed << setprecision(2);
out << setw(8) << latitude;
out << setw(8) << longitude;
}
List.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "List.h"
/*************************************
* list()
*************************************/
list::list()
{ len = 0;
}
/*************************************
* insert()
*************************************/
bool list::insert(city arg)
{
// Check to see if there is room
if(len>=LIST_SIZE) return false;
// Add to array
map[len++] = arg;
// Return success
return true;
}
void list:: swap(city a,city b)
{
// swapping the latitude values
float temp=a.getlat();
a.setlat(b.getlat());
b.setlat(temp);
// swapping the longitude values
temp=a.getlong();
a.setlong(b.getlong());
b.setlong(temp);
// swapping the name values
string t=a.getname();
a.setname(b.getname());
b.setname(t);
}
/**********************
*toRadians()
***************/
float toRadians(float degree)
{
float one_deg = (M_PI) /
180;
return
(one_deg * degree);
}
/**********************
*dist()
***************/
void list::dist(city a ,city b)
{
float lat1=a.getlat();
float long1=a.getlong();
float lat2=b.getlat();
float long2=b.getlong();
lat1 =
toRadians(lat1);
long1 =
toRadians(long1);
lat2 =
toRadians(lat2);
long2 =
toRadians(long2);
float
dlong = long2 -
long1;
float
dlat =
lat2 - lat1;
float
ans
=
pow
(
sin
(dlat
/ 2), 2) +
cos
(lat1)
*
cos
(lat2) *
pow
(
sin
(dlong
/ 2), 2);
ans = 2 *
asin
(
sqrt
(ans));
float
R =
6371;
ans
= ans * R;
return
ans;
}
/*************************************
* display()
*************************************/
void list::display(ostream &out)
{
for(int i=1;i<len;i++)
{
cout<<"distance between the city first and city
"<<i
<<"is"<<distance(map[0],map[1])<<"\n";
}
}
void list ::sort(string arg)
{
for(int i=0;i<len;i++)
{
if(map[i].getname()==arg)
{ swap(map[i],map[0]);
break;
}
}
for(int i=1;i<n;i++)
{
float dis=dist(map[i],map[0]);
int min=i;
for(int j=i+1;j<n;j++)
{
float D=dist(map[j],map[0]);
if(D<dis)
{
dis=D;
min=j;
}
swap(map[i],map[min]);
}
}
/*************************************
* size()
************************************/
int list::size()
{ return len;
}
Tria.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "List.h"
/*************************************
* main()
*************************************/
void main()
{ string name;
fstream infile;
city c;
list state;
// Load from file
cout << "Enter file name:
";
cin >> name;
cout << endl;
infile.open(name,ios::in);
if(!infile.is_open()) return;
while(c.get(infile)) state.insert(c);
infile.close();
// Display
cout << state.size() << "
Cities" << endl << endl;
state.display(cout);
/* Steps 10.6-7
// Sort by closest to here and display
cout << "Enter home city:
";
cin >> name;
cout << endl;
state.sort(name);
state.display(cout);
*/
}