Calculator class (string)
by Shinigami on Apr.05, 2012, under C++
Calculator class to count equations given to object as a string. This class can be easy extended.
File calculator.h
|
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 |
#ifndef CALCULATOR #define CALCULATOR #include <string> using namespace std; class Calculator { /* This class calculates equation given as a string. * Author: Doanatas Azaravicius * Date: 2012-03-17 * Site: cplusplus.shinigami.lt * * function show_errors() will return error code. * error codes: * 100 - no errors. * 102 - one of parenthesis was not found. * 103 - wrong equation. * 104 - function not found. * 105 - sqrt() can't be used on negative numbers. * 106 - 0 can't be divisor. */ public: Calculator(); ~Calculator() {} void add_to_equation(string); void delete_from_equation(unsigned int); void delete_from_equation(); double count_equation(); int show_errors() const; private: //Variables string equation; string parenthesis; string key_actions; static const int amount_of_actions = 3; static const int amount_of_functions = 12; string actions[amount_of_actions]; string functions[amount_of_functions]; double PI; int error_code; void find_parenthesis(int []); void find_function_name(int []); int find_key_operations(string, int []); int find_operation(string, bool); int find_first_key_operation(size_t *, int); string reverse_string(string); string double_to_string(double); double count_element_in_equation(string); double count_function(double, string); double string_to_double(string); bool check_key_action(string &, int &); void count_actions(string &); int factorial(int); }; #endif //CALCULATOR |
File calculator.cc
|
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
#include <stdlib.h> #include <cstring> #include <math.h> #include <sstream> #include "calculator.h" #include <iostream> using namespace std; Calculator::Calculator() { equation = ""; PI = 3.14159265; error_code = 100; parenthesis = "()"; key_actions = "*/+-"; functions[0] = "sqrt"; functions[1] = "sin"; functions[2] = "tan"; functions[3] = "cos"; functions[4] = "acos"; functions[5] = "asin"; functions[6] = "atan"; functions[7] = "ln"; functions[8] = "log"; functions[9] = "sinh"; functions[10] = "tanh"; functions[11] = "cosh"; actions[0] = "^"; actions[1] = "%"; actions[2] = "!"; } void Calculator::add_to_equation(string line) { equation += line; } void Calculator::delete_from_equation(unsigned int amount) { size_t length = equation.length(); if (length > amount) { length -= amount; equation.erase(length); } else { equation.clear(); } } void Calculator::delete_from_equation() { equation.clear(); } double Calculator::count_equation() { //variables used in this method. double result = 0.0; int found_parenthesis_location[2], temp_location; string element_of_equation; //check if equation are not empty. if (equation.empty()) { return result; } do { //find parenthesis locations in equation. find_parenthesis(found_parenthesis_location); if (error_code > 100) { return result; } //takes part of equation. element_of_equation = equation.substr(found_parenthesis_location[0] + 1, found_parenthesis_location[1] - found_parenthesis_location[0] - 1); //counts element of equation. result = count_element_in_equation(element_of_equation); if (error_code > 100) { return result; } //if parenthesis was found and they aren't in begining of equation search if here are function name before left parenthesis. if (found_parenthesis_location[0] > 0) { temp_location = found_parenthesis_location[0]; find_function_name(found_parenthesis_location); if (error_code > 100) { return result; } //if function name was found count that function if (temp_location > found_parenthesis_location[0]) { result = count_function(result, equation.substr(found_parenthesis_location[0], temp_location - found_parenthesis_location[0])); } if (error_code > 100) { return result; } } //if parenthesis was found change equation. Change part of equation which was counted to it's result and recalculate equation. if (found_parenthesis_location[0] > 0) { equation.replace(found_parenthesis_location[0], found_parenthesis_location[1] - found_parenthesis_location[0] + 1, double_to_string(result)); } } while (found_parenthesis_location[0] > 0); return result; } double Calculator::count_function(double number, string line) { double result; //checks which function need to be used and calculates given number. if (line == functions[0]) { if (number < 0) { error_code = 105; return number; } result = sqrt(number); } else if (line == functions[1]) result = sin(number * PI / 180); else if (line == functions[2]) result = tan(number * PI / 180); else if (line == functions[3]) result = cos(number * PI / 180); else if (line == functions[4]) result = acos(number) * 180 / PI; else if (line == functions[5]) result = asin(number) * 180 / PI; else if (line == functions[6]) result = atan(number) * 180 / PI; else if (line == functions[7]) result = log(number); else if (line == functions[8]) result = log10(number); else if (line == functions[9]) result = sinh(number); else if (line == functions[10]) result = tanh(number); else if (line == functions[11]) result = cosh(number); else { //if function name are not found in functions[] gives back error code and returns 0.0. error_code = 104; result = number; } return result; } double Calculator::count_element_in_equation(string line) { double result = 0.0, number[2]; int location[3]; int fined_key_action; count_actions(line); do { //find locations of key operations in string and operation. fined_key_action = find_key_operations(line, location); if (error_code > 100) { return result; } if (fined_key_action < 0) { return string_to_double(line); } //find number to count. number[0] = string_to_double(line.substr(location[0] + 1, location[1] - location[0] - 1)); number[1] = string_to_double(line.substr(location[1] + 1, location[2] - location[1] - 1)); //count numbers. if (fined_key_action == 0) { result = number[0] * number[1]; } else if (fined_key_action == 1) { if (number[1] == 0) { error_code = 106; return result; } result = number[0] / number[1]; } else if (fined_key_action == 2) { result = number[0] + number[1]; } else if (fined_key_action == 3) { result = number[0] - number[1]; } //if not all string was counted change string and recount it. if ((location[0] > 0) or (location[2] < (int)line.length())) { line.replace(location[0] + 1, location[2] - location[0] - 1, double_to_string(result)); } } while ((location[0] > 0) or (location[2] < (int)line.length())); return result; } void Calculator::find_function_name(int location[]) { char symbol = equation[location[0] - 1]; bool found_symbol = false; int location_temp; if (symbol != parenthesis[0]) { for (int i = 0; i < 4; i++) { if (symbol == key_actions[i]) { found_symbol = true; } } } if (!found_symbol) { location_temp = find_operation(reverse_string(equation.substr(0, location[0])), true); if (error_code > 100) { return; } if (location_temp < 0) { location[0] = 0; } else { location[0] = location[0] - location_temp; } } } void Calculator::find_parenthesis(int parenthesis_location[]) { string element_of_equation; //find right parenthesis location in equation. parenthesis_location[1] = equation.find(parenthesis[1]); if (parenthesis_location[1] != (int)string::npos) { //if right parenthesis was found, take part of equation for left parenthesis seatch. element_of_equation = reverse_string(equation.substr(0, parenthesis_location[1])); //fine left parenthesis. parenthesis_location[0] = element_of_equation.find(parenthesis[0]); if (parenthesis_location[0] != (int)string::npos) { //if left parenthesis was found specify location of left parenthesis in full equation. parenthesis_location[0] = element_of_equation.length() - parenthesis_location[0] - 1; } else { //if left parenthesis was not found, but right was found set error_code. error_code = 102; } } else { //if right parenthesis was not found, find left parenthesis location. parenthesis_location[0] = equation.find(parenthesis[0]); if (parenthesis_location[0] != (int)string::npos) { //if left parenthesis was found, but left not set error_code. error_code = 102; } else { //if no parenthesis was found, set locations of parenthesis before and behind equation. parenthesis_location[0] = -1; parenthesis_location[1] = equation.length(); } } } int Calculator::find_key_operations(string line, int location[]) { int result = -1; //find location of key operations between numbers. location[1] = find_operation(line, false); if (error_code > 100) { return result; } if (location[1] < 0) { return result; } location[0] = find_operation(reverse_string(line.substr(0, location[1])), true); if (error_code > 100) { return result; } if (location[0] < 0) { location[0] = -1; } else { location[0] = location[1] - location[0] - 1; } location[2] = find_operation(line.substr(location[1] + 1), true); if (error_code > 100) { return result; } if (location[2] < 0) { location[2] = location[1] + line.substr(location[1] + 1).length() + 1; } else { location[2] = location[2] + location[1] + 1; } //find key operation number to return. if (line[location[1]] == key_actions[0]) { return 0; } else if (line[location[1]] == key_actions[1]) { return 1; } else if (line[location[1]] == key_actions[2]) { return 2; } else if (line[location[1]] == key_actions[3]) { return 3; } return result; } int Calculator::find_operation(string line, bool reverse) { size_t found[4]; int result; bool check; for (int i = 0; i < 4; i++) { found[i] = line.find(key_actions[i]); } if (!reverse) { if (found[0] == string::npos and found[1] == string::npos) { if (found[2] == string::npos and found[3] == string::npos) { return -1; } else if (found[2] == 0) { error_code = 103; return 0; } else if (found[3] == 0) { result = find_operation(line.substr(1), reverse) + 1; if (error_code > 100) { return -1; } } else if (found[2] == string::npos) { result = found[3]; } else if (found[3] == string::npos) { result = found[2]; } if (found[2] > found[3]) { result = found[3]; } else { result = found[2]; } check = check_key_action(line, result); if (error_code > 100) { return result; } if (check) { return result; } return -1; } if (found[0] == 0 or found[1] == 0) { error_code = 103; return 0; } if (found[0] == string::npos) { result = found[1]; } else if (found[1] == string::npos) { result = found[0]; } else if (found[0] < found[1]) { result = found[0]; } else { result = found[1]; } check = check_key_action(line, result); if (error_code > 100) { return result; } if (check) { return result; } return -1; } else { for (int i = 0; i < 4; i++) { if (found[i] == string::npos) { found[i] = line.length(); } else if (found[i] == (line.length() - 1)) { found[i] = -1; } } if ((found[0] == found[1]) and (found[0] == found[2]) and (found[0] == found[3])) { return -1; } result = find_first_key_operation(found, 4); check = check_key_action(line, result); if (error_code > 100) { return result; } if (check) { return result; } return -1; } } bool Calculator::check_key_action(string & line, int & place) { size_t temp, length = line.length(); if (place < (int)length) { if (place == -1) { return true; } else { temp = place + 1; } if (line[temp] == key_actions[0] or line[temp] == key_actions[1] or line[temp] == key_actions[2]) { error_code = 103; return false; } else if (line[temp] == key_actions[3]) { if (line[place] == key_actions[3]) { line.replace(place, 2, "+"); } else if (line[place] == key_actions[2]) { line.replace(place, 2, "-"); } } } if (place > 0) { temp = place - 1; if (line[place] == key_actions[3]) { if (line[temp] == key_actions[3]) { line.replace(place - 1, 2, "+"); place = temp; } else if (line[temp] == key_actions[2]) { line.replace(place - 1, 2, "-"); place = temp; } else if (line[temp] == key_actions[0] or line[temp] == key_actions[1]) { place = temp; } } else { if (line[temp] == key_actions[0] or line[temp] == key_actions[1] or line[temp] == key_actions[2] or line[temp] == key_actions[3]) { error_code = 103; return false; } } } return true; } void Calculator::count_actions(string & line) { int location[3]; double number[2], result; for (int i = 0; i < amount_of_actions;) { location[1] = line.find(actions[i]); if (location[1] == (int)string::npos) { i++; } else { location[0] = find_operation(reverse_string(line.substr(0, location[1])), true); if (error_code > 100) { return; } if (location[0] < 0) { location[0] = -1; } else { location[0] = location[1] - location[0] - 1; } location[2] = find_operation(line.substr(location[1] + 1), true); if (error_code > 100) { return; } if (location[2] < 0) { location[2] = location[1] + line.substr(location[1] + 1).length() + 1; } else { location[2] = location[2] + location[1] + 1; } number[0] = string_to_double(line.substr(location[0] + 1, location[1] - location[0] - 1)); number[1] = string_to_double(line.substr(location[1] + 1, location[2] - location[1] - 1)); if (i == 0) { result = pow(number[0], number[1]); } else if (i == 1) { result = (int)number[0] % (int)number[1]; } else if (i == 2) { result = factorial((int)number[0]); } line.replace(location[0] + 1, location[2] - location[0] - 1, double_to_string(result)); } } } string Calculator::double_to_string(double number) { string result; stringstream str; str << number; str >> result; return result; } int Calculator::find_first_key_operation(size_t * x, int y) { int number; number = x[0]; for (int i = 1; i < y; i++) { if ((int)number > (int)x[i]) { number = x[i]; } } return number; } double Calculator::string_to_double(string line) { stringstream convert; convert << line; double result; convert >> result; return result; } string Calculator::reverse_string(string line) { string::reverse_iterator it; string result; for (it = line.rbegin(); it < line.rend(); it++) { result += *it; } return result; } int Calculator::factorial(int number) { if (number <= 1) { return number; } return (number * factorial(number - 1)); } int Calculator::show_errors() const { return error_code; } |
GUI for calculator class made with GTKMM
File gtkmm_calculator.h
|
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 |
#ifndef GTKMM_CALCULATOR_H #define GTKMM_CALCULATOR_H #include <gtkmm.h> #include "calculator.h" class GTKmmCalculator : public Gtk::Window { public: GTKmmCalculator(); virtual ~GTKmmCalculator(); protected: void put_in_calculator(Glib::ustring); void on_button_clicked(Glib::ustring); Glib::ustring double_to_string(double number); bool on_key_press_event(GdkEventKey *event); Gtk::Box VBox_main, HBox_entry, HBox_buttons, VBox_buttons1, VBox_buttons2, HBox_buttons1, HBox_buttons2, HBox_buttons3, HBox_buttons4, HBox_buttons5, HBox_buttons6, HBox_buttons7, HBox_buttons8; Gtk::Button Button_0, Button_1, Button_2, Button_3, Button_4, Button_5, Button_6, Button_7, Button_8, Button_9, Button_plus, Button_minus, Button_division, Button_multiplication, Button_comma, Button_equals, Button_clear, Button_delete, Button_right_parenthesis, Button_left_parenthesis, Button_pow, Button_sqrt, Button_mod, Button_sin, Button_cos, Button_tan, Button_log, Button_ln, Button_pi, Button_asin, Button_acos, Button_atan, Button_sinh, Button_cosh, Button_tanh, Button_e, Button_factorial; Gtk::Entry main_entry; Calculator equation; }; #endif //GTKMM_CALCULATOR_H |
File gtkmm_calculator.cc
|
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
#include "calculator.h" #include "gtkmm_calculator.h" #include <sstream> #include <cstdlib> #include <iostream> using namespace std; GTKmmCalculator::GTKmmCalculator() : VBox_main(Gtk::ORIENTATION_VERTICAL, 5), HBox_entry(Gtk::ORIENTATION_HORIZONTAL, 5), HBox_buttons(Gtk::ORIENTATION_HORIZONTAL, 5), VBox_buttons1(Gtk::ORIENTATION_VERTICAL, 2), VBox_buttons2(Gtk::ORIENTATION_VERTICAL, 2), HBox_buttons1(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons2(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons3(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons4(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons5(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons6(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons7(Gtk::ORIENTATION_HORIZONTAL, 2), HBox_buttons8(Gtk::ORIENTATION_HORIZONTAL, 2), Button_0("0"), Button_1("1"), Button_2("2"), Button_3("3"), Button_4("4"), Button_5("5"), Button_6("6"), Button_7("7"), Button_8("8"), Button_9("9"), Button_plus("+"), Button_minus("-"), Button_division("/"), Button_multiplication("*"), Button_comma("."), Button_equals("="), Button_clear("C"), Button_delete("<--"), Button_right_parenthesis(")"), Button_left_parenthesis("("), Button_pow("^"), Button_sqrt("sqrt"), Button_mod("%"), Button_sin("sin"), Button_cos("cos"), Button_tan("tan"), Button_log("log"), Button_ln("ln"), Button_pi("Pi"), Button_asin("asin"), Button_acos("acos"), Button_atan("atan"), Button_sinh("sinh"), Button_cosh("cosh"), Button_tanh("tanh"), Button_e("e"), Button_factorial("x!") { set_size_request(350, 80); set_border_width(1); set_title("Calculator"); set_resizable(false); set_icon_from_file("icon_calc.png"); Button_0.set_size_request(20,20); Button_1.set_size_request(20,20); Button_2.set_size_request(20,20); Button_3.set_size_request(20,20); Button_4.set_size_request(20,20); Button_5.set_size_request(20,20); Button_6.set_size_request(20,20); Button_7.set_size_request(20,20); Button_8.set_size_request(20,20); Button_9.set_size_request(20,20); Button_0.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "0" )); Button_1.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "1" )); Button_2.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "2" )); Button_3.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "3" )); Button_4.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "4" )); Button_5.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "5" )); Button_6.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "6" )); Button_7.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "7" )); Button_8.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "8" )); Button_9.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "9" )); Button_plus.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "+" )); Button_minus.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "-" )); Button_division.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "/" )); Button_multiplication.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "*" )); Button_comma.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "." )); Button_equals.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "=" )); Button_clear.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "C" )); Button_delete.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "del" )); Button_right_parenthesis.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), ")" )); Button_left_parenthesis.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "(" )); Button_pow.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "^" )); Button_sqrt.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "sqrt(" )); Button_mod.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "%" )); Button_sin.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "sin(" )); Button_cos.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "cos(" )); Button_tan.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "tan(" )); Button_log.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "log(" )); Button_ln.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "ln(" )); Button_pi.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "3.14159" )); Button_asin.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "asin(" )); Button_acos.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "acos(" )); Button_atan.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "atan(" )); Button_sinh.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "sinh(" )); Button_cosh.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "cosh(" )); Button_tanh.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "tanh(" )); Button_e.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "2.71828" )); Button_factorial.signal_clicked().connect( sigc::bind(sigc::mem_fun(*this, >KmmCalculator::on_button_clicked), "!" )); add(VBox_main); VBox_main.pack_start(HBox_entry); HBox_entry.pack_start(main_entry); main_entry.set_alignment(1.0); main_entry.set_text("0"); main_entry.set_editable(false); main_entry.set_has_frame(true); main_entry.set_activates_default(true); VBox_main.pack_start(HBox_buttons); HBox_buttons.pack_start(VBox_buttons1); HBox_buttons.pack_start(VBox_buttons2); VBox_buttons1.pack_start(HBox_buttons1); HBox_buttons1.pack_start(Button_0); HBox_buttons1.pack_start(Button_1); HBox_buttons1.pack_start(Button_2); HBox_buttons1.pack_start(Button_3); HBox_buttons1.pack_start(Button_4); VBox_buttons1.pack_start(HBox_buttons2); HBox_buttons2.pack_start(Button_5); HBox_buttons2.pack_start(Button_6); HBox_buttons2.pack_start(Button_7); HBox_buttons2.pack_start(Button_8); HBox_buttons2.pack_start(Button_9); VBox_buttons1.pack_start(HBox_buttons3); HBox_buttons3.pack_start(Button_plus); HBox_buttons3.pack_start(Button_minus); HBox_buttons3.pack_start(Button_division); HBox_buttons3.pack_start(Button_multiplication); HBox_buttons3.pack_start(Button_comma); VBox_buttons1.pack_start(HBox_buttons4); HBox_buttons4.pack_start(Button_delete); HBox_buttons4.pack_start(Button_clear); HBox_buttons4.pack_start(Button_equals); VBox_buttons2.pack_start(HBox_buttons5); HBox_buttons5.pack_start(Button_left_parenthesis); HBox_buttons5.pack_start(Button_right_parenthesis); HBox_buttons5.pack_start(Button_pow); HBox_buttons5.pack_start(Button_mod); HBox_buttons5.pack_start(Button_sqrt); VBox_buttons2.pack_start(HBox_buttons6); HBox_buttons6.pack_start(Button_sin); HBox_buttons6.pack_start(Button_cos); HBox_buttons6.pack_start(Button_tan); HBox_buttons6.pack_start(Button_log); HBox_buttons6.pack_start(Button_ln); VBox_buttons2.pack_start(HBox_buttons7); HBox_buttons7.pack_start(Button_asin); HBox_buttons7.pack_start(Button_acos); HBox_buttons7.pack_start(Button_atan); HBox_buttons7.pack_start(Button_sinh); HBox_buttons7.pack_start(Button_cosh); VBox_buttons2.pack_start(HBox_buttons8); HBox_buttons8.pack_start(Button_tanh); HBox_buttons8.pack_start(Button_factorial); HBox_buttons8.pack_start(Button_pi); HBox_buttons8.pack_start(Button_e); Button_equals.set_can_default(); Button_equals.grab_default(); add_events(Gdk::KEY_PRESS_MASK); signal_key_press_event().connect(sigc::mem_fun(*this, >KmmCalculator::on_key_press_event)); show_all_children(); } GTKmmCalculator::~GTKmmCalculator() { } void GTKmmCalculator::on_button_clicked(Glib::ustring line) { if (line == "C") { main_entry.set_text("0"); equation.delete_from_equation(); } else if (line == "=") { Glib::ustring result = double_to_string(equation.count_equation()); if (equation.show_errors() > 100) { result = "Error "; result += double_to_string(equation.show_errors()); result += ": bad equation."; } equation.delete_from_equation(); equation.add_to_equation(result); main_entry.set_text(result); } else if (line == "del") { equation.delete_from_equation(1); Glib::ustring entry_text; entry_text = main_entry.get_text(); if (entry_text.length() == 1) { main_entry.set_text("0"); } else { entry_text.erase(entry_text.length() - 1); main_entry.set_text(entry_text); } } else { Glib::ustring result = main_entry.get_text(); if (result == "0" && line != ".") { main_entry.set_text(""); } put_in_calculator(line); } } void GTKmmCalculator::put_in_calculator(Glib::ustring line) { main_entry.set_text(main_entry.get_text() + line); equation.add_to_equation(line); } Glib::ustring GTKmmCalculator::double_to_string(double number) { Glib::ustring result; stringstream str; str << number; result = str.str(); return result; } bool GTKmmCalculator::on_key_press_event(GdkEventKey *event) { switch (event->keyval) { case GDK_KEY_1: on_button_clicked("1"); break; case GDK_KEY_2: on_button_clicked("2"); break; case GDK_KEY_3: on_button_clicked("3"); break; case GDK_KEY_4: on_button_clicked("4"); break; case GDK_KEY_5: on_button_clicked("5"); break; case GDK_KEY_6: on_button_clicked("6"); break; case GDK_KEY_7: on_button_clicked("7"); break; case GDK_KEY_8: on_button_clicked("8"); break; case GDK_KEY_9: on_button_clicked("9"); break; case GDK_KEY_0: on_button_clicked("0"); break; case GDK_KEY_period: on_button_clicked("."); break; case GDK_KEY_minus: on_button_clicked("-"); break; case GDK_KEY_plus: on_button_clicked("+"); break; case GDK_KEY_asterisk: on_button_clicked("*"); break; case GDK_KEY_slash: on_button_clicked("/"); break; case GDK_KEY_parenleft: on_button_clicked("("); break; case GDK_KEY_parenright: on_button_clicked(")"); break; case GDK_KEY_percent: on_button_clicked("%"); break; case GDK_KEY_BackSpace: on_button_clicked("del"); break; case GDK_KEY_Delete: on_button_clicked("C"); break; case GDK_KEY_equal: on_button_clicked("="); break; default : ; } return true; } |
File run.cc
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include "gtkmm_calculator.h" #include <gtkmm/main.h> int main(int argc, char *argv[]) { Gtk::Main kit(argc, argv); GTKmmCalculator window; Gtk::Main::run(window); return 0; } |
compile command for linux
g++ gtkmm_calculator.cc gtkmm_run.cc calculator.cc -o run pkg-config gtkmm-3.0 --cflags --libs
For it to work you need this pic to
