我会尽量保持简短。这是我的任务(太大了,无法复制):
[练习图片1] [2] [练习图片2] [1]
这么长的故事,我已经创建了一个新的数据样式,它将包含3个值(a,b和c),这些必须放入一个等式中,然后它将计算根并显示它。 我还没有完成,但我在排序值时遇到了一些问题(我们必须这样做)
答案 0 :(得分:0)
根据我的评论:
我相信如果你将pass初始化为0,将i初始化为(pass + 1),用V [pass]替换所有V [i],用V [i]替换所有V [i + 1],你的代码应该按预期工作
void swap(Quadratic_equation arr[], int lowerIndex, int higherIndex) {
Quadratic_equation temp = arr[lowerIndex];
arr[lowerIndex] = arr[higherIndex];
arr[higherIndex] = temp;
}
void sort_equation_array(Quadratic_equation V[], int howMany)
{
for ( int pass = 0; pass < howMany; pass++ )
{
for (int i = (pass + 1); i <= howMany -1; i++)
{
if (V[pass].a > V[i].a)
{
swap(V, pass, i);
}
else if (V[pass].a == V[i].a &&
V[pass].b > V[i].b)
{
swap(V, pass, i);
}
else if (V[pass].a == V[i].a &&
V[pass].b == V[i].b &&
V[pass].c > V[i].c)
{
swap(V, pass, i);
}
}
}
}
您还应该清理输入,以便更容易分辨哪些值在哪里。这就是我所做的,你可以改变它来说出你喜欢的,但你明白了。
void get_quadratic_equation(Quadratic_equation &e)
{
cout << "a: ";
cin >> e.a;
cout << "b: ";
cin >> e.b;
cout << "c: ";
cin >> e.c;
}
int load_equation(Quadratic_equation V[], int n)
{
Quadratic_equation e;
int counter = 0;
cout << endl << "Set values for equation " << counter << endl;
get_quadratic_equation(e);
while (e.a != 0)
{
V[counter] = e;
++counter;
if (counter == n) break;
cout << endl << "Set values for equation " << counter << endl;
get_quadratic_equation(e);
}
cout << endl;
return counter;
}
** Enter Quadratic Equations ** Set values for equation 0 a: 1 b: 2 c: 3 Set values for equation 1 a: 4 b: 5 c: 6 Set values for equation 2 a: 7 b: 8 c: 9 Set values for equation 3 a: 8 b: 7 c: 6 Set values for equation 4 a: 5 b: 4 c: 3 Set values for equation 5 a: 2 b: 1 c: 0 Set values for equation 6 a: 0 b: 0 c: 0 ** NOT SORTED ** 1x^2 + 2x + 3 root 1 = -1.00+i 1.41 root 2 = -1.00-i 1.41 4x^2 + 5x + 6 root 1 = 0.00+i 1.05 root 2 = 0.00-i 1.05 7x^2 + 8x + 9 root 1 = 0.00+i 0.98 root 2 = 0.00-i 0.98 8x^2 + 7x + 6 root 1 = 0.00+i 0.75 root 2 = 0.00-i 0.75 5x^2 + 4x + 3 root 1 = 0.00+i 0.66 root 2 = 0.00-i 0.66 2x^2 + 1x + 0 root 1 = 0.00 root 2 = -0.50 ** SORTED BY A ** 1x^2 + 2x + 3 root 1 = -1.00+i 1.41 root 2 = -1.00-i 1.41 2x^2 + 1x + 0 root 1 = 0.00 root 2 = -0.50 4x^2 + 5x + 6 root 1 = 0.00+i 1.05 root 2 = 0.00-i 1.05 5x^2 + 4x + 3 root 1 = 0.00+i 0.66 root 2 = 0.00-i 0.66 7x^2 + 8x + 9 root 1 = 0.00+i 0.98 root 2 = 0.00-i 0.98 8x^2 + 7x + 6 root 1 = 0.00+i 0.75 root 2 = 0.00-i 0.75