Académique Documents
Professionnel Documents
Culture Documents
01
Task # 1
Objective:
Recode lab1a.cpp and implement the function of factorial by replacing the recursion with a loop.
Code:
//lab1a.cpp
// revising basic concepts using an example of a calculator
private: //private members only accessible by functions of the class and not by functions outside
the class (like main)
int c;
int remainder(int a, int b)
{
c = a%b;
return c;
}
case'-':
cout << "\nDifference: " << myCalc.sub(x, y) << "\n\n";
break;
case'*':
cout << "\nProduct: " << myCalc.mult(x, y) << "\n\n";
break;
case'/':
myCalc.div(x, y);
break;
case'f':
cout << "\nFactorial of first number : " << factorial(x) << "\n\n";
cout << "Factorial of second number : " << factorial(y) << "\n\n";
break;
default: //exception handling
cout << "\ninvalid choice";
}
return 0; // indicate successful termination
}
Output:
Lab N0. 01
Task # 2
Objective:
Recode lab1a.cpp and replace the switch statement with if-else statements for using
multi-functions.
Code:
//lab1a.cpp
// revising basic concepts using an example of a calculator
private: //private members only accessible by functions of the class and not by functions outside
the class (like main)
int c;
int remainder(int a, int b)
{
c = a%b;
return c;
}
if (choice == '-')
cout << "\nDifference: " << myCalc.sub(x, y) << "\n\n";
if (choice == '*')
cout << "\nProduct: " << myCalc.mult(x, y) << "\n\n";
if (choice == '/')
myCalc.div(x, y);
if (choice == 'f')
{
cout << "\nFactorial of first number : " << factorial(x) << "\n\n";
cout << "Factorial of second number : " << factorial(y) << "\n\n";
}
Output:
Lab N0. 02
Task # 3
Objective:
Use macros to create a calculator.
Code:
//recoding lab1a.cpp using macros
case'-':
result = subtract(x, y);
output(x, y, result);
break;
case'*':
result = multiply(x, y);
output(x, y, result);
break;
case'/':
result = divide(x, y);
output(x, y, result);
break;
default: //exception handling
cout << "\n\t Invalid choice\n";
}
Output:
Lab N0. 02
Task # 4
Objective:
Recode the program made for task 1 to insert macros for calculating area of a rectangle and a
circle.
Code:
#define pi 3.142
#define add(a, b) (a+b)
#define subtract(a, b) (a-b)
#define multiply(a, b) (a*b)
#define divide(a, b) (a / b)
#define output(a, b, c) cout<< a <<" "<< choice << " "<< b << " = "<< c <<"\n"<<endl;
case'-':
result = subtract(x, y);
output(x, y, result);
break;
case'*':
result = multiply(x, y);
output(x, y, result);
break;
case'/':
result = divide(x, y);
output(x, y, result);
break;
case'r':
cout << "\nEnter length of rectangle: ";
cin >> x;
cout << "\nEnter width of rectangle: ";
cin >> y;
case'c':
cout << "\nEnter radius of circle: ";
cin >> x;
result = pi * multiply(x, x);
cout << "\nArea of circle = " << result << endl;
break;
Output:
Lab N0. 02
Task # 5
Objective:
Recode the program made in task 2 to insert macros that indicates the maximum and minimum
number, also to indicate if the numbers are equal.
Code:
#define pi 3.142
#define add(a, b) (a+b)
#define subtract(a, b) (a-b)
#define multiply(a, b) (a*b)
#define divide(a, b) (a / b)
#define output(a, b, c) cout<< a <<" "<< choice << " "<< b << " = "<< c <<"\n"<<endl;
#define compare(a, b) (a > b ? a : b) // macro to compare two numbers
case'*':
result = multiply(x, y);
output(x, y, result);
break;
case'/':
result = divide(x, y);
output(x, y, result);
break;
case'r':
cout << "\nEnter length of rectangle: ";
cin >> x;
cout << "\nEnter width of rectangle: ";
cin >> y;
case'c':
cout << "\nEnter radius of circle: ";
cin >> x;
result = pi * multiply(x, x);
cout << "\nArea of circle = " << result << endl;
break;
case '?’:
if (x == y)
cout << x << " = " << y << endl;
else
cout << "\nGreater of the two numbers = " << compare(x, y) << endl;
break;