Tuesday, July 14, 2009

C or C++ code?

a) How can create the equivalents of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point). It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch case statement to select the operation. Finally display the result.


When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘y’ or ‘n’.


Sample Output


Some sample interaction with the program might look like this:


Enter first number, operator, second number: 10/3


Answer: 3.33333


Do another (y/n)?y


Enter first number, operator, second number: 12+100


Answer: 112


Do another (y/n)?n





b) Write C++ code to replace two variables A and B with each other.


Given A=2, B=1, you need to swap the values of A and B without using any third variable.





Sample Output


A =2


B=1


After swapping


A=1


B=2

C or C++ code?
a)


#include %26lt;iostream%26gt;


using std;


#define f(n,o) float n (float x, float y) {return x o y;}


f(p,+);f(m,-);f(t,*);f(d,/); static float (*fs)(float x, float y)[] = {p,m,t,d};


static char *os="+-*/";


int main(){do{


cout%26lt;%26lt;"Enter: ";float x,y;char c;cin%26gt;%26gt;x%26gt;%26gt;c%26gt;%26gt;y;


cout%26lt;%26lt; "Result: "%26lt;%26lt; fs[strchr(os, c) - os](x,y)%26lt;%26lt; endl%26lt;%26lt;"more? ";cin%26gt;%26gt;c;}while(c=='y');}








b)


a+=b;a-=b=a-b;





happy?
Reply:a = b + a;


b = a - b;


a = a - b;





or





a = a + b - (b = a)
Reply:b) I dont think is posible without another variable





a)


int a ,b


string operator


string yesno="y"


int answer








dowhile(yesyno=="y")


{


enter a


enter operator


enter b








switch(operator)


//your case %26amp; operation on 2 numbers things go in here








print answer


print do another


input yesno


}


dont recal much c++ synatx but thats it

wedding florist

No comments:

Post a Comment