2011-04-06 173 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你會得到什麼?