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

C++ code for infix to postfix conversion

cpp

#include <iostream>
using namespace std;
class InToPost
{
   char *infx,*pofx,*s,ch,elem;
   int top,size;
   int i,j;
   char temp;
   public:
	
     void push(char elem);
     char pop();
     void convert();
     int prec(char elem);
     InToPost()
     {
	top = -1;
	cout<<"enter size of the string"<<endl;
	cin>>size;
	infx=new char[size]; 
	pofx=new char[size];
	s=new char[size];
	cout<<"read the infix expression ? ";
	cin>>infx;
     }
};
void InToPost::push(char elem) 
{
 s[top] = elem;
 top++;
}


char InToPost:: pop() 	
{
    top--;
 return (s[top]);
}

/* Write code in convert() to convert infix to postfix expr */
int InToPost::prec(char elem)
{
    if(elem=='(')
    {
        return 1;
    }
    if(elem=='+'||elem=='-')
    {
        return 2;
    }
    if(elem=='*'||elem=='/')
    {
        return 3;
    }
}
void InToPost::convert()
{
    for(i=0;i<size;i++)
    {
        if(infx[i]!='+'&&infx[i]!='-'&&infx[i]!='*'&&infx[i]!='/'&&infx[i]!='(')
        {
            pofx[j++]=infx[i];
        }
        else
        {
            if(top==0)
            {
                push(infx[i]);
            }
            else
            {
                if(infx[i]!='(')
                {
                    if(prec(infx[i])<=prec(s[top-1]))
                    {
                        temp=pop();
                        pofx[j++]=temp;
                        push(infx[i]);
                    }
                    else
                    {
                        push(infx[i]);
                    }
                }
                    else
                    {
                        if(infx[i]=='(')
                        {
                            push(infx[i]);
                        }
                    }
                }
            }
        }
        while(top!=0)
        {
            pofx[j++]=pop();
        }
        cout<<infx<<endl;
        cout<<pofx<<endl;
}
/*
  Write code here to find the precedence of the operators as indicated below
  # = 0, ( = 1, +,- = 2, *,/ = 3 using function int prec(char elem);
*/


int main()
{
 char ch;
 int i = 0, k = 0;
 InToPost ip;
 ip.push('#');
 ip.convert();
 return 0;
}

Python example to read URL and search for the specific word

from urllib2 import urlopen
import re
import subprocess 

pagesrc = urlopen("https://www.vpnbook.com").read()
password = re.search('\<strong\>Password: (\w+)',pagesrc).group(1)
print("Today's Password is: " + password)

C++ STL code to input numbers and sort them

cpp

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

bool cmp(int a, int b)
{
    return (a < b);
}

int main()
{
    int num[1000];
    int N;

    while (cin >> N) {
        for (int i = 0; i < N; i++)
            cin >> num[i];

        sort(num, num + N, cmp);

        for (int i = 0; i < N - 1; i++)
            cout << num[i] << " ";

        cout << num[N - 1] << endl;
    }

    return 0;
}

Java example to print sum of given two number

public class HelloWorld{

     public static void main(String []args){
        int a = 3;
        int b = 4;
        int sum = 0;
        sum = a+b;
        System.out.println(a+"+"+b+"="+sum);
     }
}

C example to define functions for arithmetic operations

c

//Lab 1- Bryan Regn 
// Created 9/6/17 Last updated 9/9/17

//main 
#include <stdio.h>
#include <math.h>


//math.c
//all the following functions require their specific Operator (+,-,*,/,%) and two integers to correctly call the function
//Adds two integers together prints back their sum
void add(int num1, int num2){
   int sum = 0; 
   sum = num1 + num2;
   printf("\n%d",sum);
}
//subtracts two integers prints back the difference
void sub(int num1, int num2){
    int difference = 0;
    difference = num1 - num2;
    printf("\n%d",difference);
}
//multiplies together two integers prints back product
void mul(int num1, int num2){
    int product = 0;
    product = num1*num2;
    printf("\n%d",product);
}
//divides two integers prints back answer
void divide(int num1, int num2){
    int division = 0;
    division = num1/num2;
    printf("\n%d",division);
}
//divides two integers prints back remainder of answer
void modulus(int num1, int num2){
    int result = 0; //remainder was a reserved word and didnt want to use that
    result = num1%num2;
    printf("\n%d",result);
}

//main file used to check user inputs and call different mathmatical operations
int main()
{
    add(10,20);
    sub(10,20);
    mul(10,20);
    divide(10,20);
    modulus(10,20);
    return 0;
}

Java example to declare an array and print using for each loop

public class HelloWorld{

     public static void main(String []args){
         HelloWorld o = new HelloWorld();
         o.printArray(new int[] {3,4,5,6,7});
        System.out.println("Done");
     }
     
     public void printArray(int[] arr) {
         for (int i : arr) {
             System.out.println(i);
         }
     }
}

sfl;kfl;szkdosk fg[sdegdxfg

Program Q2;
const pi =3.1416 ;
var radius, area ,circum :real ;
begin
  write('Input the radius :');
readln(r) ;
area := r^2 *pi ;
circum := 2*r*pi ;
writeln(' Circumference =  ' circum :2:2 ) ;
writeln(' Area =  'area :2:2 ) ;
end.

C++ example to implement virtual inheritance

cpp

#include <iostream>

using namespace std;

class Person {
public:
    Person()
    {
        cout << "Default Constructor of Person() " << endl;
    }
    Person(int x)
    {
        cout << "In Person:" << x << endl;
    }
};
class Faculty : virtual public Person {
public:
    Faculty(int x)
        : Person(x)
    {
        cout << "In Faculty:" << x << endl;
    }
};
class Student : virtual public Person {
public:
    Student(int x)
        : Person(x)
    {
        cout << "In Student:" << x << endl;
    }
};
class TA : public Faculty, public Student {
public:
    TA(int x)
        : Student(x)
        , Faculty(x)
    {
        cout << "In TA:" << x << endl;
    }
};

class A {
    int x;

public:
    /*void setX(int i)
    {
        x=i;
    }*/
    A()
    {
        cout << "Default fo A" << endl;
    }
    A(int i)
    {
        x = i;
    }
    void print()
    {
        cout << "A:" << x << endl;
    }
};
class B : virtual public A {
public:
    B()
        : A(10)
    {
        //setX(60);
    }
};
class C : virtual public A {
public:
    C()
        : A(10)
    {
        // setX(80);
    }
};
class D : public B, public C {
public:
    D()
        : B()
        , C()
        , A(10)
    {
    }
};

int main()
{
    // TA ta1(30);
    D d;
    d.print();
    return 0;
}

Python example to find factorial of a given number

# function to find factorial
def factorial (n):
	if (n <= 1):
		return 1

	i = 1
	product = 1
	while (i <= n):
		product = product * i
		i = i + 1

	return product

# main() function	
print(factorial(0))
print(factorial(1))
print(factorial(2))
print(factorial(8))

Advertisements
Loading...

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