Skip to content

Commit

Permalink
lab 4
Browse files Browse the repository at this point in the history
  • Loading branch information
tzf101 committed Jul 1, 2021
0 parents commit f3ca6ea
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
58 changes: 58 additions & 0 deletions Lab 3/101_task1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<iostream>
using namespace std;

class Counter{
private:
int count, step;
public:
Counter():count(0), step(1)
{

}
void setIncrementStep(int step_val){
step = step_val;
}
int getCount(){
return count;
}
void increment(){
count += step;
}
void resetCount(){
count = 0;
}
};

void mainMenu(){
cout<<"What do you want to do?\n1.Set increment step\n2.Get count\n3.Increment\n4.Reset Count\n5.Exit\nOption: ";
}

int main(){
Counter c;
int option;
bool runProgram = true;
while(runProgram){
mainMenu();
cin >> option;
switch (option)
{
case 1:
int i_step;
cin >> i_step;
c.setIncrementStep(i_step);
break;
case 2:
cout<<"The counter is :"<<c.getCount()<<endl;
break;
case 3:
c.increment();
break;
case 4:
c.resetCount();
break;
default:
runProgram = false;
break;
}
}
}
73 changes: 73 additions & 0 deletions Lab 3/101_task2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include<iostream>
using namespace std;

class RationalNumber
{
private:
int numenator, denominator;
public:
RationalNumber():numenator(0), denominator(1)
{

}
void assign(int nom, int denom){
if(denom == 0){
cout<<"Error\n";
return;
}
numenator = nom;
denominator = denom;
}
double convert(){
double d = (double)numenator/(double)denominator;
return d;
}
void invert(){
if(numenator == 0){
cout<<"Error\n";
return;
}
swap(numenator, denominator);
cout<<"Inverted\n";
}
void print(){
cout<<"The rational number is: "<<numenator<<"/"<<denominator<<endl;
}
};

void mainMenu(){
cout<<"What do you wanna do?\n1.Assign\n2.Convert\n3.Invert\n4.Print\n5.Exit\nOption: ";
}

int main(){
RationalNumber r;
bool runProgram = true;
int option;
while(runProgram){
mainMenu();
cin >> option;
switch (option)
{
case 1:
int nom, denom;
cout<<"Nominator: ";
cin >> nom;
cout<<"Denominator: ";
cin >> denom;
r.assign(nom, denom);
break;
case 2:
cout<<"Double result: "<<r.convert()<<"\n";
break;
case 3:
r.invert();
break;
case 4:
r.print();
break;
default:
runProgram = false;
break;
}
}
}
93 changes: 93 additions & 0 deletions Lab 3/101_task3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>
#include <cstring>
using namespace std;

class Product
{
private:
char Pname[50];
double discount, unitPrice;

public:
Product() : Pname("x"), discount(0.5), unitPrice(10.0)
{
}

void assignName(char name[])
{
strcpy(Pname, name);
}
void setPrice(double price)
{
unitPrice = price;
}
void setDiscPercent(double percent)
{
if(percent < 0 || percent > 10) {
cout<<"Error\n";
return;
}
discount = percent;
}
double getSellingPrice(int nos)
{
double priceRatio = (100 - discount) / 100;
double total = nos * unitPrice * priceRatio;
return total;
}
void display()
{
cout << Pname << " has a unit price of BDT " << unitPrice << ". Current discount " << discount << "%.\n";
}
};

void mainMenu(){
cout<<"What do you want to do?\n1.Assign name\n2.Set Price\n3.set Discount\n4.Get selling price\n5.Display\n6.Exit\nOption: ";
}

int main(){
Product p;
bool runProgram = true;
int option;
while (runProgram)
{
mainMenu();
cin >> option;
switch (option)
{
case 1:
cout<<"Give name: ";
char name[50];
getchar();
gets(name);
p.assignName(name);
break;
case 2:
double price;
cout<<"Price: ";
cin >> price;
p.setPrice(price);
break;
case 3:
cout<<"Discount: ";
double discount;
cin >> discount;
p.setDiscPercent(discount);
break;
case 4:
cout<<"No. of items: ";
int items;
cin >> items;
cout<<"Selling price: ";
cout<<p.getSellingPrice(items)<<"\n";
break;
case 5:
p.display();
break;
default:
runProgram = false;
break;
}
}

}
Binary file added Lab 3/lab 3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions Lab 4/101_task1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
using namespace std;
class Calculator
{
private:
int value;

public:
Calculator() : value(0)
{
}
Calculator(int x) : value(x)
{
}
int getValue()
{
return value;
}
void setValue(int _value)
{
value = _value;
}
void add(int _value)
{
value += _value;
}
void substract(int _value)
{
value -= _value;
}
void multiply(int _value)
{
value *= _value;
}
void divideBy(int _value)
{
if (_value)
value /= _value;
else
cout << "Error : divide by 0 is undefined." << endl;
}
void clear()
{
value = 0;
}
void display()
{
cout << "Calculator Display:" << value << endl;
}
~Calculator()
{
cout << "Calculator object is destroyed." << endl;
}
};

int main()
{
Calculator calc;
calc.add(10);
calc.display();
calc.add(7);
calc.display();
calc.multiply(31);
calc.display();
calc.substract(42);
calc.display();
calc.divideBy(7);
calc.display();
calc.divideBy(0);
calc.display();
calc.add(3);
calc.display();
calc.substract(1);
calc.display();
calc.clear();
calc.display();
return 0;
}
Loading

0 comments on commit f3ca6ea

Please sign in to comment.