Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

#include #include #include #include #include using namespace std; // Data member to store Restaurant information struct Restaurant { string Name, City, Price_range; double Rating; int

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

#include

#include

#include

#include

#include

using namespace std;

// Data member to store Restaurant information

struct Restaurant

{

string Name,

City,

Price_range;

double Rating;

int Ranking,

Review;

};

//Function prototypes

int readRestaurants(ifstream& in, Restaurant restaurants[]);

void printRestaurants(Restaurant restaurants[],string columns[] , int SIZE);

void sortRestaurantsByRanking(Restaurant restaurants[], int SIZE);

void sortRestaurantsByName(Restaurant restaurants[], int SIZE);

int binarySearchName(Restaurant restaurants[], int SIZE, string name);

bool isInPriceRange(string priceRange, string targetPrice);

int findBestRestaurantWithPrice(Restaurant restaurants[], int SIZE, string targetPrice);

bool compareRestByName(Restaurant r1,Restaurant r2)

{

return r1.Name

}

bool compareRestByRank(Restaurant r1,Restaurant r2)

{

return r1.Ranking

}

int readRestaurants(ifstream &in, Restaurant restaurants[])

{

string line;

int i = 0,

j = 0;

while(getline(in,line))

{

stringstream ss(line);

string word;

j = 0;

while(getline(ss, word, ','))

{

if(j == 0)

{

restaurants[i].Name = word;

j++;

}

else if (j==1)

{

restaurants[i].City = word;

j++;

}

else if (j==2)

{

restaurants[i].Ranking = stoi(word);

j++;

}

else if (j==3)

{

restaurants[i].Rating = stof(word);

j++;

}

else if(j==4)

{

restaurants[i].Price_range = word;

j++;

}

else if(j==5)

{

restaurants[i].Review = stoi(word);

j=0;

}

}

i++;

}

return i;

}

void printRestaurants(Restaurant restaurants[],string columns[6],int SIZE)

