### 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227`` ``````#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.