Sorting Complex Numbers c++

拥有回忆 提交于 2019-12-13 09:55:29

问题


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;
}

回答1:


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)\n"
                           "(1.0,1.2)\n"
                           "(-2, 0)\n"
                           "(0, 2)\n");
    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 << '\n';
}

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



来源:https://stackoverflow.com/questions/28289847/sorting-complex-numbers-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!