-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
193 lines (160 loc) · 6.61 KB
/
main.cpp
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
#include <gtk/gtk.h>
#include <string>
#include <stack>
#include <sstream>
#include <stdexcept> // For std::runtime_error
#include <cmath> // For std::isnan and std::isinf
// Utility function to evaluate the arithmetic expression
double evaluate_expression(const std::string &expression) {
std::istringstream iss(expression);
std::stack<double> values;
std::stack<char> operators;
double number;
char token;
auto apply_operator = [](char op, double val1, double val2) -> double {
switch (op) {
case '+': return val1 + val2;
case '-': return val1 - val2;
case '*': return val1 * val2;
case '/':
if (val2 == 0) throw std::runtime_error("Division by zero");
return val1 / val2;
default: throw std::runtime_error("Unknown operator");
}
};
while (iss >> token) {
if (isdigit(token) || token == '.') {
iss.putback(token);
iss >> number;
values.push(number);
} else if (token == '(') {
operators.push(token);
} else if (token == ')') {
while (!operators.empty() && operators.top() != '(') {
char op = operators.top();
operators.pop();
if (values.size() < 2) throw std::runtime_error("Invalid expression");
double val2 = values.top();
values.pop();
double val1 = values.top();
values.pop();
values.push(apply_operator(op, val1, val2));
}
if (!operators.empty()) {
operators.pop();
}
} else {
while (!operators.empty() && operators.top() != '(' &&
((token == '+' || token == '-') || (token == '*' || token == '/'))) {
char op = operators.top();
operators.pop();
if (values.size() < 2) throw std::runtime_error("Invalid expression");
double val2 = values.top();
values.pop();
double val1 = values.top();
values.pop();
values.push(apply_operator(op, val1, val2));
}
operators.push(token);
}
}
while (!operators.empty()) {
char op = operators.top();
operators.pop();
if (values.size() < 2) throw std::runtime_error("Invalid expression");
double val2 = values.top();
values.pop();
double val1 = values.top();
values.pop();
values.push(apply_operator(op, val1, val2));
}
if (values.size() != 1) {
throw std::runtime_error("Invalid expression");
}
return values.top();
}
// Callback functions for button clicks
static void on_number_clicked(GtkButton *button, gpointer data) {
const gchar *num = gtk_button_get_label(button);
GtkEntry *entry = GTK_ENTRY(data);
const gchar *current_text = gtk_entry_get_text(entry);
gchar *new_text = g_strdup_printf("%s%s", current_text, num);
gtk_entry_set_text(entry, new_text);
g_free(new_text);
}
static void on_clear_clicked(GtkButton *button, gpointer data) {
GtkEntry *entry = GTK_ENTRY(data);
gtk_entry_set_text(entry, "");
}
static void on_equal_clicked(GtkButton *button, gpointer data) {
GtkEntry *entry = GTK_ENTRY(data);
const gchar *expression = gtk_entry_get_text(entry);
try {
double result = evaluate_expression(expression);
// Check for invalid result
if (std::isnan(result) || std::isinf(result)) {
gtk_entry_set_text(entry, "Error");
} else {
gchar *result_text;
// Check if the result is an integer
if (result == (int)result) {
result_text = g_strdup_printf("%d", (int)result);
} else {
result_text = g_strdup_printf("%f", result);
}
gtk_entry_set_text(entry, result_text);
g_free(result_text);
}
} catch (const std::exception &e) {
gtk_entry_set_text(entry, "Error");
}
}
// Main function to create the application
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Fast Basic Calculator 2.0");
gtk_window_set_icon_from_file(GTK_WINDOW(window), "icon.png", NULL);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE); // You won't be able to resize the window
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
GtkWidget *entry = gtk_entry_new();
gtk_grid_attach(GTK_GRID(grid), entry, 0, 0, 4, 1);
gtk_widget_set_size_request(entry, 250, 70); // Width: 250, Height: 70
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(provider,
"entry { font-size: 30px; }", -1, NULL);
GtkStyleContext *context = gtk_widget_get_style_context(entry);
gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
const gchar *button_labels[4][4] = {
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", "C", "=", "+"}
};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
GtkWidget *button = gtk_button_new_with_label(button_labels[i][j]);
gtk_widget_set_size_request(button, 100, 150); // Changing the button size
GtkCssProvider *button_provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(button_provider,
"button { font-size: 60px; }", -1, NULL);
GtkStyleContext *button_context = gtk_widget_get_style_context(button);
gtk_style_context_add_provider(button_context, GTK_STYLE_PROVIDER(button_provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_grid_attach(GTK_GRID(grid), button, j, i + 1, 1, 1);
if (g_strcmp0(button_labels[i][j], "C") == 0) {
g_signal_connect(button, "clicked", G_CALLBACK(on_clear_clicked), entry);
} else if (g_strcmp0(button_labels[i][j], "=") == 0) {
g_signal_connect(button, "clicked", G_CALLBACK(on_equal_clicked), entry);
} else {
g_signal_connect(button, "clicked", G_CALLBACK(on_number_clicked), entry);
}
}
}
gtk_widget_show_all(window);
gtk_main();
return 0;
}