-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometric-ser.cpp
75 lines (60 loc) · 1.68 KB
/
geometric-ser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
[A geometric series sub element adder]
START STOP
↓ ↓
[A B C D E F G] elements
↑ ↑ ↑ ↑ ↑ ↑ ↑
------------- PUBL_RATIO
Code written by Lawrence Link.
Sub organization LPD rights reserved.
*/
#include <iostream>
#include <cmath>
using namespace std;
unsigned long START_AT;
unsigned long STOP_AT;
unsigned long PUBL_RATIO;
void calc_fail();
int main()
{
cout << "Please input a number which the series start with." << endl;
cin >> START_AT;
cout << "Now, enter the number which the series end with." << endl;
cin >> STOP_AT;
cout << "The Public Ratio is" << endl;
cin >> PUBL_RATIO;
if (cin.good())
{
cout << "CALCULATING STATRED." << endl;
static unsigned long proc_num = START_AT;
static long answer_final = START_AT;
static bool calFailedFlag = false;
do
{
proc_num *= PUBL_RATIO;
answer_final += proc_num;
//cout << answer_final << endl;
cout << proc_num << endl;
if (answer_final < 0 || proc_num > STOP_AT){ // NOT fine
calc_fail();
calFailedFlag = true;
break;
}
} while (proc_num != STOP_AT && proc_num < STOP_AT);
if (!calFailedFlag && cin.good() && proc_num == STOP_AT)
{
cout << "This is the final answer." << endl;
cout << answer_final << endl;
}
}
else
{
cerr << "Input valid. Check if anything goes wrong, maybe you just entered characters?";
}
system("PAUSE");
return 0;
}
void calc_fail()
{
cerr << "\nCalculation failed. The answer does not exist." << endl;
}