Making a Calculator

Hello, all. I'm currently trying to make a visual calculator, but I'm having some serious issues with the logic regarding the mathematical functions of the calculator. My code is as follows:

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
#include "library.h"

void button(int s, int x, int y, int & a, string val[])
{
	set_pen_color(color::black);
	fill_rectangle(x-2,y-2,s+4,s+4);
	set_pen_color(color::grey);
	fill_rectangle(x,y,s,s);
	move_to(x+(s/4.0),y+(0.75*s));
	set_font_size(s);
	set_pen_color(color::black);
	write_string(val[a]);
	a++;
}
void row_buttons(int s, int x, int y, int & a, string val[])
{
	int i = 0;
	while(i<4)
	{
		button(s,x,y,a,val);
		x = x+1.5*s;
		i++;
	}
}
void block_buttons(int s, int x, int y, int & a, string val[])
{
	double height_space = (4*y*1.5)/8.0;
	int i = 0;
	while(i<4)
	{
		row_buttons(s,x,y,a,val);
		y = y+height_space;
		i++;
	} 
}
void mouse(int s, int x, int y, int window_w, int window_h, string & num)
{
	double height_space = (4*y*1.5)/8.0;
	double x_space = window_w/15;
	wait_for_mouse_click();
	int const click_x = get_click_x();
	int const click_y = get_click_y();
	if(click_x >= x && click_x <= x+s+2)
	{
		if(click_y >= y && click_y <= y+s+4)
		{
			num = "7";
		}
		else if(click_y >= y+height_space && click_y <= y+(s+4)+height_space)
		{
			num = "4";
		}
		else if(click_y >= y+2*height_space && click_y <= y+(s+4)+2*height_space)
		{
			num = "1";
		}
		else if(click_y >= y+3*height_space && click_y <= y+(s+4)+3*height_space)
		{
			num = "C";
		}
	}
	else if(click_x >= x+(s+4)+ x_space && click_x <= x+2*(s+4)+ x_space)
	{
		if(click_y >= y && click_y <= y+s+4)
		{
			num = "8";
		}
		else if(click_y >= y+height_space && click_y <= y+(s+4)+height_space)
		{
			num = "5";
		}
		else if(click_y >= y+2*height_space && click_y <= y+(s+4)+2*height_space)
		{
			num = "2";
		}
		else if(click_y >= y+3*height_space && click_y <= y+(s+4)+3*height_space)
		{
			num = "0";
		}
	}
	else if(click_x >= x+2*(s+4)+ 2*x_space && click_x <= x+3*(s+4)+ 2*x_space)
	{
		if(click_y >= y && click_y <= y+s+4)
		{
			num = "9";
		}
		else if(click_y >= y+height_space && click_y <= y+(s+4)+height_space)
		{
			num = "6";
		}
		else if(click_y >= y+2*height_space && click_y <= y+(s+4)+2*height_space)
		{
			num = "3";
		}
		else if(click_y >= y+3*height_space && click_y <= y+(s+4)+3*height_space)
		{
			num = "=";
		}
	}
	else if(click_x >= x+3*(s+4)+ 3*x_space && click_x <= x+4*(s+4)+ 3*x_space)
	{
		if(click_y >= y && click_y <= y+s+4)
		{
			num = "+";
		}
		else if(click_y >= y+height_space && click_y <= y+(s+4)+height_space)
		{
			num = "-";
		}
		else if(click_y >= y+2*height_space && click_y <= y+(s+4)+2*height_space)
		{
			num = "*";
		}
		else if(click_y >= y+3*height_space && click_y <= y+(s+4)+3*height_space)
		{
			num = "/";
		}
	}
}
void display(double s, double x, double y, double window_w, double window_h, string & num, int & mult)
{
	double rect_height = s;
	double height_space = (4*y*1.5)/8.0;
	double rect_width = window_w - 2*x;
	set_pen_color(color::black);
	fill_rectangle(x,y-height_space,rect_width,rect_height);
	set_pen_color(color::blue);
	set_font_size(s);
	move_to(x,y-height_space+s);
	write_string(mult);
}
void click(int s, int x, int y, int window_w, int window_h, string & num, int & mult, int & op)
{
	while(true)
	{
		display(s,x,y,window_w,window_h,num,mult);
		mouse(s,x,y,window_w,window_h, num);
		if(num == "9")
		{
			mult = mult*10 + 9;
		}
		else if(num == "8")
		{
			mult = mult*10 + 8;
		}
		else if(num == "7")
		{
			mult = mult*10 + 7;
		}
		else if(num == "6")
		{
			mult = mult*10 + 6;
		}
		else if(num == "5")
		{
			mult = mult*10 + 5;
		}
		else if(num == "4")
		{
			mult = mult*10 + 4;
		}
		else if(num == "3")
		{
			mult = mult*10 + 3;
		}
		else if(num == "2")
		{
			mult = mult*10 + 2;
		}
		else if(num == "1")
		{
			mult = mult*10 + 1;
		}
		else if(num == "0")
		{
			mult = mult*10 + 0;
		}
		else if(num == "C")
		{
			mult = 0;
			num = "0";
			int rect_height = s;
			int height_space = (4*y*1.5)/8.0;
			int rect_width = window_w - 2*x;
			set_pen_color(color::black);
			fill_rectangle(x,y-height_space,rect_width,rect_height);
		}
		else if(num == "+")
		{
			op = mult + op;
			mult = op;
		}
		else if(num == "-")
		{
			if(op == 0)
			{
				op = mult - op;
			}
			else
			{
				op = -1*(mult - op);
			}
			mult = 0;
			print(op);
		}
		else if(num == "=")
		{
			print(op);
		}
	}
}
void main()
{
	string val[16] = {"7","8","9","+","4","5","6","-","1","2","3","*","C","0","=","/"};
	int a = 0;
	string num = "0";
	int mult = 0;
	int op = 0;
	double window_w = 300;
	double window_h = 400;
	double x = window_w/25;
	double y = window_h/4;
	double s = window_w/6.0;
	make_window(window_w,window_h);
	block_buttons(s,x,y,a,val);
	click(s,x,y,window_w,window_h,num,mult,op);
}

I imagine I'll have to make the mathematical button responses (+,-,*,/,=) work in the click function, as that's where I recognize each string returned by each button press. I started trying to do this with +, and I figure the logic will be similar for each mathematical operation once I figure out how to do one, but I can't for the life of me figure out how to make this work. I started out by trying to store mult, which is my entered number, in another int and then set mult to zero to enable another number to be entered, but this doesn't seem right. I'm not sure where to go from here. Any suggestions?
Topic archived. No new replies allowed.