排序复杂数字c ++ [英] Sorting Complex Numbers c++
问题描述
我试图取一个用户输入的复数(3-8i或-1 + 5i),然后按照与上面相同的格式按实数升序排序,如果相同,则按虚数排序。
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.
我首先要求用户输入一个复数或ctr-d来终止程序,然后排序。然后我想将字符串转换为浮点。一旦浮动我想快速排序。这是一个正确的方法吗?我知道我有很多错误。
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;
}
推荐答案
解析一些复杂的数字,然后使用C ++标准库对它们排序。如果你想作为一个学习练习,你可以一次替换一个部分 - 例如。引入您自己的类型,而不是 std :: complex
,读取一些其他输入格式(而不是括号中逗号分隔的数字不包含尾随i),和/或使用您的自己排序。
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';
}
输出:
(-2,0)
(0,2)
(1,-2.3)
(1,1.2)
注意:我刚刚使用了 std :: istringstream
输入可以在程序中硬编码以方便测试:只要更改为 while(std :: cin>> c)
如果要从标准输入
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).
您可以在此处 a>
这篇关于排序复杂数字c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!