1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Person{
string name;
string number;
};
void namesort(Person data[], int antal);
void telesort(Person data[], int antal);
int _tmain(int argc, _TCHAR* argv[])
{
int ans;
const int NUMBER = 3;
Person man[NUMBER];
cout << "Write the full name of 3 persons: \n";
for (int i = 0; i < NUMBER;){
cout << "Person " << i+1 << ": ";
getline(cin, man[i].name);
cin.ignore(1000, '\n');
cout << "Telephone: ";
getline(cin, man[i].number);
cout << "Put new person? 1 = Yes , 2 = Present Current Persons";
cin >> ans;
if(ans == 1){
i++;
}
for (int i = 0; i < NUMBER; i++){
int pos = man[i].name.find(' ', 0);
if (pos != -1){
string fName = man[i].name.substr(0, pos);
int start = pos+1;
int langd = man[i].name.length()-start;
string eName = man[i].name.substr(start, langd);
cout << eName << ", " << fName << " Telephone " << man[i].number << "\n";
}
}
}
namesort(man, NUMBER);
int answer;
cout << "\nHow would you like to sort the list? (1 = Surname, 2 = Telephone number):";
cin >> answer;
if (answer == 1){
namesort(man, NUMBER);
cout << "\nSorted after surname.\n";
for(int o=0; o < NUMBER; o++){
cout << left << setw(20) << man[o].name << ", " << man[o].name
<< setw(15) << man[o].number << endl;
}
}
if (answer == 2){
telesort(man, NUMBER);
cout << "\nSorted after number.\n";
for(int p = 0; p < NUMBER; p++){
cout << left << setw(20) << man[p].name
<< setw(15) << man[p].number << endl;
}
}
return 0;
}
void namesort(Person data[], int numberof)
{
for(int m = 1; m < numberof; m++){
int pos = m;
Person temp = data[m];
while (pos > 0 && data[pos-1].name > temp.name){
data[pos] = data[pos-1];
pos--;
}
data[pos] = temp;
}
}
void telesort(Person data[], int numberof)
{
for (int m = 1; m < numberof; m++){
int position = m;
Person temp = data[m];
while (position > 0 && data[position-1].number > temp.number){
data[position] = data[position-1];
position--;
}
data[position] = temp;
}
}
|