Tuesday, July 14, 2009

C++ programming-calculator fucntion?

how to create a program with some functions for a calculator. Functions include:


1.) add


2.) subtract


3.) multiply


4.) division





a.) Write the algorithm with pseudo code to provide solution to the problem


statements stated above.


b.) Implement all the calculator functions listed above with C++ programming.





Sample output:


Enter two integer:8 12


Enter function u want to perform:+


The answer is :20


Do u want to continue?(Y/N):Y


Enter the second integer:3


Enter function u want to perform:*


The answer is :60


Do u want to continue?(Y/N):N

C++ programming-calculator fucntion?
Accept two numbers


accept a char like +,-,*,/


use switch to do the work


Here is the outline





main()


{


float a,b,res;


char ch;


char choice = 'y';


while ( choice == 'y' )


{


cin%26gt;%26gt;a%26gt;%26gt;b;





cin%26gt;%26gt;ch; // the operator





switch (ch)


{


case '+' :


res= a +b; break;


case '-' :


res= a-b; break;


case '*'


res = a*b;


......


default :


cout%26lt;%26lt;"Invalid operator"





}





cout %26lt;%26lt;"Result is " %26lt;%26lt; res;


cout%26lt;%26lt;"do you wwant to continue";


cin %26gt;%26gt; choice;


}


}
Reply:content=// Program to implement Calculator functions for : +, -, *, /, ^





#include %26lt;iostream.h%26gt;


#include %26lt;conio.h%26gt;


float inputOne, inputTwo, answer;


char operator_, yn;


int main()








{


textcolor(LIGHTBLUE);


while (yn != 'n')








{


clrscr();


cout %26lt;%26lt; "First number + - / * ^ second number\n";


cin %26gt;%26gt; inputOne %26gt;%26gt; operator_ %26gt;%26gt; inputTwo;


if (operator_ == '+')


answer = inputOne + inputTwo;


if (operator_ == '-')


answer = inputOne - inputTwo;


if (operator_ == '*')


answer = inputOne * inputTwo;


if (operator_ == '/')








{


if (inputTwo == 0)








{


cout %26lt;%26lt; "Cannot divide by 0";


}


else


answer = inputOne / inputTwo;


}


if (operator_ == '^')








{


answer = inputOne;


for (int i=2; i%26lt;=inputTwo; i++)


answer = answer * inputOne;


}


cout %26lt;%26lt; endl;


cout %26lt;%26lt; inputOne %26lt;%26lt; " " %26lt;%26lt; operator_ %26lt;%26lt; " ";


cout %26lt;%26lt; inputTwo %26lt;%26lt; " = " %26lt;%26lt; answer;


cout %26lt;%26lt; "\n\nSolve another? %26lt;yn%26gt; ";


cin %26gt;%26gt; yn;


cout %26lt;%26lt; "\n";


}


return 0;


}

alstroemeria

No comments:

Post a Comment