C + + Experiment 5 template

Posted by Nulletz on Mon, 24 Jan 2022 19:35:17 +0100

Swap

describe

The template function Swap is used to exchange different types of data.

And use the following main function to test.

int main()
{
    int a1, a2;
    std::cin >> a1 >> a2;
    Swap(a1, a2);
    std::cout << a1 << "," << a2 << std::endl;

    double b1, b2;
    std::cin >> b1 >> b2;
    Swap(b1, b2);
    std::cout << b1 << "," << b2 << std::endl;

    char c1, c2;
    std::cin >> c1 >> c2;
    Swap(c1, c2);
    std::cout << c1 << "," << c2 << std::endl;

    return 0;
}

Note that this problem only needs to submit the Swap function code, and the header file and main function system have been provided.

input

There are three lines to enter. The first line has two integers, the second line has two floating point numbers, and the third line has two characters

output

The output consists of three lines. Each line corresponds to the result of input processing, and the two output numbers are separated by commas

Input sample 1

2 3
1.2 2.3
a b

Output sample 1

3,2
2.3,1.2
b,a
//Note that this problem only needs to submit the Swap function code, and the header file and main function system have been provided.
#include<iostream>
using namespace std;
template<typename T>
void Swap(T& t1, T& t2)
{
    T temp = t1;
    t1 = t2;
    t2 = temp;
}

SortFunctionTemplate

describe

Use template function to realize the input, sorting and output of array. And use the following main function to test your template

int main()
{
    const int LEN = 5;
    int type;
    while (std::cin >> type)
    {
        switch (type) 
        {
            case 0: 
            {
                int a1[LEN];
                Input<int>(a1, LEN); Sort<int>(a1, LEN); Output<int>(a1, LEN);
                break;
            }
            case 1: 
            {
                char a2[LEN];
                Input(a2, LEN); Sort(a2, LEN); Output(a2, LEN); 
                break; 
            }
            case 2: 
            {
                double a3[LEN];
                Input(a3, LEN); Sort(a3, LEN); Output(a3, LEN);
                break;
            }
        }
    }

    return 0;
}

Note: only Input, Sort and Output function codes are submitted. The rest of the system has been included.

input

The input contains multiple sets of test data. Each group of data consists of two rows. The first row is integer type(0, 1, 2). The second line is the five elements of the corresponding array.

output

For each group of test data, it is sorted and output in one line. Adjacent elements are separated by commas and spaces, and finally a new line is formed.

Input sample 1

0 
3 6 1 4 5
1 
A B C B A

Output sample 1

1, 3, 4, 5, 6
A, A, B, B, C
#include<iostream>
using namespace std;
template<typename T>
void Input(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cin >> arr[i];
    }
}

template<typename T>
void Swap(T &a, T &b)
{
    T t = a;
    a = b;
    b = t;
}
template <typename T>
void Sort(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        int max = i;
        for (int j = i + 1; j < len; j++)
        {
            if (arr[max] >arr[j])
            {
                max = j;
            }
        }
        if (max != i)
        {
            Swap(arr[max], arr[i]);
        }
    }
}
template<class T>
void Output(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i];
        if (i < len - 1)
            cout << ", ";
    }
    cout << endl;
}
//Only Input, Sort and Output function codes are submitted for this problem, otherwise compilation errors will occur (the rest of the system has been included)

Search

describe

Design a template function to find whether the given data exists in a given array. If it exists, output the smallest subscript of the data in the array. If it does not exist, output - 1. The following is the test function

int main()
{
    int n;
    std::cin >> n;
    int *nValues  = new int[n];
    for (int i = 0; i < n; i++) 
    {
        std::cin >> nValues[i];
    }    
    int d;
    std::cin >> d;
    std::cout << Search(nValues, n, d) << std::endl;
    delete[] nValues;

    double f;
    std::cin >> n;
    double *dValues = new double[n];
    for (int i = 0; i < n; i++) 
    {
        std::cin >> dValues[i];
    }
    std::cin >> f;
    std::cout << Search(dValues, n, f) << std::endl;
    delete[] dValues;

    std::cin >> n;
    char *cValues = new char[n];
    for (int i = 0; i < n; i++)
    {
        std::cin >> cValues[i];
    }
    char c;
    std::cin >> c;
    std::cout << Search(cValues, n, c) << std::endl;
    delete[] cValues;

    return 0;
}

