Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- #define MAX 200
- using namespace std;
- struct MACHINE
- {
- int firNum, secNum, thiNum, fouNum;
- string name;
- };
- bool ReadRecord(MACHINE &a, istream &in);
- void PrintRecord(MACHINE &a, ostream &out, bool NameOnly);
- void exchange(MACHINE &a, MACHINE &b);
- void compareNum(MACHINE &a, MACHINE &b);
- int main()
- {
- bool nameOnly = true;
- int count = 0;
- int group[MAX] = {0};
- int groupCounter = 0;
- int printed[MAX] = {0};
- MACHINE individual[MAX];
- cout << "What is the name of the input file?" << endl;
- string iFileName;
- cin >> iFileName;
- ifstream text;
- text.open(iFileName.c_str());
- if(!text.is_open())
- {
- cout << "Error opening file!" << endl;
- return -1;
- }
- //READS FILE INTO STRUCT
- while(count < MAX && ReadRecord(individual[count], text))
- {
- count++;
- }
- text.close();
- cout << "What is the name of the output file?" << endl;
- string oFileName;
- cin >> oFileName;
- ofstream output;
- output.open(oFileName.c_str());
- if(!output.is_open())
- {
- cout << "Error opening file!" << endl;
- return -1;
- }
- //group name of same sub-network UNSORTED
- for(int i = 0; i <= count - 1; i++)
- {
- int addDash = 0;
- for(int j = i+1; j <= count; j++)
- {
- if(individual[i].firNum == individual[j].firNum
- && individual[i].secNum == individual[j].secNum)
- {
- if(printed[j] == 0 && printed[i] == 0)
- {
- groupCounter++;
- printed[j] = 1;
- printed[i] = 1;
- group[i] = groupCounter;
- group[j] = groupCounter;
- addDash++;
- PrintRecord(individual[i], output, nameOnly);
- PrintRecord(individual[j], output, nameOnly);
- }
- else if (printed[j] == 0)
- {
- printed[j] = 1;
- group [j] = groupCounter;
- PrintRecord(individual[j], output, nameOnly);
- }
- }
- }
- if(addDash > 0)
- {
- output << "-------------------------" << endl;
- }
- }
- // file contents UNSORTED
- output << "UNSORTED LIST" << endl << endl;
- for(int i = 0; i <= count; i++)
- {
- PrintRecord(individual[i], output, !nameOnly);
- }
- output << "---------------------------------------------" << endl;
- //sort machine by name
- for(int i = 0; i <= count - 1; i++)
- {
- for(int j = i+1; j <= count; j++)
- {
- if(individual[i].name < individual[j].name)
- {
- }
- else
- {
- exchange(individual[i], individual[j]);
- }
- }
- }
- output << "SORTED BY MACHINE NAME" << endl << endl;
- for(int i = 0; i <= count; i++)
- {
- PrintRecord(individual[i], output, !nameOnly);
- }
- output << "-----------------------------------------" << endl;
- //move 0.0.0.0 none to the first position
- int i = 0;
- while(i <= count)
- {
- if(individual[i].firNum == 0 && individual[i].secNum == 0
- && individual[i].thiNum == 0 && individual[i].fouNum == 0)
- {
- exchange(individual[0], individual[i]);
- i = count + 1;
- }
- i++;
- }
- // sort machine by IP
- for(int i = 1; i <= count - 1; i++)
- {
- for(int j = i+1; j <= count; j++)
- {
- compareNum(individual[i], individual[j]);
- }
- }
- output << "SORTED BY IP ADDRESS" << endl << endl;
- for(int i = 0; i <= count; i++)
- {
- PrintRecord(individual[i], output, !nameOnly);
- }
- output << endl;
- // Number of groups of 2+ machines in the file
- output << "There are " << groupCounter << " groups of two or more machines in the file!"
- << endl;
- // Number of machines in the largest group
- int largestGroup = 0,test =0;
- for(int i = 1; i <= groupCounter; i++)
- {
- for(int j = 0; j < MAX - 1; j++)
- {
- if(group[j] == i)
- {
- test++;
- }
- }
- if(test > largestGroup)
- {
- largestGroup = test;
- }
- test = 0;
- }
- output << "There are " << largestGroup << " machines in the largest group!"
- << endl;
- // number of machines in the smallest group
- int smallestGroup = MAX + 1;
- for(int i = 1; i <= groupCounter; i++)
- {
- for(int j = 0; j <= count; j++)
- {
- if(group[j] == i)
- {
- test++;
- }
- }
- if(test < smallestGroup)
- {
- smallestGroup = test;
- }
- test = 0;
- }
- if(count == 0 || groupCounter == 0)
- {
- smallestGroup = 0;
- }
- output << "There are " << smallestGroup << " machines in the smallest group!"
- << endl;
- return 0;
- }
- void compareNum(MACHINE &a, MACHINE &b)
- {
- if(a.firNum < b.firNum)
- {
- exchange(a, b);
- }
- else if(a.firNum == b.firNum)
- {
- if(a.secNum < b.secNum)
- {
- exchange(a, b);
- }
- else if( a.secNum == b.secNum)
- {
- if(a.thiNum < b.thiNum)
- {
- exchange(a, b);
- }
- else if(a.thiNum == b.thiNum)
- {
- if(a.fouNum < b.fouNum)
- {
- exchange(a,b);
- }
- }
- }
- }
- }
- void exchange(MACHINE &a, MACHINE &b)
- {
- string temp = a.name;
- int tempa = a.firNum, tempb = a.secNum,
- tempc = a.thiNum, tempd = a.fouNum;
- a.name = b.name;
- a.firNum = b.firNum;
- a.secNum = b.secNum;
- a.thiNum = b.thiNum;
- a.fouNum = b.fouNum;
- b.name = temp;
- b.firNum = tempa;
- b.secNum = tempb;
- b.thiNum = tempc;
- b.fouNum = tempd;
- }
- bool ReadRecord(MACHINE &z, istream &in)
- {
- char dot;
- if(in >> z.firNum >> dot >> z.secNum >> dot >> z.thiNum
- >> dot >> z.fouNum >> z.name)
- {
- if(z.firNum == 0 && z.secNum == 0 && z.thiNum == 0
- && z.fouNum == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- return false;
- }
- void PrintRecord(MACHINE &a, ostream &out, bool z)
- {
- if(z)
- {
- out << a.name << endl;
- }
- else
- {
- int i = 0, j = 0, k = 0, l = 0;
- if(a.firNum > 99)
- {
- i = 0;
- }
- else if (a.firNum > 9)
- {
- i = 1;
- }
- else if (a.firNum > 0)
- {
- i = 2;
- }
- if(a.secNum > 99)
- {
- j = 0;
- }
- else if (a.secNum > 9)
- {
- j = 1;
- }
- else if (a.secNum > 0)
- {
- j = 2;
- }
- if(a.thiNum > 99)
- {
- k = 0;
- }
- else if (a.thiNum > 9)
- {
- k = 1;
- }
- else if (a.thiNum > 0)
- {
- k = 2;
- }
- if(a.fouNum > 99)
- {
- l = 0;
- }
- else if (a.fouNum > 9)
- {
- l = 1;
- }
- else if (a.fouNum > 0)
- {
- l = 2;
- }
- out << a.firNum << "." << a.secNum << "." << a.thiNum
- << "." << a.fouNum;
- int m = 4 + i + j + k + l;
- for(m; m > 0; m--)
- {
- out << " ";
- }
- out << a.name << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement