C++ 使用指针的二次方程问题
我成功地编制了一个程序,在给定三个系数的情况下,找到标准形式二次方程的解。我现在试图简化结果,以便去掉底部的/2a。因此,简化功能 我已经开始创建简化函数,并尝试将解的虚部和实部除以2a。不知怎的,它给出了错误:C++ 使用指针的二次方程问题,c++,C++,我成功地编制了一个程序,在给定三个系数的情况下,找到标准形式二次方程的解。我现在试图简化结果,以便去掉底部的/2a。因此,简化功能 我已经开始创建简化函数,并尝试将解的虚部和实部除以2a。不知怎的,它给出了错误: "error:invalid operands of types 'int' and 'double *' to binary 'operator*'" on lines 105 and 106. 我怀疑这与作为参数传递的指针和引用有关。我对那个想法不熟悉 另外,顺便说一句,我从来没
"error:invalid operands of types 'int' and 'double *' to binary 'operator*'" on lines 105 and 106.
我怀疑这与作为参数传递的指针和引用有关。我对那个想法不熟悉
另外,顺便说一句,我从来没有真正看到过被使用过。这是允许的吗?我知道+=可以
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib> //simplify the answer
using namespace std;
int count = 0;
//prototyping
double ans_1(double, double, double);
double ans_2(double, double, double);
double nanhe(double, double, double);
void simplify(double*, double*, double*);
int main()
{
double a, b, c;
cout << "Quadratic Equation Solver \n";
cout << "Enter a value for a: ";
cin >> a;
cout << endl;
cout << "Enter a value for b: ";
cin >> b;
cout << endl;
cout << "Enter a value for c: ";
cin >> c;
cout << endl;
if (isnan(sqrt((b * b) - (4 * a * c)))) {
count++;
}
if (!count) {
double answer01 = ans_1(a, b, c);
double answer02 = ans_2(a, b, c);
cout << "X=" << answer01 << endl;
cout << "X=" << answer02 << endl;
}
else if (count) //could route these imag ones to separate funcitons instead of count++
{
double answer01 = ans_1(a, b, c);
double answer02 = ans_1(a, b, c);
cout << "X=(" << -b << "+" << answer01 << "i)/" << 2 * a << endl;
cout << "X=(" << -b << '-' << answer02 << "i)/" << 2 * a << endl;
}
system("pause");
}
double ans_1(double a, double b, double c)
{
double ans1;
double temp_c = sqrt((b * b) - (4 * a * c));
if (isnan(temp_c)) {
temp_c = nanhe(a, b, c);
}
if (!count) {
ans1 = ((-b + temp_c) / (2 * a));
}
else if (count) {
ans1 = ((temp_c));
}
simplify(&a, &b, &ans1);
return ans1;
}
double ans_2(double a, double b, double c)
{
double ans2;
double temp_d = sqrt((b * b) - (4 * a * c));
if (isnan(temp_d)) {
temp_d = nanhe(a, b, c);
}
if (!count) {
ans2 = ((-b - temp_d) / (2 * a));
}
else if (count) {
ans2 = (temp_d);
}
simplify(&a, &b, &ans2); //line under this should alter ans2 so its returning the simplified version instead, or just make a new variable instead of ans2
return ans2;
}
double nanhe(double a, double b, double c) //still need to apply simplify() to nanhe
{
double temp_help;
temp_help = sqrt(-1 * ((b * b) - (4 * a * c)));
count++;
return temp_help;
}
void simplify(double* a, double* b, double* ans) //only run if complex
{
ans /= (2 * a);
b /= (2 * a);
}
您需要访问或更改指针指向的值,而不是指针本身:
void simplify(double* a, double* b, double* ans) //only run if complex
{
*ans /= (2 * *a);
*b /= (2 * *a);
}
编辑:
或者,正如@Hans Passant所说:
并考虑C++允许您将其参数声明为“双”。 而不是双*。去买一个像样的IDE,这样你就没有 猜测语法错误的位置
在simplify函数中使用*。并且考虑C++允许您将其参数声明为double,而不是double *。然后去买一个像样的IDE,这样你就不必猜测语法错误在哪里了。double*a,double*b,double*ans仍然不起作用……关于你的+=和/=问题:x=y是x=xy的缩写,其中有有效的运算符。最常用的是:+=-=*=/=|=&=^=得到了它,但是,我试图更改一些行,但仍然得到相同的错误。。。