-
Notifications
You must be signed in to change notification settings - Fork 0
/
learncpp.cpp
530 lines (340 loc) · 8.87 KB
/
learncpp.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/********************************************************************************
* Made by Chris *
* Started 2/11/23 *
* Finished 1/17/23 *
* *
* *
* Made so people newer to c++ can learn easier *
* *
********************************************************************************/
// #ifdef notused
#include <iostream>
#include <vector>
// std::count = standard output
// <<name; - "<<" displays something.
// "\n" = "\n" is a way to end the line being displayed and go to the next line.
// Ex: << "Hello" << name << '\n';
// Part 1 and 2 - basic data types and variables. And const
// // Int = whole number
// Ex: int age = 14;
// // double = numbers with decimal portion
// Ex: double price = 9.99;
// // Char = single Charecter
// Ex: Char grade = 'A';
// Bool = True or false
// Ex: bool student = false;
// String = sequence of text
// Ex: std::string name = "Chris";
// Const = a keyword that will modify a variable, or some value so that it is read only and cant be changed.
// Ex:
const double PI = 3.14;
const int LIGHT_SPEED_MPH = 670616629;
// Part 3 - Namespace
// A "Namespace" provides a solution for preventing name conflicts
// in large projects. each entity needs a unique name. A namespace
// allows for identically named entities as long as the namespaces
// are different.
// Ex:
#include <iostream>
namespace first{
int x = 1;
}
namespace second{
int x = 2;
}
int main() {
int x = 0;
return 0;
}
// // std::cout << x; OR std::cout << first::x;
// std::cout << second::x;
// OR
namespace first{
int x = 1;
}
namespace second{
int x = 2;
}
int main() {
using namespace second;
std::cout << x;
return 0;
}
// Part 4 - Typedef and type aliases
// Typedef = reserved keyword used to create an additional
// name (alias) for another data type. New identifier for
// an existing type helps with readability and reduces typos.
// Easy Ex:
#include <iostream>
#include <vector>
//typedef std::string text_t;
//typedef int number_t
using text_t = std::string;
using number_t = int;
int main(){
text_t firstName = "Chris";
number_t age = 14;
std::cout << firstName << '\n';
std::cout << age << '\n';
return 0;
}
// Advanced Ex:
#include <iostream>
#include <vector>
typedef std::vector<std::pair<std::string, int>> pairlist_t;
int main(){
pairlist_t pairlist;
return 0;
}
// Part 5 - Arithmetic operators
// Arithmetic oporators = return the result of a specific
// arithmetic operation (+ - * /)
// int remainder = students % 2; this finds the remainder of a division equastion.
// DOES NOT USE STANDARD ORDER OF OPORATIONS!
// c++ order of oporations - parenthesis, multiplication, division, addtion, subtraction.
int main(){
int students = 6 - 5 + 4 * 3 / 2;
// students = students + 1;
// students+=1;
// students++;
// students = students - 1;
// students-=;
// students--;
// students = students * 2;
// students*=2;
// students = students / 2;
// students/=2;
std::cout << students;
return 0;
}
// Part 6 - Type conversion
// Conversion of a value of one data type to another
// Implicit = automation
// Explicit = Precede value with new data type (int)
int main(){
// double x = (int) 3.14;
// char x = 100;
int correct = 8;
int questions = 10;
double score = correct/ (double) questions * 100;
std::cout <<score << "%";
return 0;
}
// #endif
// Part 7 - User input
// cout << (insertion operator)
// cin >> (extraction operator)
#include <iostream>
int main(){
std::string name;
int age;
std::cout << "Whats your first and last name?: ";
std::getline(std::cin >> std::ws, name);
std::cout <<"Whats your age?";
std::cin >> age;
std::cout <<"Hello " << name << '\n';
std::cout <<"You are " << age << " years old.";
return 0;
}
// #ifdef nrn
#include <iostream>
#include <cmath>
// Part 8 - Useful math related functions
int main(){
double x = 3.14;
double y = 7;
double z;
// z = std::max(x, y);
// z = std::min(x, y);
// z = pow(2, 3);
// z = sqrt(9);
// z = abs(-3);
// z = round(x);
// z = ceil(x);
// z = floor(x);
std::cout << z;
return 0;
}
#include <iostream>
#include <cmath>
// Part 9 - Hypotenuse calculator practice program
int main(){
double a;
double b;
double c;
std::cout << "Enter side A:";
std::cin >> a;
std::cout <<"Enter size B";
std::cin >> b;
a = pow(a, 2);
b = pow(b, 2);
c = sqrt(a + b);
std::cout << "side C: " << c;
return 0;
}
#include <iostream>
// Part 10 - if statements
// If statements do something if a condition is true.
// if not, then they dont do it.
int main(){
int age;
std::cout <<"Enter your age: ";
std::cin >> age;
if(age >= 18 ){
std::cout << "Welcome to the site!";
}
if(age < 18 ){
std::cout << "You Must be 18+ to enter!";
}
return 0;
}
// Part 11 - Switches
// A "switch" is an alternative to using many "else if" statements
// compare one value againt matching cases.
#include <iostream>
int main()
{
int month;
std::cout << "Enter the month (1-12): ";
std::cin >> month;
// Ex: 1
switch(month){
case 1:
;std::cout << "It is janurary";
break;
case 2:
std::cout << "It is feburary";
break;
case 3:
std::cout << "It is march";
break;
case 4:
std::cout << "It is april";
break;
case 5:
std::cout << "It is may";
break;
case 6:
std::cout << "It is june";
break;
case 7:
std::cout << "It is july";
break;
case 8:
std::cout << "It is august";
break;
case 9:
std::cout << "It is september";
break;
case 10:
std::cout << "It is october";
break;
case 11:
std::cout << "It is november";
break;
case 12:
std::cout << "It is december";
break;
default:
std::cout <<"Please enter in only numbers (1-12)";
}
return 0;
}
// Ex:2
#include <iostream>
int main()
{
char grade;
std::cout << "What letter grade?: ";
std::cin >> grade;
switch(grade){
case 'A':
std::cout <<"You did great!";
break;
case 'B':
std::cout << "you did good";
break;
case 'C':
std::cout << "you did alright";
break;
case 'D':
std::cout << "you could do better";
break;
case 'E':
std::cout << "How did you fail?";
break;
default:
std::cout <<"Please enter a letter grade from (A - E)";
}
return 0;
}
// Part 12 - console calculator
#include <iostream>
int main()
{
char op;
double num1;
double num2;
double result;
std::cout << "********************** CALCULATOR **********************\n";
std::cout << "Enter either (+ - * /):";
std::cin >> op;
std::cout << "Enter #1: ";
std::cin >> num1;
std::cout << "Enter #2: ";
std::cin >> num2;
switch (op){
case '+':
result = num1 + num2;
std::cout << "result: " << result << '\n';
break;
case '-':
result = num1 - num2;
std::cout << "result: " << result << '\n';
break;
case '*':
result = num1 * num2;
std::cout << "result: " << result << '\n';
break;
case '/':
result = num1 / num2;
std::cout << "result: " << result << '\n';
break;
default:
std::cout <<"That wasn't a valid operator. \n ";
break;
}
std::cout << "********************************************************";
return 0;
}
// Part 13 - Ternary operator
// A "tenary operator" ?: is a replacement to an if/else
// statement condition ? expression1 : experssion2
#include <iostream>
int main()
{
// int grade = 75;
// grade >= 60 ? std::cout << "you pass!" : std::cout << "you fail!";
// int number = 9;
// number % 2 == 1 ? std::cout << "ODD" : std::cout << "EVEN";
// value can be true or false
bool hungry = true;
// hungry ? std::cout << "Your are hungry" : std::cout << "You are full" ;
std::cout << hungry ? "You are hungry" : "You are full";
return 0;
}
// Part 14 - Logical Operators
// && = check if two condition are true
// || = check if at least one of two conditions is true
// ! = reverses the logical state of its operand
#include <iostream>
int main()
{
int temp;
std::cout <<"Enter the temperature: ";
std::cin >> temp;
if(temp > 0 && temp < 30 )
return 0;
}
// #endif
// Part 15 -