Calculator - Past the basics of 2+2
I have made a rather simple calculator for every day use, but I hoped to take the calculator one step more and get it to where I can add, subtract, multiply, and divide more than two numbers at a time. For example if you have ever messed around with the python compiler you can actually add, subtract, multiply, and divide more than just two numbers. You can do 2+2+2..., if you want to, instead of only being limited to do just 2+2. I know this would be a rather simple task by making an API and all, but I hope to use this in both linux and windows, plus I need the experience. I have checked at Planet Source Code and a few other places for any example ways to do this, but they only had calculators with APIs or were limited to only adding, subtracting, multiplying, and dividing two numbers at a time.
The problem that I am really running into is that I don't know how to grasp how to really do this with my limited knowledge over the C language. I have thought of maybe doing this by making the inputed information from the user into a string and looking for key word like the =, but it didn't really sound like a really great way to go about doing it. I am going to post the source of my simple calculator that can only add, subtract, multiple, and divide two numbers, to hopefully allow you to get the idea of where I am at in programming in the C language.
Code:
#include <stdio.h>
#include <math.h>
#include <time.h>
int add (int x, int y)
{
return x + y;
}
int sub (int x, int y)
{
return x - y;
}
int mult (int x, int y)
{
return x * y;
}
int divide (int x, int y)
{
return x / y;
}
float sqroot (float x)
{
return sqrt (x);
}
float cosine (float x)
{
return cos (x);
}
float sine (float x)
{
return sin (x);
}
float tangent (float x)
{
return tan (x);
}
float loggy (float x)
{
return log (x);
}
int main()
{
int choice;
int x;
int y;
time_t local;
struct tm *info;
time (&local);
info = localtime (&local);
printf("C Caculator : written by the Rat\n");
printf("Current date and time are:\n %s", asctime (info) );
printf("What would you like to do?\n");
printf("[1} Add\n");
printf("[2] Subtract\n");
printf("[3] Multiply\n");
printf("[4] Divide\n");
printf("[5] Square Root\n");
printf("[6] Cosine, Sine, Tangent\n");
printf("[7] Log\n");
printf("[8] Quit\n");
scanf("%d", &choice);
if (choice == 1)
{
printf("What two numbers would you like to add?\n");
scanf("%d", &x);
scanf("%d", &y);
printf("The product of your two numbers are : %d\n", add(x,y));
}
else if (choice == 2)
{
printf("What two numbers would you like to subrtract?\n");
scanf("%d", &x);
scanf("%d", &y);
printf("The quotient of your two numbers are : %d\n", sub(x,y));
}
else if (choice == 3)
{
printf("What two numbers would you like to multiply?\n");
scanf("%d", &x);
scanf("%d", &y);
printf("The product of your two numbers are : %d\n", mult(x,y));
}
else if (choice == 4)
{
printf("What two numbers would you like to divide?\n");
scanf("%d", &x);
scanf("%d", &y);
printf("The quotient of your two numbers are : %d\n", divide(x,y));
}
else if (choice == 5)
{
printf("What number do you want to find the sqrt of ?");
scanf("%d", &x);
printf("The answer is : %f\n", sqroot(x));
}
else if (choice == 6)
{
printf("What number is what you want to find ?\n");
scanf("%d", &x);
printf("[1] Cos\n");
printf("[2] Sin\n");
printf("[3] Tan\n");
scanf("%d", &y);
if (y == 1) {
printf("The answer is : %f\n", cosine(x));
}
else if (y == 2) {
printf("The answer is : %f\n", sine(x));
}
else if (y == 3) {
printf("The answer is : %f\n", tangent(x));
}
else {
printf("Invalid Selection");
}
}
else if (choice == 7)
{
printf("What number would you like to find the log of ?\n");
scanf("%d", &x);
printf("The answer is : %f\n", loggy(x));
}
else if (choice == 8)
{
printf("Bye!\n");
return 0;
}
else
{
printf("Invalid Selection\n");
}
}
I hope I explained well enough as to where I am trying to head in this project.
Thanks for any help!!!
Revekius