Разработать схему алгоритма для подсчёта количества отрицательных чисел среди целых чисел а,в,с.Протестировать алгоритм для всех возможных случаев (когда количество отрицательных чисел равно 0,1,2,3,)
Помогите кто понимает))

/* Язык C++11. Здесь решение вместе с юнит-тестами  */

#include
#include
#include

int count_negatives(int a, int b, int c)
{
  return (a<0 ? 1:0) + (b<0 ? 1:0) + (c<0 ? 1:0)
}

void solution(std::istream &input = std::cin, std::ostream &output)
{
  int a, b, c;
  input >> a >> b >> c;
  output << count_negatives(a, b, c) << std::endl;
}


void checkTest(std::string input_data, std::string correct_answer)
{
  std::istringstream input(input_data);
  std::istringstream correct_answer_stream(correct_answer);
  std::stringstream algorithm_answer_stream;
  
  int correct_value, algorithm_value;
  correct_answer_stream >> correct_value;
  
  solution(input, algorithm_answer_stream);
  algorithm_answer_stream >> algorithm_value;
  
  if (correct_value != algorithm_value) {
    std::cerr << "Input: " << input_data << std::endl;
    std::cerr << "Correct: " << correct_value << std::endl;
    std::cerr << "Algorithm: " << algorithm_value << std::endl;
    throw std::runtime_error("Test failed");
  }
}

void runTests()
{
  checkTest("1 2 3", "0");
  checkTest("-1 2 3", "1");
  checkTest("1 -2 3", "1");
  checkTest("1 2 -3", "1");
  checkTest("-1 -2 3", "2");
  checkTest("-1 2 -3", "2");
  checkTest("1 -2 -3", "2");
  checkTest("-1 -2 -3", "3");
}

#ifdef __DEBUG
int main(int argc, const char *argv[])
{
  runTests();
  return 0;
}

#else
int main(int argc, const char *argv[])
{
  solution();
  return 0;
}

#endif

Оцени ответ
Подпишись на наш канал в телеграм. Там мы даём ещё больше полезной информации для школьников!

Загрузить картинку
×