2011-04-06 206 views
0

我们正在做一个涉及存储和比较各个城市的项目。在数据库中添加一个新的城市后,我们陷入了困境,它进入了列表的底部 - 我们希望它进入数据库,按字母顺序排序。由于一次只能添加一个值,所有其他条目将已按字母顺序排列。C++按字母顺序插入排序

下面是相关的代码。

问题领域是//插入时添加//排序位时排序。

的错误代码是:

[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 

回答

1

不要你的意思是使用db[l] - 变量 - 而不是node - 类型。

+0

当我尝试,我当时送四个错误消息指该行: '[BCC32错误] File1.cpp(250):E2294结构需要在左侧。或* [BCC32错误] File1.cpp(250):E2034无法将 '节点*' 到 '为const char *' [BCC32错误] File1.cpp(250):在参数E2342类型不匹配“ __s2'(想'const char *',得到'node *') [BCC32错误] File1.cpp(250):E2188表达式语法' – Azorath 2011-04-06 15:51:27

+0

使用'db [l] - > city' – Erik 2011-04-06 15:55:17

+0

谢谢。它有点更喜欢,我只有三个错误。 '需要在左边的结构。或'.',''node :: city'不可访问'和'表达式语法' – Azorath 2011-04-06 16:06:49

0

Azorath写道: (的strcmp(cityTemp.fname,节点[1])< 0)& &(升< = CT)

和后一些给予和接受我们已经有了,与之间一些混乱I,I和L ....

(的strcmp(cityTemp.fname,分贝[I] - > getcity())< 0)& &(升??? < = CT)

是? (Erik如何知道db [i]是否在代码中?)

我在猜测cityTemp是节点的一个实例,而不是指向节点的指针,而db []是一个指针数组到节点,是吗?

有一件事是错误的是“cityTemp.fname” - 类中没有“fname”成员。它寻找一个包含成员“fname”的结构。你的意思是cityTemp.city?

尝试这一点,trport你会得到什么?