Vous êtes sur la page 1sur 33

#include<iostream.

h>
#include<conio.h>
class Matrix{
int a[3][3],b[3][3],c[3][3],i,j;
public:
void getAMatrix(){
cout<<"Enter 3x3 Matrix for A :";
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cin>>a[i][j];
}
}
}
void getBMatrix(){
cout<<"Enter 3x3 Matrix for B :";
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cin>>b[i][j];
}
}
}
void dis(){
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<c[i][j]<<"\t";
}
cout<<endl;
}

}
Matrix operator+(Matrix m);
Matrix operator-(Matrix m);
};
Matrix Matrix::operator+(Matrix m){
Matrix temp;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
temp.c[i][j]=a[i][j]+m.b[i][j];
}
}
return temp;
}
Matrix Matrix::operator-(Matrix m){
Matrix temp;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
temp.c[i][j]=a[i][j]-m.b[i][j];
}
}
return temp;
}
void main(){
Matrix a,b,c;
clrscr();
a.getAMatrix();
b.getBMatrix();
c=a+b;
cout<<"The Matrix Addition = \n";
c.dis();
c=a-b;
cout<<"The Matrix Subtraction = \n";
c.dis();
getch();
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#define MAX 100
class Stack{

public:
int stack[MAX],top,n;
Stack(){
top=-1;
n=0;
}
void size(){
cout<<"Enter stack size :";
cin>>n;
}
void push(int data){
top++;
stack[top]=data;
}
void pop(){
int item;
if(top==-1){
cout<<"\nStack is empty...";
}
else{
item=stack[top];
top--;
cout<<"\nThe Deleted Item is :"<<item;
}

};

void main(){
Stack s;
int data,ch;
char ex;
clrscr();
s.size();
do{
cout<<"\n\n1.Push..\n2.Pop..\n3.Exit..\nEnter Your
Choice :";
cin>>ch;
switch(ch)
{
case 1:
if(s.top==s.n-1)
{
cout<<"\nStack Overflow...";
}
else
{
cout<<"\nEnter Insert element on to the
Stack..";
cin>>data;
s.push(data);
}
break;
case 2:
s.pop();
break;
case 3:
cout<<"\nAre you sure you want to exit from stack
operations...?(Y/N)";
cin>>ex;
break;
default:
cout<<"Choice is Invalid...";
}
}while((ex!='Y')&&(ex!='y'));
getch();
}
OUTPUT
#include<iostream.h>
#include<conio.h>
#define MAX 100
class Queue{

public:
int queue[MAX],front,rear,n;
Queue(){
front=-1;
rear=-1;
n=0;
}
void size(){
cout<<"\nEnter queue size :";
cin>>n;
}
void insert(int data){
rear++;
queue[rear]=data;
}
void delet(){
int item;
if(front==rear){
cout<<"\nQueue is empty...";
}
else{
front++;
item=queue[front];
cout<<"\nThe Deleted Item is :"<<item;
}

};

void main(){
Queue s;
int data,ch;
char ex;
clrscr();
s.size();
do{
cout<<"\n\n1.Insert..\n2.Delete..\n3.Exit..\nEnter Your
Choice :";
cin>>ch;
switch(ch)
{
case 1:
if(s.rear==s.n-1)
{
cout<<"\nQueue Overflow...";
}
else
{
cout<<"\nEnter Insert element on to the
Queue..";
cin>>data;
s.insert(data);
}
break;
case 2:
s.delet();
break;
case 3:
cout<<"\nAre you sure you want to exit from
queue operations...?(Y/N)";
cin>>ex;
break;
default:
cout<<"Choice is Invalid...";
}
}while((ex!='Y')&&(ex!='y'));
getch();
}
OUTPUT
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct poly{
int coeff;
int pow;
poly *next;
};

class add2poly
{
poly *poly1, *poly2, *poly3;
public:
add2poly(){poly1=poly2=poly3=NULL;}
void addpoly();
void display();
};

void add2poly :: addpoly(){


int i,p;
poly *newl=NULL,*end=NULL;
cout<<"Enter highest power for x\n";
cin>>p;
//Read first poly
cout<<"\nFirst Polynomial\n";
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly1==NULL)
poly1=newl;
else
end->next=newl;
end=newl;
}

//Read Second poly


cout<<"\n\nSecond Polynomial\n";
end=NULL;
for(i=p;i>=0;i--)
{
newl=new poly;
newl->pow=p;
cout<<"Enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coeff;
newl->next=NULL;
if(poly2==NULL)
poly2=newl;
else
end->next=newl;
end=newl;
}

//Addition Logic
poly *p1=poly1,*p2=poly2;
end=NULL;
while(p1 !=NULL && p2!=NULL){
if(p1->pow == p2->pow){
newl=new poly;
newl->pow=p--;
newl->coeff=p1->coeff + p2->coeff;
newl->next=NULL;
if(poly3==NULL)
poly3=newl;
else
end->next=newl;
end=newl;
}
p1=p1->next;
p2=p2->next;
}
}

void add2poly :: display(){


poly *t=poly3;
cout<<"\n\nAnswer after addition is : ";
while(t!=NULL){
cout.setf(ios::showpos);
cout<<t->coeff;
cout.unsetf(ios::showpos);
cout<<"X"<<t->pow;
t=t->next;
}
}

void main(){
clrscr();
add2poly obj;
obj.addpoly();
obj.display();
getch();
}
OUTPUT

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct tree_node
{
tree_node *left;
tree_node *right;
int data;
};
class bst
{
tree_node *root;
public:
bst()
{
root=NULL;
}
int isempty()
{
return(root==NULL);
}
void insert(int item);
void inordertrav();
void inorder(tree_node *);
void postordertrav();
void postorder(tree_node *);
void preordertrav();
void preorder(tree_node *);
};
void bst::insert(int item)
{
tree_node *p=new tree_node;
tree_node *parent;
p->data=item;
p->left=NULL;
p->right=NULL;
parent=NULL;
if(isempty())
root=p;
else
{
tree_node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(item>ptr->data)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(item<parent->data)
parent->left=p;
else
parent->right=p;
}
}
void bst::inordertrav()
{
inorder(root);
}
void bst::inorder(tree_node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<" "<<ptr->data<<" ";
inorder(ptr->right);
}
}
void bst::postordertrav()
{
postorder(root);
}
void bst::postorder(tree_node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<" "<<ptr->data<<" ";
}
}
void bst::preordertrav()
{
preorder(root);
}
void bst::preorder(tree_node *ptr)
{
if(ptr!=NULL)
{
cout<<" "<<ptr->data<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void main()
{
bst b;
clrscr();
b.insert(52);
b.insert(25);
b.insert(50);
b.insert(15);
b.insert(40);
b.insert(45);
b.insert(20); cout<<"inorder"<<endl;
b.inordertrav();
cout<<endl<<"postorder"<<endl;
b.postordertrav();
cout<<endl<<"preorder"<<endl;
b.preordertrav();
getch();
}
OUTPUT:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
// Function prototype
void showState(fstream &);

void main(void)
{
fstream testFile("stuff.txt", ios::out);
clrscr();
if (testFile.fail())
{
cout << "cannot open the file.\n";
return;
}
int num = 10;
cout << "Writing to the file.\n";
testFile << num; // Write the integer to testFile
showState(testFile);
testFile.close(); // Close the file
testFile.open("stuff.txt", ios::in); // Open for input
if (testFile.fail())
{
cout << "cannot open the file.\n";
return;
}
cout << "Reading from the file.\n";
testFile >> num; // Read the only number in the file
showState(testFile);
cout << "Forcing a bad read operation.\n";
testFile >> num; // Force an invalid read operation
showState(testFile);
testFile.close(); // Close the file
getch();
}
void showState(fstream &file)
{
cout << "File Status:\n";
cout << " eof bit: " << file.eof() << endl;
cout << " fail bit: " << file.fail() << endl;
cout << " bad bit: " << file.bad() << endl;
cout << " good bit: " << file.good() << endl;
file.clear(); // Clear any bad bits
}
OUTPUT
#include<iostream.h>
#include<conio.h>
#include<process.h>
class slink
{
private:
struct node
{
int data;
node*next;
}*head;
public:
slink()
{
head=NULL;
}
void create();
void insert();
void delnode();
void display();
};
void slink::create()
{
head=new node;
cout<<"Enter the data for the node:";
cin>>head->data;
head->next=NULL;
}
void slink::insert()
{
int n;
char p;
node*list,*prev,*temp;
list=head;
cout<<"Insert node at front/middle/last? f/m/l:"<<endl;
cin>>p;
if(p=='f')
{
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
temp->next=head;
head=temp;
}
if(p=='m')
{
cout<<"Enter the data for the new node before:"<<endl;
cin>>n;
while(list->data!=n)
{
prev=list;
list=list->next;
}
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
prev->next=temp;
temp->next=list;
}
if(p=='l')
{
while(list->next!=NULL)
list=list->next;
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
temp->next=NULL;
list->next=temp;
}
}
void slink::delnode()
{
int n,p=0;
node *list,*prev,*temp;
list=head;
cout<<"Enter the data for the node to be deleted:";
cin>>n;
if(head->data==n)
{
head=head->next;
p=1;
}
else
{
while((list->next!=NULL)&&(list->data!=n))
{
prev=list;
list=list->next;
}
if((list->next!=NULL)&&(list->data==n))
{
temp=list;
prev->next=NULL;
p=1;
}
else if(list->data==n)
{
temp=list;
prev->next=list->next;
p=1;
}
delete(temp);
}
if(p==0)
{
cout<<"Node is not present";
}
}
void slink::display()
{
node*list;
list=head;
if(list==NULL)
cout<<"List is empty";
else
{
cout<<"The list is: ";
while(list!=NULL)
{
cout<<list->data<<"<==>";
list=list->next;
}
cout<<"NULL";
}
getch();
}
void main()
{
int option;
slink s;
while(option!=5)
{
clrscr();
cout<<"1.Create()"<<endl;
cout<<"2.Insert()"<<endl;
cout<<"3.Delete()"<<endl;
cout<<"4.Display()"<<endl;
cout<<"5.Exit()"<<endl;
cout<<"Enter your option:";
cin>>option;
switch(option)
{
case 1:
s.create();
break;
case 2:
s.insert();
break;
case 3:
s.delnode();
break;
case 4:
s.display();
break;
case 5:
break;
}
}
}
OUTPUT

Vous aimerez peut-être aussi