อยากให้ชข่วยดูให้หน่อยค่ะว่าผิดตรงไหน รันได้ไม่หมดค่ะ แต่ไม่ขึ้น error น่ะคะ
#include<iostream>
#define maxcity 50
using namespace std;
//Lin6klist
//structure of a city in list
struct City
{
string cityName; //name of city
int x; // X coordinate of city
int y; // Y coordinate of city
};
//structure of city node in list
struct CityNode
{
CityNode *nextCity;
City *cityinfo;
};
//class definition of city database
class CityDB
{
private:
CityNode *headCity; //pointer for head city
public:
CityDB()
{
headCity = NULL;
}
void insertCity(City* city)
{
cout<<"casjdaisld";
//if list empty
if(headCity == NULL)
{ cout<<"checka";
headCity->cityinfo = city;
cout<<"checka";
}
else
{
headCity->nextCity = headCity;
headCity->cityinfo = city;
cout<<"checkb";
}
}
void delCityByName(string cityName)
{
if(headCity == NULL)
{
cout << "Underflow" <<endl;
}
CityNode *currCity, *prevCity;
currCity = headCity;
prevCity = NULL;
while (currCity != NULL)
{
if(cityName.compare(currCity->cityinfo->cityName) == 0)
{
break;
}
prevCity = currCity;
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout << "Does not exist." <<endl;
}
else
{
prevCity->nextCity = currCity->nextCity;
delete currCity;
}
}
void delCityByCoor(int x , int y)
{
if(headCity == NULL)
{
cout << "Underflow" << endl;
}
CityNode *currCity, *prevCity;
currCity = headCity;
prevCity = NULL;
while(currCity != NULL)
{
if(currCity->cityinfo->x == x && currCity->cityinfo->y == y)
{
break;
}
prevCity = currCity;
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout << "Does not exist."<<endl;
}
else
{
prevCity->nextCity = currCity->nextCity;
delete currCity;
}
}
void searchCityByName(string cityName)
{
if(headCity == NULL)
{
cerr << "Empty! Nothing to search!" <<endl;
}
CityNode *currCity;
currCity = headCity;
while (currCity != NULL)
{
if(cityName.compare(currCity->cityinfo->cityName)==0)
{
cout <<"City found:"<<cityName <<endl;
}
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout <<"City not found: "<<cityName<<endl;
}
}
void searchCityByCoor(int x, int y)
{
if(headCity == NULL)
{
cerr << "Empty! Nothing to search!" <<endl;
}
CityNode *currCity;
currCity = headCity;
while (currCity != NULL)
{
if(currCity->cityinfo->x == x && currCity->cityinfo->y == y)
{
cout <<"City found:"<<endl;
}
currCity = currCity -> nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout <<"City not found: "<<endl;
}
}
bool run()
{
int x, y, n , choice;
string cityName;
bool start = true;
cout<<"->>";
cin>>choice;
if(choice < 0 || choice >7)
{
cout<<"Invalid input, please enter again"<<endl;
cout<<"->>";
cin>>choice;
}
switch(choice)
{
case 1:
cout<<"How many cities do you want to store: ";
cin>>n;
for(int i = 0; i < n; i++)
{
City* city = new City[n];
cout<<"Enter city " <<i+1<< " name :";
cin>>cityName;
cout<<"Enter coordinate X and Y in order: ";
cin>>x>>y;
cout<<endl;
city.cityName = cityName;
city.x = x;
city.y = y;
insertCity(city);
}
break;
case 2:
cout<<"Enter name of the city you want to delete: ";
cin>>cityName;
delCityByName(cityName);
break;
case 3:
cout<<"Enter coordinate of the city you want to delete: ";
cin>>x>>y;
delCityByCoor(x,y);
break;
case 4:
cout<<"Enter name of the city you want to search: ";
cin>>cityName;
searchCityByName(cityName);
break;
case 5:
cout<<"Enter coordinate of the city you want to search: ";
cin>>x>>y;
searchCityByCoor(x,y);
break;
case 6:
start = false;
break;
}
return start;
}
};
int main(){
CityDB citydb;
int n,x,y;
string cityName;
cout<<"--Press 1 to insert"<<endl;
cout<<"--Press 2 to delete by name"<<endl;
cout<<"--Press 3 to delete by coordinate"<<endl;
cout<<"--Press 4 to search by name"<<endl;
cout<<"--Press 5 to search by coordinate"<<endl;
cout<<"--Press 6 to end program"<<endl;
cout<<"________________________________________________"<<endl;
while(citydb.run());
return 0;
}
แก้ไขโค้ดให้หน่อยค่ะ c++
#include<iostream>
#define maxcity 50
using namespace std;
//Lin6klist
//structure of a city in list
struct City
{
string cityName; //name of city
int x; // X coordinate of city
int y; // Y coordinate of city
};
//structure of city node in list
struct CityNode
{
CityNode *nextCity;
City *cityinfo;
};
//class definition of city database
class CityDB
{
private:
CityNode *headCity; //pointer for head city
public:
CityDB()
{
headCity = NULL;
}
void insertCity(City* city)
{
cout<<"casjdaisld";
//if list empty
if(headCity == NULL)
{ cout<<"checka";
headCity->cityinfo = city;
cout<<"checka";
}
else
{
headCity->nextCity = headCity;
headCity->cityinfo = city;
cout<<"checkb";
}
}
void delCityByName(string cityName)
{
if(headCity == NULL)
{
cout << "Underflow" <<endl;
}
CityNode *currCity, *prevCity;
currCity = headCity;
prevCity = NULL;
while (currCity != NULL)
{
if(cityName.compare(currCity->cityinfo->cityName) == 0)
{
break;
}
prevCity = currCity;
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout << "Does not exist." <<endl;
}
else
{
prevCity->nextCity = currCity->nextCity;
delete currCity;
}
}
void delCityByCoor(int x , int y)
{
if(headCity == NULL)
{
cout << "Underflow" << endl;
}
CityNode *currCity, *prevCity;
currCity = headCity;
prevCity = NULL;
while(currCity != NULL)
{
if(currCity->cityinfo->x == x && currCity->cityinfo->y == y)
{
break;
}
prevCity = currCity;
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout << "Does not exist."<<endl;
}
else
{
prevCity->nextCity = currCity->nextCity;
delete currCity;
}
}
void searchCityByName(string cityName)
{
if(headCity == NULL)
{
cerr << "Empty! Nothing to search!" <<endl;
}
CityNode *currCity;
currCity = headCity;
while (currCity != NULL)
{
if(cityName.compare(currCity->cityinfo->cityName)==0)
{
cout <<"City found:"<<cityName <<endl;
}
currCity = currCity->nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout <<"City not found: "<<cityName<<endl;
}
}
void searchCityByCoor(int x, int y)
{
if(headCity == NULL)
{
cerr << "Empty! Nothing to search!" <<endl;
}
CityNode *currCity;
currCity = headCity;
while (currCity != NULL)
{
if(currCity->cityinfo->x == x && currCity->cityinfo->y == y)
{
cout <<"City found:"<<endl;
}
currCity = currCity -> nextCity;
}
//city to be deleted not found
if(currCity == NULL)
{
cout <<"City not found: "<<endl;
}
}
bool run()
{
int x, y, n , choice;
string cityName;
bool start = true;
cout<<"->>";
cin>>choice;
if(choice < 0 || choice >7)
{
cout<<"Invalid input, please enter again"<<endl;
cout<<"->>";
cin>>choice;
}
switch(choice)
{
case 1:
cout<<"How many cities do you want to store: ";
cin>>n;
for(int i = 0; i < n; i++)
{
City* city = new City[n];
cout<<"Enter city " <<i+1<< " name :";
cin>>cityName;
cout<<"Enter coordinate X and Y in order: ";
cin>>x>>y;
cout<<endl;
city.cityName = cityName;
city.x = x;
city.y = y;
insertCity(city);
}
break;
case 2:
cout<<"Enter name of the city you want to delete: ";
cin>>cityName;
delCityByName(cityName);
break;
case 3:
cout<<"Enter coordinate of the city you want to delete: ";
cin>>x>>y;
delCityByCoor(x,y);
break;
case 4:
cout<<"Enter name of the city you want to search: ";
cin>>cityName;
searchCityByName(cityName);
break;
case 5:
cout<<"Enter coordinate of the city you want to search: ";
cin>>x>>y;
searchCityByCoor(x,y);
break;
case 6:
start = false;
break;
}
return start;
}
};
int main(){
CityDB citydb;
int n,x,y;
string cityName;
cout<<"--Press 1 to insert"<<endl;
cout<<"--Press 2 to delete by name"<<endl;
cout<<"--Press 3 to delete by coordinate"<<endl;
cout<<"--Press 4 to search by name"<<endl;
cout<<"--Press 5 to search by coordinate"<<endl;
cout<<"--Press 6 to end program"<<endl;
cout<<"________________________________________________"<<endl;
while(citydb.run());
return 0;
}