Мы делаем проект, который включает в себя хранение и сравнение разных городов. Мы застряли после добавления нового города в базу данных, он идет в конец списка - мы хотим, чтобы он вошел в базу данных, отсортированную по алфавиту. Поскольку за один раз можно добавить только одно значение, все остальные записи будут уже в алфавитном порядке.
Ниже приведен соответствующий код для этого.
Проблемной областью является // вставка сортировки при добавлении // выходного бита.
Коды ошибок:
[BCC32 Error] File1.cpp(250): E2294 Structure required on left side of . or .*
[BCC32 Error] File1.cpp(250): E2108 Improper use of typedef 'node'
[BCC32 Error] File1.cpp(250): E2188 Expression syntax
Все, что соответствует линии
while (strcmp(cityTemp.fname, node[l]) < 0) && (l <= ct )
Если бы вы могли указать нам правильное направление, это было бы здорово. Спасибо
// -------------------------------------------------------------------------- //
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <iomanip>
#define PI 3.14159265
using namespace std;
// -------------------------------------------------------------------------- //
class node
{
private:
char city[100];
char country[100];
float longitudeDegree;
float longitudeMinutes;
char eastWest[2];
float latitudeDegree;
float latitudeMinutes;
char northSouth[2];
//useful for link list!
node *next;
// -------------------------------------------------------------------------- //
public:
//Constructor for class node
// ctn = city node
// cyn = country node
// longD = longitude degree
// longM = longitude minutes
// ew = east / west
// latD = latitude distance
// latM = latitude minutes
// ns = north / south
node(char ctn[], char cyn[], float longD,
float longM, char ew[], float latD, float latM, char ns[])
{
//Copy char array ctn to class array city
//string copy is part of string header
strcpy(city, ctn); //copy to string city name
strcpy(country, cyn); //copy to string country name
longitudeDegree = longD;
longitudeMinutes = longM;
strcpy(eastWest, ew);
latitudeDegree = latD;
latitudeMinutes = latM;
strcpy(northSouth, ns);
cout << "Hello from node with name: " << city << endl;
}
// -------------------------------------------------------------------------- //
// Get function to return city stored in class //
char* getCity()
{
return city;
}
char* getCountry()
{
return country;
}
float getLongDe()
{
return longitudeDegree;
}
float getLongMin()
{
return longitudeMinutes;
}
char* getEastWest()
{
return eastWest;
}
float getLatDe()
{
return latitudeDegree;
}
float getLatMin()
{
return latitudeMinutes;
}
char* getNorthSouth()
{
return northSouth;
}
};
// -------------------------------------------------------------------------- //
class menu
{
private:
int fnum, mnum, ct, xx, fg, ans, ans2;
float longitudeDegree;
float longitudeMinutes;
float latitudeDegree;
float latitudeMinutes;
char country[100];
char eastWest[2];
char northSouth[2];
//array of pointers of type class node
node *db[200]; //denotes how many pointers to use for the amount of cities in database
char fname[200];
char pt[200];
char sfnum[200];
char yn[2]; //yes/no
public:
//constructor
menu()
{
//Read the serialized data file
ct= Readit();
}
// -------------------------------------------------------------------------- //
void start()
{
// Add a city //
case 1:
cout << "Enter the name of the city:";
cin.getline(fname,100);
fg=Findit(ct,fname);
if(fg>0)
{
cout << "This entry is already in the database: " << fname <<endl;
}
else
{
cout << "Enter the Country of " << fname << endl;
cin >> country;
//ans2 = '-1';
do
{
cout << "Enter the Longitude Degrees (0 - 180) of " << fname <<endl ;
cout << "Enter degrees between 0 - 180 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=180)));
longitudeDegree = ans2;
//ans2 = '-1';
do
{
cout << "Enter the Longitude Minutes (0 - 60) of " << fname << endl;
cout << "Enter minutes between 0 - 60 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=60)));
longitudeMinutes = ans2;
ans = 'Z'; //default to an answer not in while loop
do
{
cout << "East or West?\n";
cout << "You must type a capital 'E' or a capital 'W'.\n";
cin >> ans;
}
while((ans !='E')&&(ans !='W'));
eastWest[0] = ans;
eastWest[1] = '\0';
//ans2 = '-1';
do
{
cout << "Enter the Latitude Degree (0 - 90) of " << fname << endl;
cout << "Enter degrees between 0 - 90 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=180)));
latitudeDegree = ans2;
//ans2 = '-1';
do
{
cout << "Enter the Latitude Minutes (0 - 60) of " << fname << endl;
cout << "Enter minutes between 0 - 60 \n";
cin >> ans2;
}
while(!((ans2 >=0)&&(ans2 <=60)));
latitudeMinutes = ans2;
ans = 'Z'; //default to an answer not in while loop
do
{
cout << "North or South?\n";
cout << "You must type a capital 'N' or a capital 'S'.\n";
cin >> ans;
}
while((ans !='N')&&(ans !='S'));
northSouth[0] = ans;
northSouth[1] = '\0';
ct++;
db[ct]=new node(fname,country,longitudeDegree,longitudeMinutes,
eastWest,latitudeDegree,latitudeMinutes,northSouth);
/* // Insertion Sort when adding //
node *cityTemp;
cityTemp=db[ct];
//cityTemp = new node (fname, country, longitudeDegree, longitudeMinutes, eastWest, latitudeDegree, latitudeMinutes, northSouth);
int j, l;
// Find place to insert the new city //
// All other cities will already be in alphabetical order
l=0;
while (strcmp(cityTemp.fname, node[l]) < 0) && (l <= ct )
//strcmp(cityTemp.fname, node[l].fname)<0) && (l<=ct)
{
l++;
}
// Move down rest
for (j = l, j <= ct);
{
j++;
}
db[j+1] = db[j];
db[j] = cityTemp; */
}
break;
}
// -------------------------------------------------------------------------- //
// Function to convert string to lower case ascii //
char *strLower( char *str )
{
char *temp;
for ( temp = str; *temp; temp++ )
{
*temp = tolower( *temp );
}
return str;
}
// -------------------------------------------------------------------------- //
// Function to search through the names stored in nodes //
int Findit(int ctt,char fnamef[])
{
int nn=0;
int xx;
for(int k=1;k<=ctt;k++)
{
xx=strcmp(strLower(db[k]->getCity()),strLower(fnamef));
//xx is zero if names are the same
if(xx==0)
nn=k;
}
return nn;
}
// -------------------------------------------------------------------------- //
// Function to do serialization of nodes and store in a file //
void Storeit(int ctt)
{
fstream outfile;
outfile.open("cityList.txt",ios::out);
outfile<<ctt<<endl;
for(int k=1;k<=ctt;k++)
{
//outfile<<db[k]->getName()<<endl;
outfile<<db[k]->getCity()<<endl;
outfile<<db[k]->getCountry()<<endl;
outfile<<db[k]->getLongDe()<<endl;
outfile<<db[k]->getLongMin()<<endl;
outfile<<db[k]->getEastWest()<<endl;
outfile<<db[k]->getLatDe()<<endl;
outfile<<db[k]->getLatMin()<<endl;
outfile<<db[k]->getNorthSouth()<<endl;
}
outfile.close();
}
// -------------------------------------------------------------------------- //
int Readit()
// Function to open the file, read in the data and create the nodes
{
int ctx=0;
fstream infile;
infile.open("cityList.txt",ios::in);
//infile>>ctx;
infile.getline(sfnum,200);
ctx=atoi(sfnum);
/*
for(int k=1;k<=ctx;k++)
{
//infile>>fname;
infile.getline(fname,200);
//infile>>weight;
infile.getline(sfnum,200);
weight=atof(sfnum);
//infile>>height;
infile.getline(sfnum,200);
height=atof(sfnum);
db[k]=new node(fname,height,weight);
}
*/
// Read in file to variables i.e. longitudeDegree = atof(sfnum)
for(int k=1;k<=ctx;k++)
{
//infile>>fname;
infile.getline(fname,100);
//infile>>country ;
infile.getline(sfnum,100);
strcpy(country,sfnum);
//infile>>longitudeDegree;
infile.getline(sfnum,100);
longitudeDegree=atof(sfnum);
//infile>>longitudeMinutes;
infile.getline(sfnum,100);
longitudeMinutes=atof(sfnum);
//infile>>eastWest ;
infile.getline(sfnum,100);
strcpy(eastWest,sfnum);
//infile>>latitudeDegree;
infile.getline(sfnum,100);
latitudeDegree=atof(sfnum);
//infile>>latitudeMinutes;
infile.getline(sfnum,100);
latitudeMinutes=atof(sfnum);
//infile>>northSouth ;
infile.getline(sfnum,100);
strcpy(northSouth,sfnum);
db[k]=new node(fname,country, longitudeDegree,
longitudeMinutes, eastWest, latitudeDegree, latitudeMinutes, northSouth);
}
infile.close();
return ctx;
}
};
// End of class menu