Вы находитесь на странице: 1из 4

#include <iostream>

#include "Element.h"
using namespace std;
void main()
{
int choice;
Element <int> whole;
Element <double> decimal;
Element <char> character;
cout << "1. Whole number" << endl;
cout << "2. Decimal number" << endl;
cout << "3. Character" << endl;
cout << "Enter type: " << endl;
cin >> choice;
if (choice == 1)
whole.input();
else if (choice == 2)
decimal.input();
else
character.input();
}
############################################################
#ifndef ELEMENT_H
#define ELEMENT_H
template <class T>
class Element{
private:
T data[5];
T target;
bool found;
int location;
public:
void input();
void enterTarget();
void sequSearch();
void display();
Element();
~Element();
};
#endif
template <class T>
void Element<T>::input(){
for (int i = 0; i < 5; i++)
{
cout << "Enter data: ";
cin >> data[i];
}
enterTarget();
}
template <class T>
void Element<T>::enterTarget(){
cout << "Enter target: ";

cin >> target;


sequSearch();
}
template <class T>
void Element<T>::sequSearch(){
int index = 0;
while (index < 5)
{
if (target == data[index])
{
found = true;
location = index;
break;
}
else
index++;
}
display();
}
template <class T>
void Element <T>::display(){
if (found == true)
{
cout << "The location of the element is " << location << endl;
}
else
cout << "Element not found! ";
}
template <class T>
Element <T>::Element(){
found = false;
}
template <class T>
Element <T>::~Element(){}
###############################################################
#ifndef ELEMENT_H
#define ELEMENT_H
template <class T>
class Element{
private:
T data[5];
T target;
bool found;
int location;
public:
void input();
void enterTarget();
void sequSearch();
void display();
Element();
~Element();
};

#endif
template <class T>
void Element<T>::input(){
for (int i = 0; i < 5; i++)
{
cout << "Enter data: ";
cin >> data[i];
}
enterTarget();
}
template <class T>
void Element<T>::enterTarget(){
cout << "Enter target: ";
cin >> target;
sequSearch();
}
template <class T>
void Element<T>::sequSearch(){
int index = 0;
while (index < 5)
{
if (target == data[index])
{
found = true;
location = index;
break;
}
else
index++;
}
display();
}
template <class T>
void Element <T>::display(){
if (found == true)
{
cout << "The location of the element is " << location << endl;
}
else
cout << "Element not found! ";
}
template <class T>
Element <T>::Element(){
found = false;
}
template <class T>
Element <T>::~Element(){}
################################################################
1. Whole number
2. Decimal number
3. Character
Enter type:
1

Enter data: 1
Enter data: 2
Enter data: 3
Enter data: 4
Enter data: 5
Enter target: 3
The location of the element is 2
Press any key to continue . . .

Вам также может понравиться