{

cout

cout

for(int i=0 ; i

{

cout

cout

}

}

void sortRestaurantsByRanking(Restaurant restaurants[],int SIZE)

{

bool swapped = false;

do

{

swapped = false;

for(int i = 0; i

{

if(restaurants[i].Ranking > restaurants[i+1].Ranking)

{

swap(restaurants[i], restaurants[i+1]);

swapped = true;

}

}

} while (swapped);

}

void sortRestaurantsByName(Restaurant restaurants[],int SIZE)

{

bool swapped = false;

do

{

swapped = false;

for(int i = 0; i

{

if(restaurants[i].Name > restaurants[i+1].Name)

{

swap(restaurants[i], restaurants[i+1]);

swapped = true;

}

}

} while (swapped);

}

int binarySearchName(Restaurant restaurants[],int SIZE,string R_Name)

{

sortRestaurantsByName(restaurants,SIZE);

int mid,

low = 0,

high = SIZE-1;

while(low

{

mid = low + ( high - low ) / 2;

if(restaurants[mid].Name == R_Name)

{

return mid;

}

else if(restaurants[mid].Name > R_Name)

{

high = mid - 1;

}

else

low = mid + 1;

}

return -1;

}

bool isInPriceRange(string PriceRange,string TargetPrice)

{

if(PriceRange == "$$-$$$")

{

if(TargetPrice == "$$" || TargetPrice == "$$$")

return true;

else

return false;

}

else

{

if(PriceRange == "$$$$" && TargetPrice == "$$$$")

return true;

else if(PriceRange == "$" && TargetPrice == "$")

return true;

else

return false;

}

}

int findBestRestaurantWithPrice(Restaurant restaurants[],string TargetPrice,int SIZE)

{

sortRestaurantsByRanking(restaurants,SIZE);

for(int i = 0; i

{

if(isInPriceRange(restaurants[i].Price_range,TargetPrice) == true)

return i;

}

return -1;

}

int main()

{

//Array of flights

Restaurant restaurants[100];

//File path

ifstream in("restaurants.csv");

//Error in file open

if (!in)

{

cout

return 0;

}

string columns[6],

R_Name,

TPrice;

//Read file data

int SIZE = readRestaurants(in, restaurants);

int opt,

index;

//Loop unti exit

do

{

//Get user options

cout

cin >> opt;

switch(opt)

{

case 1 : sortRestaurantsByName(restaurants,SIZE); //Sort restaurants by name

printRestaurants(restaurants,columns,SIZE); //Print restaurants

break;

case 2 : sortRestaurantsByRanking(restaurants,SIZE);//Sort restaurants by ranking

printRestaurants(restaurants,columns,SIZE);//Print restaurants

break;

case 3 : //Find restaurant by a Name usinga binary search.

cout

cin >> R_Name;

index = binarySearchName(restaurants,SIZE,R_Name);

if(index != -1)

printRestaurants(restaurants,columns,index);

else

cout

break;

case 4 : //Read a price range then find the restaurant

//with thehighestRankingthat has agiven price range.

cout

cin >> TPrice;

index = findBestRestaurantWithPrice(restaurants,TPrice,SIZE);

if(index != -1)

printRestaurants(restaurants,columns,index);

else

cout

break;

case 5 : cout

in.close();

return 0;

break;

default :

break;

}

} while(true);

//Close file

in.close();

return 0;

}

Having error: libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion

csv file: restaurants.csv

Name,City,Ranking,Rating,Price Range,Number of Reviews

Uma,Barcelona,1,5.0,$$$$,792

Viana,Barcelona,2,5.0,$$ - $$$,2707

Blavis,Barcelona,3,5.0,$$ - $$$,643

My Restaurant,Barcelona,4,5.0,$$ - $$$,159

Bodega Biarritz,Barcelona,5,5.0,$$ - $$$,1078

Santa Rita Experience,Barcelona,6,5.0,$$$$,329

Rudi of Pirate Cooking,Barcelona,7,5.0,$$ - $$$,110

Chaka Khan,Barcelona,8,5.0,$$ - $$$,479

Spoonik Restaurant,Barcelona,9,5.0,$$$$,408

The Box,Barcelona,10,4.5,$,834

Problem description Today you'll be working as a Trip Advisor software engineer. You'll help a user of your application to find a place to eat in Barcelona. The user is interested in restaurants sorted by (1) Name and (2) Ranking. Also, he/she wants to (3) find restaurants by a Name and (4) get the best restaurant with a given prices. You'll be given a ".csv" file in the following format: 5.0 Name City Santa Rita Experience Barcelona Uma Barcelona Spoonik Restaurant Barcelona Viana Barcelona Chaka Khan Barcelona The Box Barcelona Blavis Barcelona My Restaurant Barcelona Bodega Biarritz Barcelona Rudi of Pirate Cooking Barcelona Ranking Rating Price Range Reviews 6 $$$$ 329 5.0 $$$$ 792 5.0 $$$$ 408 5.0 $$ - $$$ 2707 8 $$ - $$$ 479 4.5 834 5.0 $$ - $$$ 643 5.0 $$ - $$$ 159 5.0 $$ - $$$ 1078 5.0 $$ - $$$ 110 5.0 Column descriptions: Name: Name of the restaurant. A string. City: Name of the city where the restaurant is located. A string. Ranking: Rank of the restaurant in a given city (e.g. restaurant with rank 1 is considered to be the best restaurant in a city it is located). An integer number. Rating: An average rating of the restaurant. A non-integer number. Price Range: Price range of the dishes. A string. Reviews: Number of review. An integer number. Reviews: Number of review. An integer number. This is a part of a database that you can find here. The database is stored in a CSV (comma-separated values) file. It's a file format used to store tabular data. Rows are separated with an endline symbol and individual values are separated by comma. Note, values stored cannot contain comma or endline symbol. Note, that unlike "flights.csv" from the previous assignment this file has col- umn names as a first row. Column names are always in the same See "restaurants.csv" for an example. Implementation The program should run in a while loop. Each iteration the program should ask to enter the menu option (1-5). Depending on the menu option the program should do the following: 1. Show all the restaurants in a database in a human readable for- mat sorted by a Name column. 2. Show all the restaurants in a database in a human readable for- mat sorted by a Ranking column. 3. Find restaurant by a Name using a binary search. 4. Read a price range then find the restaurant with the highest Ranking that has a given price range. For example, if the user inputs "$$" you should find restaurant with a name "Viana. The result will be the same if user enters "$$$". In another words, "$$ - $$$ means that restaurant has dishes with prices "$$" and "$$$. Note, that it is the highest ranking restaurant with price range $$ for city Barcelona. But it should work for other cities as well (".csv" files with a different content). 5. Exit the loop. The program should contain at least 7 functions (excluding main). 1. "readRestaurants". Given the file input stream, and empty array of restaurants, read the .csv file with restaurants and return a size of an array 2. "printRestaurants. Given the array of restaurants and its size it shows them in a human-readable format. 3. sort RestaurantsByRanking. Sorts an array of restaurants by Ranking column. 4. sort RestaurantsByName". Sorts an array of restaurants by Name column. 5. "binary SearchName". Given an array of restaurants sorted by Name, its size and a target name find a restaurant with a given name using binary search. Return its index or -1 if it's not found. 6. "isInPriceRange". Given the restaurant price range as a string and a target price (was entered by the user) computes whether a restaurant price range contains a target price. You can assume that a restaurant price range is "$", "$$ - $$$" or "$$$$". When a target price range is "$", "$$", "$$$" or "$$$$. 7. "find Best Restaurant WithPrice". Given the array of restaurants, its size and a target price previously entered by user, find the best restaurant (highest Ranking) with target price in its price range. Return the index of a the found restaurant or -1 if there are no restaurant with a target price. The easiest way to do it is to do a linear search in the array of restaurants sorted by Ranking. Note, you should use isInPriceRange" inside this function. IMPORTANT: don't use cin or cout inside functions that doesn't have read or print in a name (except for main() function). Do what function is supposed to do and print results outside. Note: you can assume that your input file has no more than 100 restaurants stored, so you can create an array of restaurants with 100 elements in it and then use only as many as you read from the file. Problem description Today you'll be working as a Trip Advisor software engineer. You'll help a user of your application to find a place to eat in Barcelona. The user is interested in restaurants sorted by (1) Name and (2) Ranking. Also, he/she wants to (3) find restaurants by a Name and (4) get the best restaurant with a given prices. You'll be given a ".csv" file in the following format: 5.0 Name City Santa Rita Experience Barcelona Uma Barcelona Spoonik Restaurant Barcelona Viana Barcelona Chaka Khan Barcelona The Box Barcelona Blavis Barcelona My Restaurant Barcelona Bodega Biarritz Barcelona Rudi of Pirate Cooking Barcelona Ranking Rating Price Range Reviews 6 $$$$ 329 5.0 $$$$ 792 5.0 $$$$ 408 5.0 $$ - $$$ 2707 8 $$ - $$$ 479 4.5 834 5.0 $$ - $$$ 643 5.0 $$ - $$$ 159 5.0 $$ - $$$ 1078 5.0 $$ - $$$ 110 5.0 Column descriptions: Name: Name of the restaurant. A string. City: Name of the city where the restaurant is located. A string. Ranking: Rank of the restaurant in a given city (e.g. restaurant with rank 1 is considered to be the best restaurant in a city it is located). An integer number. Rating: An average rating of the restaurant. A non-integer number. Price Range: Price range of the dishes. A string. Reviews: Number of review. An integer number. Reviews: Number of review. An integer number. This is a part of a database that you can find here. The database is stored in a CSV (comma-separated values) file. It's a file format used to store tabular data. Rows are separated with an endline symbol and individual values are separated by comma. Note, values stored cannot contain comma or endline symbol. Note, that unlike "flights.csv" from the previous assignment this file has col- umn names as a first row. Column names are always in the same See "restaurants.csv" for an example. Implementation The program should run in a while loop. Each iteration the program should ask to enter the menu option (1-5). Depending on the menu option the program should do the following: 1. Show all the restaurants in a database in a human readable for- mat sorted by a Name column. 2. Show all the restaurants in a database in a human readable for- mat sorted by a Ranking column. 3. Find restaurant by a Name using a binary search. 4. Read a price range then find the restaurant with the highest Ranking that has a given price range. For example, if the user inputs "$$" you should find restaurant with a name "Viana. The result will be the same if user enters "$$$". In another words, "$$ - $$$ means that restaurant has dishes with prices "$$" and "$$$. Note, that it is the highest ranking restaurant with price range $$ for city Barcelona. But it should work for other cities as well (".csv" files with a different content). 5. Exit the loop. The program should contain at least 7 functions (excluding main). 1. "readRestaurants". Given the file input stream, and empty array of restaurants, read the .csv file with restaurants and return a size of an array 2. "printRestaurants. Given the array of restaurants and its size it shows them in a human-readable format. 3. sort RestaurantsByRanking. Sorts an array of restaurants by Ranking column. 4. sort RestaurantsByName". Sorts an array of restaurants by Name column. 5. "binary SearchName". Given an array of restaurants sorted by Name, its size and a target name find a restaurant with a given name using binary search. Return its index or -1 if it's not found. 6. "isInPriceRange". Given the restaurant price range as a string and a target price (was entered by the user) computes whether a restaurant price range contains a target price. You can assume that a restaurant price range is "$", "$$ - $$$" or "$$$$". When a target price range is "$", "$$", "$$$" or "$$$$. 7. "find Best Restaurant WithPrice". Given the array of restaurants, its size and a target price previously entered by user, find the best restaurant (highest Ranking) with target price in its price range. Return the index of a the found restaurant or -1 if there are no restaurant with a target price. The easiest way to do it is to do a linear search in the array of restaurants sorted by Ranking. Note, you should use isInPriceRange" inside this function. IMPORTANT: don't use cin or cout inside functions that doesn't have read or print in a name (except for main() function). Do what function is supposed to do and print results outside. Note: you can assume that your input file has no more than 100 restaurants stored, so you can create an array of restaurants with 100 elements in it and then use only as many as you read from the file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions