Keyboard Converter for 5 Keys

(1) Fetch keyboard input sequence

(2) Normalize key seqeunce so that only key "1" ~ "5" is taken

(3) Decode key and process

(4) Output

/**************************************************************/
/* bobo                                                       */
/* 20230211                                                   */
/* Keyboard Converter for 5-Key Keyboard                      */
/**************************************************************/

#include <stdio.h>

/* Defines */
#define SIZE_KEY_SEQUENCE 100
#define SIZE_CLIP_BOARD   100
#define SIZE_SCREEN       100
#define STATUS_VERSION    VERSION_NORMAL
#define VERSION_DEBUG     0
#define VERSION_NORMAL    1
#define FLAG_INCOMPLETE   0
#define FLAG_COMPLETE     1
#define FLAG_ERROR        2
/* Structures */
/* Function Prototypes*/
int key_sequence_normalize(char*, char*, int);
int key_sequence_decode(char*, char*, char*, int);
int screen_count(char*, int);
/* Globals */

/* To-do List*/
/*
 * (1) structural program that can run
 * (2) implementation
 * (3) standarized input / output
 * (4) error detction and process
 */

int main()
{
	/* Explore the C World */


	/* Variables */
	char in_char_key_sequence[SIZE_KEY_SEQUENCE] = { 0 };
	char inter_char_clipboard[SIZE_CLIP_BOARD] = { 0 };
	char inter_char_screen[SIZE_SCREEN] = { 0 };
	char inter_char_key_sequence_std[SIZE_KEY_SEQUENCE] = { 0 };
	int out_int_num = 0, out_int_size_word_sys = 0;


	/* Input Interface Selection */
#if(STATUS_VERSION == VERSION_DEBUG)
	/* Welcoming Information */
	printf("Welcome to 5-Key Keyboard Conversion Porgam! \n");
	/* System Information */
	out_int_size_word_sys = sizeof(int);
	printf("Word Length of Integer Type of Current System is: %lu \n", out_int_size_word_sys);
#elif(STATUS_VERSION == VERSION_NORMAL)
	/* Standard Input */
#endif


	/* Step 01. Fetch Input */
	fgets(in_char_key_sequence, SIZE_KEY_SEQUENCE, stdin);


	/* Step 02. Process */
	key_sequence_normalize(inter_char_key_sequence_std, in_char_key_sequence, SIZE_KEY_SEQUENCE);
	key_sequence_decode(inter_char_clipboard, inter_char_screen, inter_char_key_sequence_std, \
		SIZE_KEY_SEQUENCE);
	out_int_num = screen_count(inter_char_screen, SIZE_KEY_SEQUENCE);

	/* Step 03. Drive Output */


	/* Output Interface Selection */
#if(STATUS_VERSION == VERSION_DEBUG)
	printf("Number of Characters is: %d", out_int_num);
#elif(STATUS_VERSION == VERSION_NORMAL)
	/*
	 * puts(inter_char_key_sequence_std);
	 * puts(inter_char_screen);
	 * puts(inter_char_clipboard);
	 */
	printf("%d \n", out_int_num);
#endif


	/* Return */
	return 0;
}

int key_sequence_normalize(char* out_sequence, char* in_seqeunce, int in_num_valid)
{
	/* You should know that a space will not be taken */

	/* Variables */
	int index_key_in = 0;
	int index_key_out = 0;

	/* func 01 step 01. look for correct keys */
	for (index_key_in = 0; index_key_in < in_num_valid; index_key_in++) {
		if (in_seqeunce[index_key_in] > '0' && in_seqeunce[index_key_in] < '6') {
			out_sequence[index_key_out] = in_seqeunce[index_key_in];
			index_key_out++;
		}
	}

	return FLAG_COMPLETE;
}

int key_sequence_decode(char* out_clipboard, char* out_screen, char* in_key_sequence, \
	int in_num_valid)
{
	/* Decode normalized key sequence, that is continuous number (1 ~ 5) string */

	/* This function has no external storage, that is it starts over from the   
	 * beginning every time it is called
	 */

	/* Variables */
	int index_key = 0, key_current = 0, ptr_clipboard = 0, ptr_screen = 0;
	int index_char = 0, index_char_2 = 0, num_sel = 0, num_ins = 0, num_rem = 0, num_ext = 0;
	int ptr_screen_sel_stt = 0, ptr_screen_sel_stp = 0, ptr_screen_sel_swap = 0;

	/* func 02 step 01. iterate key elements */
	for (index_key = 0; index_key < in_num_valid; index_key++) {
		/* Determine number of selected characters */
		if (ptr_screen_sel_stp < ptr_screen_sel_stt) {
			ptr_screen_sel_swap = ptr_screen_sel_stt;
			ptr_screen_sel_stt = ptr_screen_sel_stp;
			ptr_screen_sel_stp = ptr_screen_sel_swap;
		}
		else {
			/* Do nothing */
		}
		num_sel = ptr_screen_sel_stp - ptr_screen_sel_stt;

		/* Decode */
		switch (in_key_sequence[index_key]) {
		case '1':
			/* Output character "a" to the screen */
			key_current = 1;
			/* Process */
			if (num_sel > 0) {
				/* Clear */
				for (index_char = 0; index_char < num_sel; index_char++) {
					out_screen[ptr_screen_sel_stt + index_char] = '\0';
				}
				/* Insert */
				num_ins = 1;
				out_screen[ptr_screen_sel_stt] = 'a';
				/* Move */
				num_rem = ptr_screen - ptr_screen_sel_stp;
				for (index_char = 0; index_char < num_rem; index_char++) {
					out_screen[ptr_screen_sel_stt + num_ins + index_char] = out_screen\
						[ptr_screen_sel_stp + index_char];
					out_screen[ptr_screen_sel_stp + index_char] = '\0';
				}
				ptr_screen = ptr_screen - num_sel + num_ins;
				/* Cancel selection */
				ptr_screen_sel_stt = 0;
				ptr_screen_sel_stp = 0;
			}
			else {
				/* Add character at the end of the screen */
				out_screen[ptr_screen] = 'a';
				ptr_screen++;
			}
			break;
		case '2':
			/* Copy */
			key_current = 2;
			if (num_sel > 0) {
				/* Clear previous clipboard content */
				for (index_char = 0; index_char < ptr_clipboard; index_char++) {
					out_clipboard[index_char] = '\0';
				}
				ptr_clipboard = 0;
				/* Add new clipboard content */
				for (index_char = 0; index_char < num_sel; index_char++) {
					out_clipboard[index_char] = out_screen[ptr_screen_sel_stt + index_char];
					ptr_clipboard++;
				}
			}
			else {
				/* Do nothing */
			}
			break;
		case '3':
			/* Cut */
			key_current = 3;
			if (num_sel > 0) {
				/* Clear previous clipboard content */
				for (index_char = 0; index_char < ptr_clipboard; index_char++) {
					out_clipboard[index_char] = '\0';
				}
				ptr_clipboard = 0;
				/* Add new clipboard content */
				for (index_char = 0; index_char < num_sel; index_char++) {
					out_clipboard[index_char] = out_screen[ptr_screen_sel_stt + index_char];
					out_screen[ptr_screen_sel_stt + index_char] = '\0';
					ptr_clipboard++;
					ptr_screen--;
				}
				/* Cancel selection */
				ptr_screen_sel_stt = 0;
				ptr_screen_sel_stp = 0;
			}
			else {
				/* Do nothing */
			}
			break;
		case '4':
			/* Paste */
			key_current = 4;
			/* Clear characters currently selected */
			if (num_sel > 0) {
				/* Clear */
				for (index_char = 0; index_char < num_sel; index_char++) {
					out_screen[ptr_screen_sel_stt + index_char] = '\0';
				}
				/* Insert and move */
				num_ins = ptr_clipboard;
				num_rem = ptr_screen - ptr_screen_sel_stp;
				if (num_ins <= num_sel) {
					/* Inserted no longer than deleted */
					for (index_char = 0; index_char < num_ins; index_char++) {
						out_screen[ptr_screen_sel_stt + index_char] = out_clipboard[index_char];
					}
					for (index_char = 0; index_char < num_rem; index_char++) {
						out_screen[ptr_screen_sel_stt + num_ins + index_char] = out_screen\
							[ptr_screen_sel_stp + index_char];
						ptr_screen--;
					}
				}
				else {
					/* Insterted longer than deleted */
					num_ext = num_ins - num_sel;
					for (index_char = 0; index_char < num_sel; index_char++) {
						out_screen[ptr_screen_sel_stt + index_char] = out_clipboard[index_char];
					}
					for (index_char = 0; index_char < num_ext; index_char++) {
						for (index_char_2 = 0; index_char_2 < num_rem; index_char_2++) {
							out_screen[ptr_screen - index_char_2] = out_screen[ptr_screen - \
								index_char_2 - 1];
						}
						ptr_screen++;
						out_screen[ptr_screen_sel_stp + index_char] = out_clipboard[num_sel + \
							index_char];
					}
				}
				/* Cancel selection */
				ptr_screen_sel_stt = 0;
				ptr_screen_sel_stp = 0;
			}
			else {
				/* Add characters at the end of screen */
				for (index_char = 0; index_char < ptr_clipboard; index_char++) {
					out_screen[ptr_screen] = out_clipboard[index_char];
					ptr_screen++;
				}
			}
			break;
		case '5':
			/* Select all */
			key_current = 5;
			ptr_screen_sel_stt = 0;
			ptr_screen_sel_stp = ptr_screen;
			break;
		default:
			break;
		}
	}

	return FLAG_COMPLETE;
}

int screen_count(char* in_screen, int in_num_valid)
{
	/* Count numbers of characters on screen */

	/* Variables */
	int index_screen = 0;
	int out_num = 0;

	/* Count */
	for (index_screen = 0; index_screen < in_num_valid; index_screen++) {
		if (in_screen[index_screen] == 'a') {
			out_num++;
		}
		else {
			/* Do nothing */
		}
	}
	return out_num;
}

/* End of File */

全部评论
The max number of input keys is limited by "SIZE_KEY_SEQUENCE", which is not flexible. And string operation can be implemented by functions provided according to <string.h>.</string.h>
点赞 回复 分享
发布于 2023-02-12 15:47 湖南
不明觉厉的感jio
点赞 回复 分享
发布于 2023-02-13 09:09 山东
看得我头都大
点赞 回复 分享
发布于 2023-02-13 09:21 山西

相关推荐

贪食滴🐶:你说熟悉扣篮的底层原理,有过隔扣职业球员的实战经验吗
点赞 评论 收藏
分享
评论
15
收藏
分享
牛客网
牛客企业服务