-
Notifications
You must be signed in to change notification settings - Fork 5
/
224088_hw1.cc
261 lines (213 loc) · 8.64 KB
/
224088_hw1.cc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Author: Abu Shumon
* Student number: 224088
*/
/*
* This is a c++ program to caltulate the postage fee for Itella Corporation.
* What it does basically; takes amount of letter(s), weight of one letter, and
* the destination of the letter(s) as input.
* And then give output to the user the total
* postage fee that is needed to send the letter(s).
*/
#include <iostream>
#include <vector>
#include <iomanip>
// Constant declaration thats used in functions
const int TWENTY = 10*2;
const int FIFTY = 10*5;
const int HUNDRED = 10*10;
const int T_FIFTY = 10*10+10*5;
const int F_HUNDRED = 10*10*5;
const int ONE_T = 10*10*10;
const int TWO_T = 10*10*10*2;
using namespace std;
/*
* Function: cost_domestic
* Argument: two integer type parameter
* It takes the letter amount and letter weight and calculate
* the total cost for domestic destination of the letter(s)
*/
//function declaration and definitation
float cost_domestic (int letter_amount, int letter_weight)
{
int l_a = 0; //variable declaration and initialization
int l_w = 0; //variable declaration and initialization
double cost = 0; //variable declaration and initialization
// Assigns the values to local variables
l_a = letter_amount;
l_w = letter_weight;
// It checks the weight of the letter and
//afterwords calculate the total cost and display it.
if ( l_w <= FIFTY ){
cost = l_a * (0.10 * 8); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= HUNDRED ){
cost = l_a * 1.10; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= T_FIFTY ){
cost = l_a * (1.00 + 0.10 * 6); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= F_HUNDRED ){
cost = l_a * 3.00; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= ONE_T ){
cost = l_a * (4.00 + 0.10 * 5); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= TWO_T ){
cost = l_a * (7.00 + 0.10 * 5); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
return 0;
}
/*
* Function: cost_europe
* Argument: two integer type parameter
* It takes the letter amount and letter
* weight and calculate the total cost for Europian destination of the letter(s)
*/
//function declaration and definitation
float cost_europe (int letter_amount, int letter_weight)
{
int l_a = 0; //variable declaration and initialization
int l_w = 0; //variable declaration and initialization
double cost = 0; //variable declaration and initialization
// Assigns the values to local variables
l_a = letter_amount;
l_w = letter_weight;
// It checks the weight of the letter and
// afterwords calculate the total cost and display it.
if ( l_w <= TWENTY ){
cost = l_a * 0.8; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= FIFTY ){
cost = l_a * 1.10; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= HUNDRED ){
cost = l_a * 1.00 + 0.10 * 5.5; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= T_FIFTY ){
cost = l_a * (2.00 + 0.10 * 5.5); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= F_HUNDRED ){
cost = l_a * (4.00 + 0.10 + 0.10); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= ONE_T ){
cost = l_a * (7.00 + 0.10 * 6); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= TWO_T ){
cost = l_a * (10.00 + 3.00 + 0.10 * 8); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
return 0;
}
/*
* Function: cost_other
* Argument: two integer type parameter
* It takes the letter amount and letter weight
* and calculate the total cost for the other
* destination of the letter(s)
*/
//function declaration and definitation
float cost_other (int letter_amount, int letter_weight)
{
int l_a = 0; //variable declaration and initialization
int l_w = 0; //variable declaration and initialization
double cost = 0; //variable declaration and initialization
// Assigns the values to local variables
l_a = letter_amount;
l_w = letter_weight;
// It checks the weight of the letter and
// afterwords calculate the total cost and display it.
if ( l_w <= TWENTY ){
cost = l_a * 0.8; // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= FIFTY ){
cost = l_a * (1.00 + 0.10 * 5.5); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= HUNDRED ){
cost = l_a * (3.00 + 0.10 + 0.10); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= T_FIFTY ){
cost = l_a * (5.00 + 0.10 + 4); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= F_HUNDRED ){
cost = l_a * (8.00 + 0.10 * 8); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= ONE_T ){
cost = l_a * (10.00 + 5.00 + (0.10 * (9.00 + 0.10*5))); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
else if ( l_w <= TWO_T ){
cost = l_a * (10.00 + 10.00 + 8.00 + 0.10 * 6); // calculates the cost
cout << fixed << setprecision(2) << cost << endl; // display the post fee with fixed precision
}
return 0;
}
int main ()
{
char letter_destination; //variable declaration
int letter_amount = 0; //variable declaration and initialization
int letter_weight = 0; //variable declaration and initialization
do
{
center: // Center declaration because of "goto"
// implementation in the main function.
cout << "Enter the amount of letters to send:" << endl; // Ask user to give input the amount of letter
cin >> letter_amount;
if ( letter_amount > 0 ){ // Checks valid weight.
top: // Another "goto" statement declaration.
// It takes the user to put the weight again
//if the given weight is invalid.
cout << "Enter the weight of one letter in grams:" << endl; // Ask user to give input the weight of letter
cin >> letter_weight;
if ( letter_weight <= 0 ){ // Checks if the letter weight is less than equal zero.
cout << "Error: invalid weight" << endl; // error message
goto top;
}
else if ( letter_weight > TWO_T ){ // Checks if the letter weight is greater than 2000.
cout << "Error: cannot be sent as a letter" << endl; //error message
break; // breaks the loop instead the looping condition is not false yet.
}
else {
cout << "Enter the destination (D)omestic, (E)urope, (O)ther:" << endl; // Ask user to give input the destination of letter
cin >> letter_destination;
// Compare the destination to call the appropriate function
if ( letter_destination == 'D' || letter_destination == 'E' || letter_destination == 'O' ){
if (letter_destination == 'D'){
cost_domestic(letter_amount, letter_weight); //Calling function
}
else if ( letter_destination == 'E' ){
cost_europe(letter_amount, letter_weight); // Calling function
}
else if ( letter_destination == 'O' ){
cost_other(letter_amount, letter_weight); // Calling function
}
}
else{
cout << "Error: unknown destination" << endl; //error message
goto center;
}
}
}
else{
break;
}
}while( letter_amount != 0 );
return 0;
}