Thursday, 16 March 2017

PPS5

College application-

Question1-A student may apply for an arts college or an engineering college after his school. Admission to arts college or engineering college, is based on the marks obtained in the six subjects : English, Second language, Maths, Physics, Chemistry and Computer Science.  Both the applications have the following details : application number, name, age, marks in all six subjects and cut-off for the eligibility. For Arts college, cut-off is the   average of  marks in all the subjects and whereas for engineering colleges cut-off is the average of marks in maths, physics, chemistry plus the marks scored in the entrance exam.  Given all the required details, Design an OOP model to compute the cut-off marks and implement it using C++.
Note: Syntax to print 'x' decimal places of variable 'a'
include <iomanip>
use
cout<<fixed<<setprecision(x)<<a;

Input Format
Type of application (0 for arts student and 1 for engineering student)
Name of student
Age          
Marks scored in English
Marks scored in Second Language
Marks scored in Maths
Marks scored in Physics
Marks scored in Chemistry
Marks scored in Computer Science
Marks scored in entrance exam (only for engineering student)
Output Format
Name of student

Cut-off marks



c++ code-

void college_Appln::get()
{
cin>>appln_No>>name>>age;
for(int i=0;i<6;i++)
{cin>>marks[i];}
}
void college_Appln::print()
{
cout<<name;
}
void arts_Appln::calc_Cutoff()
{
for(int i=0;i<6;i++)
cutoff=(cutoff+marks[i]);
cutoff=cutoff/6;
}
void arts_Appln::print()
{
cout<<name<<"\n";
cout<<fixed<<setprecision(2)<<cutoff;
}
void engg_Appln::get()
{
cin>>appln_No>>name>>age;
for(int i=0;i<6;i++)
{cin>>marks[i];}
cin>>entrance;
}
void engg_Appln::calc_Cutoff()
{
for(int i=2;i<=4;i++)
{
cutoff=(cutoff+marks[i]);
}
cutoff=cutoff/3;
cutoff=cutoff+entrance;
}
void engg_Appln::print()
{
cout<<name<<"\n";
cout<<fixed<<setprecision(2)<<cutoff;
}


Cost of pizza


Description Pizza is a delicious circular food item that is a favorite for many people. Given the radius of the pizza, ingredients required for the preparation of the pizza, per square cm (cm2) area of the pizza and cost of its ingredients per 100 grams, design an OOP model and write a C++ program to calculate the cost of the pizza. Add Rs 50 for veg pizza and Rs 100 for chicken pizza. Use 3.14 for pi. Your program should  get all the ingredients available in the kitchen with their cost per 100 grams, as an input.  Assume that all the ingredients required for the preparation of the pizza is available in the kitchen.
Note: Syntax to print 'x' decimal places of variable 'a'
include <iomanip>
use
cout<<fixed<<setprecision(x)<<a;
Input Format
Give 0 for veg pizza and 1 for chicken pizza
Radius of pizza
Number of ingredients required for the preparation of the pizza
Name of ingredient1
Quantity of ingredient1 required (in grams)
Name of ingredient2
Quantity of ingredient2 required
....
Name of ingredient-n
Quantity of ingredient-n required

Number of ingredients available in the kitchen.

Name of ingredient-1 available in the kitchen
Cost of 100 gm of ingredient1 available in kitchen
Name of ingredient-2 in kitchen
Cost of 100 gm of ingredient2 in kitchen
....
Name of ingredient-n in kitchen
Cost of 100 gm of ingredient-n in kitchen

Output Format
Cost of pizza

Precode
#include<iostream>

using namespace std;
#include<string.h>
#include<iomanip>
class circle
{
protected:
float radius;
public:
void get_C();
void print_C();
float area();
};
struct ingre_Cost
{
char name[30];
float price;
};
class kitchen
{
protected:
int num1;
//ingredients in the kitchen and their cost
ingre_Cost ing_Cost[20];
public:
void get_K();
void print_K();
//Given name of ingredients
//return cost of 100gm of it
float get_Cost(char*);
};
struct ingre_Qty
{
char name[30];
float qty;
};
class cookeditem
{
protected:
//number of ingredients
int num;
// names of ingredients and their quantity in
// Pizza
ingre_Qty ing_Qty[20];
public:
void get_CI();
void print_CI();
};