input

Enter a total of three groups of data, each of which accounts for three rows.

The first row of the first group contains n1 integers, the second row contains n1 integers, and the third row contains n integers to be searched

The first line of the second group is integer n2, the second line is n2 floating-point numbers, and the third line is floating-point number d to be found

The first line of the third group is an integer n3, the second line is n3 characters, and the third line is the character c to be found

output

For each group of inputs, if the search data exists, the minimum subscript (the subscript starts from 0) will be output, otherwise - 1 will be output.

Input sample 1

7
1 1 2 5 8 10 13
8
5
-1.0 1.1 1.2 1000.10101 8.9
3.5
4
B J F U
j

Output sample 1

4
-1
-1

Tips

Using template functions

Using template functions

template <class T>
int Search(const T * array, int arrayLen, const T & value)
#include<iostream>
using namespace std;
template <class T>
int Search(const T* array, int arrayLen, const T& value)
{
    
        int index = -1;
        for (int i = 0; i < arrayLen; i++)
        {
            if (value == array[i])

                index = i;
        }
        return index;
}


int main()
{
    int n;
    std::cin >> n;
    int* nValues = new int[n];
    for (int i = 0; i < n; i++)
    {
        std::cin >> nValues[i];
    }
    int d;
    std::cin >> d;
    std::cout << Search(nValues, n, d) << std::endl;
    delete[] nValues;

    double f;
    std::cin >> n;
    double* dValues = new double[n];
    for (int i = 0; i < n; i++)
    {
        std::cin >> dValues[i];
    }
    std::cin >> f;
    std::cout << Search(dValues, n, f) << std::endl;
    delete[] dValues;

    std::cin >> n;
    char* cValues = new char[n];
    for (int i = 0; i < n; i++)
    {
        std::cin >> cValues[i];
    }
    char c;
    std::cin >> c;
    std::cout << Search(cValues, n, c) << std::endl;
    delete[] cValues;

    return 0;
}

TVector3

describe

Construct a template class (Vector), and the data members are as follows:

template<typename T>
class Vector
{
private:
    T x, y, z;
};

Complete the Vector and test it with the following function

int main()
{
    double a, b, c;
    std::cin >> a >> b >> c;
    Vector<double> v1(a, b, c), v2(v1), v3, v4;
    double d;
    std::cin >> d;
    v4 = d * v1 + v2;

    std::cout << v4 <<std::endl;

    Vector<double>  v;
    std::cin >> v;

    int flag = (v4 == v);
    std::cout << flag << std::endl; 

    return 0;
}

input

See Example

output

See Example

Input sample 1

3 4 5
2.2
9.6 12.8 16

Output sample 1

9.6 12.8 16
1
#include<iostream>
using namespace std;
template<typename T>
class Vector
{
private:
    T X, Y, Y;
public:
    Vector(T a=0.0,T b=0.0,T c=0.0):X(a),Y(b),Y(c){}
    Vector(const Vector& v)
    {
        this->X = v.X;
        this->Y = v.Y;
        this->Y = v.Y;

    }
    friend Vector operator*(const double X, const Vector &v)
    {
        Vector c;
        c.X = X * v.X;
        c.Y = X * v.Y;
        c.Y = X * v.Y;
        return c;
    }
    friend Vector operator+(const Vector& v1,const Vector &v2)
    {
        Vector c;
        c.X = v1.X + v2.X;
        c.Y = v1.Y + v2.Y;
        c.Y = v1.Y + v2.Y;
        return c;
    }
    friend ostream & operator<<(ostream& os,const Vector &v)
    {
        os << v.X <<" " <<v.Y<<' '<< v.Y;
        return os;
    }
    friend istream & operator>>(istream& is,Vector& v)
    {
        double a, b, c;
        std::cin >> a >> b >> c;
        v.X = a;
        v.Y = b;
        v.Y = c;
        return is;
    }
    friend int operator==(const Vector& v1,const Vector& v2)
    {
        if ((fabs(v1.X - v2.X) < 1e-6) && (fabs(v1.Y - v2.Y) < 1e-6) && (fabs(v1.Y - v2.Y) < 1e-6))
            return 1;
        else return 0;
    }
};



