English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2006-10-18 10:53:27 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

a)
#include
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<<"Enter: ";float x,y;char c;cin>>x>>c>>y;
cout<< "Result: "<< fs[strchr(os, c) - os](x,y)<< endl<<"more? ";cin>>c;}while(c=='y');}


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

happy?

2006-10-18 11:16:23 · answer #1 · answered by n0body 4 · 1 0

it is actual that in case you create an empty C++ console challenge and manually rename your cpp report to offer it .c extension, seen Studio *will* execute the Microsoft C compiler, even though it is a doubly-out of date 1989 C compiler. Microsoft abandoned C a protracted time in the past. in case you % to stay with this IDE and assemble C, replace the underlying compiler. Intel C/C++ is a sturdy determination.

2016-10-19 23:11:14 · answer #2 · answered by daw 4 · 0 0

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 & operation on 2 numbers things go in here


print answer
print do another
input yesno
}
dont recal much c++ synatx but thats it

2006-10-18 11:07:22 · answer #3 · answered by tru_story 4 · 0 1

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

or

a = a + b - (b = a)

2006-10-19 10:22:01 · answer #4 · answered by maiqbal 1 · 0 0

fedest.com, questions and answers