Вы находитесь на странице: 1из 28

Write a program that uses a structure called Point to model a point.

Define three points and have the user input value of two of them. Then set third point equal to sum of the other two and display the value of the new point.

#include <iostream.h> #include <conio.h> struct point { int x,y; } p,p1,p2;

void main() { clrscr(); cout<<Enter the co-ordinates of first point<<endl; cout<<Enter x1 : ; cin>>p1.x; cout<<Enter y1 : ; cin>>p1.y; cout<<endl<<First point is =<<(<<p1.x<<,<<p1.y<<)<<endl<<endl; cout<<Enter the co-ordinates of second point<<endl; cout<<Enter x2 : ; cin>>p2.x; cout<<Enter y2 : ; cin>>p2.y; cout<<endl<<First point is =<<(<<p2.x<<,<<p2.y<<)<<endl<<endl; p.x=p1.x+p2.x; p.y=p1.y+p2.y; cout<<The co-ordinates of Third Point is<<endl; cout<<endl<<First point is =<<(<<p.x<<,<<p.y<<)<<endl<<endl; getch(); }

Create the equivalent of a four function calculator. The program should request the user to enter a number, an operator and another number. It should then carry out the specified arithmetical operation adding, subtracting, multiplicating or dividing the number finally. It should display the result. When it finishes the calculation, the program should ask of the user wants to do another calculation

#include <iostream.h> #include <conio.h>

void main() { clrscr(); int a, b, result; char ch, op; do { cout<<Enter the First Number<<endl; cin>>a; cout<<Operations for calculator are +,-,* and / only<<endl; cout<<Enter the Operator<<endl; cin>>op; cout<<Enter the Second Number<<endl; cin>>b; switch(op) { case +: result = a+b; cout<<The result of operation is ; cout<<result; break; case -: result = a-b; cout<<The result of operation is ; cout<<result; break; case *: result = a*b; cout<<The result of operation is ; cout<<result; break;

case /: result = a/b; cout<<The result of operation is ; cout<<result; break; default: cout<<Your Choice is wrong; } cout<<Do you want to continue using calculator .(Enter Y/N) cin>>ch; }while(ch != n || ch != N); getch(); }

A Phone number such as 212-767-8900 can be thought of as having 3 parts such as area code (212), exchange code (767) and the number (8900). WAP that uses a structure to store these 3 parts separately. Call the structure variable and type phone int and have input a number for the other one there. Display both numbers.

#include <iostream.h> #include <conio.h> struct phone { int area; int ex; int number; }ph1, ph2={111,455,3678}; void main() { clrscr(); cout<<Enter Details of Your Phone Number<<endl; cout<<Enter Area Code : ; cin>>ph1.area; cout<<Enter Exchange Code : ; cin>>ph1.ex; cout<<Enter Number : ; cin>>ph1.number; cout<<endl<<My number is <<endl; cout<<ph2.area<<-<<ph2.ex<<-<<ph2.number<<endl; cout<<endl<<Your number is <<endl; cout<<ph1.area<<-<<ph1.ex<<-<<ph1.number<<endl; getch(); }

Create two classes DM and DB which store the value of distance. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DB object, depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeters depending on the object on display.

#include <iostream.h> #include <conio.h> class DB; class DM { float m,cm; public : void setValue(float a, float b); friend void convert (DM, DB); };

class DB { float feet, inch; public : void setValue(float c, float d); friend void convert (DM, DB); };

void DM :: setValue(float a, float b) { m=a; cm=b; } void DB :: setValue(float c, float d) { feet=c; inch=d; }

void convert(DM p, DB q) { int ch; float x,y; do { cout<<endl<<Enter your choice of converting the Distance<<endl; cout<<1 for entering meter and centimeter; cout<<2 for entering feet and inch; cin>>ch; switch(ch) { case 1: q.feet = q.feet * .3048; x=p.m + q.feet; q.inch = q.inch * 2.54; y=p.cm + q.inch; cout<<endl<<Total in meter and centimeters = ; cout<<x<< <<y; break; case 2: p.m = p.m / .3048; x=p.m + q.feet; p.cm = p.cm / 2.54; y=p.cm + q.inch; cout<<endl<<Total in feet and inches = ; cout<<x<< <<y; break; case 3: exit(0); default: cout<<Wrong Choice !!<<endl<<Try Again.; } } while (k !=3); }

int main() { clrscr(); DM p; p.setValue(2.0, 4.5); DB q; q.setValue(6.0, 8.5); convert(p,q); return(0); }

Consider the following class definition class father{ protected: int age; public: father(int x){age=x;} virtual void iam() { cout<<I AM THE FATHER. My age is :<<age<<endl; } }; Derive two classes son and daughter from above class and for each define iam() to write similar messages, also use constructors for these classes. Write a main() that create objects of three classes and then calls iam() for them. Declare pointer to father. Successively assign the addresses of objects of two derived classes to this pointer and in each case call iam() through this pointer to demonstrate polymorphism.

#include <iostream.h> #include <conio.h> class son; class daughter; class father { protected : int age; public : father (int x) { age=x; } virtual void iam() { cout<<I AM THE FATHER.<<endl; cout<<endl<<My age is <<age; } };

class son : public father { protected : int s_age; public : son(int x) { s_age=x; } virtual void iam() { cout<<I am the Son.<<endl; cout<<My age is <<s_age; } };

class daughter : public father { protected : int d_age; public : daughter(int x) { d_age=x; } virtual void iam() { cout<<I am the Daughter.<<endl; cout<<My age is <<d_age; } };

void main() { father f(40); son s(10); daughter d(5); father *fptr;

fptr = &f; fptr->iam(); fptr = &s; fptr->iam(); fptr = &d; fptr->iam(); getch(); }

Imagine a tollbooth with a class called tollbooth. The two data items are a type unsigned int to hold the total number of cars and a type double to hold the total amount of money collected. A constructor initializes both these to 0. A member function called nopayCar() increments the car total but adds nothing to the cash total. Finally a member function called display(), displays to the two totals. Include a program to test this class. This program should allow the user to push one key to count a paying car and another to count a nonpaying car. Pushing the Esc Key should cause the program to print out the total cars and total ash and then exit.

#include <iostream.h> #include <conio.h> classs tollbooth { unsigned int total_cars; double money; public : tollbooth() { total_cars = 0; money=0; } void payingcar(void) { total_cars++; money = money + 0.50; } void nopaycar(void) { total_cars++; } void display(void) { cout<<endl<<Total number of cars :<<total_cars; cout<<endl<<Toll Tax per car :<<0.50<<endl; cout<<endl<<Total Tax collected :<<money<<endl; } };

void main() { clrscr(); tollbooth t; int ch; cout<<Pay.car(a)<<endl; cout<<Nopaycar(b)<<endl; cout<<Exit (ESC)<<endl; do { ch=getch(); if(ch==97) { t.payingcar(); cout<<DONE..<<endl; } else if(ch = 98) { t.nopaycar(); cout<<DONE..<<endl; } else if(ch==27) { t.display(); cout<<Exitting; getch(); exit(0); } } while(ch != 27); getch(); }

Create a class rational which represents a numerical value by two double values, NUMERATOR and DENOMINATOR. Include the follwing public member functions constructor with no arguments(default) 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 numbers overload >> operator to enable input through cin overload << operator to enable output through cout write a main () to test all the functions in the class.

#include <iostream.h> #include <conio.h> #include <stdio.h> /*rational class*/ class rational { int denominator, numerator; public rational() { denominator = 0; numerator = 0; } public rational(int x, int y) { denominator = x; numerator = y; } void reduce(); void display(); rational operator +(rational); friend istream &operator >> (istream &, rational &); friend ostream &operator >> (ostream &, rational &); };

