# Untitled

By: a guest on Sep 18th, 2012  |  syntax: None  |  size: 1.45 KB  |  hits: 18  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. /*
2. Program: Challenge 2
3. Author: Chapman Siu
4. Date: 5 Aug
5.
6. Description:
7. Hello, coders! An important part of programming is being able to apply your programs,
8. so your challenge for today is to create a calculator application that has use in your
9. life. It might be an interest calculator, or it might be something that you can use in
10. the classroom. For example, if you were in physics class, you might want to make a
11. F = M * A calc.
12. EXTRA CREDIT: make the calculator have multiple functions!
13. Not only should it be able to calculate F = M * A, but also A = F/M, and M = F/A!
14. */
15.
16. #include <iostream>
17. #include <string>
18.
19. using namespace std;
20.
21. double f(int,int);
22. double a(int,int);
23. double m(int,int);
24.
25. double f(int a, int m) {
26.         return a*m;
27. }
28.
29. double a(int f,int m) {
30.         return f/m;
31. }
32.
33. double m(int f, int a) {
34.         return f/a;
35. }
36.
37. int main()
38. {
39.         int F, A, M ;
40.         double ans;
41.         string what;
42.
43.         cout << "What do you want to calculate?" <<endl;
44.     cin >> what;
45.
46.         if (what == "f") {
47.                 cout << "What is the value of 'A'?" << endl;
48.                 cin >> A;
49.                 cout << "What is the value of 'M'?" << endl;
50.                 cin >> M;
51.                 ans = f(A,M);
52.         }
53.         if (what == "a") {
54.                 cout << "What is the value of 'F'?" << endl;
55.                 cin >> F;
56.                 cout << "What is the value of 'M'?" << endl;
57.                 cin >> M;
58.                 ans = a(F,M);
59.         }
60.         if (what == "m") {
61.                 cout << "What is the value of 'F'?" << endl;
62.                 cin >> F;
63.                 cout << "What is the value of 'A'?" << endl;
64.                 cin >> A;
65.                 ans = m(F,A);
66.         }
67.
68.         cout << "The value of '"<<what<<"' is " << ans;
69.         return 0;
70. }