Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

Sorting Complex Numbers c++

I am attempting to take a complex number entered by a user (3-8i or -1+5i) and then sort it in ascending order in the same format as above by real number then imaginary if the same.

I start by asking the user to enter a complex number or ctr-d to terminate the program and then sort. I then want to convert the string to a float. Once a float I want to quick sort it. Is this a proper way of doing this? I know I have a lot of errors.

void QuickSort(vector <float> &vec)
{
  quick_sort(vec, 0, vec.size() - 1);
}

void quick_sort (vector<float> &vec, float left, float right){
  float pivot, ltemp, rtemp;
  ltemp = left;
  rtemp = right;
  pivot = vec;

  while (left < right){
    while ((vec >= pivot) && (left < right)){
      right--;
    }
    if (left != right){
      vec = vec;
      left++;
    }
    while ((vec <=pivot) && (left < right)) {
      left++;
    }
    if (left != right){
      vec = vec;
      right--;
    }

    vec = pivot;
    pivot = left;
    left = ltemp;
    right = rtemp;

    if (left < pivot){
      quick_sort(vec, left, pivot -1);
    }
    if (left != right){
      vec = vec;
      left++;
    }
    while ((vec <=pivot) && (left < right)) {
      left++;
    }
    if (left != right){
      vec = vec;
      right--;
    }

    vec = pivot;
    pivot = left;
    left = ltemp;
    right = rtemp;

    if (left < pivot){
      quick_sort(vec, left, pivot -1);
    }
    if (right > pivot){
      quick_sort(vec, pivot +1, right);
    }

}

int main(){
  string user;

  vector <float> V;

  for (int y = 0; y < 5; y++)
  {
      cout << "Enter a complex number:" << endl;
      cin >> user >> test1;
      float usr = atof(user.c_str());
      V.push_back(usr);
  }

  QuickSort(V);

  for (int z = 0; z < V.size(); z++)
    cout << V[z] << endl;

  return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an example of how to parse some complex numbers then sort them using the C++ Standard Library. If you want to as a learning exercise, you can replace one part at a time - e.g. introduce your own type instead of std::complex, read some other input format (rather than parenthesised comma-separated numbers sans a trailing "i"), and/or use your own sort.

#include <iostream>
#include <complex>
#include <vector>
#include <sstream>
#include <algorithm>

int main()
{
    std::istringstream iss("(1.0,-2.3)
"
                           "(1.0,1.2)
"
                           "(-2, 0)
"
                           "(0, 2)
");
    typedef std::complex<double> Complex;
    std::vector<Complex> nums;
    Complex c;
    while (iss >> c)
        nums.push_back(c);
    std::sort(std::begin(nums), std::end(nums),
              [](Complex x, Complex y)
              {
                  return x.real() < y.real() ||
                         x.real() == y.real() && x.imag() < y.imag();
              });
    for (auto& c : nums)
        std::cout << c << '
';
}

Output:

(-2,0)
(0,2)
(1,-2.3)
(1,1.2)

Note: I've just used a std::istringstream so the input can be hard-coded in the program for easy testing: just change to while (std::cin >> c) if you want to read from standard input (keyboard by default).

You can see the code running here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...