简介
好像是4月份写的,挺古老的了,控件交互还是在类里的好 可以退格,也可以开方,当初写了好久,很多功能没弄出来,可惜FLTK不支持中文(c4droid的FLTK不支持中文)
代码
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Output.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Group.H>
#include <vector>
#include <sstream>
#include <cmath>
typedef void (*CALLBACK) (Fl_Widget * widget, void *data);
enum InputNum
{
NUMBER1,
NUMBER2,
RESULT
};
enum Op
{
OP_NONE,
OP_PLUS,
OP_MIN,
OP_MULT,
OP_DIV
};
int input_num = NUMBER1;
double number1 = 0;
double number2 = 0;
double ans = 0;
int op = OP_NONE;
void num_add(int, void *);
void updata(void *);
void clear_num();
void button_num_7_cb(Fl_Widget *, void *);
void button_num_8_cb(Fl_Widget *, void *);
void button_num_9_cb(Fl_Widget *, void *);
void button_num_4_cb(Fl_Widget *, void *);
void button_num_5_cb(Fl_Widget *, void *);
void button_num_6_cb(Fl_Widget *, void *);
void button_num_1_cb(Fl_Widget *, void *);
void button_num_2_cb(Fl_Widget *, void *);
void button_num_3_cb(Fl_Widget *, void *);
void button_op_plus_cb(Fl_Widget *, void *);
void button_op_min_cb(Fl_Widget *, void *);
void button_op_mult_cb(Fl_Widget *, void *);
void button_op_div_cb(Fl_Widget *, void *);
void button_solve_cb(Fl_Widget *, void *);
void button_clear_cb(Fl_Widget *, void *);
void button_num_0_cb(Fl_Widget *, void *);
void button_bs_cb(Fl_Widget *, void *);
void button_sqrt_cb(Fl_Widget *, void *);
void button_fun_cb(Fl_Widget *, void *);
int main()
{
fl_message("Welcome to FLTK Calculator!\nversion:1.1.1 beta");
FILE *f;
f = fopen("log.txt", "w");
fprintf(f, "");
fclose(f);
Fl_Window window(600, 800, "");
window.begin();
Fl_Group main(0, 200, 600, 600);
main.begin();
Fl_Button button_num_7(0, 400, 150, 100, "7");
Fl_Button button_num_8(150, 400, 150, 100, "8");
Fl_Button button_num_9(300, 400, 150, 100, "9");
Fl_Button button_num_4(0, 500, 150, 100, "4");
Fl_Button button_num_5(150, 500, 150, 100, "5");
Fl_Button button_num_6(300, 500, 150, 100, "6");
Fl_Button button_num_1(0, 600, 150, 100, "1");
Fl_Button button_num_2(150, 600, 150, 100, "2");
Fl_Button button_num_3(300, 600, 150, 100, "3");
Fl_Output output_output(0, 0, 600, 200, "0");
Fl_Button button_op_plus(450, 400, 150, 100, "+");
Fl_Button button_op_min(450, 500, 150, 100, "-");
Fl_Button button_op_mult(450, 600, 150, 100, "*");
Fl_Button button_op_div(450, 700, 150, 100, "/");
Fl_Button button_solve(300, 700, 150, 100, "=");
Fl_Button button_clear(0, 300, 150, 100, "C");
Fl_Button button_num_0(0, 700, 150, 100, "0");
Fl_Button button_bs(450, 300, 150, 100, "BS");
Fl_Button button_sqrt(0, 200, 150, 100, "SQRT");
Fl_Button button_fun(300, 300, 150, 100, "FUN");
main.end();
std::vector < void *>widgets = {
(void *)(&button_num_7),
(void *)(&button_num_8),
(void *)(&button_num_9),
(void *)(&button_num_4),
(void *)(&button_num_5),
(void *)(&button_num_6),
(void *)(&button_num_1),
(void *)(&button_num_2),
(void *)(&button_num_3),
(void *)(&output_output),
(void *)(&button_op_plus),
(void *)(&button_op_min),
(void *)(&button_op_mult),
(void *)(&button_op_div),
(void *)(&button_solve),
(void *)(&button_clear),
(void *)(&button_num_0),
(void *)(&main),
(void *)(&button_bs),
(void *)(&button_sqrt),
(void *)(&button_fun)
};
button_num_7.labelsize(100);
button_num_8.labelsize(100);
button_num_9.labelsize(100);
button_num_4.labelsize(100);
button_num_5.labelsize(100);
button_num_6.labelsize(100);
button_num_1.labelsize(100);
button_num_2.labelsize(100);
button_num_3.labelsize(100);
output_output.labelsize(50);
button_op_plus.labelsize(100);
button_op_min.labelsize(100);
button_op_mult.labelsize(100);
button_op_div.labelsize(100);
button_solve.labelsize(100);
button_clear.labelsize(100);
button_num_0.labelsize(100);
button_bs.labelsize(100);
button_sqrt.labelsize(50);
button_fun.labelsize(75);
button_num_7.callback(button_num_7_cb, &widgets);
button_num_8.callback(button_num_8_cb, &widgets);
button_num_9.callback(button_num_9_cb, &widgets);
button_num_4.callback(button_num_4_cb, &widgets);
button_num_5.callback(button_num_5_cb, &widgets);
button_num_6.callback(button_num_6_cb, &widgets);
button_num_1.callback(button_num_1_cb, &widgets);
button_num_2.callback(button_num_2_cb, &widgets);
button_num_3.callback(button_num_3_cb, &widgets);
button_op_plus.callback(button_op_plus_cb, &widgets);
button_op_min.callback(button_op_min_cb, &widgets);
button_op_mult.callback(button_op_mult_cb, &widgets);
button_op_div.callback(button_op_div_cb, &widgets);
button_solve.callback(button_solve_cb, &widgets);
button_clear.callback(button_clear_cb, &widgets);
button_num_0.callback(button_num_0_cb, &widgets);
button_bs.callback(button_bs_cb, &widgets);
button_sqrt.callback(button_sqrt_cb, &widgets);
button_fun.callback(button_fun_cb, &widgets);
window.end();
window.show();
return Fl::run();
}
void num_add(int n, void *data)
{
if (input_num == NUMBER1)
number1 = number1 * 10 + n;
else if (input_num == NUMBER2)
number2 = number2 * 10 + n;
else if (input_num == RESULT)
{
clear_num();
number1 = number1 * 10 + n;
input_num = NUMBER1;
}
updata(data);
}
void updata(void *data)
{
FILE *f;
f = fopen("log.txt", "a");
fprintf(f, "----------\nnumber1:%f\nnumber2:%f\nans:%f\nop:%f\n----------\n", number1, number2,
ans, op);
fclose(f);
std::stringstream str;
if (number2 == 0 && op == OP_DIV && input_num == NUMBER2)
str << "Error:divisor can't be 0";
else if (number1 > DBL_MAX)
str << "Error:esult is too big";
else if (input_num == NUMBER1)
str << number1;
else if (input_num == NUMBER2)
str << number2;
else if (input_num == RESULT)
str << ans;
((Fl_Output *) (((std::vector < void *>*)(data))->at(9)))->value(str.str().data());
}
void clear_num()
{
number1 = 0;
number2 = 0;
}
void button_num_7_cb(Fl_Widget * widget, void *data)
{
num_add(7, data);
}
void button_num_8_cb(Fl_Widget * widget, void *data)
{
num_add(8, data);
}
void button_num_9_cb(Fl_Widget * widget, void *data)
{
num_add(9, data);
}
void button_num_4_cb(Fl_Widget * widget, void *data)
{
num_add(4, data);
}
void button_num_5_cb(Fl_Widget * widget, void *data)
{
num_add(5, data);
}
void button_num_6_cb(Fl_Widget * widget, void *data)
{
num_add(6, data);
}
void button_num_1_cb(Fl_Widget * widget, void *data)
{
num_add(1, data);
}
void button_num_2_cb(Fl_Widget * widget, void *data)
{
num_add(2, data);
}
void button_num_3_cb(Fl_Widget * widget, void *data)
{
num_add(3, data);
}
void button_op_plus_cb(Fl_Widget * widget, void *data)
{
op = OP_PLUS;
if (input_num == NUMBER2)
{
ans = number1 + number2;
number1 = ans;
number2 = 0;
}
else if (input_num == RESULT)
{
number1 = ans;
number2 = 0;
}
updata(data);
input_num = NUMBER2;
}
void button_op_min_cb(Fl_Widget * widget, void *data)
{
op = OP_MIN;
if (input_num == NUMBER2)
{
ans = number1 - number2;
number1 = ans;
}
else if (input_num == RESULT)
{
number1 = ans;
number2 = 0;
}
updata(data);
input_num = NUMBER2;
}
void button_op_mult_cb(Fl_Widget * widget, void *data)
{
op = OP_MULT;
if (input_num == NUMBER2)
{
ans = number1 * number2;
number1 = ans;
}
else if (input_num == RESULT)
{
number1 = ans;
number2 = 0;
}
updata(data);
input_num = NUMBER2;
}
void button_op_div_cb(Fl_Widget * widget, void *data)
{
op = OP_DIV;
if (input_num == NUMBER2)
{
ans = number1 / number2;
number1 = ans;
}
else if (input_num == RESULT)
{
number1 = ans;
number2 = 0;
}
updata(data);
input_num = NUMBER2;
}
void button_solve_cb(Fl_Widget * widget, void *data)
{
if (op != OP_NONE && input_num == NUMBER2)
{
input_num = RESULT;
switch (op)
{
case OP_PLUS:
ans = number1 + number2;
break;
case OP_MIN:
ans = number1 - number2;
break;
case OP_MULT:
ans = number1 * number2;
break;
case OP_DIV:
ans = number1 / number2;
break;
}
clear_num();
updata(data);
}
}
void button_clear_cb(Fl_Widget * widget, void *data)
{
input_num = NUMBER1;
clear_num();
ans = 0;
updata(data);
}
void button_num_0_cb(Fl_Widget * widget, void *data)
{
num_add(0, data);
}
void button_bs_cb(Fl_Widget * widget, void *data)
{
if (input_num == NUMBER1)
number1 = long (number1 / 10);
else
if (input_num == NUMBER2)
number2 = long (number2 / 10);
updata(data);
}
void button_sqrt_cb(Fl_Widget * widget, void *data)
{
if (input_num == NUMBER1)
ans = sqrt(number1);
else if (input_num == NUMBER2)
ans = sqrt(number2);
else if (input_num == RESULT)
ans = sqrt(ans);
clear_num();
input_num = RESULT;
updata(data);
}
void button_fun_cb(Fl_Widget * widget, void *data)
{
((Fl_Group*)((std::vector<void*>*)(data))->at(17))->clear_visible();
}
运行效果
刚打开 输入111 输入+123= sqrt
|