Jul 28, 2012 at 2:13am UTC
Hi If anyone could help me I would appreciate it. I am trying to make a calculator program. I am currently using MS Studios 2010, with Window 7 Premium system. This is where I am so far:
#include<math.h>
#include <iostream>
using namespace std;
int main()
{
int x,y,ans,i;
int choice;
float div;
char loop;
ans=0;
void clrsc();
do
{
printf("\n Do you wish to continue (Y/N) : ");
scanf_s("%s",&loop);
if (loop=='y' || loop=='Y')
{
clrsc();
printf("\n Enter any two numbers ");
printf("\n --------------------- ");
printf("\n\n Enter the first number : ");
scanf_s("%d",&x);
printf("\n Enter the second number : ");
scanf_s("%d",&y);
clrsc();
printf("\n Select the operation to be carried out ");
printf("\n -------------------------------------- ");
printf("\n 1. Addition ");
printf("\n 2. Substraction ");
printf("\n 3. Multiplication ");
printf("\n 4. Division ");
printf("\n Enter your choice : ");
scanf_s("%d",&choice);
switch(choice)
{
case 1 :
{
ans = x+y;
printf("\n Answer = %d",ans);
break;
}
case 2 :
{
ans = x-y;
printf("\n Answer = %d", ans);
break;
}
case 3 :
{
ans = x*y;
printf("\n Answer = %d", ans);
break;
}
case 4:
{
div = x/y;
printf("\n Answer = %.2f", div);
break;
}
default:
printf("\n\n Illegal operation......");
break;
}
}
else
printf("\n Bye....... Bye...........");
cin.get();
}while(loop=='y' || loop=='Y');
}
Jul 28, 2012 at 2:41am UTC
Could you paste it as a code? in square brackets code?
Jul 28, 2012 at 5:37am UTC
Another way to do it is with callback functions.
It's fairly simple, so I'm not going to use any class this time.
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
#include <iostream>
#include <map>
#include <math.h>
#include <string>
typedef float (*callback)(float , float );
/*natives begin*/
float add(float f, float s)
{
return f+s;
}
float sub(float f, float s)
{
return f-s;
}
float div(float f, float s)
{
if (s)
return f/s;
std::cout << "Cannot divide by zero" << std::endl;
return 0.0f;
}
float mult(float f, float s)
{
return f * s;
}
float power(float f, float s)
{
return pow(f, s);
}
/*natives end*/
void prompt(bool &b)
{
std::cout << "continue? y : n" << std::endl;
char c;
std::cin >> c;
if (c == 'n' || c == 'N' )
b= true ;
}
int main()
{
std::map<std::string, callback> functions;
functions["+" ] = add;
functions["-" ] = sub;
functions["/" ] = div;
functions["*" ] = mult;
functions["^" ] = power;
for (bool isFinished= false ; !isFinished; prompt(isFinished))
{
std::cout << "Enter an expression\n[number] [operation] [number]" << std::endl;
float f, s;
std::string op;
std::cin >> f >> op >> s;
if (functions.find(op) != functions.end())
std::cout << "The result is: " << functions[op](f,s) << std::endl;
else
std::cout << "operation not found" << std::endl;
}
}
Last edited on Jul 28, 2012 at 5:37am UTC
Jul 28, 2012 at 8:08pm UTC
It is kind of a bad calculator, but I think it might be a little more efficient thinking about number of lines:
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
#include <iostream>
int main ()
{
using namespace std;
float num1;
float num2;
cout << "Write your first number(use punctuary for desimals, not comma) :\n" ;
cin >> num1;
cout << "Now write your second number :\n" ;
cin >> num2;
float numm = num1*num2;
float nump = num1+num2;
float nummi = num1-num2;
float nums = num1/num2;
cout << "\n" << num1 << "*" << num2 << "=" ;
cout << numm << "\n \n" ;
cout << num1 << "+" << num2 << "=" ;
cout << nump << "\n \n" ;
cout << num1 << "-" << num2 << "=" ;
cout << nums << "\n \n" ;
cout << num1 << "/" << num2 << "=" ;
cout << nums << "\n \n" ;
cin.get();
cin.get();
}
Edit: Also, in the future post your source-code in "Source Code" format.
Last edited on Jul 28, 2012 at 8:09pm UTC