c++ Need my output sorted to dictionary order. The cards recognize the group the
ID: 3873025 • 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.
// 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
Hi,
Good news, you dont have to do so many manual comparisons :)
we can write our custom sort in c++ with our own comparator like below, you can replace that full nested for loop with
std::sort(std::begin(card), std::end(cards), comp); //pass array and comparator
bool comp(const SetCard& left, const SetCard& right) //define comparator
{
return (left.num < right.num) ||
((left.num == right.num) && strcmp(left.shape > right.shape)) ||
((left.num == right.num) && strcmp(left.shape,right.shape)==0)) &&(strcmp(left.color, right.color) < 0)) &&
((left.num == right.num) && strcmp(left.shape,right.shape)==0&& &&(strcmp(left.color, right.color)==0))
&& (strcmp(left.fills, right.fills) < 0));
}
here, there are 4 statements
1. Return if num is diff, which is lesser
2. If num is same, compare shape using strcmp
3. If num and shape are same then compare with color
4. If num,shape and color are same, compare with fills
Now, after this sort is done, you can just print the cards array
for(i=0;i<len-1;i++)
{
cout<<cards[i].toString();
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.