Because the epidemic can only watch online classes at home. After learning C language for a few days, I plan to find a project. Today is the third day of this project. If you want to see the previous content, you can go to my same column to find it
Project: imperial concubine selection system
Six modules:
1. Imperial concubine selection - complete Enter the name of a new concubine, add a new concubine, and then reduce the favorability of other concubines by 10 points 2. Turn over the favor - complete Enter a concubine's name, turn over the card to favor her, increase her favorability by 10 points, and reduce other concubines' favorability by 10 points 3. Go to hell - Finish Enter the name of a concubine, delete a concubine, and then all the other concubines will be spoiled by 10 points. If there is no one, it will show a false alarm 4. Where's my concubine - Finish Find the concubine, input the name of the concubine and display the basic information. The selected concubine's favor degree is increased by 10 points. The other concubines' favor degree is decreased by 10 points 5. Patrol the harem - complete Patrol the rear palace and display all the information about the concubines in the palace 6. Rules of the game - complete 1. If there are three concubines with a pet degree below 50, there will be a riot, and the game is over 2. If someone's favor reaches 100, suppress the harem and the game is over
On the third day, we basically finished the qualified project today. Why is it basic? There are still some small problems to be solved. OK, let's talk less nonsense. The pits and problems we encountered today will be placed below. The complete code is at the end
1. Rules of the game
In fact, this module only writes 10 minutes in total, which is very fast. Here is the code. There are two functions. One function judges a rule, and then performs and calculates the return value to the initial while loop condition
//Judgment function, return 0 in case of failure, otherwise 1 //Do not put the complete code. Need to look at the last int judege_1(concu *ptca, int *num); int judege_2(concu *ptca, int *num); int c, d; //Control, cycle control interface while (d)//d is the cyclic condition {.... switch{} d = judege_1(p, num) && judege_2(p, num);//!!! if (d == 0) { printf("Harem rebellion,Game failure!"); printf("Enter any key to exit.....\n"); scanf("%d", &c); } }
2. Select the imperial concubine to prevent name repetition
When I did this, I didn't consider the problem of adding the same name repeatedly. I knew that I had chosen two "concubines" with the same name in succession. My method was to add another layer of judgment before selecting concubines and increasing or decreasing the degree of favor. If I found the same name, I would directly exit the function. Although it was a little tedious, there was no way
Here is the implementation code
for (int i = 0; i < n + 1; i++) { if (strcmp(p->name, ch) == 0)//P is an array, pointer, array, structure { printf("Emperor,%s It's already in the palace!\n", ch); return 0;//Exit the function directly with return } p++; } //Judge whether it is in the palace concu *p = ptca; int a_count = 0; //Different name Feizi counter for (int i = 0; i < n; i++) { // printf("%s --- %s\n", p->name, a_name); if (strcmp(p->name, a_name) != 0) { a_count++; } // printf("%d\n", a_count); p++; } if (a_count == n) { printf(" %s Not yet in the palace...\n", a_name); return 0; }
In fact, the above work was done this morning, but it has been a bit annoying. In the process of solving the problem, I found that if I want to modify it, I need to make a big difference to the previous code. In this project, it's good that I use the variable name uniformly, so I just changed the return type of each subfunction and added a judgment, but I still feel dissatisfied, This project has 420 lines of comments in total, which is not small or small, but the logic is simple, so it can be seen how important it is to conceive before carrying out medium and large-scale projects, and how important it is to realize pseudo code!! don't rush up to write code like me, it's a lesson to learn, continue to add oil! Ollie gives!
/* Imperial concubine selection system Six modules: 1.Imperial concubine selection - complete Enter the name of a new concubine, add a new concubine, and then reduce the favorability of other concubines by 10 points 2.Lucky turn - done Enter a concubine's name, turn over the card to favor her, increase her favorability by 10 points, and reduce other concubines' favorability by 10 points 3.Go to hell - Finish Enter the name of a concubine, delete a concubine, and then all the other concubines will be spoiled by 10 points. If there is no one, it will show a false alarm 4.Where's my concubine - done Find the concubine, input the name of the concubine and display the basic information. The selected concubine's favor degree is increased by 10 points. The other concubines' favor degree is decreased by 10 points 5.Tour the harem - complete Patrol the rear palace and display all the information about the concubines in the palace 6.Rules of the game - no Imperial hierarchy (first emperor system): After one Three ladies Nine wives Twenty seventh wife 81 Royal daughter Rules of the game: 1. If there are three concubines with a pet degree of less than 50, there will be riots. The game is over 2.If someone's favor reaches 100, suppress the harem and the game is over 3.Each concubine's default favor is 70 points */ #include <stdio.h> #include <string.h> typedef struct concubine { char name[10]; //Name, up to ten characters int level; //Harem rank int likability; //Favors } concu; //Function declaration int addOne(concu *ptca, int *num); int chooseOne(concu *ptca, int *num); int forsakeOne(concu *ptca, int *num); int findOne(concu *ptca, int *num); void seeAll(concu *ptca, int *num); int judege_1(concu *ptca, int *num); int judege_2(concu *ptca, int *num); // Game rule judgment counter 1 total number of people in cold palace int count = 0, leave = 0; //Initialization is better than not int main(void) { //The (X variable length) array of storage Cong concu concus[31]; int n = 0, *num = &n; //Store the total number of concubines in the rear palace, which is easy to change by using the pointer! When defining the number pointer, you need to & take the address concu *p; //The harem is initialized. There are three concubines by default concu c1 = {"Jie Jie Liu", 1, 70}; concu c2 = {"Sun Shang Xiang", 1, 70}; concu c3 = {"Juvenile fish", 1, 70}; concus[0] = c1; concus[1] = c2; concus[2] = c3; *num = 3; //Interface control of imperial concubine selection system int c, d; //Control, cycle control interface while (d) { p = concus; //Initialization pointer printf("-------Imperial concubine selection system-------\n\ //Four modules: \ n\ 1.Selected imperial concubine\n\ 2.Flip flop\n\ 3.Relegate to limbo\n\ 4.My beloved Princess\n\ 5.Harem Tour\n\ 6.Exit the imperial concubine selection system\n\ //Please enter serial number selection function: \ n“); scanf("%d", &c); switch (c) { case 1: // printf("1\n"); addOne(p, num); //Function 1 printf("Enter any key to continue.....\n"); scanf("%d", &c); break; case 2: // printf("2"); chooseOne(p, num); //Function 2 printf("Enter any key to continue.....\n"); scanf("%d", &c); break; case 3: // printf("3"); forsakeOne(p, num); //Function 3 printf("Enter any key to continue.....\n"); scanf("%d", &c); break; case 4: // printf("4"); findOne(p, num); //Function 4 printf("Enter any key to continue.....\n"); scanf("%d", &c); break; case 5: // printf("5"); seeAll(p, num); //Function 5 printf("Enter any key to continue.....\n"); scanf("%d", &c); break; case 6: printf("Exit successfully...\n"); c = 0; break; default: printf("Little Lord,No such function!\n"); printf("Enter any key to continue.....\n"); scanf("%d", &c); break; } d = judege_1(p, num) && judege_2(p, num); if (d == 0) { printf("Harem rebellion,Game failure!"); printf("Enter any key to exit.....\n"); scanf("%d", &c); } } } //1. Order to select a concubine - enter a new concubine's name, add a new concubine, and then reduce the favor of other concubines by 10 points int addOne(concu *ptca, int *num) { //Initialize the information of a concubine according to the input char ch[10]; int lev; printf("Please enter the name of the concubine,Grade(1-5)(Spacing):\n"); scanf("%s %d", ch, &lev); concu c; strcpy(c.name, ch); c.level = lev; c.likability = 70; int n = *num; //Temporary quantity controller, control cycle //Use traversal to complete the operation of imperial concubine selection concu *p = ptca; for (int i = 0; i < n + 1; i++) { if (strcmp(p->name, ch) == 0) { printf("Emperor,%s It's already in the palace!\n", ch); return 0; } p++; } for (int i = 0; i < n + 1; i++) { if (i < *num) { ptca->likability -= 10; ptca++; } else { *ptca = c; *num += 1; //Total plus one printf("Congratulations on the success of the princess selection;Imperial concubine information:Full name:%s,Grade:%d,Favors:%d\n", ptca->name, ptca->level, ptca->likability); // printf("total number of people in the current harem:% d\n", *num); } } } //2. Turn over the card to favor - input the name of a concubine, turn over the card to favor her, increase her favor by 10 points, and reduce the favor of other concubines by 10 points int chooseOne(concu *ptca, int *num) { int n = *num; //Temporary variable, control cycle char a_name[10]; //Prepare for flop printf("Emperor,It's time to flip,Just input the name of the concubine you want to turn over the card:\n"); scanf("%s", a_name); // gets(name); // printf("1"); // int i = 0; printf("The name of the concubine who is about to be overturned is %s ...\n", a_name); //Judge whether it is in the palace concu *p = ptca; int a_count = 0; //Different name Feizi counter for (int i = 0; i < n; i++) { // printf("%s --- %s\n", p->name, a_name); if (strcmp(p->name, a_name) != 0) { a_count++; } // printf("%d\n", a_count); p++; } if (a_count == n) { printf(" %s Not yet in the palace...\n", a_name); return 0; } printf("\n"); for (int i = 0; i < n; i++) { if ((strcmp(a_name, (ptca->name)) == 0)) //Determine whether the names are equal, equal returns 0 { ptca->likability += 10; printf(" %s Flop success!\n", ptca->name); } else { ptca->likability -= 10; printf("The empress is jealous, %s Reduce favorability by 10 points!\n", ptca->name); } ptca++; //Pointer to next structure } }; //3. Enter a concubine's name, delete a concubine, and then all other concubines will be favored by 10 points. If there is no such person, it will be a false alarm int forsakeOne(concu *ptca, int *num) { int n = *num; //Temporary variable, control cycle char a_name[10]; concu c; strcpy(c.name, "0"); c.level = 0; c.likability = 0; //Prepare for flop printf("Emperor,Which concubine are you going to abandon,Just enter the name of the concubine:\n"); scanf("%s", a_name); // gets(name); // printf("1"); int i = 0; printf("The name of the concubine who is about to be sent to the cold palace is %s ...", a_name); //Judge whether it is in the palace concu *p = ptca; int a_count = 0; //Different name Feizi counter for (int i = 0; i < n; i++) { // printf("%s --- %s\n", p->name, a_name); if (strcmp(p->name, a_name) != 0) { a_count++; } // printf("%d\n", a_count); p++; } if (a_count == n) { printf(" %s Not in the palace yet...\n", a_name); return 0; } printf("\n"); for (int i = 0; i < n; i++) { if ((strcmp(a_name, (ptca->name)) == 0)) //Determine whether the names are equal, equal returns 0 { *ptca = c; } else { ptca->likability += 10; printf("because %s Be sent to a cold house, %s Favor increased by 10 points!\n", a_name, ptca->name); } ptca++; //Pointer to next structure } }; //4. Where is my concubine? Find the concubine, input the name of the concubine and display the basic information. The selected concubine's favorability is increased by 10 points. The other concubines' favorability is decreased by 10 points int findOne(concu *ptca, int *num) { int n = *num; //Temporary variable, control cycle char a_name[10]; printf("Which imperial concubine do you want to find,Just enter your name:\n"); scanf("%s", a_name); printf("The concubine you are looking for is%s....\n", a_name); //Judge whether it is in the palace concu *p = ptca; int a_count = 0; //Different name Feizi counter for (int i = 0; i < n; i++) { // printf("%s --- %s\n", p->name, a_name); if (strcmp(p->name, a_name) != 0) { a_count++; } // printf("%d\n", a_count); p++; } if (a_count == n) { printf(" %s Not in the palace yet...\n", a_name); return 0; } //Search for qualified concubines in traversal and print information for (int i = 0; i < n; i++) { if ((strcmp(a_name, (ptca->name)) == 0)) { printf("Full name\t Grade\t Favors\t\n"); printf("%s\t", ptca->name); //Print level and replace with Chinese switch (ptca->level) { case 1: printf("have sexual intercourse with a woman\t"); break; case 2: printf("A woman\t"); break; case 3: printf("Pin\t"); break; case 4: printf("Madam\t"); break; case 5: printf("after\t"); break; default: printf("0\t"); break; } //Print favorability printf("%d\n", ptca->likability); } ptca++; } }; //5. Patrol the rear palace, print all the information about the concubines, and skip the information about the concubines who have been beaten into the cold palace void seeAll(concu *ptca, int *num) { int n = *num; //Temporary variable control cycle printf("-----------------------------------------\n"); printf("Full name\t Grade\t Favors\t\n"); for (int i = 0; i < n; i++) { if (ptca->level != 0) { //Print name printf("%s\t", ptca->name); //Print level and replace with Chinese switch (ptca->level) { case 1: printf("have sexual intercourse with a woman\t"); break; case 2: printf("A woman\t"); break; case 3: printf("Pin\t"); break; case 4: printf("Madam\t"); break; case 5: printf("after\t"); break; default: printf("0"); break; } //Print favorability printf("%d\n", ptca->likability); } ptca++; } }; //6.1. If there are three concubines with a pet degree below 50, there will be riots and the game will be over int judege_1(concu *ptca, int *num) { int n = *num; //Temporary variable, control cycle count = 0; //Counter clear 0 //Cycle counter for (int i = 0; i < n; i++) { if (ptca->likability < 50) { count++; } } //judge if (count >= 3) { return 0; } else { return 1; } } //6.2. If someone's favor reaches 100, suppress the harem and the game is over int judege_2(concu *ptca, int *num) { int n = *num; //Temporary variable, control cycle //Cycle counter for (int i = 0; i < n; i++) { if (ptca->likability >= 100) { return 0; } } return 1; }