Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

HUIS

cpp

#include <iostream>
#include <time.h>
#include <iostream>
using namespace std;


struct el
{
    int ch;
    struct el *p;
};




void slist(struct el *p11, struct el *p22, struct el **p3)
{
    struct el *p4,*p1,*p2;
    p1=p11;
    p2=p22;
    if (p1->ch <= p2->ch)
    {
        *p3=p1;
        p4=p1;
        p1=p1->p;
    }
    else
    {
        *p3=p2;
        p4=p2;
        p2=p2->p;
    }
    while ((p1!=NULL)&&(p2!=NULL))
        if (p1->ch <= p2->ch)
        {
            p4->p=p1;
            p4=p1;
            p1=p1->p;
        }
        else
        {
            p4->p=p2;
            p4=p2;
            p2=p2->p;
        }
        if (p1!=NULL) p4->p=p1;
        else p4->p=p2;
}



void sortlist(struct el **p, int n)
{
    struct el *p1,*p2;
    int k,i;
    if (n>1)
    {
        k=n/2;
        p1=*p;
        for (i=1; i<k-1; i++) p1=p1->p;
        
        p2=p1->p;
        p1->p=NULL;
        p1=*p;
        
        sortlist(&p1,k);
        sortlist(&p2,n-k);
        slist(p1,p2,p);
    }
}



int main()
{
    int n = 100;
    srand(time(NULL));
    int i;
    struct el *p1,*p2,*p3;
    p1 = new el;
    p2 = p1;
    for (i=0; i < n; i++)
    {
        p2->ch = rand() % 2000 - 999;
        if (i<n-1)
        {
            p3 = new struct el;
            p2->p = p3;
            p2 = p3;
        }
    }
    p2->p = NULL;
    
    p3=p1;
    for (i=0; i <n; i++)
    {
        cout << p3->ch <<"\n";
        p3=p3->p;
    }
    
    sortlist(&p1,n);
    
    return 0;
}

Suma fraccions

cpp

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
   cout << "\tSuma de fraccions" << endl; 
   
   double valor1 = 3;
   double valor2 = 5;
   double valor3 = 7;
   double valor4 = 3;
   double fraccio1 = valor1/valor2;
   double fraccio2 = valor3/valor4;
   double resultat = fraccio1 + fraccio2;
   
   cout << "Fracció 1 = " << valor1 << "/" << valor2 <<endl;
   cout << "Fracció 2 = " << valor3 << "/" << valor4 <<endl;
   cout << "resultat suma de les fraccions = " << resultat << endl;
   
   return 0;
}

Compile and Execute C++ Online

cpp

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   
   return 0;
}

Compile and Execute C++ Online

cpp

#include <iostream>
#include<iomanip>
using namespace std;

const int MAX=30;

class Queue //Queue for BFS TRAVERSAL
{
	int front,rear;
	string data[MAX];
public:
	Queue()
{
		front=-1;
		rear=-1;
}
	bool empty()
	{
		if(rear==-1)
			return 1;
		else
			return 0;
	}
	bool inqueue(string str)
	{
		if(front==-1 && rear==-1)
		{
			front=rear=0;
			data[rear]=str;
			return true;
		}
		else
		{
			rear=rear+1;
			data[rear]=str;
			return true;
		}
	}
	string dequeue()
	{
		string  p;
		if(front==rear)
		{
			p=data[front];
			front=-1;
			rear=-1;
		}
		else
		{
			p=data[front];
			front=front+1;
		}
		return p;
	}

};

