Problem Statement: Execute simple arithmetic operations sum, subtract, multiplication, division on integers.
How its different from others on internet : There are so many websites which have code for simple calculator program. However most of them, asks for two integers first and then asks for operator which is not the case in real time calculator. In this code, you can directly enter expressions like 76 * 75, which is similar to real time calculator.
Input : operand1 operator operand2
Output : result of the input arithmetic expression.
Code:
#include <stdio.h>
#include <string.h>
float calculate(int op1, char op, int op2, unsigned short * flagptr); // calculator function
int main()
{
char oparr1[10], oparr2[10];
char op;
float result;
unsigned short flag = 1; // flag which indicates any error in input
unsigned short *flagptr = &flag; // pointer to the above flag
printf("enter expression operand1 operator operand2\n");
int ch;
ch = getchar(); // start reading input
int i= 0;
while(ch >= '0' && ch <= '9') //store in array of operand1 if its number
{
oparr1[i] = ch;
ch = getchar();
i++;
}
while(ch == ' ') // skip all the spaces between first operand and operator, if any
{
ch = getchar();
}
op = ch; // store operator in op
ch = getchar();
while(ch == ' ')
{
ch = getchar();
}
i= 0;
while(ch >= '0' && ch <= '9') //start reading operand2
{
oparr2[i] = ch;
ch = getchar();
i++;
}
int op1, op2;
op1 = atoi(oparr1); // convert char array of operand1 to integer
op2 = atoi(oparr2);
result = calculate(op1, op, op2, flagptr);
if (flag == 1) // no error in input
printf("result = %f\n", result);
else
printf("undefined result\n");
return 0;
}
float calculate(int op1, char op, int op2, unsigned short *flagptr)
{
float result;
switch(op)
{
case '+': result = op1 + op2;
break;
case '-': result = op1 - op2;
break;
case '*': result = op1 * op2;
break;
case '/':
if (op2 != 0)
result = (float)op1/op2;
else
{
printf("divide by zero error\n");
*flagptr = 0; // indicate error in input
}
break;
default : printf("Use integers only\nUse +, -, * and / only\n");
*flagptr = 0;
break;
}
return result;
}
Sample input: 25 * 25
Output : 625.00000
What can be improved?
Ans: Well, this program works only for integers. Try for floating point values and use it as your own calculator. Discuss in comments, how to implement it for float values.
How its different from others on internet : There are so many websites which have code for simple calculator program. However most of them, asks for two integers first and then asks for operator which is not the case in real time calculator. In this code, you can directly enter expressions like 76 * 75, which is similar to real time calculator.
Input : operand1 operator operand2
Output : result of the input arithmetic expression.
Code:
#include <stdio.h>
#include <string.h>
float calculate(int op1, char op, int op2, unsigned short * flagptr); // calculator function
int main()
{
char oparr1[10], oparr2[10];
char op;
float result;
unsigned short flag = 1; // flag which indicates any error in input
unsigned short *flagptr = &flag; // pointer to the above flag
printf("enter expression operand1 operator operand2\n");
int ch;
ch = getchar(); // start reading input
int i= 0;
while(ch >= '0' && ch <= '9') //store in array of operand1 if its number
{
oparr1[i] = ch;
ch = getchar();
i++;
}
while(ch == ' ') // skip all the spaces between first operand and operator, if any
{
ch = getchar();
}
op = ch; // store operator in op
ch = getchar();
while(ch == ' ')
{
ch = getchar();
}
i= 0;
while(ch >= '0' && ch <= '9') //start reading operand2
{
oparr2[i] = ch;
ch = getchar();
i++;
}
int op1, op2;
op1 = atoi(oparr1); // convert char array of operand1 to integer
op2 = atoi(oparr2);
result = calculate(op1, op, op2, flagptr);
if (flag == 1) // no error in input
printf("result = %f\n", result);
else
printf("undefined result\n");
return 0;
}
float calculate(int op1, char op, int op2, unsigned short *flagptr)
{
float result;
switch(op)
{
case '+': result = op1 + op2;
break;
case '-': result = op1 - op2;
break;
case '*': result = op1 * op2;
break;
case '/':
if (op2 != 0)
result = (float)op1/op2;
else
{
printf("divide by zero error\n");
*flagptr = 0; // indicate error in input
}
break;
default : printf("Use integers only\nUse +, -, * and / only\n");
*flagptr = 0;
break;
}
return result;
}
Sample input: 25 * 25
Output : 625.00000
What can be improved?
Ans: Well, this program works only for integers. Try for floating point values and use it as your own calculator. Discuss in comments, how to implement it for float values.