我用用户定义的函数编写了此代码,但似乎不起作用。我一直在努力找出错误的位置,持续几个小时。但是找不到任何东西。看起来问题出在传递参数。但是我不知道,我对此很陌生!
#include <iostream>
#include <cmath>
using namespace std;
double solutionFun (double a, double b, double c) {
double delta, solution1, solution2;
delta = b*b - 4*a*c;
if (delta > 0 ){
solution1 = (-b + sqrt(delta)) / (2*a);
solution2 = (-b - sqrt(delta)) / (2*a);
cout << "There are 2 solutions." << endl;
cout << "The solutions are:";
return solution1, solution2;
}
else if (delta == 0){
solution1 = (-b) / (2*a);
cout << "There is 1 solution." << endl;
cout << "The solution is:";
return solution1;
}
else {
cout << "There is no solution.";
return 0;
}
}
int main(){
double a ,b ,c;
cout << "Please enter the values of a, b, and c respectively:";
cin >> a ,b ,c;
solutionFun(a ,b ,c);
return 0;
}
答案 0 :(得分:2)
此代码不是您获得多个输入的方式:
cin >> a ,b ,c;
您要:
cin >> a >> b >> c;
此代码不是您显示答案的方式:
cout << "The solutions are:";
return solution1, solution2;
您反而想要:
cout << "The solutions are:" << solution1 << " and also " << solution2;
答案 1 :(得分:2)
有关代码有效性和所需行为的一些问题(不包括编码实践/设计)。首先,了解如何从solutionFun()
返回多个值(当前定义为返回double) by using
std :: vector -- even though you are not using anything returned in this piece of code. Second, you didn't print (
cout << {) the solution values themselves, and it seemed like you were going for it. Third, see how to use
std :: cin`中的多个输入一行代码。固定版本是这样:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
std::vector<double> solutionFun (double a, double b, double c) {
double delta, solution1, solution2;
delta = b*b - 4*a*c;
if (delta > 0 ){
solution1 = (-b + sqrt(delta)) / (2*a);
solution2 = (-b - sqrt(delta)) / (2*a);
cout << "There are 2 solutions." << endl;
cout << "The solutions are: " << solution1 << " and " << solution2;
return {solution1, solution2};
}
else if (delta == 0){
solution1 = (-b) / (2*a);
cout << "There is 1 solution." << endl;
cout << "The solution is: " << solution1;
return {solution1};
}
else {
cout << "There is no solution.";
return {};
}
}
int main(){
double a ,b ,c;
cout << "Please enter the values of a, b, and c respectively:";
cin >> a >> b >> c;
auto result = solutionFun(a ,b ,c);
for (auto scalar : result)
{
// Do something with a component, or don't return anything from the function : )
}
return 0;
}
答案 2 :(得分:0)
另一种方法是在命令行中传递a='<?xml version="1.0" encoding="utf-8"?><!DOCTYPE note SYSTEM "Note.dtd"><book><name>ABC</name></book>'
str1=""
start=a.index('<!DOCTYPE')
end=a.index('>',start)
str1=a[:start]
str1+='<!--'+'<!DOCTYPE note SYSTEM "Note.dtd">'+'-->'
str1+=a[end+1:]
str1 #'<?xml version="1.0" encoding="utf-8"?><!--<!DOCTYPE note SYSTEM "Note.dtd">--><book><name>ABC</name></book>'
变量:
solution
void solutionFun (double a, double b, double c,
double& solution1, double& solution2, size_t& solution_count) {
double delta;
delta = b*b - 4*a*c;
solution2 = 0.0;
if (delta > 0 ){
solution1 = (-b + sqrt(delta)) / (2*a);
solution2 = (-b - sqrt(delta)) / (2*a);
solution_count = 2;
cout << "There are 2 solutions." << endl;
cout << "The solutions are: " << solution1 << " and " << solution2;
return;
}
else if (delta == 0){
solution1 = (-b) / (2*a);
solution_count = 1;
cout << "There is 1 solution." << endl;
cout << "The solution is: " << solution1;
return;
}
else {
cout << "There is no solution.";
solution_count = 0;
return;
}
}
和solution1, solution2
通过引用传递 ,这意味着该函数可以修改调用者的变量。