C++ DBMS Lab
C++ DBMS Lab
_______________________________
PART 1
(Attempt all questions from Part-1)
1. Create a class rational which represents a numerical value by two double values-NUMERATOR &
DENOMINATOR. Include the following public member Functions:
default constructor
constructor with two arguments.
void reduce( ) that reduces the rational number by eliminating the highest common factor between the
numerator and denominator.
Overload + operator to add two rational number.
Overload >> operator to enable input through cin.
Overload << operator to enable output through cout.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class rational
{
private:
int num;
int den;
int lcm(int a,int b);
int hcf(int a,int b);
public:
rational()
{
num=0;
den=1;
}
rational(int n,int d=1)
{
if(d==0)
{
cout<<"denominator can't be zero";
getch();
exit(1);
}
else
{
num=n;
den=d;
}
}
rational reduce()
{
rational temp;
int h=hcf(num,den);
temp.num=num/h;
temp.den=den/h;
return temp;
}
rational operator +(rational r)
{
int l,t1,t2,x1,x2,den1,den2,p;
rational ans;
if(den<0)
den1=-den;
else
den1=den;
if(r.den<0)
den2=-r.den;
else
den2=r.den;
p=lcm(den1,den2);
if((den<0&&r.den>0)||(den>0&&r.den<0))
l=-p;
else
l=p;
ans.den=l;
t1=l/den;
t2=l/r.den;
x1=num*t1;
x2=r.num*t2;
ans.num=x1+x2;
return ans;
}
friend istream& operator >> (istream& s,rational& r);
friend ostream& operator << (ostream& s,rational& r);
};
int rational::lcm(int a,int b)
{
int i,lcm=1;
while(!(a==1&&b==1))
{
i=2;
while(!(a%i==0||b%i==0)&&i<(a>b?a:b))
i++;
lcm*=i;
if(a%i==0)
a/=i;
if(b%i==0)
b/=i;
}
return lcm;
}
int rational::hcf(int a,int b)
{
int r=a%b;
while(r)
{
a=b;
b=r;
r=a%b;
}
return b;
}
istream& operator >>(istream& s,rational& r)
{
int a,b;
char c;
s>>a>>c>>b;
if(c!='/')
{
cout<<"use of invalid notation";
getch();
exit(0);
}
if(b==0)
{
cout<<"denominator can't be zero.";
getch();
exit(1);
}
r.num=a;
r.den=b;
return s;
}
ostream& operator <<(ostream& s,rational& r)
{
if(r.den==1)
s<<r.num;
else
{
if(r.den==-1)
s<<-r.num;
else
s<<r.num<<'/'<<r.den;
}
return s;
}
int main()
{
clrscr();
rational r1,r2,r3;
cout<<"enter r1:";
cin>>r1;
cout<<"enter r2:";
cin>>r2;
r3=r1+r2;
cout<<r1<<" + "<<r2<<" = "<<r3<<" = "<<r3.reduce();
getch();
return 0;
}
Write a C++ program to check either a given number is palindrome or not.
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
test("12345");
test("12321");
}
3. Write a program in C++ to find factorial of a given number using recursion.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
PART II
(Attempt any two questions)
Delete a record in database
Modify the record in database
Generate queries
Generate the record
List all the records of database in ascending order
Add Records
PRODUCTS
---
INSERT INTO PRODUCTS VALUES('REI','2A45C','Ratchet Link',79.00,210);
INSERT INTO PRODUCTS VALUES('ACI','4100Y','Widget Remover',2750.00,25);
INSERT INTO PRODUCTS VALUES('QSA','XK47 ','Reducer',355.00,38);
INSERT INTO PRODUCTS VALUES('BIC','41627','Plate',180.00,0);
INSERT INTO PRODUCTS VALUES('IMM','779C ','900-LB Brace',1875.00,9);
INSERT INTO PRODUCTS VALUES('ACI','41003','Size 3 Widget',107.00,207);
INSERT INTO PRODUCTS VALUES('ACI','41004','Size 4 Widget',117.00,139);
INSERT INTO PRODUCTS VALUES('BIC','41003','Handle',652.00,3);
INSERT INTO PRODUCTS VALUES('IMM','887P ','Brace Pin',250.00,24);
INSERT INTO PRODUCTS VALUES('QSA','XK48 ','Reducer',134.00,203);
INSERT INTO PRODUCTS VALUES('REI','2A44L','Left Hinge',4500.00,12);
INSERT INTO PRODUCTS VALUES('FEA','112 ','Housing',148.00,115);
INSERT INTO PRODUCTS VALUES('IMM','887H ','Brace Holder',54.00,223);
INSERT INTO PRODUCTS VALUES('BIC','41089','Retainer',225.00,78);
INSERT INTO PRODUCTS VALUES('ACI','41001','Size 1 Wiget',55.00,277);
INSERT INTO PRODUCTS VALUES('IMM','775C ','500-lb Brace',1425.00,5);
INSERT INTO PRODUCTS VALUES('ACI','4100Z','Widget Installer',2500.00,28);
INSERT INTO PRODUCTS VALUES('QSA','XK48A','Reducer',177.00,37);
INSERT INTO PRODUCTS VALUES('ACI','41002','Size 2 Widget',76.00,167);
INSERT INTO PRODUCTS VALUES('REI','2A44R','Right Hinge',4500.00,12);
INSERT INTO PRODUCTS VALUES('IMM','773C ','300-lb Brace',975.00,28);
INSERT INTO PRODUCTS VALUES('ACI','4100X','Widget Adjuster',25.00,37);
INSERT INTO PRODUCTS VALUES('FEA','114 ','Motor Mount',243.00,15);
INSERT INTO PRODUCTS VALUES('IMM','887X ','Brace Retainer',475.00,32);
INSERT INTO PRODUCTS VALUES('REI','2A44G','Hinge Pin',350.00,14);
go
Delete a Record
Generate a Record
SELECT *
FROM PRODUCTS
ORDER BY MFR_ID;
MFR_ID PRODUCT_ID DESCRIPTION PRICE QTY_ON_HAND
ACI 41001 Size 1 Wiget 55.00 277
ACI 41002 Size 2 Widget 76.00 167
ACI 41003 Size 3 Widget 107.00 207
ACI 41004 Size 4 Widget 117.00 139
ACI 4100X Widget Adjuster 25.00 37
ACI 4100Y Widget Remover 2750.00 25
ACI 4100Z Widget Installer 2500.00 28
BIC 41003 Handle 652.00 3
BIC 41089 Retainer 225.00 78
BIC 41627 Plate 180.00 0
FEA 112 Housing 148.00 115
FEA 114 Motor Mount 243.00 15
IMM 773C 300-lb Brace 975.00 28
IMM 775C 500-lb Brace 1425.00 5
IMM 779C 900-LB Brace 1875.00 9
IMM 887H Brace Holder 54.00 223
IMM 887P Brace Pin 250.00 24
IMM 887X Brace Retainer 475.00 32
QSA XK47 Reducer 355.00 38
QSA XK48 Reducer 134.00 203
QSA XK48A Reducer 177.00 37
REI 2A44G Hinge Pin 350.00 14
REI 2A44L Left Hinge 4500.00 12
REI 2A44R Right Hinge 4500.00 12
REI 2A45C Ratchet Link 79.00 210
5. A large organization that does automobile repairs must keep track of its repair facilities or garages,
the mechanics, and their qualifications in terms of the courses they have taken, the dates they took
the courses, and the grade each earned for each course. Descriptions of these items are as follows:
Garage: Garages identification number and managers name
Mechanic: Employee number and name
Course: Number, name, and duration (in weeks)
Display the numbers of sales persons, with orders currently in the orders table without any
repeats.
SELECT SNUM
FROM SALESPEOPLE INNER JOIN ORDERS
ON SALESPEOPLE.SNUM = ORDERS.SNUM;
Display all customers where city is Mumbai rating is more than 100.
SELECT *
FROM CUSTOMERS
WHERE RATING > 100 AND CITY = MUMBAI;
Display all sales persons names starting with character G, the 4 th character is A & the rest
of characters will be any.
SELECT SNAME
FROM SALESPEOPLE
WHERE SNAME LIKE [G__A]%;
Find all records from customers table where city is not known i.e. NULL.
SELECT SNUM
FROM SALESPEOPLE INNER JOIN ORDERS
ON CITY IS NULL;