mirror of https://github.com/daleclack/My_GtkUi
Update calc app
This commit is contained in:
parent
c8c5cbaca9
commit
d48ada0e0d
|
@ -44,7 +44,7 @@
|
|||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<!-- n-columns=5 n-rows=4 -->
|
||||
<!-- n-columns=5 n-rows=5 -->
|
||||
<object class="GtkGrid">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
|
@ -64,7 +64,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -77,7 +77,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -90,7 +90,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -103,7 +103,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">3</property>
|
||||
<property name="top-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -116,7 +116,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -129,7 +129,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -142,7 +142,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -155,7 +155,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">3</property>
|
||||
<property name="top-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -168,7 +168,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">2</property>
|
||||
<property name="top-attach">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -181,7 +181,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
<property name="top-attach">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -194,7 +194,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">2</property>
|
||||
<property name="top-attach">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -207,7 +207,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">3</property>
|
||||
<property name="top-attach">2</property>
|
||||
<property name="top-attach">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -220,7 +220,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">3</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -233,7 +233,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">3</property>
|
||||
<property name="top-attach">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -245,8 +245,8 @@
|
|||
<property name="relief">none</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">3</property>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -259,7 +259,7 @@
|
|||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">3</property>
|
||||
<property name="top-attach">3</property>
|
||||
<property name="top-attach">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
|
@ -299,7 +299,59 @@
|
|||
<packing>
|
||||
<property name="left-attach">4</property>
|
||||
<property name="top-attach">2</property>
|
||||
<property name="height">2</property>
|
||||
<property name="height">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="btnpoint">
|
||||
<property name="label" translatable="yes">.</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<property name="relief">none</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="btnsqrt">
|
||||
<property name="label" translatable="yes">sqrt(x)</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<property name="relief">none</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">2</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="btnpow">
|
||||
<property name="label" translatable="yes">x^2</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<property name="relief">none</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">3</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="btndiv100">
|
||||
<property name="label" translatable="yes">%</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<property name="relief">none</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
|
|
|
@ -19,12 +19,15 @@ private:
|
|||
|
||||
// Child widgets
|
||||
Gtk::Entry *entry_ans;
|
||||
Gtk::Button *btns[16], *btnback, *btnclear, *btnanswer;
|
||||
Gtk::Button *btns[18], *btnback, *btnclear, *btnanswer,
|
||||
*btnsqrt, *btnpow;
|
||||
|
||||
// Signal Handlers
|
||||
void btns_clicked(Gtk::Button *button);
|
||||
void btnclear_clicked();
|
||||
void btnback_clicked();
|
||||
void btnanswer_clicked();
|
||||
void btnpow_clicked();
|
||||
void btnsqrt_clicked();
|
||||
void entry_ans_focus();
|
||||
};
|
|
@ -32,12 +32,18 @@ CalcApp::CalcApp(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_
|
|||
ref_builder->get_widget("btndiv", btns[13]);
|
||||
ref_builder->get_widget("btnleft", btns[14]);
|
||||
ref_builder->get_widget("btnright", btns[15]);
|
||||
ref_builder->get_widget("btnpoint",btns[16]);
|
||||
ref_builder->get_widget("btndiv100",btns[17]);
|
||||
ref_builder->get_widget("btnsqrt",btnsqrt);
|
||||
ref_builder->get_widget("btnpow",btnpow);
|
||||
|
||||
// Link Signals
|
||||
btnclear->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnclear_clicked));
|
||||
btnback->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnback_clicked));
|
||||
btnanswer->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnanswer_clicked));
|
||||
for (int i = 0; i < 16; i++)
|
||||
btnpow->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnpow_clicked));
|
||||
btnsqrt->signal_clicked().connect(sigc::mem_fun(*this,&CalcApp::btnsqrt_clicked));
|
||||
for (int i = 0; i < 18; i++)
|
||||
{
|
||||
btns[i]->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &CalcApp::btns_clicked), btns[i]));
|
||||
}
|
||||
|
@ -75,6 +81,18 @@ void CalcApp::btnclear_clicked(){
|
|||
entry_ans->set_text("");
|
||||
}
|
||||
|
||||
void CalcApp::btnpow_clicked(){
|
||||
Glib::ustring string = entry_ans->get_text();
|
||||
Glib::ustring string2 = string + "^2";
|
||||
entry_ans->set_text(string2);
|
||||
}
|
||||
|
||||
void CalcApp::btnsqrt_clicked(){
|
||||
Glib::ustring string = entry_ans->get_text();
|
||||
Glib::ustring string2 = string + "^(1/2)";
|
||||
entry_ans->set_text(string2);
|
||||
}
|
||||
|
||||
void CalcApp::btnback_clicked(){
|
||||
// Get Original text and add number from button
|
||||
|
||||
|
@ -102,9 +120,9 @@ void CalcApp::btnanswer_clicked(){
|
|||
|
||||
// Calculation
|
||||
calc_reset();
|
||||
int result = calc_expression_value(text.c_str());
|
||||
double result = calc_expression_value(text.c_str());
|
||||
char result_str[40];
|
||||
sprintf(result_str,"%d",result);
|
||||
sprintf(result_str,"%f",result);
|
||||
entry_ans->set_text(Glib::ustring(result_str));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include "calc.hh"
|
||||
|
||||
static int pos = 0;
|
||||
|
@ -9,33 +10,63 @@ void calc_reset(){
|
|||
pos = 0;
|
||||
}
|
||||
|
||||
int calc_factor_value(const char * factor){
|
||||
int result = 0;
|
||||
double calc_factor_value(const char * factor){
|
||||
double result1 = 0.0, result2 = 0.0, final_result = 0.0;
|
||||
char c = factor[pos];
|
||||
if( c == '('){ // if expression has '(', calculate the expression in the '()'
|
||||
pos++;
|
||||
result = calc_expression_value(factor);
|
||||
pos++;
|
||||
pos++; // Pass the '('
|
||||
result1 = calc_expression_value(factor);
|
||||
pos++; // Pass the ')'
|
||||
}else{
|
||||
while(isdigit(c)){ // Get the number to calculate
|
||||
result = 10 * result + c - '0';
|
||||
result1 = 10 * result1 + c - '0';
|
||||
pos++;
|
||||
c = factor[pos];
|
||||
}
|
||||
if(c == '.'){ // Get number between 0 to 1
|
||||
pos++; // Pass the '.'
|
||||
c = factor[pos];
|
||||
while(isdigit(c)){
|
||||
result2 = 0.1 * result2 + (c - '0') * 0.1;
|
||||
pos++;
|
||||
c = factor[pos];
|
||||
}
|
||||
}
|
||||
final_result = result1 + result2;
|
||||
if(c == '%'){
|
||||
final_result /= 100.0; // Pass the '%'
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return final_result;
|
||||
}
|
||||
|
||||
int calc_term_value(const char * term){
|
||||
int result = calc_factor_value(term); // Get the first number
|
||||
while(true){
|
||||
double calc_term_value(const char * term){
|
||||
double result = calc_factor_value(term); // Get the first number
|
||||
double value = 0.0;
|
||||
while(pos < strlen(term)){
|
||||
//pos++; // Calc the multiplication and divide
|
||||
char op = term[pos];
|
||||
if(op == '*' || op == '/'){
|
||||
pos++;
|
||||
int value = calc_factor_value(term); // Get another value
|
||||
if(op == '*') result *= value;
|
||||
else result /= value;
|
||||
if(op == '^' || op == '*' || op == '/'){
|
||||
switch (op)
|
||||
{
|
||||
case '^':
|
||||
pos++;
|
||||
value = calc_factor_value(term); // Pow
|
||||
result = pow(result,value);
|
||||
break;
|
||||
case '*':
|
||||
pos++;
|
||||
value = calc_factor_value(term); // Subb
|
||||
result *= value;
|
||||
break;
|
||||
case '/':
|
||||
pos++;
|
||||
value = calc_factor_value(term); // Get another value
|
||||
result /= value;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
|
@ -43,14 +74,14 @@ int calc_term_value(const char * term){
|
|||
return result;
|
||||
}
|
||||
|
||||
int calc_expression_value(const char * expression){
|
||||
int result = calc_term_value(expression); // Calc the result of expression
|
||||
double calc_expression_value(const char * expression){
|
||||
double result = calc_term_value(expression); // Calc the result of expression
|
||||
//bool more = true;
|
||||
while(true){
|
||||
while(pos < strlen(expression)){
|
||||
char op = expression[pos];
|
||||
if(op == '+' || op == '-'){ // Calculate for add and subb
|
||||
pos++;
|
||||
int value = calc_term_value(expression);
|
||||
double value = calc_term_value(expression);
|
||||
if( op == '+') result += value;
|
||||
else result -= value;
|
||||
}else{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
int calc_expression_value(const char * expression);
|
||||
double calc_expression_value(const char * expression);
|
||||
|
||||
void calc_reset();
|
||||
|
|
Loading…
Reference in New Issue