//Create a class pizza that inherits circle and cookeditem
//Create another class veg_Pizza inherited that inherits pizza
//Create another class chik_Pizza inherited that inherits pizza

type this-

void circle :: get_C()
{
cin>>radius;
}
float circle :: area()
{
return(3.14*radius*radius);
}
void kitchen :: get_K()
{
cin>>num1;
for(int i=0;i < num1;i++)  
cin>>ing_Cost[i].name>>ing_Cost[i].price;
}
float kitchen :: get_Cost(char *n)
{
for(int i=0;i < num1;i++)  
if(strcmp(n,ing_Cost[i].name)==0)  
return(ing_Cost[i].price);  
}  
void cookeditem :: get_CI()  
{  
cin>>num;
for(int i=0;i < num;i++)  
cin>>ing_Qty[i].name>>ing_Qty[i].qty;
}
class pizza : public circle,cookeditem
{
protected :
float p_cost;
void get()
{
get_C();
get_CI();
}
void cost(kitchen k)
{
p_cost=0;
for(int i=0;i < num;i++)
p_cost+=ing_Qty[i].qty*k.get_Cost(ing_Qty[i].name)*0.01*area();
}
};
class veg_Pizza : public pizza
{
public :
void get_P()
{
get();
}
void compute_Cost(kitchen k)
{
cost(k);
}
void print_P()
{
cout<<fixed<<setprecision(2)<<p_cost+50;
}
};
class chik_Pizza : public pizza
{
public :
void get_P()
{
get();
}
void compute_Cost(kitchen k)
{
cost(k);
}
void print_P()
{
cout<<fixed<<setprecision(2)<<p_cost+100;
}
};




Postcode
int main()
{
int ch;
cin>>ch;
if (ch==0)
{
//Create an oject for veg pizza
veg_Pizza p;
//get radius of circle(pizza)
// get ingredients and quantity required for 1 square centimeter
p.get_P();
//get items in kitchen and their cost
kitchen k;
k.get_K();
//compute cost
p.compute_Cost(k);
p.print_P();
}
else
{
if (ch==1)
{
chik_Pizza c;
c.get_P();
kitchen k1;
k1.get_K();
c.compute_Cost(k1);
c.print_P();
}
}
}




customer discount


A retail store is maintaining their customer details such as customer id and name, mobile number and address. They have decided to introduce a preferred customer plan for the existing customer. Any customer is given the status of preferred customer if they have made purchases more than 10 times. Preferred customers earn discounts based on their cumulative purchase amount as follows:
Design an OOP model and implement it using C++ program. Refer the declaration of classes and application program to do the design. The program must get the details, compute bill amount and print details both for ordinary and preferred customers.
For example, if a preferred customer makes three purchases of amounts 3000, 7000, 8000 then the amount to be paid for each bill is 3000, 6860 and 7760 and the total amount to be paid is 17620
Input Format
Type of customer (0 for ordinary customer and 1 for preferred customer)
Name
Mobile number
Address
Customer id
Number of bills
Total Cost of products in bill1
....
Total Cost of products in bill-n
Output Format
            Total amount to be paid by the customer for 'n' bills


Type this-

#include < iostream >
using namespace std;
class details
{
    protected :
    char name[20],mob[20],add[20],id[10];
    int num,bills[20];
    void get_details()
    {
        cin>>name>>mob>>add>>id>>num;
        for(int i=0;i < num;i++)   
        cin>>bills[i];
    }
};
class customer : public details
{
    public :
    void get()
    {
        get_details();
    }
    float calc_Total()
    {
        float total=0;
        for(int i=0;i < num;i++)
        total+=bills[i];
        return(total);
    }
};
class preferred_Customer : public details
{
    public :
    void get()
    {
        get_details();
    }
    float calc_Total()
    {
        float total=0,spent=0;
        for(int i=0;i < num;i++)   
        {  
            spent+=bills[i];   
            if(spent > =20000)
            total+=bills[i]*0.96;
            else if(spent > =15000)
            total+=bills[i]*0.97;
            else if(spent > =10000)
            total+=bills[i]*0.98;
            else if(spent > =5000)
            total+=bills[i]*0.99;
            else
            total+=bills[i];
        }
        return(total);
    }
};

