// temp.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
using std::cout;
using std::endl;
class Example1
{
private:
public:
double Ex1UsingFor();
double Ex1UsingWhile();
double Ex1UsingDoWhile();
};
class Example2 {
double Ex2UsingFor();
double Ex2UsingWhile();
double Ex2UsingDoWhile();
class Example3 {
double Ex3UsingFor();
double Ex3UsingWhile();
double Ex3UsingDoWhile();
double Example1::Ex1UsingFor()
double res = 0;
for (int r = 1; r < 15; r++)
res += (r + 2.4) / (r * r + 7 * r + 1);
}
return res;
double Example1::Ex1UsingWhile()
double r = 1;
while (r < 15) {
r++;
double Example1::Ex1UsingDoWhile()
do {
} while (r < 15);
double Example2::Ex2UsingFor()
for (int q = 1; q < 19; q++)
res += pow(-1, q) * (cos(q * q + 5) / (pow(q, 4) - abs(q - 71)));
double Example2::Ex2UsingWhile()
int q = 1;
while (q < 19)
q++;
double Example2::Ex2UsingDoWhile()
double q = 0;
do
} while (q < 19);
double Example3::Ex3UsingFor()
for (int i = 1; i < 17; i++)
double temp = 1;
for (int m = 1; m < 14; m++)
temp *= atan(i + m) / (log(i) + 0.24);
res += temp;
double Example3::Ex3UsingWhile()
double i = 1;
while (i < 17)
double m = 1;
while (m < 14)
m++;
i++;
double Example3::Ex3UsingDoWhile()
} while (m < 14);
while (i < 17);
int main()
Example1* ex1 = new Example1();
Example2* ex2 = new Example2();
Example3* ex3 = new Example3();
cout << "example 1" << endl;
double res = ex1->Ex1UsingFor();
cout << "res=" << res << endl;
res = ex1->Ex1UsingWhile();
res = ex1->Ex1UsingDoWhile();
cout << "example 2" << endl;
res = ex2->Ex2UsingFor();
res = ex2->Ex2UsingWhile();
res = ex2->Ex2UsingDoWhile();
cout << "example 3" << endl;
res = ex3->Ex3UsingFor();
res = ex3->Ex3UsingWhile();
res = ex3->Ex3UsingDoWhile();
delete ex1;
delete ex2;
delete ex3;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Verified answer
// temp.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//
#include <iostream>
using std::cout;
using std::endl;
class Example1
{
private:
public:
double Ex1UsingFor();
double Ex1UsingWhile();
double Ex1UsingDoWhile();
};
class Example2 {
private:
public:
double Ex2UsingFor();
double Ex2UsingWhile();
double Ex2UsingDoWhile();
};
class Example3 {
private:
public:
double Ex3UsingFor();
double Ex3UsingWhile();
double Ex3UsingDoWhile();
};
double Example1::Ex1UsingFor()
{
double res = 0;
for (int r = 1; r < 15; r++)
{
res += (r + 2.4) / (r * r + 7 * r + 1);
}
return res;
}
double Example1::Ex1UsingWhile()
{
double res = 0;
double r = 1;
while (r < 15) {
res += (r + 2.4) / (r * r + 7 * r + 1);
r++;
}
return res;
}
double Example1::Ex1UsingDoWhile()
{
double res = 0;
double r = 1;
do {
res += (r + 2.4) / (r * r + 7 * r + 1);
r++;
} while (r < 15);
return res;
}
double Example2::Ex2UsingFor()
{
double res = 0;
for (int q = 1; q < 19; q++)
{
res += pow(-1, q) * (cos(q * q + 5) / (pow(q, 4) - abs(q - 71)));
}
return res;
}
double Example2::Ex2UsingWhile()
{
double res = 0;
int q = 1;
while (q < 19)
{
res += pow(-1, q) * (cos(q * q + 5) / (pow(q, 4) - abs(q - 71)));
q++;
}
return res;
}
double Example2::Ex2UsingDoWhile()
{
double res = 0;
double q = 0;
do
{
res += pow(-1, q) * (cos(q * q + 5) / (pow(q, 4) - abs(q - 71)));
q++;
} while (q < 19);
return res;
}
double Example3::Ex3UsingFor()
{
double res = 0;
for (int i = 1; i < 17; i++)
{
double temp = 1;
for (int m = 1; m < 14; m++)
{
temp *= atan(i + m) / (log(i) + 0.24);
}
res += temp;
}
return res;
}
double Example3::Ex3UsingWhile()
{
double res = 0;
double i = 1;
while (i < 17)
{
double temp = 1;
double m = 1;
while (m < 14)
{
temp *= atan(i + m) / (log(i) + 0.24);
m++;
}
res += temp;
i++;
}
return res;
}
double Example3::Ex3UsingDoWhile()
{
double res = 0;
double i = 1;
do
{
double m = 1;
double temp = 1;
do
{
temp *= atan(i + m) / (log(i) + 0.24);
m++;
} while (m < 14);
res += temp;
i++;
}
while (i < 17);
return res;
}
int main()
{
Example1* ex1 = new Example1();
Example2* ex2 = new Example2();
Example3* ex3 = new Example3();
cout << "example 1" << endl;
double res = ex1->Ex1UsingFor();
cout << "res=" << res << endl;
res = ex1->Ex1UsingWhile();
cout << "res=" << res << endl;
res = ex1->Ex1UsingDoWhile();
cout << "res=" << res << endl;
cout << "example 2" << endl;
res = ex2->Ex2UsingFor();
cout << "res=" << res << endl;
res = ex2->Ex2UsingWhile();
cout << "res=" << res << endl;
res = ex2->Ex2UsingDoWhile();
cout << "res=" << res << endl;
cout << "example 3" << endl;
res = ex3->Ex3UsingFor();
cout << "res=" << res << endl;
res = ex3->Ex3UsingWhile();
cout << "res=" << res << endl;
res = ex3->Ex3UsingDoWhile();
cout << "res=" << res << endl;
delete ex1;
delete ex2;
delete ex3;
}