int main()
{
    double a, b, c;
    std::cin >> a >> b >> c;
    Vector<double> v1(a, b, c), v2(v1), v3, v4;
    double d;
    std::cin >> d;
    v4 = d * v1 + v2;

    std::cout << v4 << std::endl;

    Vector<double>  v;
    std::cin >> v;

    int flag = (v4 == v);
    std::cout << flag << std::endl;

    return 0;
}

StackClassTemplate

describe

The first mock exam is to implement a Stack class template and test the template.

template<class T, int SIZE = 20>
class Stack
{
private: 
    T    array[SIZE];        //Array, which is used to store the elements of the stack
    int top;                //Stack top position (array subscript)
public:
    Stack();                //Constructor, initializing stack
    void Push(const T & );  //Element stack
    T Pop();                //Stack top element out of stack
    void Clear();           //Empty stack
    const T & Top() const;  //Access stack top element
    bool Empty() const;     //Test whether the stack is empty
    bool Full() const;     //Test whether the stack is full
    int Size();            //Returns the number of elements in the current stack
};

Test function:

int main()
{
    Stack<int, 10> intStack;

    int n;
    cin >> n; //n<=10
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        intStack.Push(temp);
    }

    for (int i = 0; i < n; i++)
    {
        cout << intStack.Top() << " ";
        intStack.Pop();
    }
    cout<<endl;

    if(intStack.Empty())
        cout<<"Now, intStack is empty."<<endl;

    Stack<string,5> stringStack;
    stringStack.Push("One");
    stringStack.Push("Two");
    stringStack.Push("Three");
    stringStack.Push("Four");
    stringStack.Push("Five");
    cout<<"There are "<<stringStack.Size()<<" elements in stringStack."<<endl;
    stringStack.Clear();
    if(stringStack.Empty()) 
        cout<<"Now, there are no elements in stringStack"<<endl;

    return 0;
}

input

Reference example

output

Reference example

Input sample 1

3
1
2
3

Output sample 1

3 2 1 
Now, intStack is empty.
There are 5 elements in stringStack.
Now, there are no elements in stringStack
#define _CTR_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<stack>

using namespace std;
template<class T, int SIZE = 20>
class Stack
{
private:
    T array[SIZE];
    int top;
public:
    Stack();
    void Push(const T &);
    T Pop();
    void Clear();
    const T & Top() const;
    bool Empty() const;
    bool Full() const;
    int Size();
};


void Stack<T, SIZE>::Push(const T &x)
{
    array[top] = x;
    top++;
}
template<class T, int SIZE>
T Stack<T, SIZE>::Pop()
{
    top--;
    return array[top + 1];
}
template<class T, int SIZE>
Stack<T, SIZE>::Stack()
{
    top = 0;
}
template<class T, int SIZE>
template<class T, int SIZE>
void Stack<T, SIZE>::Clear()
{
    top = 0;
}
template<class T, int SIZE>
const T& Stack<T, SIZE>::Top()const
{
    return array[top - 1];
}
template<class T, int SIZE>
bool Stack<T, SIZE>::Empty()const
{
    return top == 0;
}
template<class T, int SIZE>
bool Stack<T, SIZE>::Full()const
{
    return top == SIZE;
}
template <class T, int SIZE>
int Stack<T, SIZE>::Size()
{
    return top;
}

int main()
{
    Stack<int, 10> intStack;

    int n;
    cin >> n; //n<=10
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        intStack.Push(temp);
    }

    for (int i = 0; i < n; i++)
    {
        cout << intStack.Top() << " ";
        intStack.Pop();
    }
    cout << endl;

    if (intStack.Empty())
        cout << "Now, intStack is empty." << endl;

    Stack<string, 5> stringStack;
    stringStack.Push("One");
    stringStack.Push("Two");
    stringStack.Push("Three");
    stringStack.Push("Four");
    stringStack.Push("Five");
    cout << "There are " << stringStack.Size() << " elements in stringStack." << endl;
    stringStack.Clear();
    if (stringStack.Empty())
        cout << "Now, there are no elements in stringStack" << endl;
    return 0;
}