简体   繁体   English

C ++字母插入排序

[英]C++ Alphabetical Insertion Sort

We are doing a project involving storing and comparing various cities. 我们正在做一个涉及存储和比较各个城市的项目。 We have become stuck after adding a new city into the database, it goes to the bottom of the list - we want it to go into the database, sorted alphabetically. 在将新城市添加到数据库后,我们陷入了困境,它进入了列表的底部-我们希望它进入数据库,并按字母顺序排序。 As only one value can be added at a time, all the other entries will already be alphabetical. 由于一次只能添加一个值,因此所有其他条目将已经按字母顺序排列。

Below is the relevant code to this. 以下是与此相关的代码。

The problem area is the // Insertion Sort when adding // out bit. 问题区域是//添加时的//插入排序。

The error codes are: 错误代码为:

[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

All corresponding to the line 全部对应行

while (strcmp(cityTemp.fname, node[l]) < 0) && (l <= ct )

If you could point us in the right direction, that would be great. 如果您能指出正确的方向,那就太好了。 Thanks 谢谢

// -------------------------------------------------------------------------- //

#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

您不是要使用db[l] -变量-而不是node类型。

Azorath wrote: (strcmp(cityTemp.fname, node[i]) < 0) && (l <= ct ) Azorath写道:(strcmp(cityTemp.fname,node [i])<0)&&(l <= ct)

and after some give and take we've got, with some confusion between i, I and l.... 经过一番让步之后,我,我和我之间有些困惑。

(strcmp(cityTemp.fname, db[i]->getcity()) < 0) && (l??? <= ct ) (strcmp(cityTemp.fname,db [i]-> getcity())<0)&&(l ??? <= ct)

yes? 是? (how does Erik know that db[i] is in the code?) (Erik怎么知道db [i]在代码中?)

I'm taking a guess that cityTemp is an instance of node, not a pointer to node, and that db[] is an array of pointers to nodes, yes? 我猜测cityTemp是节点的实例,而不是指向节点的指针,而db []是指向节点的指针数组,是吗?

One thing wrong is "cityTemp.fname" - there is no "fname" member in the class. 错误的是“ cityTemp.fname”-类中没有“ fname”成员。 Its looking for a structure containing a member "fname". 它正在寻找包含成员“ fname”的结构。 Do you mean cityTemp.city? 您是指cityTemp.city吗?

Try that and trport what you get... 尝试一下,把你得到的东西运输出去...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM