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

时间:2014-02-24 01:48:22

标签: c++

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:

#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()期望接收一个字符数组来进行比较,但由于我使用字符串,我不能这样做。有什么选择?需要比较变量'lname',如果Student [i] .get_lname()大于Student [i + gap] .get_lname(),则应该返回true。然后将调用exchange函数并交换对象的局部变量的值。对象应该根据'lname'变量的值按升序排序,只有当被比较的两个'lname'相同时,才应引用'fname'变量。

1 个答案:

答案 0 :(得分:1)

C ++字符串提供implementations of operators < and >,因此您可以使用它们代替strcmp

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