Вот есть код для С++ Builder 12, который строит полилинию (фигуру) и определяет по…
- User
Вот есть код для С++ Builder 12, который строит полилинию (фигуру) и определяет по клику, находится ли точка клика внутри или снаружи фигуры. Нужно написать код юнитеста, который брал бы функции уже существующие и написанные в коде и проверял их на исходных данных, к которым результат уже известен другим методом. (пример - проверка площади окружности, зная координаты центра и радиуса нахождение площади, которую сравнивают с значением расчитанной другим способом этой же площади. Если отличается, то ложь). Результат теста должен выводиться в текстовый файл, который должен содержать: кол-во тестов всего, кол-во успешных и неуспешных, пометка о том, какие именно неуспешны. Все тексты ошибок должны быть на русском языке. Вот код: в Unit1.h: //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <Vcl.ExtCtrls.hpp> #include <Vcl.Dialogs.hpp> #include
struct triangle { double Vertex1, Vertex2, Vertex3; };
std::vector<std::vector
class CalculationNormal { public: double dx, dy, length, direction; void Calculation (double x1, double x2, double y1, double y2, double &normal_x, double &normal_y); };
//--------------------------------------------------------------------------- extern PACKAGE TForm1 Form1; //--------------------------------------------------------------------------- #endif а в Unit1.cpp : > Yuriy: //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <math.h> #include .dfm" TForm1 Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void CalculationNormal::Calculation(double x1, double x2, double y1, double y2, double &normal_x, double &normal_y) { dx = x2 - x1; dy = y2 - y1; length = sqrt(dx * dx + dy * dy); normal_x = - dy / length; normal_y = dx / length; direction = (x1 + x2) / 2 * normal_x + (y1 + y2) / 2 * normal_y; if (direction < 0) { normal_x = -normal_x; normal_y = -normal_y; } }
//--------------------------------------------------------------------------- bool intersect(const point& Point, const point& point1, const point& point2) { if ((point1.y > Point.y && point2.y <= Point.y) || (point2.y > Point.y && point1.y <= Point.y)) { double t = (Point.y - point1.y) / (point2.y - point1.y); if (t >= 0 && t <= 1) { double x_intersect = point1.x + t * (point2.x - point1.x); if (x_intersect >= Point.x) { return true; } } } return false; }
//---------------------------------------------------------------------------
Yuriy: bool isInside(const point& Point, const std::vector
//---------------------------------------------------------------------------
- Assistant
- User
На строчку std::ofstream testReport(TEST_REPORT_FILE); возникает ошибка [bcc32c Error] Unit1.h(223): implicit instantiation of undefined template 'std::basic_ofstream<char, std::char_traits >' iosfwd(537): template is declared here
- Assistant
Ошибка
codeimplicit instantiation of undefined template 'std::basic_ofstream<char, std::char_traits<char> >'означает, что компилятор
bcc32cне видит полного определения шаблонаstd::ofstream(ofstream — это typedef дляbasic_ofstream<char>), а только предварительное объявление (forward declaration). - User
Можно ли строчки с << переписать как для чисто С++ Builder, а не обычного компилятора С++ ? testReport << "Результаты юнит-тестов:\n\n";
codefor (int i = 0; i < testCount; i++) { totalTests++; std::string error; bool res = tests[i].func(error); if (res) { passedTests++; testReport << "Тест \"" << tests[i].name << "\": Успешно\n"; } else { failedTests++; testReport << "Тест \"" << tests[i].name << "\": Ошибка\n"; testReport << " Сообщение: " << error << "\n"; } } testReport << "\nИТОГ:\n"; testReport << "Всего тестов: " << totalTests << "\n"; testReport << "Успешных: " << passedTests << "\n"; testReport << "Неуспешных: " << failedTests << "\n"; - Assistant
Да, в C++ Builder часто удобнее и надёжнее работать с классическими функциями из библиотеки C (
fopen,fprintf,fclose), особенно если возникают проблемы с шаблонами и потоками STL.Вот эквивалентный код, записывающий в файл ваше содержимое с помощью
FILE*иfprintf:cpp - User
- Assistant