Calculator

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
#include<iostream>
#define pi 3.142
using namespace std;
int GCD(int g, int d)
{
    while( 1 )
    {
        g= g% d;
		if( g == 0 )
			return d;
		d = d % g;

        if( d == 0 )
			return g;
    }
}
int main()
{
long double c, a, b, l, g, r, d, s, q, v;
d=100;
string x, j;
char playAgain='y';

cout <<"This program is capable of doing negatives!"<<endl;
while (playAgain !='n') 
{
cout << "Please choose the type of operation:"<< endl;
cout <<" "<<endl;
cout << "1) Addition" << endl;
cout << "2) Subtraction" << endl;
cout << "3) Multiplication" << endl;
cout << "4) Division" << endl;
cout << "5) F = C" << endl;
cout << "6) C = F" << endl; 
cout << "7) Fraction to %" << endl; 
cout << "8) % to Fraction" << endl; 
cout << "9) Fraction to Decimal"<< endl;
cout << "10) Decimal to Fraction"<< endl;
cout << "11) % to Decimal" <<endl;
cout << "12) Decimal to %" <<endl;
cout << "13) Circumference" << endl;
cout << "14) Area of a Circle" << endl; 
 
cout <<" "<< endl;
cin >> x;
cout <<" "<< endl;
if (x == "1")
{ cout << "Enter first number: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter second number: ";
cin >> b;
cout <<" "<< endl;
c  = a + b;
cout <<"Answer is: "<< c << endl;
}

if (x == "2")
{ cout << "Enter first number: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter second number: ";
cin >> b;
cout <<" "<< endl;
c  = a - b;
cout <<"Answer is: "<< c << endl;
}
 
if (x == "3")
{ cout << "Enter first number: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter second number: ";
cin >> b;
cout <<" "<< endl;
c  = a * b;
cout <<"Answer is: "<< c << endl;
}

if (x == "4")
{ cout << "Enter first number: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter second number: ";
cin >> b;
cout <<" "<< endl;
c  = a / b;
cout <<"Answer is: "<< c << endl;
}  
if (x == "5")
    {
         cout <<" " << endl;
          cout << "Enter the value of Fahrenheit: ";
          cin >> a;
          cout <<" " << endl;
          c =(a-32)*5/9;
          cout << c <<"C"<< endl;
    }
        
        if (x == "6")
    {
        cout <<" " << endl;
          cout << "Enter the value of Celsius: ";
          cin >> a;
          cout <<" " << endl;
    c = a * 9/5 + 32;
          cout << c <<"F"<< endl;
          }
          
          if (x == "7")
{ 
          cout << "Enter Numerator: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter Denominator: ";
cin >> b;
cout <<" "<< endl;
c  = a / b *100;
cout <<"Answer is: "<< c <<"%" << endl;
}  
if (x == "8")
{ 
          cout << "Enter %: ";
cin >> g;
cout <<" "<< endl;
cout <<"Answer is: "<< g <<"/100"<< endl;
cout <<"Do you want to change the Denominator? y/n "<< endl;
cout <<"Or do you want to simplfy it to the lowest? q/n"<< endl;
cout <<" "<<endl;
cin >> j;
cout <<" "<<endl;
}  
        if (j == "y")
        { 
              cout <<" "<<endl;
              cout << "What do you want to change the Denominator to?"<<endl; 
              cout <<" "<<endl;
              cin >> l;
              c = 100/l;
              r = g/c;   
          cout <<" "<<endl;
          cout <<"The Answer is: "<< r <<"/"<<l<<endl;
          } 
   if (j == "q")
        { 
v=GCD(g, d);
s= g/v;
q= d/v;

cout <<g<<"/"<<d<<" at simplest is "<<s<<"/"<<q<<"!"<<endl;
          } 
          
          if (x == "9")
{ 
          cout << "Enter Numerator: ";
cin >> a;
cout <<" "<< endl;
cout << "Enter Denominator: ";
cin >> b;
cout <<" "<< endl;
c  = a / b;
cout <<"Answer is: "<< c <<"!"<<endl;
}  

if (x == "10")
{ 
          cout << "Enter Decimal: ";
cin >> a;
cout <<" "<< endl;
g  = a * 100;
cout <<"Answer is: "<< g <<"/100!"<<endl;
cout <<"Do you want to change the Denominator? y/n "<< endl;
cout <<"Or do you want to simplfy it to the lowest? q/n"<< endl;
cout <<" "<<endl;
cin >> j;
cout <<" "<<endl;
}  
        if (j == "y")
        { 
              cout <<" "<<endl;
              cout << "What do you want to change the Denominator to?"<<endl; 
              cout <<" "<<endl;
              cin >> l;
              c = 100/l;
              r = g/c;   
          cout <<" "<<endl;
          cout <<"The Answer is: "<< r <<"/"<<l<<endl;
          } 
   if (j == "q")
        { 
v=GCD(g, d);
s= g/v;
q= d/v;

cout <<g<<"/"<<d<<" at simplest is "<<s<<"/"<<q<<"!"<<endl;
          }  
           if (x == "11")
{ 
          cout << "Enter %: ";
cin >> a;
cout <<" "<< endl;
c  = a / 100;
cout <<"Answer is: "<< c <<"!"<<endl;
}  

if (x == "12")
{ 
cout << "Enter Decimal: ";
cin >> a;
cout <<" "<< endl;
c  = a * 100;
cout <<"Answer is: "<< c <<"%!"<<endl;
}  
          
         if (x == "13")
{ cout << "Enter Radius: ";
cin >> a;
cout <<" "<< endl;
c = a * 2 * pi;
cout <<"Answer is: "<< c << endl;
}  

 if (x == "14")
{ cout << "Enter Radius: ";
cin >> a;
cout <<" "<< endl;
c = a * pi * a;
cout <<"Answer is: "<< c << endl;
}  

do {    cout <<" "<< endl;
		cout << "Do you want to calculate again? y/n: ";
		cin >> playAgain;
		cout <<" "<< endl;
        } while (playAgain !='y' && playAgain !='n');
	
}
cout << "Okay, see you next time. Thankyou for using a program by Anmol Sethi!" << endl;

system ("pause");
return (0);
}


I made a calculator code. Is there any way I can Improve?
In terms of the actual math behind everything, it all seems to work pretty nicely. However the code itself is fairly hard to go through and could use some cleaning up.

In general switch statements are preferred over a large amount of if/if else statements as they are easier for both the programmer and the compiler to look through. Also splitting up some of the equations into their own functions would help so everything doesn't look so crowded in main.

For addition/subtraction/multiplication/division you don't need to create 4 different things to check each of them, just make a char variable and have the user pick + - * /.

Also when starting to look at the code it's very hard to tell what each variable is meant to do (seeing as they are just a,b,c etc). It would help people looking through + you if you ever needed to change anything to name them in clearer ways to show what they are actually for.

All of this is optional, it just depends how far you want to go to organize your code.
Thankyou for the feedback. Ill consider it.
Topic archived. No new replies allowed.