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
|
#include <iostream>
#include <string>
#include <fstream>
void breakup(std::string,std::string &,std::string &,std::string &);
std::string makealpha(std::string &,std::string &,std::string &);
using namespace std;
int main()
{
string line; string first; string middle; string last; string neww;
ifstream fin("A7infile.txt");
while(getline(fin,line))
{
cout<<"Original Line: "<<line<<endl<<endl;
breakup(line,first,middle,last);
cout<<first<<" :is first"<<endl;
cout<<middle<<" :is middle"<<endl;
cout<<last<<" :is last\n"<<endl;
neww=makealpha(first,middle,last);
cout<<neww<<" :is the alphabetized line\n"<<endl;
}
fin.close();
return 0;
}
void breakup(string line,string &first,string &middle,string &last)
{
string::size_type pos; string::size_type pos2; string::size_type pos3;
pos=line.find(" ");
first=line.substr(0,pos);
pos2=line.find(" ",pos+1);
middle=line.substr(pos+1,pos2-pos-1);
pos3=line.find(" ",pos2+1);
last=line.substr(pos2+1);
return;
}
std::string makealpha(string &first,string &middle,string &last)
{
string neww;
if(first<middle&&first<last&&middle<last)
neww=first+" "+middle+" "+last;
else if(first<last&&first<middle&&last<middle)
neww=first+" "+last+" "+middle;
else if(middle<first&&middle<last&&last<first)
neww=middle+" "+last+" "+first;
else if(middle<first&&middle<last&&first<last)
neww=middle+" "+first+" "+last;
else if(last<first&&last<middle&&first<middle)
neww=last+" "+first+" "+middle;
else if(last<middle&&last<first&&middle<first)
neww=last+" "+middle+" "+first;
return neww;
}
|