Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double check_solve(double coeff[], double x);
- int sum(int a, int b) {
- return a + b;
- }
- int glob; // глобальная переменная
- void task0() {
- for (int k = 0; k < sum(2, 3); k++) {
- cout << sum(k, k) << endl;
- }
- int a = sum(6, 7);
- cout << sum(a, sum(5, 6)) << endl;
- cout << sum(3, 4) << endl;
- cout << glob;
- cout << sum << endl;
- //cout << &(sum(3,4)) << endl;
- //cout << &(7) << endl;
- int f = sum(3,4);
- //7 = 5466;
- cout << &f << endl;
- }
- void show(int x) {
- cout << "Целое: " << x << endl;
- }
- void show(char x) {
- cout << "Символ: " << x << endl;
- }
- void task_overload() {
- // перегрузка <---> полиморфизм
- show(49);
- show('b');
- show(char(49)); // char(49) --> '1'
- }
- //glob = sum(1, -5); //
- //glob = sum(1, -5); // писать код вне функций аморально (и вызывает ошибку).
- int analitic_solve(double coeff[], double sol[]) {
- double a = coeff[0];
- double b = coeff[1];
- double c = coeff[2];
- if (a == 0) {
- if (b != 0) {
- sol[0] = -c / b;
- return 1;
- } else
- return 0;
- }
- double d = b * b - 4 * a * c;
- if (d > 0) {
- d = sqrt(d);
- sol[0] = (-b + d) / (2 * a);
- sol[1] = (-b - d) / (2 * a);
- return 2;
- }
- if (d == 0) {
- sol[0] = -b / (2 * a);
- sol[1] = sol[0];
- return 2;
- }
- return 0;
- }
- double numeric_solve(double coeff[], double sol[]) {
- for (double x = -2; x < 2; x += 0.000001) {
- if (abs(check_solve(coeff, x)) < 0.00001) {
- sol[0] = x;
- return 1;
- }
- }
- return 0;
- }
- double check_solve(double coeff[], double x) {
- // эта функция вызывается много раз
- // поэтому роскошь дополнительнх присвоений
- // желатьльно не использовать и работать с coeff напрямую
- double a = coeff[0];
- double b = coeff[1];
- double c = coeff[2];
- return a * x * x + b * x + c;
- }
- void task_solve() {
- double coeff[]{3, 2, -7};
- double sol[2];
- int num_sol = analitic_solve(coeff, sol);
- for (int k = 0; k < num_sol; k++) {
- cout.width(15);
- cout.precision(12);
- cout << sol[k] << " : "
- << check_solve(coeff, sol[k]) << endl;
- }
- double nsol[2];
- num_sol = numeric_solve(coeff, nsol);
- for (int k = 0; k < num_sol; k++) {
- cout.width(15);
- cout.precision(12);
- cout << nsol[k] << " : "
- << check_solve(coeff, nsol[k]) << endl;
- }
- }
- int main() {
- setlocale(LC_ALL, "ru");
- //task0();
- //task_overload();
- task_solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment