Playing card sorting – the use of sort function and find function, structure

This is an OJ question from a school. This question can be used to initially grasp the usage of the sort function and the find function:

Here are the topics:

topic description

The custom structure represents a poker card, including types – spades, hearts, clubs, diamonds, kings; sizes – 2,3,4,5,6,7,8,9,10, J, Q, K, A, little king (indicated by 0), king (indicated by 1). Input n, input n playing card information, and output their sorting results from big to small.

Assume that the ordering rules of the playing cards are that the king and the king are the first and the second, and the remaining 52 playing cards are sorted by suit first and then size.

Suit: Spades>Hearts>Clubs>Diamonds.

Size: A>K>Q>J>>10>9>…>2.

Tips: Use Baidu sort function and strstr function.

enter:

Number of tests t

Two rows for each set of test data:

The first line: n, means input n playing cards

The second line: n playing card information, see the sample for the format

output:

For each set of test data, output the sorting results from large to small

Sample input:

3
5
Spades 4 Hearts 10 Clubs Q Diamonds K Spades A
10
King Club 10 Hearts K Diamonds 9 Spades 8 Clubs A Diamonds Q Xiao Wang Spades 2 Spades J
5
Hearts K Clubs K Spades K Diamonds K Xiaowang

Sample output:

Spades A Spades 4 Hearts 10 Clubs Q Diamonds K
Big King Little Wang Spades J Spades 8 Spades 2 Hearts K Club A Club 10 Diamonds Q Diamonds 9
Xiao Wang Spades K Hearts K Clubs K Diamonds K

AC code:

#include <algorithm>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
string number_rule[13]={"2","3","4","5","6",
                        "7","8","9","10","J",
                        "Q","K","A"};
string flower[6]={"Big King","Little King","Spades","Hearts"
,"Plum Blossom","Block"};

struct Poke {
    string name;
    int number;
    int flower;
};

int judge_number(string A)
{
    for(int i=0;i<13;i ++ )
    {
        if(A.find(number_rule[i])!=string::npos)
        {
            return i;
        }
    }
    return -1;
}

int judge_flower(string A)
{
    for(int i=0;i<6;i++)
    {
        if(A.find(flower[i])!=string::npos)
        {
            return i;
        }
    }
    return -1;
}

bool cmp(Poke A,Poke B)
{
    if(A. flower!=B. flower)
    {
        return A.flower<B.flower;
    }
    else
    {
        return A.number>B.number;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        Poke*A=new Poke[n];
        for(int i=0;i<n;i++)
        {
            cin>>A[i].name;
            A[i].number= judge_number(A[i].name);
            A[i].flower= judge_flower(A[i].name);
        }
        sort(A,A+n,cmp);
        for(int i=0;i<n;i++)
        {
            if(i!=n-1)
            {
                cout<<A[i].name<<" ";
            }
            else
            {
                cout<<A[i].name<<endl;
            }
        }
    }
    return 0;
}

First of all, according to the sorting method of the title, we first divide the information of a card into suits and numbers (or letters), so we need to create a structure type poke to store the information of each poker card:

struct poke{
    string poker;
    int num;
    int flower;
};

The string string is the input poker information, and num and flower need to be represented by integers before they can be sorted

Therefore, we need to create an integer form corresponding to num and flower, here we implement it in the form of an array:

string num_rule[13]={"2","3","4","5","6","7","8\ ","9","10","J","Q","K","A"};
string flower_rule[6]={"Big King","Little King","Spades","Hearts","Plum Blossoms","Boxes"};
//The two-dimensional array is used here because the string is to be found when the find function is used later, so a single character is also represented as a string here

Therefore, we have defined the sorting order of the information on the poker cards, and the next step is to input the poke information. Here we first write out the main function according to the meaning of the question, and then implement the functional function in detail:

//Next is the part of the main function:

int main()
{
    int t;
    cin>>t;
    while(t--)//Test t times
    {
        int n;
        cin>>n;//n playing cards
        poke*A=new poke[n];//Create an array of poker cards to store the information of these poker cards
        for(int i=0;i<n;i + + )//every card
        {
            cin>>A[i].poker;//Input card information
            A[i].num=judge_number(A[i].poker);//Extract the number from it
            A[i].flower=judege_flower(A[i].poker);//Extract the suit information from it, turn it into an integer, and store it in the element of the structure array
        }//Numbers and suits are separated into ints only for sorting
        // store data complete
        sort(A,A + n,cmp);//Sort the structure array, use the sort function in the <algorithm> header file
        for(int i=0;i<n;i + + )//output result
        {
            if(i!=n-1)
            {
                cout<<A[i].poker<<" ";
            }
            else
            {
                cout<<A[i].poker<<endl;
            }
        }
    }
    return 0;
}

Next, we need to implement the judge_number and judge_flower functions that extract numbers and suits

First of all, we can know that to achieve this function, the poker string must be passed into the function, so the parameter is a string A

Furthermore, after separation and its integer sorting form is returned, so the return type of the function should be int

Then, it is how to separate the string into the string, and find its integer sorting form in one-to-one correspondence with the above sorting rules.

To find the string, here we need to use the find function, which belongs to the header file.

The following is the usage and related information of the find function:

Here are several forms of the find() function under string:

size_t find (const string & amp; str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find(char c, size_t pos = 0) const;

Among them, str is the substring to be searched; s is the character array to be searched; c is the character to be searched; pos is the starting position of the search; n is the number of characters to be searched.

If the search is successful, the find() function returns the first occurrence of the substring or character in the string; otherwise, it returns a special value string::npos, indicating that the search failed.

Here we use whether find(string & amp;str (i.e. the target string)) returns string::npos as the judgment condition

int judge_number(string A)
{
    for(int i=0;i<13;i ++ )
    {
        if(A.find(number_rule[i])!=string::npos)//Traverse the sample array number--rule, find the corresponding integer number and return it, and use A.find() to indicate it in the string where to find the target string
        {
            return i;
        }
    }
    return -1;
}

Similarly, suits are the same:

int judge_flower(string A)
{
    for(int i=0;i<6;i++)
    {
        if(A.find(flower[i])!=string::npos)
        {
            return i;
        }
    }
    return -1;
}

Here we have initialized a structure of poke type, which can be sorted

The following is the usage of the sort() function under :

Syntax

Sort(start,end,cmp)

Parameters

(1) start indicates the starting address of the array to be sorted;

(2) end indicates the next digit of the end address of the array;

(3) cmp is used to specify the sorting method, it can be left blank, and the default is ascending.

Features

The sort function is used in C++ to sort all elements in a given interval, the default is ascending order, and descending order is also available.

Generally, the array is sorted directly, such as sorting the array a[10], sort(a,a + 10). The power of the sort function is that it can be used in combination with the cmp function, that is, the selection of the sorting method. (sort and cmp are used together)

custom sort function

example:

bool cmp(Student s1, Student s2) { // custom sort
    if (s1.grade != s2.grade) { // if students have different grades
        return s1.grade > s2.grade; // Sort in descending order of grades
    }
    return s1.name < s2.name; // otherwise sort by name in ascending order
}

cmp is just a method of sorting, and what is returned is the way of arrangement (ascending/descending)

bool cmp(poke A, poke B)
{
    if(A.flower!=B.flower)//Sort by color first
    {
        return A.flower<B.flower;
    }
    else//If the suit is the same, it will be sorted according to the number
    {
        return A.num>B.num;
    }
}

Finally, the whole question code integration:

#include <iostream>
using namespace std;
#include <string>
#include <algorithm>
struct poke {
    string poker;
    int num;
    int flower;
};

string num_rule[13]={"2","3","4","5","6","7","8","8",\ "9","10","J","Q","K","A"};
string flower_rule[6]={"Big King","Little King","Spades","Hearts","Plum Blossoms","Boxes"};

int judge_number(string A)
{
    for(int i=0;i<13;i ++ )
    {
        if(A.find(num_rule[i])!=string::npos)
        {
            return i;
        }
    }
    return -1;
}

int judge_flower(string A)
{
    for(int i=0;i<6;i++)
    {
        if(A.find(flower_rule[i])!=string::npos)
        {
            return i;
        }
    }
    return -1;
}

bool cmp(poke A, poke B)
{
    if(A. flower!=B. flower)
    {
        return A.flower<B.flower;
    }
    else
    {
        return A.num>B.num;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)//Test t times
    {
        int n;
        cin>>n;//n playing cards
        poke*A=new poke[n];//Create an array of poker cards to store the information of these poker cards
        for(int i=0;i<n;i + + )//every card
        {
            cin>>A[i].poker;//Input card information
            A[i].num=judge_number(A[i].poker);//Extract the number from it
            A[i].flower=judge_flower(A[i].poker);//Extract the suit information from it, turn it into an integer, and store it in the element of the structure array
        }//Numbers and suits are separated into ints only for sorting
        // store data complete
        sort(A,A + n,cmp);//Sort the structure array, use the sort function in the <algorithm> header file
        for(int i=0;i<n;i + + )//output result
        {
            if(i!=n-1)
            {
                cout<<A[i].poker<<" ";
            }
            else
            {
                cout<<A[i].poker<<endl;
            }
        }
    }
    return 0;
}

Finally, I would like to answer a few questions through this blog:

1. Why doesn’t the numerical part of Da Wang and Xiao Wang affect the result?

Because if the suits are different, it will never be the turn to judge the numbers. It is necessary to ensure that the suits are judged first, and the numbers will be judged only if the suits are the same.

2. How to use the find() function

Example: A.find(flower_rule[i])!=string::nops;

A refers to the large string array, in this large string array, find if there is any part of the string that is the same as flower_rule[i], if yes, return the first address of this string, if not, return npos