class node //node class for each airport
{
	node *next;
	string city;
	int timeCost;
public:
	friend class graph;
	node()
	{
		next=NULL;
		city="";
		timeCost=-1;
	}
	node(string city,int weight)
	{
		next=NULL;
		this->city=city;
		timeCost=weight;
	}
};
class graph //Contains total graph of airports
{
	node *head[MAX];
	int n;
	int visited[MAX];
public:
	graph(int num)
{
		n=num;
		for(int i=0;i<n;i++)
			head[i]=NULL;
}
	void insert(string city1,string city2,int time);
	void insertUndirected(string city1,string city2,int time);
	void readdata(int gType);
	int getindex(string s1);
	void outFlights();
	void inFlights();
	void DFS(string str);
	void BFS();
	void dfsTraversal();

};
void graph::BFS()
{
	string str=head[0]->city;
	int j;
	//node *p;
	for(int i=0;i<n;i++)
		visited[i]=0;
	Queue queue;
	queue.inqueue(str);
	int i=getindex(str);
		cout<<"BFS Traversal: \n";
		cout<<" "<<str<<" ";
		visited[i]=1;
		while(!queue.empty())
		{
			string p=queue.dequeue();
			i=getindex(p);


			//visited[i]=1;

			for(node *q=head[i];q!=NULL;q=q->next)
			{
				i=getindex(q->city);
				str=q->city;
				if(!visited[i])
				{
					queue.inqueue(q->city);
					visited[i]=1;
					cout<<" "<<str<<" ";
				}
			}
		}
		cout<<"\n";
}
void graph::dfsTraversal()
	{
		for( int i=0;i<n;i++)
			visited[i]=0;
		cout<<"\nDFS TRAVERSAL: \n";
		DFS(head[0]->city);
		cout<<"\n";
	}
void graph::DFS(string str)
{
	node *p;
	int i=getindex(str);

	cout<<" "<<str<<" ";
	p=head[i];
	visited[i]=1;
	while(p!=NULL)
	{
		str=p->city;
		i=getindex(str);
		if(!visited[i])
			DFS(str);
		p=p->next;
	}

}
void graph::inFlights()
{
	int count[n];
	for(int i=0;i<n;i++)
		count[i]=0;
	cout<<"====== In degree =========\n";
	for(int i=0;i<n;i++)
	{
		cout<<"\n"<<setw(8)<<"Source"<<setw(8)<<"Destin."<<setw(8)<<"Time";
		for(int j=0;j<n;j++)
		{
			node *p=head[j]->next;
			while(p!=NULL)
			{
				if(p->city==head[i]->city)
				{
					count[i]=count[i]+1;
					cout<<"\n"<<setw(8)<<head[j]->city<<setw(8)<<head[i]->city<<setw(8)<<p->timeCost;
				}

				p=p->next;
			}
		}
		cout<<"\nFlights to "<<head[i]->city<<" = "<<count[i]<<endl;
		cout<<"-------------------------------------\n";
	}

}
void graph::outFlights()
{
	int count;
	for(int i=0;i<n;i++)
	{
		node *p=head[i]->next;
		count=0;
		cout<<"\n"<<setw(8)<<"Source"<<setw(8)<<"Destin."<<setw(8)<<"Time";
		if(p==NULL)
		{
			cout<<"\nNo Flights from "<<head[i]->city;
		}
		else
		{
			while(p!=NULL)

			{
				cout<<"\n"<<setw(8)<<head[i]->city<<setw(8)<<p->city<<setw(8)<<p->timeCost;
				count++;
				p=p->next;
			}
		}
		cout<<"\nNo. of flights: "<<count<<endl;;
		cout<<"-------------------------------------\n";
	}
}
int graph::getindex(string s1)
{
	for(int i=0;i<n;i++)
	{
		if(head[i]->city==s1)
			return i;
	}
	return -1;
}
void graph::insert(string city1,string city2,int time)
{
	node *source;
	node *dest=new node(city2,time);

	int ind=getindex(city1); //for getting head nodes index in array
	if(head[ind]==NULL)
		head[ind]=dest;
	else
	{
		source=head[ind];
		while(source->next!=NULL)
			source=source->next;
		source->next=dest;
	}
}
void graph::insertUndirected(string city1,string city2,int time)
{
	node *source;
	node *dest=new node(city2,time);
	node *dest2=new node(city1,time); //for second flight insertion

	int ind=getindex(city1); //for getting head nodes index in array
	int ind2=getindex(city2);
/*	if(head[ind]==NULL && head[ind2]==NULL) //when no flights in graph
	{
		head[ind]=dest;
		head[ind2]=dest2;
	}
	else if(head[ind]==NULL && head[ind2]!=NULL) //no flight in first list but flight in second list
	{
		head[ind]=dest; //inserted first flight
		source=head[ind2];
		while(source->next!=NULL)
			source=source->next;
		source->next=dest2;
	}
	else if(head[ind]!=NULL && head[ind2]==NULL)
	{
		head[ind2]=dest2; //inserted first flight
		source=head[ind];
		while(source->next!=NULL)
			source=source->next;
		source->next=dest;
	}
	else
	{*/
		source=head[ind];
		while(source->next!=NULL)
			source=source->next;
		source->next=dest;

		source=head[ind2];
		while(source->next!=NULL)
			source=source->next;
		source->next=dest2;

	//}
}
void graph::readdata(int gType)
{
	string city1,city2,tmpcity;
	int fcost;
	int flight;
	cout<<"\nENter City Details:\n ";
	for(int i=0;i<n;i++)
	{
		head[i]=new node;
		cout<<"Enter City "<<i+1<<" ";
		cin>>tmpcity;
		head[i]->city=tmpcity;
	}
	cout<<"\nEnter Number of Flights to insert: ";
	cin>>flight;
	if(gType==1)
	{
		for(int i=0;i<flight;i++)
		{
			cout<<"\nEnter Source:";
			cin>>city1;
			cout<<"Enter Destination:";
			cin>>city2;
			cout<<"Enter Time:";
			cin>>fcost;
			insert(city1,city2,fcost);
		}
	}
	else
	{
		for(int i=0;i<flight;i++)
		{
			cout<<"\nEnter Source:";
			cin>>city1;
			cout<<"Enter Destination:";
			cin>>city2;
			cout<<"Enter Time:";
			cin>>fcost;
			insertUndirected(city1,city2,fcost);
			//cout<<"\ninserted"<<i+1;
		}
	}
}
int main() {
	int number,choice;
	int graphype;
	cout<<"-------INDIA AIRLINES PVT LTD--------"
		<<"\n0. Undirected\n1.Directed\nEnter Flight data Insertion Type:";
	cin>>graphype;
	cout<<"\nENter Number of Airport Stations:";
	cin>>number;
	graph g1(number);
	g1.readdata(graphype);
	do
	{
		cout<<"-------- Menu --------"
				<<"\n1.Incoming Flights(In degree)"
				<<"\n2.Outgoing Flights(Out degree)"
				<<"\n3.DFS"
				<<"\n4.BFS"
				<<"\n5.Exit"
				<<"\nEnter your choice: ";
		cin>>choice;
		switch(choice)
		{
		case 1:
			cout <<"" << endl;
			g1.inFlights();
			break;
		case 2:
			g1.outFlights();
			break;
		case 3:
			g1.dfsTraversal();
			break;
		case 4:
			g1.BFS();
			break;
		default:
			cout<<"\nWrong Choice";
		}
	}while(choice!=5);


	return 0;
}

case study

cpp

#include <fstream>
#include <iostream>
using namespace std;
 
int main () {
   char data[100];

   // open a file in write mode.
   ofstream outfile;
   outfile.open("afile.dat");

   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 100);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Enter your age: "; 
   cin >> data;
   cin.ignore();
   
   // again write inputted data into the file.
   outfile << data << endl;

   // close the opened file.
   outfile.close();

   // open a file in read mode.
   ifstream infile; 
   infile.open("afile.dat"); 
 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // write the data at the screen.
   cout << data << endl;
   
   // again read the data from the file and display it.
   infile >> data; 
   cout << data << endl; 

   // close the opened file.
   infile.close();

   return 0;
}

Custorm string

cpp

#include <iostream>
#include <string>
#include <memory>

using namespace std;

typedef unsigned char byte;

class styledChar
{
    public:
        styledChar() {};
        styledChar(wchar_t chr): _chr(chr), _styleIndex(0) {};
        wchar_t GetChar() const { return _chr; };
        byte GetStyleIndex() const { return _styleIndex; };
        void SetStyleIndex(byte styleIndex) { _styleIndex = styleIndex; };
    private:
        wchar_t _chr;
        byte _styleIndex;
};

class styledString: public basic_string<styledChar>
{
    public:
        styledString(const wstring& right) : basic_string<styledChar>(CreateBuf(right).get(), right.length())
        {
        }
    
    private:
        static unique_ptr<styledChar[]> CreateBuf(const wstring& right)
        {
            unique_ptr<styledChar[]> buf(new styledChar[right.length()]);
            int i = 0;
            for (auto r : right)
            {
                styledChar ch(r);
                buf[i++] = ch;
            }
            return buf;
        }
        
    friend wostream& operator<< (wostream &out, const styledString &str)
    {
        for(auto c : str)
        {
            out << c.GetChar();
        }       
        return out;
    };
};

int main()
{
   styledString s(L"test string");
   

   wcout << s << endl; 
   
   return 0;
}

Permutation

cpp

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool CheckPermutation(string str1, string str2);

int main()
{
   cout << "Hello World" << endl;
   string str1 = "abcdef";
   string str2 = "cfaedd";
   
  std::cout << CheckPermutation(str1, str2);
   
   return 0;
}


bool CheckPermutation(string str1, string str2) {
    if (str1.length() != str2.length()) {
        return false;
    }
    
    //std::sort(str1.begin(), str1.end());
    //std::sort(str2.begin(), str2.end());
    bool[] letters = new  bool[128];
    
    for (int i=0; i< str1.length(); i++) {
        int val = str1[i];
        letters[val]++;
    }
    
    for(int j=0; j < str2.length(); j++) {
        int val = str2[j];
        letters[val]--;
        
        if(letters[val] < 0) {
            return false;
        }
    }
    
    return true;
    
}

help!!!!!

cpp

#include <iostream>

using namespace std;

int main()
{
    int i, j, flag,D[10],m=0,l;
    int C[]={78, 73, 90, 88, 87, 86, 41 ,62, 61, 50};
  for(i=0;i<10;i++)
    {
     flag=0;
        for(j=2;j<C[i];j++)
        {
            cout<<C[i]<<"\n";
            if(C[i]%j==0)
                flag=1;
                break;
        }
     if(flag==0)
     {
         D[m]=C[i];
            m++;
     }
 }
  for(i=0;i<m;i++)
     {
     cout<<D[i]<<" ";
     } 
}

NetworkFlow

cpp

//
//  main.cpp
//  NetworkFlow
//
//  Created by Himanshu Singh on 4/5/19.
//  Copyright © 2019 Himanshu Singh. All rights reserved.
//

#include <iostream>
#include <cstdlib>
#include <stack>
#include <queue>



using namespace std;
class Edge{
public: int tail,head,flow;
    public : Edge(int tail,int head,int flow){
        this->tail=tail;
        this->head=head;
        this->flow=flow;
    }
    
};




class Network{
    private :
    class Node{
        public :
        Edge *e;
        Node *next;
    public:
        Node(Edge *e,Node *next){
            this->e=e;
            this->next=next;
        }
    };
public:
    int v,e;
    Node** adjList;
public:
    Network(int v,int e){
        this->v=v;
        this->e=e;
        adjList=(Node**) malloc(( sizeof(Node*) * (v) ));
        //adjList=new Node[v-1];
        
        
    }
    ~Network(){
        adjList = (Node**) realloc(adjList, (v)*sizeof(Node));
        cout<<"\ndestruct";
    }
    
    void insert(Edge *edge){
        //  cout<<"size"<<sizeof(adjList);
        int tail=edge->tail;
        int head=edge->head;
        //        Node* nodeTail = new Node(edge,adjList[tail]);
        //        nodeTail->e=edge;
        //        nodeTail->next=adjList[tail];
        // adjList[head]=new Node(edge,adjList[head]);
        
        adjList[tail]=new Node(edge,adjList[tail]);
        
        //        Node* nodeHead = new Node();
        //        nodeHead->e=edge;
        //        nodeHead->next=adjList[head];
        //        adjList[head]=nodeHead;
        //cout<<" data "<<adjList[0].e.flow;
    }
    
    void print(){
        Node* n;int i=0;
        while(adjList[i]){
            cout<<"\n"<<adjList[i]->e->tail;
            cout<<" "<<adjList[i]->e->head;
            n=adjList[i]->next;
            while(n){
                cout<<" "<< n->e->head;
                n=n->next;
                
            }
            i++;
        }
    }
    
    void path(){
       
        int source=0,sink=v-1;
        stack<Node*> stack;
        Node *point=adjList[0];
        stack.push(point);
        while(point->e->head!=sink){
            point=adjList[point->e->head];
            stack.push(point);
        }
        cout<<"\n";
        while(!stack.empty()){
            point=stack.top();
            cout<<point->e->head<<" s ";
            stack.pop();
        }
        
//        while(stack.size()>0){
//            Node* n=stack.top();
//            int i=1; int min=n->e->flow;
//            while(sink==n->e->head && i<e){
//                stack.push(adjList[n->e->head]);
//                n=stack.top();
//                if(min>n->e->flow)
//                    min=n->e->flow;
//                i++;
//            }
//            queue<Node*> q;
//            while (stack.empty()) {
//                Node* p=stack.top();
//                cout<<p->e->head<<" ";
//                p->e->flow-=min;
//                q.push(p);
//                stack.pop();
//            }
//            while(q.empty()){
//                stack.push(q.front());
//                q.pop();
//            }
//            Node* k=stack.top();
//            k=k->next;
//            while(!k && !stack.empty()){
//                stack.pop();
//                k=stack.top();
//                k=k->next;
//            }
//            if(!stack.empty()){
//                stack.push(k);
//            }
//
//        }
    }
    
};

int main(int argc, const char * argv[]) {
    // insert code here...
    cout << "Hello, World!\n";
    int V,E;
    cin>>V>>E;
    Network* network=new Network(V,E);
    int head,tail,cap;
    for (int i=0; i<E ;i++){
        cin>>tail>>head>>cap;
        // Edge* edge= new Edge(tail,head,cap);
        network->insert(new Edge(tail,head,cap));
    }
    network->print();
    network->path();
    network->~Network();
    return 0;
}


RafaTorresDeveloper: First IF ELSE C++

cpp

#include <iostream>

using namespace std;

int main()
{
    // This is a comment
    // We can write what we want there to help
    // Us communicate things to other programmers
    
    // Let's setup some data here to use for our program
    // int is an integer data type like -4, -2, -1, 0, 1, 2, 3 no decimals
    // We can read up on other data types here: 
    // https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
    
    // Change the value 7 to other integers greater and smaller than 6 to change your output
    int integerData = 4;
    
    if(integerData >= 6)
    {
        cout << "My integerData " + std::to_string(integerData) + " is greater than or equal to 6" << endl;
    }
    else
    {
        cout << "My integerData " + std::to_string(integerData) + " is less than 6" << endl;        
    }
    
    if(integerData = integerData)
    {
        
        if(integerData >= 6)
        {
            cout << "In Nested If Else: My integerData " + std::to_string(integerData) + " is greater than or equal to 6" << endl;
        }
        else
        {
            cout << "In Nested If Else: My integerData " + std::to_string(integerData) + " is less than 6" << endl;        
        }
    }
   
   return 0;
}

Previous 1 ... 3 4 5 6 7 8 9 ... 518 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.