Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

here is a code #include #include #include using namespace std; const int MAX_SIZE = 20; // Card struct to store card information struct Card {

here is a code #include #include #include using namespace std; const int MAX_SIZE = 20; // Card struct to store card information struct Card { char suit; string rank; }; // SortedList class using dynamic allocated arrays class SortedList { private: Card* cards; int size; int capacity; public: SortedList() { capacity = MAX_SIZE; cards = new Card[capacity]; size = 0; } ~SortedList() { delete[] cards; } // Add new card to list in sorted order void putItem(Card card) { // If list is full, resize if (size == capacity) { capacity *= 2; Card* newCards = new Card[capacity]; for (int i = 0; i < size; i++) { newCards[i] = cards[i]; } delete[] cards; cards = newCards; } int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = compareTo(card, cards[mid]); if (cmp == 0) { // Card already exists in list return; } else if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } // Card not found, insert at position "left" for (int i = size; i > left; i--) { cards[i] = cards[i - 1]; } cards[left] = card; size++; } // Remove card from list void deleteItem(Card card) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = compareTo(card, cards[mid]); if (cmp == 0) { // Card found, delete it for (int i = mid; i < size - 1; i++) { cards[i] = cards[i + 1]; } size--; return; } else if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } } // Get card from list Card getItem(Card card) { int i = 0; while (i < size && compareTo(card, cards[i]) != 0) { i++; } if (i < size) { return cards[i]; } else { return {' ', ""}; } } // Print all cards in list void printAll() { for (int i = 0; i < size; i++) { cout << cards[i].suit << cards[i].rank; if (i < size - 1) { cout << ","; } } cout << endl; } // Compare two cards based on their suit and rank int compareTo(Card c1, Card c2) { if (c1.suit < c2.suit) { return -1; } else if (c1.suit > c2.suit) { return 1; } else { // Suits are the same, compare ranks int rank1, rank2; if (isdigit(c1.rank[0])) { rank1 = stoi(c1.rank); } else { rank1 = c1.rank[0] - 'A' + 1; } if (isdigit(c2.rank[0])) { rank2 = stoi(c2.rank); } else { rank2 = c2.rank[0] - 'A' + 1; } if (rank1 < rank2) { return -1; } else if (rank1 > rank2) { return 1; } else { return 0; } } } }; int main() { string fileName; cout << "Enter a file name: "; cin >> fileName; // Create new SortedList instance SortedList* myList = new SortedList(); // Read cards from file and add them to list ifstream inputFile; inputFile.open(fileName); if (!inputFile) { cout << "Error: Could not open file." << endl; return 1; } char suit; string rank; // Step 1: Read the first 20 cards in the first line of the file, then put them into the list using putItem() for (int i = 0; i < 20 && inputFile >> suit >> rank; i++) { Card card = {suit, rank}; myList->putItem(card); } // Step 2: Print all cards in list myList->printAll(); // Step 3: Delete cards indicated in second line of file using deleteItem() while (inputFile >> suit >> rank) { Card cardToDelete = {suit, rank}; myList->deleteItem(cardToDelete); } // Step 4: Add items in third line to list using putItem() while (inputFile >> suit >> rank) { Card cardToAdd = {suit, rank}; myList->putItem(cardToAdd); } inputFile.close(); // Step 5: Search for elements in list using getItem() and output results cout << "C9 " << (myList->getItem({'C', "9"}).rank == "" ? "NO" : "YES") << ", "; cout << "C10 " << (myList->getItem({'C', "10"}).rank == "" ? "NO" : "YES") << endl; // Step 6: Print all cards in list myList->printAll(); delete myList; return 0; } his program should follow all theses steps write a program that implements a sorted list using dynamic allocated arrays. DataFile.txt contains the information of poker cards. 1. C: clubs (lowest), D: diamonds, H: hearts, S: spades (highest) 2. 2 (lowest), 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A 3. No Joker cards 4. Any C cards are lower than any D cards. DataFile Content (You can write the file specification into your program.) : H4,C8,HJ,C9,D10,D5,DK,D2,S7,DJ,H3,H6,S10,HK,DQ,C2,CJ,C4,CQ,D8,C3,SA,S2,HQ,S8, C6,D9,S3,SQ ,C5,S4,H5,SJ,D3,H8,CK,S6,D7,S9,H2,CA,C7,H7,DA,D4,H9,D6,HA,H10,S5,C10 H4, D5, HK, D2 H4, HK, SK C9,C10 For examples, DJ means J of Diamonds; H7 means 7 of hearts. Your job 1. Create a list by dynamic allocated array and set the size to 20 2. Read the first 20 cards in the first line of the file, the put them one by one into the list by implementing and using putItem(). The list must be kept sorted in ascending order. Then print out all the cards in the list in one line separating by commas. 3. Then delete the cards indicated in the second line of the file by using deleteItem() Then print out all the cards in the list in one line separating by commas. 4. Then put the items in the third line in to the list. Must use putItem() Then print out all the cards in the list in one line separating by commas. 5. Search the current list for the elements in the list. Then output the result as the follows. Yes or No depends on whether the card exists in the current list. Must implement and use getItem() C9 NO, C10 YES 6. A printAll() function should be defined and called in order to output all the contents in the list. 7. A compareTo() function must be defined and used to compare which card is greater, less, or equal. fix the code if something is wrong because I'm getting output Enter a file name: DataFile.txt 0,S5,C10,C9,C10,D2,D5,,H4,C8,HJ,C9,D10,D5,DK,D2,S7,DJ,H3,H6,S10,HK,DQ,C2,CJ,C4,CQ,D8,C3,SA,S2,HQ,,HK,,S8,C6,D9,S3,SQ,C5,S4,H5,SJ,D3,H8,CK,S6,D7,S9,H2,CA,C7,H7,DA,D4,H9,D6,HA,H1,SK C9 YES, C10 NO 0,S5,C10,C9,C10,D2,D5,,H4,C8,HJ,C9,D10,D5,DK,D2,S7,DJ,H3,H6,S10,HK,DQ,C2,CJ,C4,CQ,D8,C3,SA,S2,HQ,,HK,,S8,C6,D9,S3,SQ,C5,S4,H5,SJ,D3,H8,CK,S6,D7,S9,H2,CA,C7,H7,DA,D4,H9,D6,HA,H1,SK which seems wrong

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions