在C+中的多个函数中使用输入变量+; 我刚刚开始从头开始学习C++,在作业中,我必须通过几个文件创建一个二次方程求解器:赋值1.CPP,它包含我的Mead()、Frase.CPP,其中包含每个函数和一个Mag文件。我很难把每件事都安排好
我必须从用户那里得到系数,然后将它们返回到一个包含三个字段的结构中,并在几个函数中使用它们输入的信息来解方程 我的编程到处都是,我用coeff输入消除了大部分错误;在每个函数和input.variable_name_中,此处为a、b和c 此外,他希望我们使用参数 我的计划还没完成一半 以下是我得到的:在C+中的多个函数中使用输入变量+; 我刚刚开始从头开始学习C++,在作业中,我必须通过几个文件创建一个二次方程求解器:赋值1.CPP,它包含我的Mead()、Frase.CPP,其中包含每个函数和一个Mag文件。我很难把每件事都安排好,c++,C++,我必须从用户那里得到系数,然后将它们返回到一个包含三个字段的结构中,并在几个函数中使用它们输入的信息来解方程 我的编程到处都是,我用coeff输入消除了大部分错误;在每个函数和input.variable_name_中,此处为a、b和c 此外,他希望我们使用参数 我的计划还没完成一半 以下是我得到的: //Program to solve quadratic equation #include <iostream> #include <cmath> using name
//Program to solve quadratic equation
#include <iostream>
#include <cmath>
using namespace std;
//Structure for coefficients to be used
struct coeff {
double a;
double b;
double c;
};
//Prototypes?
double readCoeffs(), equSolver(), discr(), outResults();
//Read coefficients from user
double readCoeffs() {
coeff input;
cout << "Enter coefficient a: ";
cin >> input.a;
cout << "Enter coefficient b: ";
cin >> input.b;
cout << "Enter coefficient c: ";
cin >> input.c;
}
//Solve quadratic equation from user's input
double equSolver() {
coeff input;
/*
need to somehow get the discrim variable from discr() to this function
discr();
*/
double solution1 = (pow(input.b, 2) + sqrt(discrim)) / (2 * input.a);
double solution2 = (pow(input.b, 2) - sqrt(discrim)) / (2 * input.a);
}
//Solves discriminent of quadratic equation
double discr() {
coeff input;
double discrim = pow(input.b, 2) - (4 * input.a * input.c);
}
/*
//Display on the screen results of the calculation
outResults() {
if (//root exists)
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << value << "; b: " << value << "; c: " << value << "\n" << endl;
cout << "has the following roots ";
cout << "Root1: " << value << "; Root2: " << value << "\n" << endl;
else
cout << "Quadratic equation with the following coefficients: ";
cout << "a: " << value << "; b: " << value << "; c: " << value << "\n" << endl;
}
*/
//求解二次方程的程序
#包括
#包括
使用名称空间std;
//要使用的系数的结构
结构系数{
双a;
双b;
双c;
};
//原型?
双readcoefs()、equalsolver()、discr()、outResults();
//从用户读取系数
双readcoefs(){
系数输入;
cout>input.a;
cout>input.b;
cout>input.c;
}
//从用户输入解二次方程
双方程求解器(){
系数输入;
/*
需要以某种方式将discrim变量从discr()获取到此函数
discr();
*/
双解1=(pow(输入b,2)+sqrt(discrim))/(2*输入a);
双解2=(pow(input.b,2)-sqrt(discrim))/(2*input.a);
}
//求解二次方程的判别法
双discr(){
系数输入;
双discrim=pow(输入b,2)-(4*输入a*输入c);
}
/*
//在屏幕上显示计算结果
outResults(){
如果(//根存在)
coutYourreadcoefs()
函数声明对象input
。这意味着input仅存在于函数中,因此您不能在其他地方使用它-函数完成时它不在范围内。此外,函数声明为double
返回类型,但不返回任何内容
考虑将参数作为对coeff
结构的引用,并将其声明为void
void readCoeffs(coeff &input)
{
cout << "enter...";
cin >> input.a;
cin >> input.b;
cin >> input.c;
}
我无法抗拒,因此我重写并重新组织了代码以解决问题。这将编译并执行您要求的操作。我还修复了二次公式,因为它应该是:
(-b+/-(b^2-4ac))/2a
你有
(b^2+/-(b^2-4ac))/2a
#包括
#包括
使用名称空间std;
//要使用的系数的结构
结构系数{
双a;
双b;
双c;
双溶胶1;
双溶胶2;
};
//声明
无效读取系数(系数和x);
无效均衡解算器(系数和x);
双discr(系数x);
结果无效(系数x);
//从用户读取系数
无效读取系数(系数和输入){
cout>input.a;
cout>input.b;
cout>input.c;
}
//从用户输入解二次方程
void equalsolver(系数和输入){
双解1=(-1*input.b)+sqrt(discr(input))/(2*input.a);
双解2=(-1*input.b)-sqrt(discr(input))/(2*input.a);
input.sol1=解决方案1;
input.sol2=解决方案2;
}
//求解二次方程的判别法
双discr(系数输入){
双d;
d=功率(输入b,2)-(4*输入a*输入c);
返回d;
}
无效输出结果(系数输入){
if(std::isnan(input.sol1)| std::isnan(input.sol2)){
当我试着说readcoefs是不完整的类型并且不允许时,coeff类型名称是不允许的,它需要a)和a;您是否也将原型更改为void并添加参数?如果void readcoefs();是实例化原型的正确方法,那么是的。它说明声明不兼容。我也在没有原型的情况下尝试过它。如果您也想传递参数(如果您想更改结构变量,则需要将其作为原型)
虽然如果您没有ReadCoefs的原型,但前提是您的结构是在函数之前定义的,而函数是在使用之前定义的,这不重要……我一定会看一看。感谢您花时间提供帮助!关于“关闭”的问题,如果descr
返回负数,可能会出现问题r、 由于equalsolver
不知道复数?(虽然这可能是你的isnan(…)
部分处理的问题?)该公式已关闭,我现在修复了它。如果你真的能问个问题,可能会有所帮助。尽量精确,例如。,“编译器抱怨说,discr
没有返回值,为什么它不选择最后计算的值?”——只需稍加训练和努力,您就可以比其他人更快地帮助自己。
double discr(coeff &input)
{
double discrim = pow(input.b, 2) - (4 * input.a * input.c);
return discrim;
}
#include <iostream>
#include <cmath>
using namespace std;
//Structure for coefficients to be used
struct coeff {
double a;
double b;
double c;
double sol1;
double sol2;
};
//Declarations
void readCoeffs(coeff &x);
void equSolver(coeff &x);
double discr(coeff x);
void outResults(coeff x);
//Read coefficients from user
void readCoeffs(coeff &input) {
cout << "Enter coefficient a: ";
cin >> input.a;
cout << "Enter coefficient b: ";
cin >> input.b;
cout << "Enter coefficient c: ";
cin >> input.c;
}
//Solve quadratic equation from user's input
void equSolver(coeff &input) {
double solution1 = ((-1 * input.b) + sqrt(discr(input))) / (2 * input.a);
double solution2 = ((-1 * input.b) - sqrt(discr(input))) / (2 * input.a);
input.sol1 = solution1;
input.sol2 = solution2;
}
//Solves discriminent of quadratic equation
double discr(coeff input) {
double d;
d = pow(input.b, 2) - (4 * input.a * input.c);
return d;
}
void outResults(coeff input) {
if( std::isnan(input.sol1) || std::isnan(input.sol2)) {
cout << "Quadratic equation with the following coefficients could not be solved: \n";
cout << "a: " << input.a << "; b: " << input.b << "; c: " << input.c << "\n" << endl;
} else {
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << input.a << "; b: " << input.b << "; c: " << input.c << "\n" << endl;
cout << "has the following roots ";
cout << "Root1: " << input.sol1 << "; Root2: " << input.sol2 << "\n" << endl;
}
}
int main() {
coeff Coeff;
readCoeffs(Coeff);
equSolver(Coeff);
outResults(Coeff);
}