My first attempt to create a calculator as a novice in Qt programming
-
- In the slot:
QString s = reinterpret_cast<QPushButton*>(sender())->text();
- Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split
-
@jsulm said in My first attempt to create a calculator as a novice in Qt programming:
- In the slot:
QString s = reinterpret_cast<QPushButton*>(sender())->text();
- Why? If user enters number 123, why do you want to store this number in a vector like ['1', '2', '3']? It is actually a vector of strings. For that you can split the string, see http://doc.qt.io/qt-5/qstring.html#split
Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.
Guys, I think the program is much more complicated than these items. The app should be able to separate the operands from operators. And also it should be able to observe the priority of the operators, for example:
2+3*4 to be 14 not 20.What methods would you generally use for each step?
I'm thinking of two tools: first the Dijkstra's algorithm to make the statement a Reverse Polish Notation and then use a stack for calculating. Do you agree?
-
@tomy said in My first attempt to create a calculator as a novice in Qt programming:
Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.
But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
Vector of char would be: ['1', '2', '3', '+', '2', '5, '6'] -
@jsulm said in My first attempt to create a calculator as a novice in Qt programming:
@tomy said in My first attempt to create a calculator as a novice in Qt programming:
Because the user types something like "123+256" so it con't be seemingly stored as a (vector of) int.
But it can be stored as a vector of strings: ["123", "+", "256"] - that's what I said.
Vector of char would be: ['1', '2', '3', '+', '2', '5, '6']Buttons send each number as a string; "1", "3", "+" and so on. When the user types "123+256" a string containing "123+256" will be shown on lineEdit. Hence, I should somehow parse to make operators and operands detached. That was why (at the time) I thought of a vector of chars.
But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:
2+30-12/3+(4^8-1)+sqrt(65)*2.12 -
Hi all,
It's done. :) :) :)
The app is finished and it works fine. Please download it from here:
http://www.4shared.com/file/H5brsoaPce/My_First_Calculator.htmlIt needs only a few .dll files for to be run.
I have some question please:
1- How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
2- I want to design the buttons better from the size, position end etc. How can I do this?
3- Is it possible to make it appear like a professional app? -
@tomy said in My first attempt to create a calculator as a novice in Qt programming:
But anyway, I'm thinking of a good algorithm to cover expressions like this, that is when we input all of this line once, it makes a correct result:
Reverse polish notation is what you're probably searching for. See here.
-
This is a infix to postfix conversion for the 4 basic operations and parenthesis I wrote some time ago. it works just for integers but it's a start. once you have the string in postfix (i.e. reverse polish) the calculation is very easyEdit
Ignore my sh*ty code, see http://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm#C for an idea of converting infix to postfix. thanks @kshegunov for talking some sense into me
-
Thank you guys but the work is done!
I appreciate your reactions so much.
Please help me on these questions:@tomy said in My first attempt to create a calculator as a novice in Qt programming:
Hi all,
It's done. :) :) :)
The app is finished and it works fine. Please download it from here:
http://www.4shared.com/file/H5brsoaPce/My_First_Calculator.htmlIt needs only a few .dll files for to be run.
I have some question please:
1- How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
2- I want to design the buttons better from the size, position end etc. How can I do this?
3- Is it possible to make it appear like a professional app? -
@tomy said in My first attempt to create a calculator as a novice in Qt programming:
How to change the title of the window from "My_First_Calculator" to "A Qt Calculator!"?
You can use
QWidget::setText
on the main window. That should work.I want to design the buttons better from the size, position end etc. How can I do this?
I haven't downloaded the code as I haven't the time to check it out just now, but putting the widgets in layouts should arrange them well.
Is it possible to make it appear like a professional app?
Well, you could polish the look a bit with a stylesheet, look up the docs on them.