rational rational :: operator +(rational r2) {

rational r3; r3.numerator = numerator * r2.denominator + denominator * r2.numerator; r3.denominator = denominator * r2.denominator; r3.reduce(); return r3; }

void rational :: reduce() { int i,s,fac=1; if(numerator<denominator) { s=numerator; } else { s=denominator; } //divide numerator and denominator so as to produce min. fraction for(i=2; i<=s; i++) { if(numerator %i ==0 && denominator %i == 0) fac=i; } numerator=numerator/fac; denominator=denominator/fac; }

istream & operator >> (istream & s, rational r2) { cout<<Enter numerator; s>>r2.numerator; cout<<Enter denominator; s>>r2.denominator; return s; }

ostream & operator >> (ostream & o, rational r2) { o<<endl<<Rational number is .; o<<Numerator<<r2.numerator;

o<<Denominator<<r2.denominator; return o; }

void rational :: display() { cout<<endl<<Result :<<numerator<</<<denominator; }

void main() { clrscr(); rational r1, r2(2,5), r3; cin>>r1; cout<<r1; cout<<r2; r3=r1+r2; r3.display(); getch(); }

Raising a number n to a power p is the same as multiplying n by itself p times. Write a program, function called power() that takes a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number will be squared. Write a main() function that gets values from the user to test the function.

#include <iostream.h> #include <conio.h> #include <stdio.h> double power(double n, int p=2) { int i; double k=1; for(i=1; i<=p; i++) { k=k*n; } return k; } void main() { clrscr(); int p; double s, num, z; cout<<endl<<Enter the base Number :; cin>>n; cout<<endl<<Enter the Power :; cin>>p; z=power(n,p); s=power(n); cout<<endl<<The calculated value is :<<z; cout<<endl<<The default value is :<<s; getch(); }

Write a function called reverseit() that reverses a string (an array of characters). Use a for loop that swaps the first and last characters, then second and next to last characters and so on. The string should be passed to reverseit() as an argument. Write a program to exercise reverseit(). The program should get a string from the user, call reverseit(), and print out the results. Use an input method that allows embedded blanks. Test the program with Napoleons famos phase, Able was I ere I Elba

#include <iostream.h> #include <conio.h> #include <stdio.h> #include <string.h> void reverseit(char s[200]) { int i, n, k; char a; i=strlen(s); k=0; for(n=i-1; n>=i/2; n--) { a=s[n]; s[n]=s[k]; s[k]=a; k++; } cout<<endl; cout<<Reversed string is :<<s; } void main() { char a[200]; clrscr(); cout<<Input the string :; gets(a); reverseit(a); //function call getch(); }

Make a class employee with a name and salary. Make a class manager inherit from employee. Add an instance variable named department of type string. Supply a method toString that prints the managers name, department and salary. Mame a executive inherit from manager. Supply a method toString that prints the string Executive followed by the information stored in the manager superclass object. Supply a test program that tests classes and methods.

#include <iostream.h> #include <conio.h> #include <stdio.h> class employee { protected : char name[20]; int salary; public : void getdata() { cout<<Enter Name; gets(name); cout<<Enter Salary; cin>>salary; } };

class manager : public employee { protected : char dept[20]; public : void getdata1() { cout<<Enter Department :; gets(dept); } void showdata1() { cout<<endl<<Managerss Name :<<name; cout<<endl<<Salary :<<salary;

cout<<endl<<Department :<<dept; } };

class executive : public manager { public : void showdata2() { cout<<endl<<Executive : ; cout<<endl<<Managers Name : <<name; cout<<endl<<Salary : <<salary; cout<<endl<<Department : <<dept; } };

main() { clrscr(); executive e; e.getdata(); e.getdata1(); e.getdata2(); e.showdata(); e.showdata1(); e.showdata2(); getch(); }

WAP that creates a binary file by reading the data for the each student from the terminal. The data of each student consists of rollno, name(a string of 60 or lesser number of characters) and marks.

#include <fstream.h> #include <iostream.h> #include <conio.h> #include <stdio.h>

class student { char name[60]; int rollNo; int marks; public : void getdata() { cout<<endl<<Enter Name :; gets(name); cout<<endl<<Enter Roll Number :; cin>>rollNo; cout<<endl<<Enter Marks :; cin>>marks; } void showdata() { cout<<endl<<Name :<<name; cout<<endl<<Roll No. :<<rollNo; cout<<endl<<Marks :<<marks; } };

void main() { clrscr(); student s[10]; int u; fstream file; file.open(student.dat,ios::in|ios::out|ios::binary); cout<<Enter the Number of Student :; cin>>u;

//for the number of records to be entered, read the record from console //and write into the file for(int i=0; i<u; i++) { s[i].getdata(); file.write((char * *s[i], sizeof(s[i]))); } file.seekg(0); cout<<endl<<FILE CONTENTS ARE<<endl<<endl; //reading the data from the file and showing it using showdata function for(i=0; i<u; i++) { file.read((char *) *s[i]); s[i].showdata(); } //closing the file file.close(); getch(); }

Create some objects of the string class and put them in Deque, some at the head of the Deque and some at the tail. Display the contents of the Deque using the forEach() function and a user written display function. Then search the Deque for a particular string using the first That() function and display any string that match. Finally remove all the items from the Deque using the getLeft() function and display each item. Notice the order in which the items are displayed using getLeft(), those interested on the left (head) of the Deque are removed in first in first out order. The opposite would be true if getRight() were used.

A hospital wants to create a database regarding its indoor patients. The information to store includes (a) Name of Patient (b) Date of Admission (c) Disease (d) Date of Discharge Create a structure to store the data (year, month, and date as its members). Create a base class to store the above information. The member function should include function to enter information and display a list of all the patients in the database. Create a derived class to store the age of the patients. List the information about all the patients to store the age of the patients. List the information about all the pediatric patients (less than 12 years in age)

Oracle File

Create a database and perform following operation:Add records into it Modify a record Generate queries Data operations List all the records Create table student (stud_id varchar2(20), stud_name varchar2(20), stud_dept varchar2(15)); Alter table student add (stud_fees varchar2(20)); Create table Teacher (teach_id varchar2(20), teach_name varchar2(20), teach_subject varchar2(20), dept varchar2(15), DOJ date); Insert into student values (MTCSE-01, Mandeep, MTech, 58808); Insert into student values (MTCSE-02, Sukhdeep, MTech, 58809); Insert into student values (BTCSE-03, Nipun, BTCSE, 12000); Insert into student values (BTCSE-04, Nirupam, BTCSE, 12000); Insert into student values (BTCSE-05, Nirmala, BTCSE, 34111); Insert into student values (BTCSE-06, Sujata, BTCSE, 34113); Insert into student values (MCA-05, Nirmala, MCA, 1234111); Insert into student values (MCA-06, Sujata, MCA, 1234113); Insert into teacher values (MCA-01, Sukhvinder, Data Structures, MCA, 10/10/1998); Insert into teacher values (MCA-02, Sandeep, Database Mgt, MCA, 1/1/2000); Insert into teacher values (MCA-03, Navdeep, DMS, MCA, 1/4/1999); Insert into teacher values (BT-01, Sukhdeep, C, BTCSE, 1/1/2004); Insert into teacher values (BT-02, Kuldeep, C++, BTCSE, 5/7/2008);

Insert into teacher values (BT-03, Surender, Web Engineering, BTCSE, 6/9/2007); Select * from student; Select stud_id, stud_name from student; Select from student where stud_stream = MTech; Select stud_name, stud_fees from student where stud_fees = 58808; Update student set stud_name = Sukhvinder Singh where stud_name = Mandeep; Delete from student where stud_id=MTCSE-01; Rename student to Mandeep; Drop table Mandeep; Arithmetic Operators Select stud_fees+500 from student; Select stud_fees-300 from student; Select stud_fees*2 from student; Select stud_fees/2 from student; Select stud_name from student where stud_fees = 58808; Select stud_name, stud_fees from student where stud_fees > 50000; Select stud_name, stud_fees from student where stud_fees <> 50000; Select stud_name, stud_fees from student where stud_name LIKE M%; Select stud_name || stud_fees from student;

Logical Operators Select stud_id, stud_name, stud_fees from student where stud_fees > 50000 AND stud_name LIKE M%; Select stud_id, stud_name, stud_fees from student where stud_fees > 50000 OR stud_name LIKE M%;

Select stud_id, stud_name, stud_fees from student where NOT stud_fees < 50000;

Set Operations Intersection Select stud_id, stud_name from student INTERSECT select teach_id, teach_name, teach_subject from teacher; Union Select stud_id, stud_name from student UNION select teach_id, teach_name, teach_subject from teacher; Minus Select stud_name from student MINUS select stud_name from student where stud_fee > 50000;

Other Operators IN Select stud_name from student where stud_fee IN (58808, 12000); BETWEEN Select stud_name from student where stud_fee BETWEEN 58808 AND 12000;

Functions Aggregate Functions COUNT Select count(stud_name) from student; SUM Select SUM(stud_fees) from student; AVERAGE Select AVG(stud_fees) from student; MAXIMUM

Select MAX(stud_fees) from student; MINIMUM Select MIN(stud_fees) from student;

Date Functions Select add_month(DOJ,1) from teacher; Select months_between(SYSDATE, DOJ) from teacher; Arithmetic Functions Select CEIL(3.7) from tab; Select FLOOR(5.2) from tab; Select Mod(10,3) from teacher; Select Exp(2) from student where stud_fees > 50000; Select power(2,3) from student; Select sqrt(9) from student; Character Functions Select chr(65) from student; Select concatenate (stud_name, stud_id) from student; Select initcap(stud_name) from student; Select lower (stud_name) from student; Select upper(stud_name) from student; Select Lpad(stud_name, 10, #) from student; Select Rpad(stud_name, 10, ?) from student;

Ordering the Records of Database

Ascending Order SELECT * from teacher order by teach_name; Descending Order SELECT * from teacher order by teach_name desc; View CREATE view students_details as select * from student where stud_dept IN (select dept from teacher where teach_name = Sukhdeep);

Create a database with data constraints ensuring that tables with the similar names do not exist within the database. Also Add records in it. CREATE table Voter(Name char(15) UNIQUE, ID Number(3) PRIMARY KEY, City char(8) NOT NULL, Age Number(3) CHECK(Age >= 18), Salary Number(5) DEFAULT 1000); Insert into Voter (Mandeep, 1, Panipat, 20, 20000); Insert into Voter (Sukhdeep, 2, Karnal, 25, 17000); Insert into Voter (Mandeep, 3, Gharaunda, 27, 42000);

Create table Ward (Ward_Name char(20) Unique, NoOfVoters Number(5) NOT NULL, )

Generate Queries on table data using:Arithmetic Operators Logical Operators Aggregate Functions Date Manipulation functions Write a program to implement Having and Group By clause Write a program to implement Joins and Correlation Join Select stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJ from student, teacher where stud_dept = dept group by dept desc; Left Outer Join

Select stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJ from student, teacher where stud_dept = dept(+) group by dept desc; Right Outer Join Select stud_id, stud_name, stud_dept, teach_id, teach_name, teach_subject, dept, DOJ from student, teacher where stud_dept(+) = dept group by dept desc;

Generate subqueries to Update and list the records with specific conditions. Execute queries using Union, Intersect and Minus clauses. Create View on OrderNo, OrderDate, OrderDate, OrderStatus of the Sales_Order and ProductNo, ProductRate and QtyOrdered of Sales_Order_Details table.

Вам также может понравиться