当前位置:网站首页>C language homework - matching system
C language homework - matching system
2022-06-29 15:46:00 【Ruogen】
c Language homework , Matching system , Three threads are opened ,c89, Need to support pthread.h library .
file 1 color.h
#include <windows.h>
const WORD FORE_BLUE = FOREGROUND_BLUE; // Blue text properties
const WORD FORE_GREEN = FOREGROUND_GREEN; // Green text properties
const WORD FORE_RED = FOREGROUND_RED; // Red text properties
const WORD FORE_YELLOW = 6; //(FORE_RED | FORE_GREEN); // Yellow text attribute //c89 The standard does not allow (FORE_RED | FORE_GREEN) operation , So the value is given directly , But portability is reduced .
const WORD FORE_GRAY = FOREGROUND_INTENSITY; // Gray text properties // If it is c99 Standard, please remove the number and replace it with the following expression
const WORD BACK_BLUE = BACKGROUND_BLUE; // Blue background attribute
const WORD BACK_GREEN = BACKGROUND_GREEN; // Green background attribute
const WORD BACK_RED = BACKGROUND_RED; // Green background attribute
const WORD BACK_PURPLE = 80; //(BACK_BLUE | BACK_RED); // Purple background attribute
const WORD BACK_CYAN = 48;//(BACK_BLUE | BACK_GREEN); // Cyan background attribute
const WORD BACK_YELLOW = 96;//(BACK_RED | BACK_GREEN); // Yellow background attribute
const WORD BACK_GRAY = BACKGROUND_INTENSITY; // Gray background attribute
// The following is a windows Change the window color to call
#define get_yellow SetConsoleTextAttribute(handle_out, FORE_YELLOW) // yellow
#define get_red SetConsoleTextAttribute(handle_out, FOREGROUND_INTENSITY | FORE_RED)// Red
#define get_white SetConsoleTextAttribute(handle_out, FORE_GREEN | FORE_BLUE | FORE_RED) // Mixing the three primary colors is not white ( Primary colors ) Ha ha ha .
#define get_blue SetConsoleTextAttribute(handle_out, FORE_BLUE) // Blue
#define get_green SetConsoleTextAttribute(handle_out, FORE_GREEN) // green
// I only use so many colors for the time being
void getcolor()
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
}file 2 match_data.h
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
struct people
{
int id;// user id
char name[500];// user name
int grade; // fraction
int wait_time; // Match wait time
};
struct gaming
{
struct people a;
struct people b;
int average_score;
int time_ing;
};file 3 match_list.h
/// Save linked list operation header file
#include "sstring.h"
//#include "solve_error.h"
#include"menu.h"
extern int list_size;
typedef struct data_list // The structure of the list , Contains match_data.h Basic data of this program in (id Name and score ), And a pointer to the next .
{
struct people data;
struct data_list *next;
} list_data;
struct data_list * list_inint(int n) // Initialize a linked list (list_data type ), It includes n Tables , However, this program does not require an initial n Tables , Just initialize .
{
list_data * head;
list_data * tail;
list_data * p;
head = tail = p = NULL;
list_size=n;
while (n--)
{
p = (list_data *) malloc(sizeof(list_data));// Allocate space to the table
scanf("%d%s%d", &p->data.id, p->data.name, &p->data.grade);
p->next = NULL;
if (head == NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
}
return head;// Return head pointer
}
list_data * list_add(list_data *head,struct people a,int * kase)// Insert a data into the linked list ( Insert to ensure that the linked list is in accordance with “i d” Arrange order well ).
{
list_size++;
list_data * p = (list_data *) malloc(sizeof(list_data));// Allocate space to the table
p->data=a;
p->next=NULL;
list_data * head1 = head; // Create a new header —— Used to traverse to just like id A small place , Then put in the data .
if(head==NULL||head->data.id>a.id)
{
p->next=head;
head=p;
return head;
}
else
{
while(head1->next!=NULL && head1->next->data.id <= a.id)
{
head1=head1->next;
}
if(head1->data.id == a.id)// If there is a heavy load id An error is entered .
{
free(p);// Remember to free up space when there is an error , Prevent vulnerabilities .
error(1);// Output error message .
list_size--;
*kase=1;
}
else// Insert
{
p->next = head1->next;
head1->next = p;
}
}
return head;
// Returns a new header pointer .
}
list_data * list_remove(list_data *head,struct people a,int *kase)
{
list_size--;
if(head == NULL)
{
error(2);
list_size++;
*kase = 1;
return head;
}
if(head->data.id == a.id)
{
list_data * temp = head;
head=head->next;
free(temp);
}
else
{
list_data * head1 = head;
while((head1->next != NULL) && (head1->next->data.id !=a.id) )// Traverse to the person to delete
{
head1=head1->next;
}
if(head1->next == NULL)// error 2, The deleted character does not exist .
{
error(2);
list_size++;
*kase = 1;
}
else
{
list_data * temp = head1->next;
head1->next=head1->next->next; // Delete operation
free(temp);// Remember to free memory .
}
}
return head;
}
int remove_now(list_data * a)// Delete the current node
{
if(a == NULL)
return 0;
if(a->next==NULL)
{
list_data * temp =a;
a=NULL;
free(temp);
list_size--;
return 1;
}
a->data=a->next->data;
list_data * temp =a->next;
a->next=a->next->next;
free(temp);
list_size--;
return 1;
}
int list_print(list_data * head)
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
list_data *p =head;
if(p == NULL)
{
get_red;
printf("there if no one in the match pool\n");
get_white;
}
else
{
int i=1;
get_yellow;
while(p!=NULL)
{
printf("%3d:id=%2d,name=%s,grade=%5d waiting for %d seconds\n",i,p->data.id,p->data.name,p->data.grade,p->data.wait_time);
p = p->next;
i++;
}
get_white;
}
return 1;
}file 4 menu.h
#include<stdio.h>
//#include"color.h"
#include"solve_error.h"
int menu()// Main menu function
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
printf(" The producer :");
get_yellow;
printf(" Luochunyang \n\n");Sleep(1*20);
get_white; // These things are the color of the font , You can ignore
printf(" The main menu \n");Sleep(1*20);
get_green;
printf(" notes : You can enter 'q'( Add return ) To refresh the page ,( Commands are not case sensitive , Case free )\n");
get_white;
printf("########################################################################################################\n");Sleep(1*20);
printf("##");
get_red;
printf(" function 1: Add or remove users from the matching pool . function 2: Check the matching pool or game pool ");Sleep(1*20);
get_white;
printf("##\n");
printf("## add/remove ‘id’ ‘username’ ‘grade’ fgamewait fgameing(-t)(-av) fpoolsize ##\n");Sleep(1*20);
printf("##");
get_red;
printf(" function 3: End a user's game . function 4: View various histories ");Sleep(1*20);
get_white;
printf("##\n");
printf("## gameover ‘id’ or gameover ‘username’ system-history history ##\n");Sleep(1*20);
printf("##");
get_red;
printf(" function 5: Switch the system information output mode . function 6: view help ");Sleep(1*20);
get_white;
printf("##\n");
printf("## system-change help ##\n");Sleep(1*20);
printf("## Input \"exit\" Exit procedure ##\n");Sleep(1*20);
printf("########################################################################################################\n\n");Sleep(1*20);
printf(" Enter the code and press enter to execute the corresponding request \n"); Sleep(1*20);
return 1;
}
int match_exit()
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
printf(" ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■■ \n");Sleep(1*50);
printf(" ■■■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■ ■ ■ \n");Sleep(1*50);
printf(" ■■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ \n");Sleep(1*50);
printf(" \n\n");
printf(" ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■■ \n");Sleep(1*50);
printf(" ■■■ ■■■■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■ ■ ■ \n");Sleep(1*50);
printf(" ■■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ \n");Sleep(1*50);
printf(" \n\n");Sleep(1*50);
///
printf(" ■ ■ \n");Sleep(1*50);
printf(" ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ \n");Sleep(1*50);
printf(" ■■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■■ \n");Sleep(1*50);
printf(" ■ ■■ \n");Sleep(1*50);
printf(" ■ ■ ■■ \n");Sleep(1*50);
printf(" ■ ■■ ■■■ \n");Sleep(1*50);
printf(" ■■ ■ \n");Sleep(1*50);
printf(" \n\n");
printf(" \n");Sleep(1*50);
printf(" ■■■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■■■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■■■■■■■■■■■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ ■ ■ \n");Sleep(1*50);
printf(" ■ ■ \n");Sleep(1*50);
printf(" \n\n\n");
get_red;
printf(" Although the procedure is over , But your operation history, system history and game pool will still be saved \n They exist history.txt,system_history.txt and game.txt in \n");
get_white;
Sleep(1*1000);
system("pause");
exit(0);
}
int help()
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
FILE * fp;
fp = fopen("readme.txt","r");
char kmp[2000];
while(fscanf(fp,"%[^\n]%*c",kmp)==1)
{
if(!strcmp(kmp," function "))
{
get_red;
puts(kmp);
get_white;
}
else
puts(kmp);
}
puts("");
return 0;
}
int enter()
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
get_red;
printf(" Press enter to enter the system \n");
get_white;
if(!scanf("%*[^\n]%*c"))
{
getchar();
}
return 0;
}file 5 solve_error.h
#include"color.h"
#include<stdio.h>
void error(int n)// Functions that handle errors
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
if(n==1)
{
get_red;
printf("\n error\n Error code :001 heavy id__ Do not add matches repeatedly !\n");
get_white;
}
else if(n==2)
{
get_red;
printf("\n error\n Error code :002 this id User did not match __ Do not exit or start the game repeatedly !\n");
get_white;
}
else if(n==3)
{
get_red;
printf("\n error\n Error code :003 Input format error __ Please enter... In the format above !\n");
get_white;
}
else if(n==4)
{
get_red;
printf("\n error\n Error code :004 This game user... Was not found __ This user has not started a game , Can't end !\n");
get_white;
}
else if(n==5)
{
get_red;
printf("\n error\n Error code :005 Cannot join this user __ This user has started the game , Cannot match again !\n");
get_white;
}
return;
}file 6 sstring.h
// This program needs to use the string processing function
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"match_data.h"
int search_kongge(char a[])// Determine the number of spaces
{
int flag = 0;
int ans = 0;
int i;
for (i = 0; a[i] != '\0'; i++)
{
if (a[i] == ' ' && flag > 0)
ans++;
else
{
flag++;
}
}
return ans;
}
int fengge_one(char get[],char d[])// Split into a blank version
{
sscanf(get,"%s",d);
if(!strcmp(d,"gameover"))
{
sscanf(get,"%*s%s",d);
return 0;
}
else
{
return 1;
}
}
int fengge_three(char get[], char d[], struct people *a)// A version divided into three spaces
{
sscanf(get, "%s%d%s%d", d, &a->id, a->name, &a->grade);
return 1;
}
int string_sourch(char a[], int n)
{
if (n == 1)
{
int i;
for (i = 0; a[i] != '\0'; i++) // First convert case
{
if (a[i] >= 'A' && a[i] <= 'Z')
a[i] = a[i] - 'A' + 'a';
}
if (!strcmp(a, "add"))
return 1;
else if (!strcmp(a, "remove"))
return 2;
else
return 3;
}
else
{
int i;
for (i = 0; a[i] != '\0'; i++) // First convert case
{
if (a[i] >= 'A' && a[i] <= 'Z')
a[i] = a[i] - 'A' + 'a';
}
if (!strcmp(a, "fgamewait"))
return 1;
else if (!strcmp(a, "fgameing"))
return 2;
else if(!strcmp(a,"fpoolsize"))
return 4;
else if(!strcmp(a,"q"))
return 5;
else if(!strcmp(a,"exit"))
return 0;
else if(!strcmp(a,"history"))
return 6;
else if(!strcmp(a,"system-history"))
return 7;
else if(!strcmp(a,"system-change"))
return 8;
else if(!strcmp(a,"help"))
return 9;
else if(!strcmp(a,"fgameing-t"))
return 10;
else if(!strcmp(a,"fgameing-av"))
return 11;
else
return 3;
}
}file 7 game_pool.h
#include<stdio.h>
/*struct gaming
{
struct people a;
struct people b;
int average_score;
};*/
//#include"color.h"
//#include "menu.h"
//#include"match_data.h"
//#include"sstring.h"
#include"match_list.h"
int gameing_print(struct gaming a[],int size)// The default output game time
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
int i;
for(i=0;size>i;i++)
{
printf("%d:\n",i+1);
get_red;
printf("%s",a[i].a.name);
get_white;
printf(" vs ");
get_blue;
printf("%s\n",a[i].b.name);
get_white;
printf("played:%d seconds\n\n",a[i].time_ing);
}
if(size == 0)
{
get_red;
printf("\nthere is no one in gameing\n");
get_white;
}
puts("");
return 0;
}
int gameing_print_a(struct gaming a[],int size)// Output the average score of both sides of the game
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
int i;
for(i=0;size>i;i++)
{
printf("%d:\n",i+1);
get_red;
printf("%s",a[i].a.name);
get_white;
printf(" vs ");
get_blue;
printf("%s\n",a[i].b.name);
get_white;
printf(" The average score is :%d branch \n\n",a[i].average_score);
}
if(size == 0)
{
get_red;
printf("\nthere is no one in gameing\n");
get_white;
}
puts("");
return 0;
}Lord c file User dynamic matching system .c
int list_size=0;// Current list length
//#include "match_list.h"
#include <pthread.h>
#include "game_pool.h"// preservation
//#include"color.h"
int system_history=0;// Global variables , Used to change whether system problems are output to the screen .0 No output .
int id_card[10000000];// Yes id To judge the weight , Prevent duplication id Join the game .
//int name_card[1000000]; // Originally, I wanted to write a string hash to judge the duplicate name , But no more , Because it doesn't make sense , The name can be repeated .
pthread_mutex_t id_m;
struct mutex_match// Defines a linked list with a lock , It facilitates the management of multithreading .// This is the matching pool The matching pool is a dynamic pool , Dynamic storage .
{
list_data * head ;
pthread_mutex_t m;
}q;
struct mutex_game // This is the game pool .// The game pool will have files game.txt in .
{
struct gaming pool[2000];
int cnt;
pthread_mutex_t m;
}gameing;
struct gaming ttemp[2000];// Used to handle the deletion of the game pool // Act as an intermediary
//
pthread_mutex_t file_game_m;// File lock , Prevent multiple processes from adding, deleting, checking and modifying files at the same time .
pthread_mutex_t file_history_m;// Operation history file —— lock .
pthread_mutex_t file_history_system_m;// System operation history file ( for example match 1 and 2 succeed).—— lock .
int add_to_game(struct people a,struct people b)// Join the game pool in progress .
{
pthread_mutex_lock(&gameing.m);
gameing.pool[gameing.cnt].a=a;
gameing.pool[gameing.cnt].b=b;
gameing.pool[gameing.cnt].average_score=((a.grade+b.grade)>>1);
gameing.cnt++;
pthread_mutex_unlock(&gameing.m);
}
/*############# this It's about by horse with Line cheng open beginning #####################*/
int match_server(list_data * head)// Matching program , Traversal match , Select a legal user match , Each match improves the match of each user ( threshold )( The longer the time, the easier it is to match people ( But maybe they were abused )).
{
int a=0;
list_data * i;
for(i = head ; i!=NULL ; i = i->next)
{
i->data.wait_time++;
}
for(i = head ; i!=NULL ; i = i->next)// Traverse to match
{
list_data * j;
for(j = i->next ; j!=NULL ; j = j->next)
{
int t=abs(i->data.grade - j->data.grade);
if((t <= (i->data.wait_time * 5))&&(t <= (j->data.wait_time * 5)))// Enter the game pool if it meets the requirements , And delete... From the matching pool
{
add_to_game(i->data,j->data);
pthread_mutex_lock(&id_m);// to update id
id_card[i->data.id]=1;
id_card[j->data.id]=1;
pthread_mutex_unlock(&id_m);
if(system_history)// Keep history
{
printf("match %d and %d succeed\n",i->data.id,j->data.id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"match %d and %d succeed\n",i->data.id,j->data.id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
if(j->next==NULL)
{
q.head=list_remove(q.head,j->data,&a);
}
else
remove_now(j);
if(i->next==NULL||i==head)
{
q.head=list_remove(q.head,i->data,&a);
}
else
{
remove_now(i);
}
return 0;
}
}
}
return 0;
}
void * solve_task(void *argc)// Solve the matching problem
{
while(1)
{
while (list_size>0)// Match every second
{
pthread_mutex_lock(&q.m);
match_server(q.head);
pthread_mutex_unlock(&q.m);
Sleep(1000);
}
Sleep(1000);
}
}
/*############# this It's about by horse with Line cheng Of junction tail #####################*/
//----------------------------------------------------------------------------------------------------------------------------------------- Split line
/*############# this It's about by swim Play Line cheng Of open beginning #####################*/
int game_file_add()// Refresh every five seconds While the game is in progress
{
pthread_mutex_lock(&gameing.m);
pthread_mutex_lock(&file_game_m);
FILE *fp;
fp = fopen("game.txt","w");
int i;
for(i=0;gameing.cnt>i;i++)
{
gameing.pool[i].time_ing+=5;// Every time you refresh the game time, add 5
fprintf(fp,"%d:\n",i+1);
fprintf(fp," %d %s %d\n",gameing.pool[i].a.id,gameing.pool[i].a.name,gameing.pool[i].a.grade);
fprintf(fp," vs gameing_time:%ds\n",gameing.pool[i].time_ing);
fprintf(fp," %d %s %d\n\n",gameing.pool[i].b.id,gameing.pool[i].b.name,gameing.pool[i].b.grade);
}
fclose(fp);
pthread_mutex_unlock(&gameing.m);
pthread_mutex_unlock(&file_game_m);
return 0;
}
int game_remove_name(char name[])// End a user's game —— Delete by name .
{
pthread_mutex_lock(&gameing.m);
int temp_cnt=0;
int i;
for(i = 0;gameing.cnt>i;i++)
{
if((!strcmp(name,gameing.pool[i].a.name)) || (!strcmp(name,gameing.pool[i].b.name)) )
{
pthread_mutex_lock(&id_m);
id_card[gameing.pool[i].a.id]=0;
id_card[gameing.pool[i].b.id]=0;
pthread_mutex_unlock(&id_m);
continue;
}
ttemp[temp_cnt++]=gameing.pool[i];
}
if(temp_cnt == gameing.cnt)
{
pthread_mutex_unlock(&gameing.m);
if(system_history)// Keep history
{
printf("gameover '%s' false\n",name);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"gameover '%s' false\n",name);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
return 1;
}
else
{
for(i=0;temp_cnt>i;i++)// It was defined before
{
gameing.pool[i]=ttemp[i];
}
gameing.cnt=temp_cnt;
}
pthread_mutex_unlock(&gameing.m);
if(system_history)// Keep history
{
printf("gameover '%s' succeed\n",name);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"gameover '%s' succeed\n",name);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);// Remember to unlock .
return 0;
}
int game_remove_id(int id)// End a user's game —— use id To delete .
{
pthread_mutex_lock(&gameing.m);
int temp_cnt=0;
int i;
for(i = 0;gameing.cnt>i;i++)
{
if((id == gameing.pool[i].a.id) || (id == gameing.pool[i].b.id))
{
pthread_mutex_lock(&id_m);
id_card[gameing.pool[i].a.id]=0;
id_card[gameing.pool[i].b.id]=0;
pthread_mutex_unlock(&id_m);
continue;
}
ttemp[temp_cnt++]=gameing.pool[i];
}
if(temp_cnt == gameing.cnt)
{
pthread_mutex_unlock(&gameing.m);
if(system_history)// Keep history
{
printf("gameover '%d' false\n",id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"gameover '%d' false\n",id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
return 1;
}
else
{
for(i=0;temp_cnt>i;i++)// As defined above
{
gameing.pool[i]=ttemp[i];
}
gameing.cnt=temp_cnt;
}
pthread_mutex_unlock(&gameing.m);
if(system_history)// Keep history
{
printf("gameover '%d' succeed\n",id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"gameover '%d' succeed\n",id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);// Remember to unlock .
return 0;
}
void *solve_gaming(void *argc)// Solve game problems
{
while(1)
{
while(gameing.cnt>0)
{
game_file_add();
Sleep(5000);
}
Sleep(5000);
}
}
/*############# this It's about by swim Play Line cheng Of junction tail #####################*/
/*############# this It's about by calendar The history of writing Pieces of It's about The reason is Of open beginning #####################*/
int print_history() Output history.txt file .
{
pthread_mutex_lock(&file_history_m);
FILE * fp;
int i=1;
fp = fopen("history.txt","r");
char kmp[200];
while(fscanf(fp,"%[^\n]%*c",kmp)==1)
{
printf("%d:",i);
puts(kmp);
i++;
}
puts("");
pthread_mutex_unlock(&file_history_m);
return 0;
}
int print_system_history()/// Output system information ___history_system.txt file
{
pthread_mutex_lock(&file_history_system_m);
FILE * fp;
int i=1;
fp = fopen("history_system.txt","r");
char kmp[200];
while(fscanf(fp,"%[^\n]%*c",kmp)==1)
{
printf("%d:",i);
puts(kmp);
i++;
}
puts("");
pthread_mutex_unlock(&file_history_system_m);
return 0;
}
/*############# this It's about by calendar The history of writing Pieces of It's about The reason is Of junction tail #####################*/
/*############# this It's about by Explain " swim Play pool row order transport Out Of ask topic open beginning #####################*/
int cmp_time(const void *p1,const void *p2)// Time sorting function
{
const struct gaming *a1 = (const struct gaming *)p1;
const struct gaming *a2 = (const struct gaming *)p2;
return a1->time_ing > a2->time_ing;
}
int cmp_average(const void *p1,const void *p2)// Average score sorting function
{
const struct gaming *a1 = (const struct gaming *)p1;
const struct gaming *a2 = (const struct gaming *)p2;
return a1->average_score > a2->average_score;
}
int gameing_print_time(struct gaming a[],int n)// Output function after sorting by time
{
qsort(a,n,sizeof(struct gaming),cmp_time);
gameing_print(a,n);
return 0;
}
int gameing_print_average(struct gaming a[],int n) // Output function after sorting by average score .
{
qsort(a,n,sizeof(struct gaming),cmp_average);
gameing_print_a(a,n);
return 0;
}
/*############# this It's about by Explain " swim Play pool row order transport Out Of ask topic junction tail #####################*/
int main(void)// At the beginning of the main function 、、、、、、、、
{
// Output the tutorial first
help();// It's defined in menu.h
enter();// Block function .. Time to show the user the instructions // It's defined in menu.h
system("cls");
// Officially enter the system
FILE *file_to_zero;// Initialize each file to prevent the last run from remaining .
file_to_zero = fopen("history.txt","w");
fclose(file_to_zero);
file_to_zero = fopen("history_system.txt","w");
fclose(file_to_zero);// initialization history file .
file_to_zero = fopen("game.txt","w");
fclose(file_to_zero);
menu(); // Output menu .
char get[700]; // Read the entire line of input
char doing[200];// Save temporary operation name
struct people a;// Save temporary users .
gameing.cnt=0;
q.head=list_inint(0);// Initialize linked list
pthread_t th1_match;// Define the first thread // Used for matching
pthread_t th2_game;// Define the second thread // Used to update the game pool .
pthread_create(&th1_match,NULL,solve_task,NULL);// Create the first thread ( Matching threads , Match every second )
pthread_create(&th2_game,NULL,solve_gaming,NULL);// Create a second thread ( Game threads , Deal with changes in conditions as the game progresses __ Refresh every five seconds )
while(1==1)// Read user input repeatedly
{
if(!scanf("%[^\n]%*c",get))// Input operation
{
getchar();
continue;
}
pthread_mutex_lock(&file_history_m); // Save operation history .
FILE *history_file_hand;
history_file_hand = fopen("history.txt","a+");
fprintf(history_file_hand,"%s\n",get);
fclose(history_file_hand);
pthread_mutex_unlock(&file_history_m);
/
int kongge=search_kongge(get);// Input is divided into... By spaces 0,1,3 Space form command
if(kongge != 3 && kongge != 0 && kongge !=1)// Neither is error
error(3);
else if(kongge == 3)// Three space command
{
fengge_three(get,doing,&a);// Three space string Split it up .
int temp=0;
temp=string_sourch(doing,1);// Execute the function in the way of number one // To add and remove matches ( The way )
if(temp==3)
error(3);
else if(temp == 1)// Add users
{
int p=0;
pthread_mutex_lock(&q.m);// Lock the list , Prevent simultaneous modification .
pthread_mutex_lock(&id_m);//id Lock it, too .
if(id_card[a.id])
{
p=1;
error(5);
}
else
q.head = list_add(q.head,a,&p);
pthread_mutex_unlock(&q.m);// Release
pthread_mutex_unlock(&id_m);
if(!p) // Process system information , Judge whether the addition is successful
{
pthread_mutex_lock(&id_m);// to update id
id_card[a.id]=1;
pthread_mutex_unlock(&id_m);
if(system_history)
{
printf("add %d succeed\n",a.id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"add %d succeed\n",a.id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
}
else
{
if(system_history)
{
printf("add %d false\n",a.id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"add %d false\n",a.id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
}
///
}
else if(temp == 2)// ditto , Delete user
{
int p=0;
pthread_mutex_lock(&q.m);// Lock the list , Prevent simultaneous modification .
q.head = list_remove(q.head,a,&p);
pthread_mutex_unlock(&q.m);
if(!p)// Process system information , Judge whether the deletion is successful
{
if(system_history)
{
printf("remove %d succeed\n",a.id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"remove %d succeed\n",a.id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
}
else
{
if(system_history)
{
printf("remove %d false\n",a.id);
}
pthread_mutex_lock(&file_history_system_m);
FILE *fp;
fp =fopen("history_system.txt","a+");
fprintf(fp,"remove %d false\n",a.id);
fclose(fp);
pthread_mutex_unlock(&file_history_system_m);
}
///
}
}
else if(kongge == 1)// A space command
{
if(fengge_one(get,doing))// Divide first
{
error(3);
continue;
}
if(doing[0]>='0'&&doing[0]<='9')// Judge the numbers id Or a name
{
int id=0;
int i;
for(i=0;doing[i]!='\0';i++)
{
id=id*10 +(doing[i]-'0');
}
if(game_remove_id(id))
{
error(4);
}
}
else
if(game_remove_name(doing))
{
error(4);
}
}
else if(kongge == 0)// Zero space command
{
int temp=string_sourch(get,2);// Execute in mode two .// Some single command
if(temp ==0)//exit
{
pthread_mutex_lock(&file_game_m);
pthread_mutex_lock(&file_history_m);
pthread_mutex_lock(&file_history_system_m);
match_exit();
pthread_mutex_unlock(&file_game_m);
pthread_mutex_unlock(&file_history_m);
pthread_mutex_unlock(&file_history_system_m);
}
else if(temp==3)// Without this order
error(3);
else if(temp == 1)//fgamewait
{
pthread_mutex_lock(&q.m);// Lock the list , Prevent simultaneous modification .
list_print(q.head);
pthread_mutex_unlock(&q.m);// Release
}
else if(temp == 2)//fgameing
{
pthread_mutex_lock(&gameing.m);
gameing_print(gameing.pool,gameing.cnt);
pthread_mutex_unlock(&gameing.m);
}
else if(temp == 4)//fpoolsize
{
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get the standard output device handle
CONSOLE_SCREEN_BUFFER_INFO csbi; // Define the window buffer information structure
GetConsoleScreenBufferInfo(handle_out, &csbi); // Get window buffer information
printf("now the pool size is: ");
get_red;
printf("%d\n",list_size);
get_white;
}
else if(temp == 5)//q// Refresh
{
system("cls");
menu();
}
else if(temp == 6)//history
{
print_history();
}
else if(temp == 7)//system-history
{
print_system_history();
}
else if(temp == 8)//system-change
{
system_history ^=1;
if(system_history == 1)
printf("change to system_out ( In this mode, all system information will be output in time )\n");
else
{
printf("change to system_in( Turn off system information output )\n");
}
}
else if(temp == 9)//help
{
help();// Direct output help ____ It's defined in menu.h in .
}
else if(temp == 10)//fgameing-t// Output in chronological order from small to large .
{
pthread_mutex_lock(&gameing.m);
gameing_print_time(gameing.pool,gameing.cnt);
pthread_mutex_unlock(&gameing.m);
}
else if(temp == 11)//fgameing-av// Output from small to large according to the average scores of both sides of the competition .
{
pthread_mutex_lock(&gameing.m);
gameing_print_average(gameing.pool,gameing.cnt);
pthread_mutex_unlock(&gameing.m);
}
}
}
return 0;
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \\| |// `.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// The Buddha bless never BUG
// Buddha says :
// Office room in office building , Programmers in the office ;
// Programmers write programs , And program for drinks .
// Drunk only sit on the Internet , Drunken to sleep under the net ;
// Drunk and sober day after day , Online next year after year .
// I wish I could die in the computer room , Don't bow to the boss ;
// Mercedes Benz and BMW , Bus self programmer .
// People laugh at me for being crazy , I laugh at my life ;
// No beautiful girls in the street , Which one belongs to the programmer ?
}Tutorial files readme.txt ( Must run in the same folder as the main function file )
function
1:
add remove
Add users and delete users into the matching pool ( The matching system will dynamically match according to the added users , Each user will only match their own score difference in the first second 5 Opponents within points ,
But their threshold increases every second 5 branch ( Even if it is a game with an opponent who is very different from himself, it is better than no one ) And the second second second may match the difference with yourself 10 Point opponent , And so on ).
add operation .
add+(id)+(user_name)+(user_grade) Use a space between them and press enter to add ,(id And names and scores )
( In fact, it simulates the operations that the front-end user clicks match and then performs -- There is no graphical interface, so I use linux Type )
remove operation .
remove+(id)+(user_name)+(user_grade) Use a space between them and press enter to delete ,(id And names and scores )
( Simulate the operations performed by the front-end user after clicking cancel matching , Be careful , The user may match successfully , If the match is successful, it cannot be cancelled .)
function
2:
fgamewait fgameing(-t)(-av) fpoolsize
Check the matching pool or game pool ( The matching system may match successfully at any time , The successful person will remove the matching pool , So we can check the matching pool at any time ( You can see various values in the pool ))
fgamewait( Enter this command and enter , You can see the details of the users still waiting for matching, that is, all the people in the matching pool )
For example, output
1:id= 1,name=lcy,grade= 1500 waiting for 5 seconds
fgameing(-t)(-av)( Enter this command and enter , You can see the number of users in the game pool , Including their game duration , And this information exists game.txt The end of the program will not disappear )
( You can add suffixes (-t) perhaps (-av)( Respectively means by game " Progress time " And the gamers " Average score " Sort from small to large and output )( for example :(fgameing-t) (fgameing-av))
For example, output
1:
lcy vs hdm
played:5 seconds
fpoolsize( Enter this command and enter , You can see the users waiting for matching, that is, the number of people in the matching pool )
For example, output
now the pool size is: 0
function
3:
gameover 'id' gameover 'name'
End a user's game ( Used to simulate the request sent by the front end of the game to the server )( Users must be in the game to end )
gameover+(id) perhaps gameover+(user_name) ( Use a space between them and press enter to delete )(id and name All right, the effect is the same )
Then that user will be deleted from the game pool .. Of course game.txt He will also be deleted .
function
4:
View various histories
history ( Enter this command and enter , You can see all your history input , He will show it in order ,( Include meaningless commands )( This information exists history.txt In file ))
For example, output
1:add 1 Luochunyang 1500
2:add 2 hdm 1530
3:fgame
4:fgamewait
system-history( Enter this command and enter , You can see all the historical information of the system , He will show it in order ,( Every time the system matches a person or joins a user, it will generate a message )( This information exists history_system.txt In file ))
For example, output
1:add 1 and 2 succeed
function
5:
Switch the system information output mode
system-change( Enter this command and enter , The system output can be output to the terminal screen in time )
( Every time the system matches a person or joins a user, it will generate a message )( After the output is enabled, this information will be displayed on the terminal screen in time )
( Enter again to close ).
function
6:
view help
help( Enter this command and enter This page will be shown again )
Enter a letter 'q' Adding carriage return can refresh the page ( If you find the page too long and uncomfortable ).
above
边栏推荐
- Taro2.* 小程序配置分享微信朋友圈
- kotlin 注解聲明與使用
- 12.UDP协议-bite
- European standard plug en50075 test items
- EasyGBS调用获取实时快照接口时,出现白色方块该如何解决?
- GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
- 请说下redis命令的时间复杂度??(实际问的是redis底层结构)
- Andorid Jetpack Hilt
- MCS: discrete random variable - Hyper geometric distribution
- C#学习二:堆和栈
猜你喜欢

curl: (56) Recv failure: Connection reset by peer

12.udp protocol -bite

CKS CKA ckad change terminal to remote desktop

Excel中构建SQL语句

PostgreSQL source code learning (24) -- transaction log ⑤ - log writing to wal buffer

Stlink troubleshooting

Motion capture system for apple picking robot

12.UDP协议-bite

cmake学习-2

Imgutil image processing tool class, text extraction, image watermarking
随机推荐
BFD原理与配置
File常用工具类, 流相关运用 (记录)
请说下redis命令的时间复杂度??(实际问的是redis底层结构)
[data analysis] five common questions about learning SQL?
Andorid Jetpack Hilt
再也不用担心窗体变形了
Motion capture system for apple picking robot
这是少了什么依赖嘛?FlinkSql打包运行的时候报错,但是本地idea跑的时候是没问题的,求解,谢
mysql XA 分布式事务
Introduction to radar antenna
Classe d'outils commune de fichier, application liée au flux (enregistrement)
Unity C basic review 29 - Generic delegation (p451)
MCS: discrete random variables - geometric distribution
明德扬XILINX-K7-325T/410T核心板数据手册
postgresql源码学习(23)—— 事务日志④-日志组装
微信公告号自动回复使用图灵机器人实现智能回复
中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
JS 会有变量提升和函数提升
11.应用层数据传输格式/端口号-bite
About sql+nosql: newsql database