void print_arr(struct arr* arr, int num, CEdit* pEdit, struct print* P)
{
	
	int i;
	CString str_tmp = _T("");
	struct value* p_v = arr->p_v;

	for (i = 0; i < FORMAT_NUM * num; i++)
	{
		pEdit->SetSel(-1);
		pEdit->ReplaceSel(_T(" "));
	}

	while(p_v)
	{
		switch (p_v->type)
		{
			case INT:
				i = *(int*)p_v->p_data;
				str_tmp.Format("%d, ", i);
				pEdit->SetSel(-1);
				pEdit->ReplaceSel(str_tmp);
				
				break;
			case STR:
				str_tmp.Format("%s, ", (CString)((char*)p_v->p_data));
				pEdit->SetSel(-1);
				pEdit->ReplaceSel(str_tmp);
				break;
			case ARR:
				print_arr(arr, num + 1, pEdit, P);
				break;
			case OBJ:
				pEdit->SetSel(-1);
				pEdit->ReplaceSel("\n");
				struct print p_t;
				p_t.p_obj = (struct obj*)p_v->p_data;
				p_t.len = num + 1;
				p_t.pEdit = pEdit;
				print_book(&p_t);
				break;
			default:
				pEdit->SetSel(-1);
				pEdit->ReplaceSel("\n");
				break;
		}
		p_v = p_v->p_next;
	}
}
Ejemplo n.º 2
0
int main(void)
{

    int a,i,details,s=0,j,c=0, len,chRec, records[100],r_count=0,copy;
    book lib[100];
    char str[MAX_STR_LEN],ch,nullstr[20]={"\0"};;
    struct date due[MAX_STR_LEN];


    char *psMenu[] =
    {
                   "Please enter the ISBN of the book you want to add: ",
                   "Please enter the book you want to remove: ",
                   "Please enter the book you want to check out: ",
                   "Please enter the book you want to check in: ",
                   "Please enter the book you want details for: ",
                   "All available books: ",
                   "All checked out books: ",
                   "GoodBye! Thank you for choosing the UTA Library."
    };
    do
    {   printf("\n");
        printf(hLine);
        printf("\nWelcome to the University of Texas at Arlington Library\n");
        printf(hLine);
        printf("\n1 - Add a new book\n2 - Remove a record\n3 - Check out a book\n4 - Check in a book\n");
        printf("5 - Display all book details\n6 - Display all available books\n7 - Display all checked out books\n8 - Exit Program\n");
        printf("\nEnter a selection: ");
        scanf("%d", &i);
        len=sizeof(records);
        copy=0;
        printf("\n");
        switch(i)
        {
            case 1:
                if (r_count<10)
                {
                    printf("\n%s",psMenu[i-1]);
                    scanf("%d", &a);
                    for(i=0;i<r_count;i++)
                    {
                        if (records[i]==a)
                        {
                            printf("\n\nERROR:// This book is already in the library database!//\n\n");
                            copy=1;
                            break;
                        }

                    }
                    if (copy==1)
                    {
                        break;
                    }
                    else


                        records[r_count]=a;
                        lib[r_count].nISBN=a;
                        printf("\nPlease enter the book title: ");

                        scanf(" %[^\n]s ",&lib[r_count].szTitle);
                        printf("\nPlease enter the type of book: ");
                        scanf(" %[^\n]s",&lib[r_count].szType);
                        printf("\nPlease enter the book publisher: ");
                        scanf(" %[^\n]s",&lib[r_count].szPub);
                        printf("\nPlease enter the number of pages in the book: ");
                        scanf("%d",&lib[r_count].nPage);
                        printf("\nPlease enter the book price: $");
                        scanf("%f",&lib[r_count].nPrice);
                        printf("\nPlease enter the year the book was published: ");
                        scanf("%d",&lib[r_count].nPubyear);
                        printf("\nPlease enter 1 if checked out or 0 if available: ");
                        scanf("%d",&lib[r_count].nStatus);
                        if (lib[r_count].nStatus==1)
                        {
                            s=1;
                            printf("\nPlease enter the name of the person who checked this book out: ");
                            scanf(" %[^\n]s",&lib[r_count].szName);
                            printf("\nPlease enter the due date month: ");
                            scanf("%d", &lib[r_count].due.nMonth);
                            printf("\nPlease enter the due date day: ");
                            scanf("%d", &lib[r_count].due.nDay);
                            printf("\nPlease enter the due date year: ");
                            scanf("%d", &lib[r_count].due.nYear);
                            print_book(r_count, lib);
                            printf("\nISBN %d < %s > was successfully added: \n",lib[r_count].nISBN, lib[r_count].szTitle);
                            addbooktofile(r_count, lib, s);
                            r_count++;
                            break;
                        }
                        else
                        {

                            print_book(r_count, lib);
                            printf("\nISBN %d < %s > was successfully added: \n",lib[r_count].nISBN, lib[r_count].szTitle);
                            addbooktofile(r_count, lib, s);
                            //printf("%d",r_count); debugging
                            //printRec(records,r_count,lib);

                            r_count++;
                            break;
                        }



                }
                else
                {
                    printf("\nLibrary book shelf is full please check out or remove a book!\n");
                    break;
                }


            case 2:
                if (r_count>0)
                {
                    printRec(records,r_count,lib);
                    printf("\n%s",psMenu[i-1]);
                    scanf("%d", &a);
                    chRec=(a-1);
                    printf("\n%s has been removed from the UTA Library Database\n\n ",lib[chRec].szTitle);

                    for(j=a-1;j<r_count;j++)
                    {
                        if (j=r_count-1)
                        {
                            records[j]=' ';
                            lib[j].nISBN=nullstr;
                            strcpy(lib[j].szTitle,nullstr);
                            strcpy(lib[j].szType,nullstr);
                            strcpy(lib[j].szPub,nullstr);
                            lib[j].nPage=nullstr;
                            lib[j].nPrice=0;
                            lib[j].nPubyear=nullstr;

                            if (lib[j].nStatus==1)
                            {
                                lib[j].nStatus=nullstr;
                                strcpy(lib[j].szName,nullstr);
                                lib[j].due.nMonth=nullstr;
                                lib[j].due.nDay=nullstr;
                                lib[j].due.nYear=nullstr;

                            }
                            else
                            {
                                lib[j].nStatus=nullstr;

                            }
                        }
                        else
                        {
                            records[j]=records[j+1];
                            strcpy(lib[j].nISBN,lib[j+1].nISBN);
                            strcpy(lib[j].szTitle,lib[j+1].szTitle);
                            strcpy(lib[j].szType,lib[j+1].szType);
                            strcpy(lib[j].szPub,lib[j+1].szPub);
                            lib[j].nPage=lib[j+1].nPage;
                            lib[j].nPrice=lib[j+1].nPrice;
                            lib[j].nPubyear=lib[j+1].nPubyear;

                            if (lib[j].nStatus==1)
                            {


                                lib[j].nStatus=lib[j+1].nStatus;
                                strcpy(lib[j].szName,lib[j+1].szName);
                                lib[j].due.nMonth=lib[j+1].due.nMonth;
                                lib[j].due.nDay=lib[j+1].due.nDay;
                                lib[j].due.nYear=lib[j+1].due.nYear;

                            }
                            else
                            {
                                lib[j].nStatus=lib[j+1].nStatus;

                            }
                        }
                    }
                    r_count--;
                    printf("\n");
                    printf("TESTER % d", j);
                    break;



                }

                else
                {
                    printf("\nRecords are empty please add a record!\n");
                    break;
                }
            case 3:
                if (r_count>0)
                {
                    printRec(records,r_count,lib);
                    printf("\n%s",psMenu[i-1]);
                    scanf("%d", &a);
                    printf("Please enter your name: ");
                    scanf(" %s",&lib[a-1].szName);
                    lib[a-1].nStatus=1;
                    printf("\nPlease enter todays date: ");
                    printf("Month: ");
                    scanf("%d",&lib[a-1].due.nMonth);
                    printf("Day: ");
                    scanf("%d",&lib[a-1].due.nDay);
                    printf("Year: ");
                    scanf("%d",&lib[a-1].due.nYear);
                    /*if (lib[a-1].due.nDay>19)
                        lib[a-1].due.nDay=01;
                        lib[a-1].due.nMonth=lib[a-1].due.nMonth+1;

                    else
                        lib[a-1].due.nDay+=10;*/


                    printf("\n");
                    printf("\nYour due date for %s is : %d/%d/%d\n\n", lib[a-1].szTitle, lib[a-1].due.nMonth, lib[a-1].due.nDay, lib[a-1].due.nYear);
                    printRec(records,r_count,lib);
                    break;
                }
                else
                {
                    printf("\nRecords are empty please add a record!\n");
                    break;
                }
            case 4:
                if (r_count>0)
                {
                    printcheckedout(records,r_count,lib);
                    printf("\n%s",psMenu[i-1]);
                    scanf("%d", &a);
                    printf("Please enter your name: ");
                    strcpy(lib[a-1].szName,nullstr);
                    lib[a-1].nStatus=0;
                    lib[a-1].due.nMonth=nullstr;
                    lib[a-1].due.nDay=nullstr;
                    lib[a-1].due.nYear=nullstr;
                    printf("\nYour book %s has been checked in successfully.\n\n", lib[a-1].szTitle);
                    printcheckedin(records,r_count,lib);
                    break;
                }
                else
                {
                    printf("\nRecords are empty please add a record!\n");
                    break;
                }
            case 5:
                printRec(records,r_count,lib);
                printf("\n%s",psMenu[i-1]);
                scanf("%d",&details);
                printf("\n\n");
                print_book(details-1, lib);
                break;

            case 6:
                printf("\n%s\n\n",psMenu[i-1]);
                printcheckedin(records,r_count,lib);
                break;
            case 7:
                printf("\n%s\n\n",psMenu[i-1]);
                printcheckedout(records,r_count,lib);
                break;

            case 8:

                printf("\n%s\n\n",psMenu[i-1]);
                break;
            default:
                printf("\n\nError:// Please enter a valid selection 1-8: //\n\n");
        }

    }
    while (i!=8);

    return 0;
}
DWORD WINAPI print_book(void *P)
{
	struct obj* obj = ((struct print*)P)->p_obj;
	int num = ((struct print*)P)->len;
	CEdit * pEdit = ((struct print*)P)->pEdit;
	struct print* print_q = (struct print*)P;

	int i;
	struct key* key = obj->p_k;
	CString str_tmp = _T("");

	while(key)
	{
		for (i = 0; i < FORMAT_NUM * num; i++)
		{
			pEdit->SetSel(-1);
			pEdit->ReplaceSel(" ");
		}

		str_tmp.Format("%s:", (CString)(key->p_key));
		pEdit->SetSel(-1);
		pEdit->ReplaceSel(str_tmp);

		switch (key->p_v->type)
		{
			case INT:
				i = *(int*)(key->p_v->p_data);
				str_tmp.Format("%d\n", i);
				pEdit->SetSel(-1);
				pEdit->ReplaceSel(str_tmp);
				break;
			case STR:
				((char*)key->p_v->p_data)[STRSIZE - 1] = 0;
				str_tmp.Format("%s\n", (CString)((char*)key->p_v->p_data));
				pEdit->SetSel(-1);
				pEdit->ReplaceSel(str_tmp);
				break;
			case ARR:
				pEdit->SetSel(-1);
				pEdit->ReplaceSel("\n");
				print_arr((struct arr*)key->p_v->p_data, num + 1, pEdit, (struct print*)P);
				pEdit->SetSel(-1);
				pEdit->ReplaceSel("\n");
				break;
			case OBJ:
				pEdit->SetSel(-1);
				pEdit->ReplaceSel("\n");
				struct print p_t1;
				p_t1.p_obj = (struct obj*)key->p_v->p_data;
				p_t1.len = num + 1;
				p_t1.pEdit = pEdit;
				print_book(&p_t1);
				break;
			default:
				break;
		}
		key = key->p_next;
	}	

	return 1;
}	
/* Диалог поиска */
void find_dialog(BookDB* db) {
	# define FIELD(s) if(!strcmp(#s, field)) 
	char field[MAX_STRING_LENGTH];
	output("По какому полю будем искать?\n");
	printf("\t%s\n", "id - номер книги в каталоге");
	printf("\t%s\n", "title - название");
	printf("\t%s\n", "author - имя автора");
	printf("\t%s\n", "ganre - жанр");
	input("%s", field);
	getc(stdin);
	
	FIELD(id) {
		int id;
		output("Введите номер книги\n<< ");
		input("%d", &id);
		getc(stdin);
		Book* book = get_by_id(db, id);
		if (book) {
			print_book(book);
		} else {
			output("Книги с таким номером не найдено!\n");
		}
		return;
	}
	
	Book *book, *end;
	
	FIELD(title) {
		output("Название книги\n<< ");
		fgets(field, MAX_STRING_LENGTH, stdin);
		delns(field);
		for (book = db->books, end = db->books + db->count; book < end; book++) {
			if (!(strcmp(field, book->title)))
				print_book(book);
		}
		return;
	}
	
	FIELD(ganre) {
		output("Жанр книги\n<< ");
		fgets(field, MAX_STRING_LENGTH, stdin);
		delns(field);
		for (book = db->books, end = db->books + db->count; book < end; book++) {
			if (!(strcmp(field, book->ganre)))
				print_book(book);
		}
		return;
	}
	
	FIELD(author) {
		output("Автор книги\n<< ");
		fgets(field, MAX_STRING_LENGTH, stdin);
		delns(field);
		for (book = db->books, end = db->books + db->count; book < end; book++) {
			if (!(strcmp(field, book->author)))
				print_book(book);
		}
		return;
	}
	
	printf("!! Неизвестная поле для поиска \"%s\"\n", field);
	# undef FIELD
}
/* Распечатать содержимое БД */
void list_db(BookDB* db) {
	Book *book, *end;
	for (book = db->books, end = db->books + db->count; book < end; book++) {
		print_book(book);
	}
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
	{
	precompute();
#ifdef USE_HASH
	hash_init();
#endif
	// load_endgames();
//	load_bitbases();
	if (argc == 2 && !strcmp(argv[1], "nobook"))
		{
		printf("Skipping opening book\n");
		}
	else if (argc == 2 && !strcmp(argv[1], "makebook"))
		{
		load_opening_book(1);
		opening_book_grow(0);
		return 0;
		}
	else if (argc == 2 && !strcmp(argv[1], "showbook"))
		{
		load_opening_book(1);
		opening_book_grow(1);
		return 0;
		}
	else
		load_opening_book(2);
	new_game();

	int ply;

	int i, j, k;

	FILE * input;
	FILE * output;

	char command[80] = "";
	char buffer[4100] = "";


/*	if (argc == 2 && !strcmp(argv[1], "files"))
		{
		printf("Using files for I/O\n");
		input = fopen("input.txt", "r+");
		output = fopen("output.txt", "w");
		}
	else */
		{
		input = stdin;
		output = stdout;
		}

	if (argc == 2 && !strcmp(argv[1], "bitbases"))
		{
		bitbase_report();
		return 0;
		}

	if (argc == 7 && !strcmp(argv[1], "position"))
		{
		i = bitbase_test(
			atoi(argv[2])
			,atoi(argv[3])
			,atoi(argv[4])
			,atoi(argv[5])
			,atoi(argv[6])
			);
		printf("%s\n", i ? "is a win for black" : "not a win for black");

		int pieces[MAX_BDB_PIECES];
		int total_pieces = 0;

		for (i = 0; i < atoi(argv[2]); ++i) pieces[total_pieces++] = 0;
		for (i = 0; i < atoi(argv[3]); ++i) pieces[total_pieces++] = 1;
		for (i = 0; i < atoi(argv[4]); ++i) pieces[total_pieces++] = 2;
		for (i = 0; i < atoi(argv[5]); ++i) pieces[total_pieces++] = 3;

		position_decode(atoi(argv[6]), pieces, total_pieces);

		print_board(0);

		return 0;
		}

	if (argc == 2 && !strcmp(argv[1], "sanity"))
		{
		sanity_checks();
		memory_report();
		return 0;
		}

	if (argc == 2 && !strcmp(argv[1], "perft"))
		{
		iterative_performance_test();
#ifdef SORT_STATISTIC
	for (i = 0; i < 5; ++i)
		printf("Returns at sort level %d: %d\n"
			, i
			, sort_statistic[i]);
#endif
		return 0;
		}

	if (argc >= 4 && !strcmp(argv[1], "pdn"))
		{
		ply = 0;

		FILE * pdn;

		pdn = fopen(argv[3], "a+");
		if (!pdn)
			{
			printf("error: fopen \"%s\" for append/create\n", argv[3]);
			// printf("err: %d - %s\n", errno, strerror(errno));
			// getcwd(buffer, sizeof(buffer));
			// printf("pwd: %s\n", buffer);
			return 1;
			}
		while (!feof(pdn)) // fixme: this loader sux...
			{
			if (!fgets(buffer, 4096, pdn))
				break;

			//if (!buffer || feof(pdn))
			//	break;

			i = 0;
			while (j = load_move_from_pdn(buffer, command, &i))
				{
				generate_moves(0, 0);
				k = find_move(0, command);
				if (k < 0 || k >= move_counter[0])
					{
					fprintf(output, "error invalid move from pdn (%s)\n"
						, command);
					for (k = 0; k < move_counter[0]; ++k)
						{
						fprintf(output, "move %d ",k);
						print_move(&moves[0][k], 0);
						fprintf(output, "\n");
						}
					}
				else
					{
					++ply;
					//fprintf(output, "executing move at index %d: ", k);
					if (!strcmp(argv[2], "show") || !strcmp(argv[2], "step"))
						{
						if (ply % 2 == 1)
							printf("%3d. ", (ply+1) / 2);
						print_move(&moves[0][k], 0);
						if (ply % 2 == 0)
							{
							printf("\n");
							//print_board(0);
							}
						else
							printf(" ");
						if (!strcmp(argv[2], "step") && ply % 2 == 0)
							{
							print_board(0);
							getc(stdin);
							}
						}
					//fprintf(output, "\n");
					move_do(&moves[0][k], 0);
					move_list_push(&moves[0][k]);
					}
				}
			if (feof(pdn))
				break;
			}
		fclose(pdn);

		if (!strcmp(argv[2], "play"))
			{
			pdn = fopen(argv[3], "a");
			if (!pdn)
				{
				printf("error: fopen \"%s\" for append\n", argv[3]);
				return 1;
				}

			if (argc >= 5 && strcmp(argv[4], "hint") && strcmp(argv[4], "move"))
				{
				generate_moves(0, 0);
				i = find_move(0, argv[4]);
				if (i < 0 || i >= move_counter[0])
					{
					print_moves(0);
					i = -1;
					}
				else
					{
					move_do(&moves[0][i], 0);
					move_list_push(&moves[0][i]);
					fprintf(pdn, "%s ", argv[4]);
					if (moves[0][i].same_player_square != -1)
						i = -1;
					}
				}

			if (i != -1)
				{
				if (argc >= 6)
					strength = atoi(argv[5]);
				if (argc >= 7)
					thinking_time = atoi(argv[6]);

				if (strength == 1)
					thinking_time = 1;

				if (!strcmp(argv[4], "hint"))
					bestmove(0, 0);
				else
					while (bestmove(pdn, 1));
				}

			fclose(pdn);
			fprintf(output, "boardok ");
			for (i = 0; i < 64; ++i)
				fprintf(output, "%d", board[i]);
			fprintf(output, " %d", state.tactical_square);
			fprintf(output, " %d", state.side_to_move);
			fprintf(output, " %d", state.this_piece_must_take);
			fprintf(output, "\n");
			}
		else
			{
			print_board(0);
//debug_moves = 1;
			generate_moves(0, 0);
			if (!strcmp(argv[2], "show"))
				print_moves(0);
			}

		if (!strcmp(argv[2], "load"))
			{
			printf("PDN loaded\n");
			command[0] = 0;
			buffer[0] = 0;
			}
		else
			return 0;
		}

	for (;;)
		{
		fscanf(input, "%20s", command);

		if (parse(command, "quit"))
			break;
		else if (parse(command, "gnt"))
			fprintf(output, "id name GNTredux\nid author QuakePhil\ngntok\n");
		else if (parse(command, "new"))
			new_game();
		else if (parse(command, "flipside"))
			state.side_to_move ^= 1;
		else if (parse(command, "flipkings"))
			{
			for (i = 0; i < 64; ++i) if (initial_board[i] != 4)
				{
				if (initial_board[i] < 2)
					initial_board[i] += 2;
				else
					initial_board[i] -= 2;
				}
			new_game();
			}
		else if (parse(command, "flipboardonly"))
			{ // fixme: need to re-do materials here?
			int temp[64];
			int flip_piece[5] = {1,0,3,2,4};
			j = 64;
			for (i = 0; i < 64; ++i)
				temp[--j] = flip_piece[board[i]];
			for (i = 0; i < 64; ++i)
				board[i] = temp[i];
			state.side_to_move ^= 1;
			}
		else if (parse(command, "rand"))
			rand_board();
		else if (parse(command, "think"))
			{
			bestmove(0, 0);
			}
		else if (parse(command, "go"))
			{
			bestmove(0, 1);
			}
		else if (parse(command, "perft"))
			{
			iterative_performance_test();
			}
		else if (parse(command, "ping"))
			{
			fprintf(output, "pong %.6lf\n", timer());
			}
		else if (parse(command, "pboard"))
			print_board(0);
		else if (parse(command, "board"))
			{
			fprintf(output, "boardok ");
			for (i = 0; i < 64; ++i)
				fprintf(output, "%d", board[i]);
			fprintf(output, " %d", state.tactical_square);
			fprintf(output, " %d", state.side_to_move);
			fprintf(output, " %d", state.this_piece_must_take);
			fprintf(output, "\n");
			}
		else if (parse(command, "time"))
			{
			fscanf(input, "%d", &thinking_time);
			printf("Think time limit set to: %d seconds\n", thinking_time);
			}
		else if (parse(command, "pdn"))
			{
			fscanf(input, "%20s", buffer);
			generate_moves(0, 0);
			i = find_move(0, buffer);
			if (i < 0 || i >= move_counter[0])
				fprintf(output, "error invalid move\n");
			else
				{
				move_do(&moves[0][i], 0);
				move_list_push(&moves[0][i]);
				//fprintf(output, "executing move at index %d: ", i);
				//print_move(&moves[0][i], 0);
				//fprintf(output, "\n");
				fprintf(output, "done\n");
				}
			}
		else if (parse(command, "do") || parse(command, "toggle"))
			{
			fscanf(input, "%d", &i);
			fprintf(output, "executing move %d\n", i);
			generate_moves(0, 0);
			if (i < 0)
				fprintf(output, "error move can't be negative\n");
			else if (i >= move_counter[0])
				fprintf(output, "error no such move\n");
			else
				{
				move_do(&moves[0][i], 0);
				move_list_push(&moves[0][i]);

				if (parse(command, "toggle"))
					{
					print_board(0);
					fprintf(output, "undoing move %d\n", i);
					move_undo(&moves[0][i], 0);
					print_board(0);
					}
				}
			fprintf(output, "done\n");
			}
		else if (parse(command, "list"))
			{
			print_move_list();
			}
		else if (parse(command, "pbook"))
			{
			print_book();
			}
		else if (parse(command, "moves"))
			{
			generate_moves(0, 0);
			for (i = 0; i < move_counter[0]; ++i)
				{
				fprintf(output, "move %d ",i);
				print_move(&moves[0][i], 1);
				fprintf(output, "\n");
				}
			fprintf(output, "movesok\n");
			}
		else // if (!strcmp(command, "help"))
			{
			// fprintf(output, "?\n");
			}

		fflush(output);
		}

#ifdef HASH_STATISTIC
	printf("Hash saves:      %d\n", hash_saves);
	printf("Hash collisions: %d\n", hash_collisions);
	printf("Hash probes:     %d\n", hash_probes);
	printf("Hash hits:       %d\n", hash_hits);
#endif

#ifdef SORT_STATISTIC
	for (i = 0; i < 5; ++i)
		printf("Returns at sort level %d: %d\n"
			, i
			, sort_statistic[i]);
#endif

	fclose(output);
	fclose(input);

	return 0;
	}