c++ Need my output sorted to dictionary order. The cards recognize the group the
ID: 3873109 • Letter: C
Question
c++ Need my output sorted to dictionary order. The cards recognize the group they need to be in, i just need them to print out in order. My code is included below.
The numbers are ordered 1, 2, 3, the shading is ordered empty, striped, filled, the color is ordered brown, orange, yellow, and the shapes are ordered circle, square, triangle.
Each group is output by first sorting the cards in the group, and then printing out each of the cards in the group (using the same format as the input file) separated by a period and a space.
// Class to represent a Card
class SetCard
{
public:
SetCard() {};
SetCard(int n, string s, string c, string f);
int num;
string shape;
string color;
string fills;
string toString();
};
// constructor
SetCard::SetCard(int n, string s, string c, string f)
{
num = n;
shape = s;
color = c;
fills = f;
}
// String representation of card
string SetCard::toString()
{
ostringstream oss;
oss << num << " " << shape << " " << color << " " << fills;
return oss.str();
}
int main()
{
bool stop = false; // to keep track of when to stop
do {
cout << "Enter the set input file name (or "done" to terminate the program):" << endl;
string fname;
cin >> fname;
SetCard cards[1000];
int len = 0;
// if done entered stop the program
if (fname.compare("done") == 0)
stop = true;
else
{
ifstream infile(fname.c_str());
int n;
string s, c, f;
while (infile >> n >> s >> c >> f) // reading from input file line by line
{
SetCard card(n, s, c, f); // create Card object
cards[len] = card; // Add it to an array
len++;
}
bool isFound = false;
for (int i = 0; i < len - 2; i++) // First card in comparison
{
for (int j = i + 1; j < len - 1; j++) // Second card in comparison
{
for (int k = j + 1; k < len; k++) // Third card in comparison
{
// Checking if all or None match for attribute 1
if ((cards[i].num == cards[j].num && cards[i].num == cards[k].num) ||
(cards[i].num != cards[j].num && cards[i].num != cards[k].num && cards[j].num != cards[k].num))
{
// Checking if all or None match for attribute 2
if ((cards[i].shape.compare(cards[j].shape) == 0 && cards[i].shape.compare(cards[k].shape) == 0) ||
(cards[i].shape.compare(cards[j].shape) != 0 && cards[i].shape.compare(cards[k].shape) != 0 && cards[j].shape.compare(cards[k].shape) != 0))
{
// Checking if all or None match for attribute 3
if ((cards[i].color.compare(cards[j].color) == 0 && cards[i].color.compare(cards[k].color) == 0) ||
(cards[i].color.compare(cards[j].color) != 0 && cards[i].color.compare(cards[k].color) != 0 && cards[j].color.compare(cards[k].color) != 0))
{
// Checking if all or None match for attribute 4
if ((cards[i].fills.compare(cards[j].fills) == 0 && cards[i].fills.compare(cards[k].fills) == 0) ||
(cards[i].fills.compare(cards[j].fills) != 0 && cards[i].fills.compare(cards[k].fills) != 0 && cards[j].fills.compare(cards[k].fills) != 0))
{
cout << cards[i].toString() << ", " << cards[j].toString() << ", " << cards[k].toString() << endl;
isFound = true;
}
}
}
}
}
}
}
// If even 1 set isn't found, show the below message
if (!isFound)
cout << "No sets" << endl;
}
} while (!stop);
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
// Class to represent a Card
class SetCard
{
public:
SetCard() {};
SetCard(int n, string s, string c, string f);
int num;
string shape;
string color;
string fills;
string toString();
};
// constructor
SetCard::SetCard(int n, string s, string c, string f)
{
num = n;
shape = s;
color = c;
fills = f;
}
// String representation of card
string SetCard::toString()
{
ostringstream oss;
oss << num << " " << shape << " " << color << " " << fills;
return oss.str();
}
void swapIt(SetCard a[], int i, int j){
SetCard temp;
// Swapping two elements
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void sorting(SetCard a[]){
// Applying bubble sort to sort elements in dictionary order
for (int i = 0; i < 3 - 1; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
//sorting the different cards
if (a[j].num>=a[j + 1].num)
{
if(a[j].num > a[j + 1].num)
swapIt(a, j, j + 1);
// If numbers are in order but shades are not
else if((a[j].num == a[j + 1].num) && (((a[j].fills == "striped" || a[j].fills == "filled") && a[j + 1].fills == "empty") || (a[j].fills == "filled" && a[j+1].fills == "striped")))
swapIt(a, j, j + 1);
// If numbers and shades in order but colors are not
else if((a[j].fills == a[j + 1].fills) && a[j].color > a[j + 1].color)
swapIt(a, j, j + 1);
// If numbers, shades and colors are in order but shapes are not
else if((a[j].color == a[j + 1].color) && (a[j].shape > a[j + 1].shape))
swapIt(a, j, j + 1);
}//if
}//for j
}//for i
}
int main()
{
bool stop = false; // to keep track of when to stop
do {
cout << "Enter the set input file name (or "done" to terminate the program):" << endl;
string fname;
cin >> fname;
SetCard cards[1000], temp[3];
int len = 0;
// if done entered stop the program
if (fname.compare("done") == 0)
stop = true;
else
{
ifstream infile(fname.c_str());
int n;
string s, c, f;
while (infile >> n >> s >> c >> f) // reading from input file line by line
{
SetCard card(n, s, c, f); // create Card object
cards[len] = card; // Add it to an array
len++;
}
bool isFound = false;
for (int i = 0; i < len - 2; i++) // First card in comparison
{
for (int j = i + 1; j < len - 1; j++) // Second card in comparison
{
for (int k = j + 1; k < len; k++) // Third card in comparison
{
// Checking if all or None match for attribute 1
if ((cards[i].num == cards[j].num && cards[i].num == cards[k].num) ||
(cards[i].num != cards[j].num && cards[i].num != cards[k].num && cards[j].num != cards[k].num))
{
// Checking if all or None match for attribute 2
if ((cards[i].shape.compare(cards[j].shape) == 0 && cards[i].shape.compare(cards[k].shape) == 0) ||
(cards[i].shape.compare(cards[j].shape) != 0 && cards[i].shape.compare(cards[k].shape) != 0 && cards[j].shape.compare(cards[k].shape) != 0))
{
// Checking if all or None match for attribute 3
if ((cards[i].color.compare(cards[j].color) == 0 && cards[i].color.compare(cards[k].color) == 0) ||
(cards[i].color.compare(cards[j].color) != 0 && cards[i].color.compare(cards[k].color) != 0 && cards[j].color.compare(cards[k].color) != 0))
{
// Checking if all or None match for attribute 4
if ((cards[i].fills.compare(cards[j].fills) == 0 && cards[i].fills.compare(cards[k].fills) == 0) ||
(cards[i].fills.compare(cards[j].fills) != 0 && cards[i].fills.compare(cards[k].fills) != 0 && cards[j].fills.compare(cards[k].fills) != 0))
{
temp[0] = cards[i];
temp[1] = cards[j];
temp[2] = cards[k];
sorting(temp);
cout << temp[0].toString() << ", " << temp[1].toString() << ", " << temp[2].toString() << endl;
isFound = true;
}
}
}
}
}
}
}
// If even 1 set isn't found, show the below message
if (!isFound)
cout << "No sets" << endl;
}
} while (!stop);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.