Vous êtes sur la page 1sur 13

Lab N0.

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

#include<iostream> // allows program to output data to the screen

using std::cout; //program uses cout


using std::cin; //program uses cin
using std::endl; //program uses endl

class calc //class definition


{

public: //public access specifier

int add(int a, int b) //functions for implementing calculator operations


{
c = a + b;
return c;
}

int sub(int a, int b)


{
c = a - b;
return c;
}

int mult(int a, int b)


{
c = a*b;
return c;
}

void div(int a, int b)


{
int d = a / b;
cout << "\nQuotient: " << d << "\t\tRemainder: " << remainder(a, b) << "\n\n";
//function div can access a private fuction
}

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;
}

}; //end of class calc

int factorial(int a); //function prototype

int main() //fuction of main starts


{
calc myCalc; //object of class calc named myCalc, it can have any name
int x, y;
char choice;

cout << "THIS IS A SIMPLE CALCULATOR\n\n"; //printing text on screen


cout << "Enter choice of operation\n + for addition\n - for subtraction\n * for
multiplication \n / for division\n f for factorial\n";
cin >> choice;

cout << "\nEnter First Number: "; //prompting


cin >> x; //and taking input from user
cout << "\nEnter Second Number: ";
cin >> y; //second input from user

switch (choice) //conditional statement


{
case'+':
cout << "\nSum: " << myCalc.add(x, y) << "\n\n"; //function calling and printing
text on screen
break;

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
}

int factorial(int number) //function definition


{
int fact = 1;

if (number <= 1) // test for base case


return 1; // base cases: 0! = 1 and 1! = 1
else // multiplying the number with its previous ones
{
for (int i = 1; i <= number ; ++i) // using for loop
{
fact = fact * i;
}
return fact;

while (number >= 1) // using while loop


{
fact *= number;
number--;
}
return fact;
}

} // end of factorial function

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

#include<iostream> // allows program to output data to the screen

using std::cout; //program uses cout


using std::cin; //program uses cin
using std::endl; //program uses endl

class calc //class definition


{

public: //public access specifier

int add(int a, int b) //functions for implementing calculator operations


{
c = a + b;
return c;
}

int sub(int a, int b)


{
c = a - b;
return c;
}

int mult(int a, int b)


{
c = a*b;
return c;
}

void div(int a, int b)


{
int d = a / b;
cout << "\nQuotient: " << d << "\t\tRemainder: " << remainder(a, b) << "\n\n";
//function div can access a private fuction
}

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;
}

}; //end of class calc

int factorial(int a); //function prototype

int main() //fuction of main starts


{
calc myCalc; //object of class calc named myCalc, it can have any name
int x, y;
char choice;

cout << "THIS IS A SIMPLE CALCULATOR\n\n"; //printing text on screen


cout << "Enter choice of operation\n + for addition\n - for subtraction\n * for
multiplication \n / for division\n f for factorial\n";
cin >> choice;

cout << "\nEnter First Number: "; //prompting


cin >> x; //and taking input from user
cout << "\nEnter Second Number: ";
cin >> y; //second input from user

if (choice == '+') //conditional statement


cout << "\nSum: " << myCalc.add(x, y) << "\n\n"; //function calling and printing
text on screen

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";
}

else //exception handling


cout << "\ninvalid choice";

return 0; // indicate successful termination


}
int factorial(int number) //function definition
{
int fact = 1;

if (number <= 1) // test for base case


return 1; // base cases: 0! = 1 and 1! = 1
else // multiplying the number with its previous ones
{
for (int i = 1; i <= number ; ++i) // using for loop
{
fact = fact * i;
}
return fact;
}

} // end of factorial function

Output:
Lab N0. 02
Task # 3
Objective:
Use macros to create a calculator.

Code:
//recoding lab1a.cpp using macros

#include<iostream>// allows program to output data to the screen


using std::cout; //program uses cout
using std::cin; //program uses cin
using std::endl; //program uses endl

#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;

int main() //fuction of main starts


{
float result;
int x, y;
char choice;

cout << "THIS IS A SIMPLE CALCULATOR USING MACROS\n\n"; //printing text on


screen
cout << "Enter choice of operation\n + for addition\n - for subtraction\n * for
multiplication\n / for division\n\n";
cin >> choice;

cout << "\nEnter First Number: "; //prompting


cin >> x; //and taking input from user

cout << "\nEnter Second Number: ";


cin >> y; //second input from user

switch (choice) //conditional statement


{
case'+':
result = add(x, y);
output(x, y, result);
break;

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";
}

return 0; // indicate successful termination


}

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:

// using macros for calculating area of a rectangle and a circle.

#include<iostream>// allows program to output data to the screen


using std::cout; //program uses cout
using std::cin; //program uses cin
using std::endl; //program uses endl

#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;

int main() //fuction of main starts


{
float result;
int x, y;
char choice;

cout << "THIS IS A SIMPLE CALCULATOR USING MACROS\n\n";


cout << "Enter choice of operation\n + : addition\n - : subtraction\n * : multiplication\n / :
division\n r : area of rectangle\n c : area of circle\n\n";
cin >> choice;

if ((choice != 'r') && (choice != 'c')) //conditional statement


{
cout << "\nEnter First Number: "; //prompting
cin >> x; //and taking input from user

cout << "\nEnter Second Number: ";


cin >> y; //second input from user
}

switch (choice) //conditional statement


{
case'+':
result = add(x, y);
output(x, y, result);
break;

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;

result = multiply(x, y);


cout << "\nArea of rectangle = " << result << endl;
break;

case'c':
cout << "\nEnter radius of circle: ";
cin >> x;
result = pi * multiply(x, x);
cout << "\nArea of circle = " << result << endl;
break;

default: //exception handling


cout << "\n\t Invalid choice\n";
}
return 0; // indicate successful termination
}

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:

// inserting macros to find the maximum and minimum number

#include<iostream>// allows program to output data to the screen


using std::cout; //program uses cout
using std::cin; //program uses cin
using std::endl; //program uses endl

#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

int main() //fuction of main starts


{
float result;
int x, y;
char choice;

cout << "THIS IS A SIMPLE CALCULATOR USING MACROS\n\n"; //printing text on


screen
cout << "Enter choice of operation\n + : addition\n - : subtraction\n * : multiplication\n / :
division\n r : area of rectangle\n c : area of circle\n ? : maximum number\n\n";
cin >> choice;

if ((choice != 'r') && (choice != 'c'))


{
cout << "\nEnter First Number: "; //prompting
cin >> x; //and taking input from user

cout << "\nEnter Second Number: ";


cin >> y; //second input from user
}

switch (choice) //conditional statement


{
case'+':
result = add(x, y);
output(x, y, result);
break;
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;

result = multiply(x, y);


cout << "\nArea of rectangle = " << result << endl;
break;

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;

default: //exception handling


cout << "\n\t Invalid choice\n";
}

return 0; // indicate successful termination


}
Output:

Vous aimerez peut-être aussi