Квадратичная формула c ++ - пользовательская функция - PullRequest
0 голосов
/ 12 сентября 2018

Я написал этот код с пользовательской функцией, но, похоже, он не работает.Я пытаюсь выяснить, где ошибка часами.Но ничего не смог найти.похоже, проблема в передаче параметра.Но я не знаю, я довольно новичок в этом!

#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;
}         

Ответы [ 3 ]

0 голосов
/ 12 сентября 2018

Несколько проблем, касающихся правильности кода и желаемого поведения (практика программирования / дизайн в стороне). Сначала посмотрите, как вернуть несколько значений из вашего 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 << <code>) 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;
}
0 голосов
/ 12 сентября 2018

Другой метод - передать переменные 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 и solution_count передаются по ссылке , что означает, что функция можетизменить переменные вызывающего.

0 голосов
/ 12 сентября 2018

Этот код не предназначен для получения нескольких входов:

cin >> a ,b ,c;

Вместо этого вы хотите:

cin >> a >> b >> c;

Этот код не отображает ответы:

cout << "The solutions are:";
return solution1, solution2;

Вместо этого вы хотите:

cout << "The solutions are:" << solution1 << " and also " << solution2;
...