aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--week07/ex1.cpp13
-rw-r--r--week07/ex2.cpp27
-rw-r--r--week07/ex3.cpp36
3 files changed, 76 insertions, 0 deletions
diff --git a/week07/ex1.cpp b/week07/ex1.cpp
new file mode 100644
index 0000000..8b630ad
--- /dev/null
+++ b/week07/ex1.cpp
@@ -0,0 +1,13 @@
+#include <iostream>
+
+void coordSum(double x1, double y1, double x2, double y2, double& sumx, double& sumy) {
+ sumx = x1 + x2;
+ sumy = y1 + y2;
+}
+
+int main() {
+ double x1, y1, x2, y2, sumx, sumy;
+ std::cin >> x1 >> y1 >> x2 >> y2;
+ coordSum(x1, y1, x2, y2, sumx, sumy);
+ std::cout << sumx << " " << sumy << std::endl;
+}
diff --git a/week07/ex2.cpp b/week07/ex2.cpp
new file mode 100644
index 0000000..2e0d16f
--- /dev/null
+++ b/week07/ex2.cpp
@@ -0,0 +1,27 @@
+#include <iostream>
+
+void sell(unsigned int& stock) {
+ stock--;
+}
+
+void receive(unsigned int& stock) {
+ stock += 10;
+}
+
+int main() {
+ unsigned int stockA = 0, stockB = 0, stockC = 0;
+ unsigned int* current = &stockA;
+
+ char buf;
+ std::cin >> buf;
+ while (buf != '$') {
+ switch(buf) {
+ case 'a': current = &stockA; break;
+ case 'b': current = &stockB; break;
+ case 'c': current = &stockC; break;
+ case 'S': sell(*current); break;
+ case 'R': receive(*current); break;
+ }
+ std::cin >> buf;
+ }
+}
diff --git a/week07/ex3.cpp b/week07/ex3.cpp
new file mode 100644
index 0000000..a616dc3
--- /dev/null
+++ b/week07/ex3.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+
+double abs(double val) {
+ return (val > 0) ? val : -val;
+}
+
+double square(double val) {
+ return val * val;
+}
+
+double afterFloatingPoint(double val) {
+ return val - (int)val;
+}
+
+double beforeFloatingPoint(double val) {
+ return (int)val;
+}
+
+int main() {
+ double (*useFunc)(double);
+ char buf;
+ std::cin >> buf;
+ switch(buf) {
+ case 'a': useFunc = &abs; break;
+ case 's': useFunc = &square; break;
+ case 'h': useFunc = &afterFloatingPoint; break;
+ case 'f': useFunc = &beforeFloatingPoint; break;
+ }
+
+ for (int i = 0; i < 5; i++) {
+ double temp;
+ std::cin >> temp;
+ std::cout << useFunc(temp) << " ";
+ }
+ std::cout << std::endl;
+}