简体   繁体   English

替代strcmp()按字母顺序对字符串进行排序

[英]Alternative to strcmp() to alphabetically sort strings

stuType Class: stuType类别:

#include<iostream>
#include<cstring>

using namespace std;

#ifndef STUTYPE
#define STUTYPE

class stuType {

   private:

   string fname;
   string lname;
   string social;
   float gpa;

   public:

      stuType(void) {

         fname = "no_fname";
         lname = "no_lname";
         social = "no_social";
         gpa = 0.0;
      }

      stuType(string fname_in, string lname_in, string social_in, float gpa_in) {

         fname = fname_in;
         lname = lname_in;
         social = social_in;
         gpa = gpa_in;
      }

     ~stuType() {
        //Nothing needs to be added here.
     }

     void set_fname(string new_fname) {
        fname = new_fname;
     }

     void set_lname(string new_lname) {
        lname = new_lname;
     }

     void set_ssn(string new_ssn) {
        social = new_ssn;
     }

     void set_gpa(float new_gpa) {
        gpa = new_gpa;
     }

     string get_fname(void) {
        return fname;
     }

     string get_lname(void) {
        return lname;
     }

     string get_ssn(void) {
        return social;
     }

     float get_gpa(void) {
        return gpa;
     }

     friend istream & operator>>(istream &in, stuType &stu) {
        in>>stu.fname;
        in>>stu.lname;
        in>>stu.social;
        in>>stu.gpa;

        return in;
     }

};

#endif

Sort.cpp: Sort.cpp:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>

#include"stuType.h"

using namespace std;

/*Loads the elements of the object instance with data from the input file.*/
void load(istream &input, stuType Student[], int *size);

/*Used in combination with the shellSort method to exchange the values of two variables in the class object.*/
void exchange(stuType &a, stuType &b);

/*Sorts the objects in ascending order by comparing the values of the lname strings between object indices.*/
void shellSort(stuType Student[], int size);

int main() {

   stuType Student[10];

   int size;

   char inputFile[200];
   char outputFile[200];

   ifstream input;
   ofstream output;

   cout<<"[INPUT_FILE]: ";
   cin>>inputFile;

   cout<<"[OUTPUT_FILE]: ";
   cin>>outputFile;

   input.open(inputFile);
   output.open(outputFile);

   if (input.fail()) {
      cerr<<"\n[FILE] Error opening '"<<inputFile<<"'"<<endl;
      exit(1);
   }

   if (output.fail()) {
      cerr<<"\n[FILE] Error opening '"<<outputFile<<"'"<<endl;
      exit(1);
   }

   load(input, Student, &size);
   shellSort(Student, size);

   return 0;
}

void load(istream &input, stuType Student[], int *size) {

   int length = 0, i = 0;

   float gpa;
   string social;
   string fname;
   string lname;

   while(input >> social >> fname >> lname >> gpa) {
      cout<<"[Node::Load] Setting 'social' for index ["<<i<<"] to "<<social<<endl;
      Student[i].set_ssn(social);
      cout<<"[Node::Load] Setting 'fname' for index ["<<i<<"] to "<<fname<<endl; 
      Student[i].set_fname(fname);
      cout<<"[Node::Load] Setting 'lname' for index ["<<i<<"] to "<<lname<<endl;
      Student[i].set_lname(lname);
      cout<<"[Node::Load] Setting 'gpa' for index ["<<i<<"] to "<<gpa<<endl;
      Student[i].set_gpa(gpa);
      cout<<"[Node::Load] Incrementing 'length'..."<<endl;
      length++;
      cout<<"[Node::Load] Incrementing 'i'..."<<endl;
      i++;
   }

   cout<<"==================================="<<endl;
   for (int i = 0; i<length; i++) {
      cout<<"[ENTRY] Index: "<<i<<" | SSN: "<<Student[i].get_ssn()<<" | fname: "<<Student[i].get_fname()<<" | lname: "<<Student[i].get_lname()<<" | gpa: "<<Student[i].get_gpa()<<endl;
   }
   cout<<"==================================="<<endl;

   *size = length;
}

void exchange(stuType &a, stuType &b) {

   stuType *temp;

   *temp = a;
   a = b;
   b = *temp;

   delete temp;
}

void shellSort(stuType Student[], int size) {

   int gap = size/2;
   bool passOK;

   while(gap>0) {
      passOK = true;

      for(int i = 0; i<size-gap; i++) {
            if (strcmp(Student[i].get_lname(), Student[i+gap].get_lname)>0) {
               cout<<"[Node::Sort] Exchanging Index ["<<i<<"] with Index ["<<i+gap<<"]..."<<endl;
               exchange(Student[i], Student[i+gap]);
               passOK = false;
            } else if (strcmp(Student[i].get_lname(), Student[i+gap].get_lname())==0) {
               if (strcmp(Student[i].get_fname(), Student[i+gap].get_fname())>0) {
                  cout<<"[Node::Sort] Exchanging Index ["<<i<<"] with Index ["<<i+gap<<"]..."<<endl;
                  exchange(Student[i], Student[i+gap]);
                  passOK = false;
                }
            }
        }

        if (passOK) {
            gap /= 2;
        }
    }
}

strcmp() expects to receive a character array to do the comparison, but since I am using strings, I cannot do that. strcmp()希望接收一个字符数组来进行比较,但是由于我使用的是字符串,所以我不能这样做。 What is an alternative? 有什么选择? The variable 'lname' needs to be compared and should return true if Student[i].get_lname() is greater than Student[i+gap].get_lname(). 需要比较变量“ lname”,如果Student [i] .get_lname()大于Student [i + gap] .get_lname(),则应返回true。 The exchange function will then be called and exchange the values of the object's local variables. 然后将调用交换函数并交换对象的局部变量的值。 The objects should be sorted in ascending order based on the value of the 'lname' variable and the 'fname' variable should only be referenced if the two 'lname's being compared are the same. 应该根据'lname'变量的值以升序对对象进行排序,并且仅当两个被比较的'lname'相同时才引用'fname'变量。

C++ strings provide implementations of operators < and > , so you can use them instead of strcmp : C ++字符串提供了运算符<> ,因此您可以使用它们代替strcmp

std::string a = "hello";
std::string b = "world";
if (a < b) {
    cout << a << " is less than " << b << endl;
}

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

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