Postcode


int main()
{
int ch;
//get choice of customer
cin>>ch;
if(ch==0)
{
customer c1;
//get the details of customer
c1.get();
//calculate total amount to be paid
//develop a function in customer class that will calculate 
// the total amount and return it
cout<<c1.calc_Total();
}
else
{
//preferred customer
preferred_Customer pc;
//get details
pc.get();
//function should calculate and return total amount
cout<<pc.calc_Total();
}
}







polygon in 2d space


Description Design a class polygon to represent a polygon in a two dimensional space. Provide member functions to get the details of the polygon and compute area. Area of a polygon with 'n' vertices is given by the following formula:

Here the vertices are numbered from 1 to n either clockwise or anticlockwise. The sign of the value changes when numbering is done in the other way. So absolute value is considered.
Derive two classes: triangle and quadrilateral, from the class polygon. Overload the operator [] to get the 'i-th' point of the polygon if 'i' is less than or equal to the number of vertices 'n' and raise an user defined exception outofrange when 'i' is greater than 'n'. Catch the exception in main and print 'outofrange'.

Input Format
Next six entries are Vertices of a triangle
value of X- coordinate of point1
value of Y- coordinate of point1
....
value of X- coordinate of point6
value of Y- coordinate of point6
Next eight entries are Vertices of a quadrilateral
value of X- coordinate of point1
value of Y- coordinate of point1
....
value of X- coordinate of point8
value of Y- coordinate of point8
index of the vertex  of the polygon, whose coordinate has to be printed. 
Output Format
Area of triangle
Area of quadrilateral
Value of X- coordinate and Y- coordinate of point-n separated by tab or print Out of range if index is not in range

#include<iostream>
using namespace std;
#include<math.h>
#include<iomanip>
#include<exception>
//Declaration of classes
struct point
{
double x;
double y;
point();
void get();
friend ostream& operator<<(ostream&,point);
};
class outofrange:public exception
{
public:
void what();

};
class polygon
{
protected:
int num_Of_Ver;
point* vertices;
public:
//initialize num_Of_Ver to 'n'
//allocate memory to store points of 'n' vertices
polygon(int n);
~polygon();
//get function cannot be defined here instead define in the derived class
void get();
//we return reference of the point so that assignment can be made
point& operator[](int idx);
double area();
};
class triangle : public polygon
{
public:
triangle():polygon(3){}
};
class quadrilateral : public polygon
{
public:
quadrilateral():polygon(4){}
};


type this-


point :: point(){}
void point :: get()
{
    cin>>x>>y;
}
ostream& operator << (ostream& output,point poi)
{
    output<<poi.x<<"\t"<<poi.y;
    return(output);
}
void outofrange :: what()
{
    cout<<"Out of range";
}
polygon :: polygon(int n)
{
    num_Of_Ver=n;
    vertices=new point[n];
}
void polygon :: get()
{
    for(int i=0;i < num_Of_Ver;i++)
    vertices[i].get();
}
point& polygon :: operator [](int idx)
{
    if(idx < num_Of_Ver)
    return(vertices[idx]);
    else
    {
        outofrange o;
        throw(o);
    }
}
double polygon :: area()
{
    double a=0;
    for(int i=0;i < num_Of_Ver-1;i++)
    a+=vertices[i].x*vertices[i+1].y-vertices[i+1].x*vertices[i].y;
    a+=vertices[num_Of_Ver-1].x*vertices[0].y-vertices[num_Of_Ver-1].y*vertices[0].x;
    if(a/2<0)
    return(-a/2);
    return(a/2);
}
polygon :: ~polygon(){}



Postcode already typed content

int main()
{
triangle t;
int index;

t.get();
cout<<fixed<<setprecision(2)<<t.area()<<endl;
quadrilateral r;
r.get();
cout<<fixed<<setprecision(2)<<r.area()<<endl;
cin>>index;
try
{
cout<<r[index];
}catch(outofrange o)
{
o.what();
}
}





black coin in a board game


Colored coin game is a 8X8 board game which has many colored coins. Each coin has a weight and power. Power of a coin is defined by the moves that it can make. In a move,  a black coin can move one step vertically upwards.  A red coin can move one step either horizontally, vertically or diagonally. Given the current position of a black coin and list of movements made by it, print all possible next positions of the coin. If the total number of moves made by a black coin is greater than 6, then that coin should be treated as a red coin and the subsequent moves will be as that of the red coin. At any point of time, the coin cannot move outside the board. The rows and columns of the board are numbered as 1 to 8. Print the horizontal movement of coin in an  increasing order of columns and print the vertical movement of coin in increasing order of rows. To print the diagonal movement of the coin refer the sample board config given below. If current position of your coin is 4,4 then print P1, P2 ... P8 in order.
Assume that the current row position and column position are between 2 and 7.

Input Format
Weight of black coin
Current row position of coin
Current column position of coin
Number of moves made by black coin
Output Format
Weight of black coin
List of possible next position
One position in a line with row and column separated by a comma

Precode
#include<iostream>



using namespace std;
class coin
{
protected:
int weight;
int curr_X;
int curr_Y;
public:
void get();
void print();
//return the current row position
int get_Curr_X();
//return the current col position
int get_Curr_Y();
//return weight of the coin
int get_Weight();
//pure virtual function that the derived classes must implement
virtual void move()=0;
};
//Define two more classes black_Coin and red_Coin

type this


void coin :: get()
{
cin>>weight>>curr_Y>>curr_X;
}
void coin :: print()
{
cout<<weight<<"\n"; 
}  
int coin :: get_Curr_X() 

return(curr_X); 
}  
int coin :: get_Curr_Y() 

return(curr_Y); 
}  
int coin :: get_Weight() 

return(weight); 

void coin :: move(){} 
class black_Coin : public coin 

public : 
int moves; 
void get() 

coin :: get(); 
cin>>moves;
}
bool gained_Power()
{
if(moves > =5)
return(true);
       return(false);
}
void move()
{
cout<<curr_Y<<","<<curr_X+1;
}
};
class red_Coin : public coin
{
public :
void set_Curr_Pos_Wt(black_Coin b)
{
weight=b.get_Weight();
curr_X=b.get_Curr_X();
curr_Y=b.get_Curr_Y();
}
void move()
{
cout<<curr_Y-1<<","<<curr_X<<"\n";
cout<<curr_Y+1<<","<<curr_X<<"\n";
cout<<curr_Y<<","<<curr_X-1<<"\n";
cout<<curr_Y<<","<<curr_X+1<<"\n";
cout<<curr_Y-1<<","<<curr_X-1<<"\n";
cout<<curr_Y+1<<","<<curr_X-1<<"\n";
cout<<curr_Y-1<<","<<curr_X+1<<"\n";
cout<<curr_Y+1<<","<<curr_X+1<<"\n";
}
};

Postcode


int main()
{
coin *c;
black_Coin b;
b.get();
red_Coin r;
       //Function to check if the black coin has gained power
       // It gains power when the number of movements made is greater than or equal to 5
if(b.gained_Power())
{
//Make the black coin as red coin
        //set weight and current position of black coin to a red coin
r.set_Curr_Pos_Wt(b);
c = &r;
         //Print only weight of the coiin
c->print();
        //Print all possible moves
c->move();
}
else
{
c = &b;
       //Print only weight of the coiin
      c->print();
               //Print all possible moves
      c->move();
}

}

Saturday, 11 March 2017

PPS4

Question 1


The area is the two-dimensional amount of space that an object occupies. Area is measured along the surface of an object and has dimensions of length squared; for example, square feet of material, or centimetres squared.
The area of a rectangle is equal to the height h times the base b; A = h * b
The equation for the area of a trapezoid is one half the sum of the top t and bottom b times the height h; A = h * [ t + b ] / 2
The area of a circle is A = pi * r2, where pi = 3.14 and r = radius.
Develop a program in C++ using function overloading for computing the area of a rectangle, a trapezoid and a circle by a common function name ComputeArea() with different signature. Assume pi = 3.14. Print only two decimal places for all areas.
Note:
To print only two decimal places of a variable ‘a’, do the following:
#include
cout<<fixed<<setprecision(2)<<a;

