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
|
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <numeric> //accumulate
#include <algorithm> //remove()
using namespace std;
//splits string entered by delim
void split(const string &s, char delim, vector<string> &elements)
{
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim))
{
elements.push_back(item);
}
}
//put the string into a vector
vector<string> splitToVector(const string &s, char delim)
{
vector<string> elements;
split(s, delim, elements);
return elements;
}
//verifies either AM or AM exists in the string
bool AMorPM(vector<string> v)
{
//convert vector to string so we can use find()
bool isPM = false;
string splittedString;
splittedString = accumulate(begin(v), end(v), splittedString);
string PM = "PM";
if (splittedString.find(PM) != string::npos)
{
isPM = true;
}
return isPM;
}
void result(vector<string> vecOfString, bool isPM)
{
if (isPM == false)
{
string AM = "AM";
//delete AM
vecOfString.erase(std::remove(vecOfString.begin(), vecOfString.end(), "AM"), vecOfString.end());
for (int i = 0; i < vecOfString.size(); i++)
{
cout << vecOfString[i] << ":";
}
}
}
int main()
{
vector <string> splitString;
string userTimeInput;
bool isAMorPM = false;
cin >> userTimeInput;
splitString = splitToVector(userTimeInput, ':');
isAMorPM = AMorPM(splitString);
result(splitString, isAMorPM);
system("PAUSE");
return 0;
}
|