forked from matkatmusic/PFMCPP_Project3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
721 lines (565 loc) · 18 KB
/
Copy pathmain.cpp
File metadata and controls
721 lines (565 loc) · 18 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
Project 3 - Part 5 / 5
video: Chapter 2 - Part 10
Scope and Lifetime tasks
Create a branch named Part5
video covered:
variable scope and lifetime relative to { }
while loops
for loops()
tasks
1) add some new member functions to EACH of your types.
2) inside these new member functions, use while() and for() loops to do something interesting
a) example: have a loop that modifies a member variable of some object created outside the loop.
b) when that member variable reaches a certain threshold, return it mid-loop.
c) maybe use function parameters to control the starting value of that member variable or control the threshold
3) call those new member functions in main()
4) use std::cout statements to print out information about what your loops did.
Your code should produce a lot of console output now.
5) Remember to use pre-increment/decrement in your loops.
You can learn why post-increment/decrement is not ideal here:
https://en.cppreference.com/w/cpp/language/operator_incdec
6) click the [run] button. Clear up any errors or warnings as best you can.
if your code produces a -Wpadded warning, add '-Wno-padded' to the .replit file with the other compiler flags (-Weverything -Wno-missing-prototypes etc etc)
*/
#include <iostream>
namespace Example
{
struct Bar
{
int num = 0;
Bar(int n) : num(n) { }
};
struct Foo
{
Bar scopeLifetimeFunc( int threshold, int startingVal ) //1), 2c)
{
Bar bar(startingVal); //2a)
while( bar.num < threshold ) //2a)
{
bar.num += 1; //2a)
std::cout << " increasing bar.num: " << bar.num << std::endl; //4)
if( bar.num >= threshold ) //2b)
return bar;
}
return Bar {-1}; //if your startingValue >= threshold, the while loop never runs
}
};
int main()
{
Foo foo;
auto bar = foo.scopeLifetimeFunc(3, 1); //3)
std::cout << "bar.num: " << bar.num << std::endl; //4)
return 0;
}
}
//call Example::main() in main()
struct Piano
{
int height, width, weight, numberOfKeys, numberOfPedals;
std::string brand;
Piano() : height(132), width(198), weight(260), numberOfKeys(88), numberOfPedals(2), brand("x") { }
int countBbnotes(int totalKeys);
void playKey(int keyNumber);
void pressSustainPedal();
void pressSoftPedal();
};
int Piano::countBbnotes(int totalKeys)
{
int counter = 0;
int i = 0;
while(i < totalKeys)
{
if(i % 12 == 1)
{
counter += 1;
}
i += 1;
}
std::cout << "This piano has " << counter << " Bb octaves." << std::endl;
return counter;
}
void Piano::playKey(int keyNumber)
{
if (keyNumber > numberOfKeys)
{
std::cout << "that note is too high" << std::endl;
}
else
{
std::cout << "The " << brand << " piano is playing key " << keyNumber << std::endl;
}
}
void Piano::pressSustainPedal()
{
std::cout << "The " << brand << " sustain pedal is pressed. " << std::endl;
}
void Piano::pressSoftPedal()
{
std::cout << "The " << brand << " soft pedal is pressed. " << std::endl;
}
struct Tree
{
int numberOfLeaves, age, numberOfSquirrels;
float height;
bool Coniferous, Alive;
Tree() : numberOfLeaves(3000), age(20), numberOfSquirrels(0), height(10.f), Coniferous(false), Alive(true) {}
void grow();
void swayInTheWind(double windSpeed);
void setSquirrelResidents(int number);
int checkSquirrelResidents(); // returns number of squirrels resident
int squirrelPopulationGrowth(int initialPopulation, int numberMonths); // find squirrel population in so many Months
};
void Tree::grow()
{
std::cout << "Tree is " << age << " years old" << std::endl; //3)
}
void Tree::swayInTheWind(double windSpeed)
{
if(windSpeed < 0)
{
std::cout << "negative wind speed not allowed" << std::endl;
}
else
{
std::cout << "Wind speed is " << windSpeed << std::endl;
}
}
void Tree::setSquirrelResidents(int number)
{
numberOfSquirrels = number;
}
int Tree::checkSquirrelResidents()
{
return numberOfSquirrels;
}
int Tree::squirrelPopulationGrowth(int initialPopulation, int numberMonths)
{
numberOfSquirrels = initialPopulation;
for (int i = 0; i < numberMonths; i = i + 3 )
{
numberOfSquirrels = numberOfSquirrels * 2; // population doubles every three months
numberOfSquirrels = numberOfSquirrels - 14; // 14 die every three months from random causes
}
std::cout << "Squirrel count will be " << numberOfSquirrels << " in " << numberMonths << " months." << std::endl;
return numberOfSquirrels;
}
struct City
{
std::string name = "Toronto"; // in CLASS initialisation
std::string country = "Canada";
std::string newLawName = "Friday's Off";
int population = 5000000;
float latitude = 43.6532f;
float longitude = -79.3470f;
City();
void expand(float expansionRate = 1.1f);
std::string createLaw(); // returns new law Name
int updatePopulation(int immigrantsYear, int emigrantsYear, int birthsYear, int deathsYear, int years);
};
City::City()
{
std::cout << "City being constructed." << std::endl;
}
void City::expand(float expansionRate)
{
if(expansionRate < 0.f)
{
std::cout << "we're shrinking! \n";
}
}
std::string City::createLaw()
{
return newLawName;
}
int City::updatePopulation(int immigrantsYear, int emigrantsYear, int birthsYear, int deathsYear, int years)
{
for(int i = 0; i <= years; ++i)
{
population = population + immigrantsYear - emigrantsYear + birthsYear - deathsYear;
}
return population;
}
struct Farm
{
int annualIncome, numberEmployees, chickensTotal, acreage;
std::string owner;
Farm() : annualIncome(1500000), numberEmployees(5), chickensTotal(50), acreage(4), owner("Dan") {}
void growVegetable(std::string vegetableType);
void raiseCattle(std::string cattleType);
int payTaxes(int totalProfit); // returns taxes owed
int chickenMaximum(int chickens, int percentageIncreaseWeek ); // calculate maximum chickens we can fit on the farm
};
void Farm::growVegetable(std::string vegetableType)
{
std::cout << "We're growing " << vegetableType << std::endl;
}
void Farm::raiseCattle(std::string cattleType)
{
std::cout << "We're raising " << cattleType << std::endl;
}
int Farm::payTaxes(int totalProfit)
{
return totalProfit / 2 ;
}
int Farm::chickenMaximum(int chickens, int percentageIncreaseWeek )
{
int weeks = 1;
int maximumChickens = acreage * 50; // this is the maximum chickens we can fit
while (chickens < maximumChickens)
{
chickens = chickens + (chickens * percentageIncreaseWeek) / 100 ;
++ weeks;
}
std::cout << "Farm will be full of chickens in " << weeks << " weeks." << std::endl;
return weeks;
}
struct ControlRoom
{
ControlRoom();
int length, width, height, numberSeats;
bool studioPowerState;
std::string monitorBrand;
int hoursInBudget(int engineerRate, int studioRate, int budget);
void seatEngineer(std::string engineerName);
void houseConsole();
bool switchStudioPower(); // returns state of studio power
};
ControlRoom::ControlRoom()
: length(15), width(9), height(2), numberSeats(3), studioPowerState(false), monitorBrand("ATC")
{
std::cout << "ControlRoom being constructed." << std::endl;
}
int ControlRoom::hoursInBudget(int engineerRate, int studioRate, int budget)
{
int hours = 0;
int cost = 0;
while (cost <= budget)
{
++hours;
cost = hours*engineerRate + hours*studioRate;
}
std::cout << "You have " << hours << " hours in your budget." << std::endl;
return hours;
}
void ControlRoom::seatEngineer(std::string engineerName)
{
std::cout << "Today we're enjoying the mixing skills of " << engineerName << std::endl;
}
void ControlRoom::houseConsole()
{
}
bool ControlRoom::switchStudioPower()
{
studioPowerState = !studioPowerState;
if (studioPowerState)
{
std::cout << "Studio power is ON. " << std::endl;
}
else
{
std::cout << "Studio power is OFF. " << std::endl;
}
return studioPowerState;
}
struct LiveRoom
{
LiveRoom(); // in CLASS initialisation
int length = 26;
int width = 19;
int height = 4;
std::string wallMaterial = "cloth";
std::string floorMaterial = "wood";
bool lightsCurrentState = false;
std::string studioName = "Studio A";
struct Musician
{
std::string name, mainInstrument;
int yearsExperience, hourlyRate;
Musician() : name("John"), mainInstrument("Piano"), yearsExperience(10), hourlyRate(75) {}
void callMusician();
bool createContract(); // returns contract created or not
int totalHoursUnpaid(); // returns total hours not yet paid
};
struct Equipment
{
std::string instrument1, instrument2, instrument3;
Equipment() : instrument1("Piano"), instrument2("Guitar"), instrument3("Drums") {}
void tunePiano();
bool switchHammond();
bool enableSnaresOnSnareDrum();
};
void seatMusician(Musician musicianName, std::string thisName);
void placeEquipment(Equipment steinwayPiano);
bool switchLights();
int calculateMusicianFee(int hours, bool receivesPublishingPercentage);
};
LiveRoom::LiveRoom()
{
std::cout << "LiveRoom being constructed." << std::endl;
}
int LiveRoom::calculateMusicianFee(int hours, bool receivesPublishingPercentage)
{
int musicianFee = 0;
for (int i = 0; i <= hours; ++i)
{
if(receivesPublishingPercentage == true)
{
musicianFee += 45;
// no overtime fee if receiving album points
}
else
{
musicianFee += 115;
if(i % 10 == 0)
{
musicianFee += 30; // overtime fee
}
}
}
std::cout << "He is charging $" << musicianFee << " for this session." << std::endl;
return musicianFee;
}
void LiveRoom::seatMusician(Musician musicianName, std::string thisName)
{
musicianName.name = thisName;
std::cout << "Today we're enjoying the dulcet tones of " << musicianName.name << std::endl;
}
void LiveRoom::placeEquipment(Equipment instrumentType)
{
std::cout << "We have taken delivery of a " << instrumentType.instrument1 << std::endl;
}
bool LiveRoom::switchLights()
{
lightsCurrentState = !lightsCurrentState;
if (lightsCurrentState)
{
std::cout << "The lights are currently on." << std::endl;
}
else
{
std::cout << "The lights are currently off." << std::endl;
}
return lightsCurrentState;
}
struct Computer
{
Computer();
std::string brand = "Apple"; // in CLASS initialisation
int CPUspeed = 3200;
int RAMsize = 64;
int age = 2;
int price = 2400;
bool powerState = false;
bool switchOnOff(); // returns current power state
std::string runSoftware(std::string applicationName); // return app name
void crash();
int hoursTillComputerCrash(bool runningProTools);
};
Computer::Computer()
{
std::cout << "Computer being constructed." << std::endl;
}
int Computer::hoursTillComputerCrash(bool runningProTools)
{
int hours = 0;
float heat = 1.1f;
float numberOfPlugins = 1.f;
float willCrash = 0.3f;
while(willCrash < 1.0f )
{
willCrash = willCrash * heat * numberOfPlugins;
numberOfPlugins = numberOfPlugins * 1.2f;
if (runningProTools == true)
{
heat = heat * 1.15f;
}
else
{
heat = heat * 1.07f;
}
++hours;
}
std::cout << "Computer will crash in " << hours << " hours." << std::endl;
return hours;
}
bool Computer::switchOnOff()
{
powerState = !powerState;
if (powerState)
{
std::cout << "The computer is currently on." << std::endl;
}
else
{
std::cout << "The computer is currently off." << std::endl;
}
return powerState;
}
std::string Computer::runSoftware(std::string programName)
{
return programName;
}
void Computer::crash()
{
}
struct MixingConsole
{
MixingConsole();
std::string brand;
int numberOfChannels;
bool inlineConsole; //special word 'inline' here, change to inlineConsole to prevent Run error
int price;
bool digital;
bool powerState;
int channelMix;
struct Equaliser
{
bool switchEqualiser;
float highPassFilter, lowPassFilter, midBandFreq, midBandGain, midBandQ;
Equaliser() : switchEqualiser(false), highPassFilter(20.f), lowPassFilter(20000.f), midBandFreq(1000.f), midBandGain(0.f), midBandQ(1.f) {}
void setMidBand(float frequency = 1000.f, float gain = 0.f, float quality = 1.f);
void setHighPassFilter(float frequency = 20.f);
void setLowPassFilter(float frequency = 20000.f);
};
bool switchOnOff(); // returns current power state
int mixChannels(int channelA, int channelB); //returns mixed output
bool enableEqualiser(Equaliser thisEQ); // enables or disables the Equaliser
};
MixingConsole::MixingConsole()
: brand("Neve"), numberOfChannels(48), inlineConsole(true), price(200000), digital(false), powerState(false), channelMix(0)
{
std::cout << "MixingConsole being constructed." << std::endl;
}
void MixingConsole::Equaliser::setMidBand(float frequency, float gain, float quality)
{
midBandFreq = frequency;
midBandGain = gain;
midBandQ = quality;
}
bool MixingConsole::switchOnOff()
{
powerState = !powerState;
return powerState;
}
int MixingConsole::mixChannels(int channelA, int channelB)
{
channelMix = channelA + channelB;
return channelMix;
}
bool MixingConsole::enableEqualiser(Equaliser exampleEQ)
{
exampleEQ.switchEqualiser = !exampleEQ.switchEqualiser;
return exampleEQ.switchEqualiser;
}
struct Microphone
{
Microphone();
std::string brand = "B&K"; // in CLASS initialisation
bool condenser = true;
std::string polarPattern = "Cardioid";
int age = 6;
int price = 3600;
bool switchState = false;
bool switchOnOff(); // returns current power state
void plugInMicrophone();
int changePolarPattern(int polarPatternChoice = 0); // returns int of polar pattern selection
};
Microphone::Microphone()
{
std::cout << brand << " " << polarPattern << " microphone being constructed." << std::endl;
}
bool Microphone::switchOnOff()
{
switchState = !switchState;
return switchState;
}
void Microphone::plugInMicrophone()
{
}
int Microphone::changePolarPattern(int polarPatternChoice)
{
switch (polarPatternChoice)
{
case 0:
polarPattern = "Omni";
break;
case 1:
polarPattern = "Cardioid";
break;
case 2:
polarPattern = "Figure 8";
break;
}
return polarPatternChoice;
}
struct RecordingStudio
{
RecordingStudio();
std::string name = "Olympic";
ControlRoom defaultControlRoom;
LiveRoom defaultLiveRoom;
Computer iMac;
MixingConsole neveDesk;
Microphone condenserMic;
void recordSound(MixingConsole Neve);
void playBackSound(MixingConsole Neve);
int bookStudio(LiveRoom John, int hours = 5);
};
RecordingStudio::RecordingStudio()
{
std::cout << "RecordingStudio being constructed." << std::endl;
}
void RecordingStudio::recordSound(MixingConsole chosenConsole)
{
std::cout << "Recording on " << chosenConsole.brand << std::endl;
}
void RecordingStudio::playBackSound(MixingConsole chosenConsole)
{
std::cout << "Playing back on " << chosenConsole.brand << std::endl;
}
int RecordingStudio::bookStudio(LiveRoom studioChoice, int time )
{
std::cout << "Booking studio " << studioChoice.studioName << std::endl;
return time;
}
#include <iostream>
int main()
{
Example::main();
Piano steinway;
steinway.brand = "Steinway";
steinway.playKey(60);
steinway.pressSustainPedal();
steinway.pressSoftPedal();
steinway.countBbnotes(steinway.numberOfKeys);
Tree maple;
maple.swayInTheWind(11);
maple.grow();
maple.setSquirrelResidents(17);
std::cout << "This tree currently has " << maple.checkSquirrelResidents() << " squirrels living in it." << std::endl;
maple.squirrelPopulationGrowth(maple.numberOfSquirrels, 6); // estimate the squirrel count in 6 months
City toronto;
toronto.expand();
toronto.createLaw();
std::cout << "This population of the city will be " << toronto.updatePopulation(4000, 1500, 18000, 17000, 5) << " in 5 years." << std::endl;
Farm oldmcdonalds;
oldmcdonalds.growVegetable("potatoes");
oldmcdonalds.raiseCattle("chickens");
oldmcdonalds.payTaxes(100000);
std::cout << "This farm owes $" << oldmcdonalds.payTaxes(150000) << " in taxes." << std::endl;
oldmcdonalds.chickenMaximum(oldmcdonalds.chickensTotal, 30); // how many weeks till farm is full of chickens????
RecordingStudio factory;
factory.name = "Factory";
std::cout << factory.name << " studio has a " << factory.neveDesk.numberOfChannels << " channel " << factory.neveDesk.brand << " desk." << std::endl;
factory.condenserMic.brand = "Schoeps";
std::cout << "We'll be recording with the " << factory.condenserMic.brand << std::endl;
factory.defaultControlRoom.seatEngineer("Bobby V");
LiveRoom::Musician tony;
factory.defaultLiveRoom.seatMusician(tony, "Tony");
factory.defaultLiveRoom.calculateMusicianFee(31, false);
factory.defaultLiveRoom.switchLights();
factory.defaultControlRoom.hoursInBudget(75, 60, 5000);
factory.iMac.hoursTillComputerCrash(true);
std::cout << "good to go!" << std::endl;
}