Input Format:
Read the base and height of a rectangle.
Read the top, bottom and height of a trapezoid.
Read the radius of a circle.

Output Format:
Display the area of a rectangle, trapezoid and circle each in one line
Boundary Conditions:
You can give any valid integer or float values for inputs.

c++ code-

#include<iostream>
#include<iomanip>
using namespace std;
int area(int l,int b)
{int a;
 (a=l*b)
return(a);
}
int area(int t, int b, int h)
{
int a;
a=h*(t+b)/2;
return(a);
}
float area(double r)
{
double a;
a=3.14*r*r;
return(a);
}



int main()
{
float len,bre,top,bottom,height,radius;
double a;
cin>>len>>bre;
a = area(len,bre);
cout<<fixed<<setprecision(2)<<a<<endl; cin>>top>>bottom>>height;
a = area(top,bottom,height);
cout<<fixed<<setprecision(2)<<a<<endl; cin>>radius;
a = area(radius);
cout<<fixed<<setprecision(2)<<a<<endl;
}


Question 2


Generate boarding pass for the passengers of a ship which starts from Chennai  to Andaman. The boarding pass must be generated automatically with a pass number that begins with “CA” and followed by a number that is automatically incremented from value ‘x’, details like passenger name, age, mobile number, address, date of journey and fare. There is a seasonal discount based on the age of the passengers. Write a non member function called discount which calculates the discount in the fare for the passenger with the following discounts. For the  age group `between 12 and 58, both inclusive’  there is 20% discount in the fare,  for the age group ‘above 58’, there is 40% discount and for the children (age under 12), 50% discount. Write a C++ program to generate pass for ‘n’ users.

Input Format:
Passenger name

Value of ‘x’

Age
Address
date_of_Journey
mobile number
Original Fare

Output Format:

passenger name

Boarding pass number
age
date_of_Journey
mobile number
Total fare after discount based on age

Boundary Conditions:
>=1

c++ code-

#include<iostream>
using namespace std;
int main()
{
int x,a,f;
long long int ph;
char n[20],ad[20],db[20];
cin>>n>>x>>a>>ad>>db>>ph>>f;
cout<<n<<"\nCA"<<x<<"\n"<<a<<"\n"<<db<<"\n"<<ph<<"\n";
if(12<a && a<58)
{cout<<(f-(0.2*f));
}
if(a<12)
{cout<<(f-(0.5*f));
}
if(a.58)
{cout<<(f-f(0.4*f));
}
}


Question3-vector operations

Design a class vector to perform the operations like retrieving value for i-th component from a vector, add two vectors and subtract a vector from another if they are of same dimension. A vector of n-dimension is represented by an n-tuple (a sequence of n numbers). Addition of two vectors of same dimension,  is got by adding   the corresponding components of the two vectors. Similarly, subtraction of the two vectors, v1-v2,  is got by the subtracting the respective components of v2 from the corresponding components of v1. Overload subscript ([]) operator for retrieving the i-th element from a vector, '+' and '-' for addition and subtraction, << and >> for I/O operations. If the vectors are of different dimension, throw an exception stating "Vectors of different dimension cannot be added".

Input format:
Dimension of first vector, 'n1'
First element of first vector
Second element of first vector
...
n1-th element of first vector
Dimension of second vector, 'n2'
First element of second vector
Second element of second vector
...
n2-th element of first vector
value of i
value of j

Output Format
i-th element of vector1
j-th element of vector2
sum of vectors
vector1-vector2

c++ code-
#include<iostream>
using namespace std;
class vector
{
    int num;
    int ele[20];
    public:
    friend istream& operator>>(istream&,vector&);
    friend ostream& operator<<(ostream&,vector&);
    vector operator+(vector&);
    vector operator-(vector&);
    int operator[](int);
};


int vector :: operator [] (int n)
{
    return(ele[n-1]);
}
istream& operator >> (istream& input,vector& vec)
{
    input>>vec.num;
    for(int i=0;i < vec.num;i++) 
    input>>vec.ele[i];
    return(input);
}
ostream& operator << (ostream& output,vector& vec)
{
    for(int i=0;i < vec.num;i++)
    output<<vec.ele[i]<<"\n";
    return(output);
}
vector vector :: operator + (vector& vec)
{
    vector v=vec;
    for(int i=0;i < num;i++)
    v.ele[i]+=ele[i];
    return(v);
}
vector vector :: operator - (vector& vec)
{
    vector v;
    for(int i=0;i < num;i++)
    v.ele[i]=ele[i]-vec.ele[i];
    return(v);
}


int main()
{
    vector v1,v2,v3;
    int i,j;
    cin>>v1;
    cin>>v2;
    cin>>i;
    cin>>j;
    cout<<v1[i]<<endl;
    cout<<v2[j]<<endl;
    v3 = v1+v2;
    cout<<v3;
    v3 = v1 - v2;
    cout<<v3;
}

Question 4-Sort a given set of points


Design a OOP model to represent a point in a two dimensional space and overload the operators >>,<<, > and ==. Given ‘n’ points, design an algorithm and write a C++ code to sort them in descending order. While sorting, a point is said to be greater than the other based on their x-coordinate value. If value of x-coordinate is same for both the points then make a decision based on their value of y-coordinate.
Input Format
Number of points ‘n’
value of X- coordinate of point1
value of Y- coordinate of point1
….
value of X- coordinate of point-n
value of Y- coordinate of point-n
Output Format
Print ‘n’ points in sorted order
value of X- coordinate and Y- coordinate of point1 separated by tab
value of X- coordinate and Y- coordinate of point2 separated by tab
….
value of X- coordinate and Y- coordinate of point-n separated by tab
Solution

#include<iostream>
using namespace std;
class point
{
int x,y;
public:
friend istream& operator>>(istream&,point&);
friend ostream& operator<<(ostream&,point&); bool operator == (point&); bool operator > (point&);
};

void sort_Points(point*,int);


------------------------------------------------------------------

istream& operator >> (istream& input,point& poi)
{
    input>>poi.x>>poi.y;
    return(input);
}
ostream& operator << (ostream& output,point& poi)
{
    output<<poi.x<<"\t"<<poi.y<<"\n"; 
    return(output);  
bool point :: operator == (point& poi)  
{  
    if(poi.x==x && poi.y > y)
    {
        int temp=poi.y;
        poi.y=y;
        y=temp;
        return(true);
    }
    return(false);
}
bool point :: operator > (point& poi)
{
    if(poi.x > x)
    {
        point temp=poi;
        poi.x=x;
        poi.y=y;
        x=temp.x;
        y=temp.y;
        return(true);
    }
    return(false);
}
void sort_Points(point* poi,int n)
{
    for(int i=0;i < n;i++)
    for(int j=0;j < n;j++)  
    {  
        if(poi[j] > poi[i])
        continue;
        if(poi[j]==poi[i]);
    }
}

---------------------------------------------------------


main()
{
point p[20];
int i,n;
cin>>n;
for(i=0;i<n;i++) cin>>p[i];
sort_Points(p,n);
for(i=0;i<n;i++)
cout<<p[i];
}




Question 5 string operations


Design a class stringpos to perform the operations like retrieving the letter  in the i-th position (i-th symbol) of  a string, rotation of a string,  oddfirst  of a string, evenfirst of a  string, spin of two strings,  oddevenswap of two strings, evenoddswap of two strings.  Here the terms : rotation, oddfirst, evenfirst, are the operations performed on one string. The terms : spin, oddevenswap, evenoddswap are the operations performed on two strings.  With the string S and integer n

Oddevenswap(S1,S2) produces a string such that the  letters occurring in the odd positions  of S1 are  replaced by the letters occurring in the corresponding even positions of S2 and the letters occurring in the even position of S1 are left unchanged. In the above example, after a certain stage,  If there are no even-position letters in S2 to replace the odd-position letters in S1, those odd-position letters are left unreplaced.  For Example, oddevenswap(abcd, efgh)= fbhd, oddevenswap(ab,efgh)=fb, oddevenswap(abcd,fg)= gbcd.  Here also, oddevenswap(S1, S2) need not be same as that of oddevenswap(S2,S1).

Similarly, evenoddwap(S1,S2) produces a string such that the  letters occurring in the even  positions  of S1 are  replaced by the letters occurring in the corresponding odd  positions of S2 and the letters occurring in the odd  position of S1 are left unchanged.  For Example, evenoddswap(abcd, efgh)= aecg, evenoddswap(ab,efgh)=ae, evenoddswap(abcd,fg)= afcd  Here also, evenoddswap(S1, S2) need not be same as that of evenoddswap(S2,S1).

Overload shift left operator for rotation, prefix ++ for oddfirst, postfix ++ for evenfirst, ‘*’ for spin,’+’ operator for Oddevenswap, ‘-‘ operator for Evenoddswap, << and >> for I/O operations.
Input format:First line contains the first string, S1
Next line contains the second string, S2
Rotation index n
Output Format
rotation(S1,n)
oddfirst(S2)

evenfirst(S1)
Spin(S1,S2)
Oddevenswap(S1,S2)
Evenoddswap(S1,S2)
Solution
#include
#include
using namespace std;
class stringops
{
char str[50];
public:
friend istream& operator>>(istream&,stringops&);
friend ostream& operator<<(ostream&,stringops&); //Return a new string with letters rotated stringops operator>>(int);
//Return a new string with odd letters first
stringops operator++();
//Return a new string with even letters first
stringops operator++(int);
//Return a new string after Oddevenswap
stringops operator + (stringops&);
//Return a new string after Evenoddswap
stringops operator – (stringops&);
//Return a new string after spin
stringops operator * (stringops&);
};

------------------------------------------------------------
istream& operator >> (istream& input,stringops& string)
{
    input>>string.str;
    return(input);
}
ostream& operator << (ostream& output,stringops& string)
{
    output<<string.str;  
    return(output);  
}  
stringops stringops :: operator >> (int num)
{
    num--;
    stringops new_string;
    for(int i=0;i < strlen(str)+1;i++)  
    {  
        if(num > -1)
        new_string.str[i]=str[num--];
        else
        new_string.str[i]=str[i];
    }
    return(new_string);
}
stringops stringops :: operator ++ ()
{
    stringops new_string;
    int len=0;
    for(int i=0;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    for(int i=1;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    new_string.str[len]='\0';
    return(new_string);
}
stringops stringops :: operator ++(int)
{
    stringops new_string;
    int len=0;
    for(int i=1;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    for(int i=0;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    new_string.str[len]='\0';
    return(new_string);
}
stringops stringops :: operator + (stringops& string)
{
    stringops new_string;
    for(int i=0;i < strlen(str);i++)
    {
        if((i+1)%2==1 and i+1 < strlen(string.str))
        new_string.str[i]=string.str[i+1];
        else
        new_string.str[i]=str[i];
    }
    new_string.str[strlen(str)]='\0';
    return(new_string);
}
stringops stringops :: operator - (stringops& string)
{
    stringops new_string;
    for(int i=0;i < strlen(str);i++)
    {
        if((i+1)%2==0 && i-1 < strlen(string.str))
        new_string.str[i]=string.str[i-1];
        else
        new_string.str[i]=str[i];
    }
    new_string.str[strlen(new_string.str)]='\0';
    return(new_string);
}
stringops stringops :: operator * (stringops& string)
{
    stringops new_string;
    int s1=0,s2=0,s3=0;
    while(s1 < strlen(str) && s2 < strlen(string.str))
    {
        new_string.str[s3++]=str[s1++];
        new_string.str[s3++]=string.str[s2++];
    }
    while(s1 < strlen(str))
    new_string.str[s3++]=str[s1++];
    while(s2 < strlen(string.str))
    new_string.str[s3++]=string.str[s2++];
    new_string.str[s3]='\0';
    return(new_string);
}

int main()
{
stringops s1,s2,s3;
int n;
cin>>s1>>s2;
cin>>n;
//rotate
s3= s1>>n;
cout<<s3<<endl;
//oddfirst
s3 = ++s2;
cout<<s3<<endl;
//evenfirst
s3 = s1++;
cout<<s3<<endl;
//spin
s3 = s1*s2;
cout<<s3<<endl;
//Oddeven swap
s3 = s1+s2;
cout<<s3<<endl;
//Evenodd swap
s3 = s1-s2;
cout<<s3<<endl;
}

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...