Sat Mar 26, 2011 10:16 pm
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;
// words file path.
#define in_file "data.txt"
// max number of words.
const int maxNumWords = 10;
// function prototype
void readStrings(char[], char[], char[], char[]);
void sortStrings (char [], char [], char [], char []);
void swapStrings(char [], char []);
void printStrings(char [], char [], char [], char []);
ifstream fileStream; // global variable
int main()
{
char string1[maxNumWords], string2[maxNumWords], string3[maxNumWords], string4[maxNumWords];
char next_char;
fileStream.open("data.txt");
// Read the data.txt file until the end.
while(!fileStream.eof())
{
readStrings(string1, string2, string3, string4);
sortStrings(string1, string2, string3, string4);
// print the words group after sorting.
printStrings(string1, string2, string3, string4);
cout << endl;
fileStream.get(next_char);
}
cout << endl;
// close the file stream.
getch();
fileStream.close();
return 0;
}
void readStrings(char string1[], char string2[], char string3[], char string4[])
{
fileStream >> string1 >> string2 >> string3 >> string4;
}
void sortStrings(char str1[], char str2[], char str3[], char str4[])
{
// putting largest in its place
if (strcmp(str1, str2) > 0)
swapStrings(str1, str2);
if (strcmp(str2, str3) > 0)
swapStrings(str2, str3);
if (strcmp(str3, str4) > 0)
swapStrings(str3, str4);
// putting second largest in its place
if (strcmp(str1, str2) > 0)
swapStrings(str1, str2);
if (strcmp(str2, str3) > 0)
swapStrings(str2, str3);
//putting third largest in its place
if (strcmp(str1, str2) > 0)
swapStrings(str1, str2);
}
void swapStrings(char string1[],char string2[])
{
char temp[maxNumWords];
strcpy(temp,string1);
strcpy(string1,string2);
strcpy(string2,temp);
}
void printStrings(char string1[], char string2[], char string3[], char string4[])
{
cout << left << setw(9) << string1 << left << setw(9);
cout << string2 << left << setw(9);
cout << string3 << left << setw(9);
cout << string4 << left << setw(9);
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.