while | (условие) | оператор |
while (balance < 20000)
{ month++;
balance
= balance * (1 + rate/12/100);
}
cout << "The investment
doubled after "
<<
month / 12.0 << " years.\n";
return 0;
}
* Факториел
#include <iostream>
using namespace std;
long factorial(int n)
{ int factor = 1;
long product = 1;
while (factor <= n)
{ product = product
* factor;
factor++;
}
return product;
}
int main()
{ cout << "Please enter a
number: ";
int n;
cin >> n;
cout << n <<
"! = " << factorial(n) << "\n";
return 0;
}
* Максимална стойност
#include <iostream>
using namespace std;
int main()
{ double next;
double highest;
cout << "Please enter
the temperature values:\n";
if (cin >> next)
highest = next;
else
{ cout << "No
data!\n";
return
1; }
while (cin >> next)
if
(next > highest) highest = next;
cout << "The highest
temperature is " << highest << "\n";
return 0;
}
Цикъл for
for | (инициализация; условие; корекция) | оператор |
for (factor = 1; factor <=
n; factor++)
{ product = product
* factor; }
return product;
}
int main()
{ cout << "Please enter a
number: ";
int n;
cin >> n;
cout << n <<
"! = " << factorial(n) << "\n";
return 0;
}
Цикъл do/while
do | оператор | while | (условие); |
bool not_equal(double x, double y)
{ const double EPSILON = 1E-14;
double dmax = fabs(x);
if (fabs(y) > dmax) dmax
= fabs(y);
return fabs(x - y) > dmax
* EPSILON;
}
double squareroot(double a)
{ if (a == 0) return 0;
double xnew = a;
double xold;
do
{ xold = xnew;
xnew =
(xold + a / xold) / 2;
}
while (not_equal(xnew, xold));
return xnew;
}
int main()
{ cout << "Please enter a
number: ";
double x;
cin >> x;
cout << "The square
root is " << squareroot(x) << "\n";
return 0;
}
Конструкции с цикъл: четене на
данни и извеждане на таблици
* Четене на данни:
#include <iostream>
#include <string>
using namespace std;
int main()
{ int count = 0;
string word;
while (cin >> word)
count++;
cout << count <<
" words.\n";
return 0;
}
* Пренасочване на входния и изходния потоци
C:\my\>myprog < a_cin.txt
C:\my\>myprog > a_cout.txt
* Извеждане на таблици със стойности
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double future_value(double initial_balance,
double p, int nyear)
/*
ЦЕЛ:
Пресмята стойността на влог със сложна лихва
ПОЛУЧАВА: initial_balance - начална
стойност на влога
p - годишен лихвен процент
nyear - броя на годините
ЗАБЕЛЕЖКИ: Лихвата се начислява всеки
месец
*/
{ double b = initial_balance
*pow(1 + p/12/100, 12*nyear);
return b;
}
int main()
{ cout << "
Rate Balance\n\n";
double rate;
double initial_amount = 10000;
int nyear = 10;
cout << fixed << setprecision(2);
for (rate = 5; rate <= 10; rate
= rate + 0.5)
{ double balance = future_value(initial_amount,
rate, nyear);
cout << setw(10)
<< rate << setw(10)
<< balance << "\n";
}
return 0;
}
Rate Balance
5.00 16470.09
|
Итеративни алгоритми и сходимост
на алгоритми
#include "ccc_win.cpp"
int main()
{ double a = cwin.get_double("Please
enter a number");
if (a == 0) return 0;
double xnew = a;
double xold;
int iteration = 0;
const int MAX_ITERATIONS
= 20;
cwin.coord(0, a, MAX_ITERATIONS,
-a);
do
{ xold = xnew;
xnew =
(xold + a / xold) / 2;
cwin <<
Point(iteration, xnew);
iteration++;
} while (iteration < MAX_ITERATIONS);
return 0;
}
Генериране на случайни събития и симулации
Вложени цикли
* Таблица за сумата по влог за 5, 10, 15, 20, 25 и 30 години при годишна
лихва от 5 до 10% (през 0.5%).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double future_value(double init_balance,
double p, int n)
{ double b = init_balance*pow(1
+ p/12/100, 12*n);
return b; }
int main()
{
const double RATE_MIN = 5;
const double RATE_MAX = 10;
const double RATE_INCR = 0.5;
const int YEAR_MIN = 5;
const int YEAR_MAX = 30;
const int YEAR_INCR = 5;
/* print table header */
cout << " Rate ";
int year;
for (year=YEAR_MIN; year<=YEAR_MAX;
year=year+YEAR_INCR)
{ cout << setw(2) <<
year << " years "; }
cout << "\n";
cout << fixed << setprecision(2);
double rate;
double init_bal = 10000;
for (rate=RATE_MIN; rate<=RATE_MAX;
rate=rate+RATE_INCR)
/* print table row */
{ int year;
cout << setw(5) <<
rate;
for (year=YEAR_MIN; year<=YEAR_MAX;
year=year+YEAR_INCR)
{ double balance = future_value(init_bal,
rate, year);
cout <<
setw(10) << balance; }
cout << "\n";
}
return 0;
}
Rate 5 years 10
years 15 years 20 years
5.0 12833.59 16470.09 21137.04 27126.40 5.5 13157.04 17310.76 22775.84 29966.26 6.0 13488.50 18193.97 24540.94 33102.04 6.5 13828.17 19121.84 26442.01 36564.47 7.0 14176.25 20096.61 28489.47 40387.39 7.5 14532.94 21120.65 30694.52 44608.17 8.0 14898.46 22196.40 33069.21 49268.03 8.5 15273.01 23326.47 35626.53 54412.43 9.0 15656.81 24513.57 38380.43 60091.52 9.5 16050.09 25760.55 41345.93 66360.61 10.0 16453.09 27070.41 44539.20 73280.74 |