- ?I am trying to make a calculator in C. Is there any way to make GUIs? If not, then, is there any way to make compiler understand arthematic symbols? I mean, the user can enter 5+3*6 or anything like this and there should be the correct output. If both of them aren’t possible then what stuffs can make a calculator look kinda professional?
Answers are appreciated with thanks.
SamipBeginner
Calculator in C
Share
wel achieving what you intend is not exactly impossible but it is a rather cumbersome task…
for gui look alike you can use boarders and alignment made of //”‘:_-+*# but you eill still have to manually enter the input from keyboard
now for your arithmetic operations it is simpler to use only 2 numbers and one operator or multiple inputs but same operator….or you can try
(store operands as integers ie, 1,2,3etc and operators as the ascii value).
but this all may result in magic number errors so i suggest you avoid that
you can stick with a simpler calculator as i have said at the beginning of this answer
below is example of a simple calculator using switch case:
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
//using %.1lf to limit input with only one place decimal value
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Its better to make it simple calculator but yeah go through this links it will help you.
http://freesourcecode.net/cprojects/82590/sourcecode/CALC.CPP#.YsBf9c3BXxM
https://mobile.happycodings.com/c//beginners-lab-assignments/code1.html
C does provide GUI ya know.
Checkout GTK toolkit.