⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tedit.c

📁 Software Development in C: A Practical Approach to Programming and Design 软件开发:编程与设计(C))——国外经典教材·计
💻 C
字号:
//---------------------------------------------------------
/*
File Name:	TEdit.c
Comments:	This file contains the functions which 
			implement the text editor.
*/
//---------------------------------------------------------




//---------------------------------------------------------
// Include files

#include <stdio.h>
#include <stdlib.h>
#include "TBuffer.h"

// End Include files
//---------------------------------------------------------



//---------------------------------------------------------
// Constants

const int SCREEN_HEIGHT = 25;
const int TEXT_AREA_HEIGHT = 15;

#define SCROLL_LINE_UP_COMMAND 'U'
#define SCROLL_LINE_DOWN_COMMAND 'D'
#define SCROLL_PAGE_UP_COMMAND 'B'
#define SCROLL_PAGE_DOWN_COMMAND 'F'
#define EDIT_TEXT_COMMAND 'E'
#define QUIT_COMMAND 'Q'

// End Constants
//---------------------------------------------------------



//---------------------------------------------------------
// Types

typedef enum 
{
	FALSE = 0,
	TRUE
} boolean;

// End Types
//---------------------------------------------------------



//---------------------------------------------------------
// Prototypes

void InitializeTextBuffer(text_buffer *tBuffer);
void ClearScreen(void);
void DisplayText(const text_buffer * const tBuffer);
void PrintMenu(void);
char GetUserCommand(void);

// End Prototypes
//---------------------------------------------------------




int main()
{
	char userCommand = ' ';
	boolean done;
	int i;/*defined by LZM*/

	/* Some compilers require this to be declared
	static because of its size. */
	static text_buffer textBuffer;

	// Initialize the text buffer.
	InitializeTextBuffer(&textBuffer);

	// While the user does not want to quit...
	for (done=FALSE;!done;)
	{
		// Clear the screen.
		ClearScreen();

		// Display a page of text.
		DisplayText(&textBuffer);

		// Display the menu.
		PrintMenu();

		// Get the user's command.
		userCommand = GetUserCommand();

		switch (userCommand)
		{
			// If the user wants to scroll up one line...
			case SCROLL_LINE_UP_COMMAND:
				// Scroll the buffer up one line.
				TextBufferScrollUp(1,&textBuffer);
			break;

			// If the user wants to scroll up one screenful...
			case SCROLL_PAGE_UP_COMMAND:
				// Scroll the buffer up one screenful.
				// Leave an overlap of one line.
				TextBufferScrollUp(TEXT_AREA_HEIGHT-1,
								   &textBuffer);
			break;

			// If the user wants to scroll down one line...
			case SCROLL_LINE_DOWN_COMMAND:
				// Scroll the buffer down one line.
				TextBufferScrollDown(1,TEXT_AREA_HEIGHT,
								     &textBuffer);
			break;

			// If the user wants to scroll down one screenful...
			case SCROLL_PAGE_DOWN_COMMAND:
				// Scroll the buffer down one screenful.
				TextBufferScrollDown(TEXT_AREA_HEIGHT-1,
								     TEXT_AREA_HEIGHT,
									 &textBuffer);
			break;

			// If the user wants to edit text...
			case EDIT_TEXT_COMMAND:
			{
				int lineNumber;
				char tempString[TBUFFER_ROW_LENGTH];

				// Prompt the user for the line to edit.
				printf("Please input the buffer line number ");
				printf("of the row of text to edit >>>");

				// Get the user's input.
				scanf("%d",&lineNumber);

				// Prompt the user for a line of text.
				printf("Please type in a line of text\n");
				printf(">>>");

				// Get the user's input.
				//scanf("%s",tempString);
				
				//while( _kbhit() )   /*clear the buffer of keyboard*/
				{
					getchar();
				}

				//tempString[0] = '\0';
				for(i=0;i< TBUFFER_ROW_LENGTH;i++)
				{
					tempString[i] = getchar();
					if((tempString[i] == '\n' ) || (i == TBUFFER_ROW_LENGTH - 1))
					{
						tempString[i] = '\0';
						break;
					}
				}

				// Store the new text in the specified row.
				TextBufferSetRow(lineNumber,tempString,
								 &textBuffer);
			}
			break;

			// If the user wants to quit...
			case QUIT_COMMAND:
				// Set the loop control variable to exit.
				done = TRUE;
			break;

			// Else the command is not recognized...
			default:
				// Print an error message.
				printf("Command not recognized. ");
				printf("Press Enter to continue.");
				scanf("%c",&userCommand);
			break;
		}
	}

	return (0);
}



//---------------------------------------------------------
/* 
Function Name:	InitializeTextBuffer
Parameters:
	In:			None.
	Out:		tBuffer - A pointer to the buffer which 
					will be filled by this function.
	In/Out:		None.
Return Values:	None.
Comments:		This function fills the specified text buffer 
				with rows of text of varying length.
*/

void InitializeTextBuffer(text_buffer *tBuffer)
{
	int i,j;
	int randomValue;
	char tempChar;
	char tempString[TBUFFER_ROW_LENGTH];

	// Set the text buffer to a known state.
	TextBufferInitialize(tBuffer);

	// For each row in the buffer...
	for (i=0,tempChar = 'A';i<TBUFFER_LENGTH;i++)
	{
		/* Generate a random number between 5 and the 
		maximum length of a row. This will be the length
		of the string to store in the current row. */
		randomValue = rand()%40;
		if (randomValue < 5) 
		{
			randomValue = 5;
		}
		else if (randomValue>TBUFFER_ROW_LENGTH)
		{
			randomValue = TBUFFER_ROW_LENGTH-1;
		}

		/* Generate a string of text of the calculated 
		length. */
		for (j=0;j<randomValue;j++)
		{
			tempString[j] = tempChar++;
			if (tempChar > 'z')
			{
				tempChar = 'A';
			}
		}

		tempString[j] = '\0';

		// Store the string in the current row.
		TextBufferSetRow(i,tempString,tBuffer);
	}
}

// End InitializeTextBuffer
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	ClearScreen
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function clears the screen by sending newlines.
*/

void ClearScreen(void)
{
	int i;

	// For each line on the screen...
	for (i=0;i<SCREEN_HEIGHT;i++)
	{
		// Print a newline.
		printf("\n");		
	}

}

// End ClearScreen
//---------------------------------------------------------




//---------------------------------------------------------
/* 
Function Name:	DisplayText
Parameters:
	In:			tBuffer - A pointer to the text buffer 
					that will be displayed on the screen.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		This function starts with the current top 
				row in the text buffer and prints 
				successive rows of text from the buffer 
				to the screen. It stops when it reaches 
				the maximum number of rows to be displayed.
*/

void DisplayText(const text_buffer * const tBuffer)
{
	int i;
	char tempString[TBUFFER_ROW_LENGTH];
	int topRow;

	topRow = TextBufferGetTopLine(tBuffer);

	printf("Display  Buffer\n");
	printf("Line     Line\n");
	printf("Number   Number\n");
	// For each row to be displayed...
	for (i=0;i<TEXT_AREA_HEIGHT;i++)
	{
		printf("  %d",i);

		if (i<10)
		{
			printf("         ");
		}
		else
		{
			printf("        ");
		}

		printf("%d",topRow+i);

		if (topRow+i<10)
		{
			printf("    ");
		}
		else
		{
			printf("   ");
		}

		// Get a row of text.
		TextBufferGetRow(topRow+i,tempString,tBuffer);

		// Print it to the screen.
		printf("%s\n",tempString);
	}
}

// End DisplayText
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	PrintMenu
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	None.
Comments:		The PrintMenu function prints the menu of
				text editor commands to the screen.
*/


void PrintMenu(void)
{
	// Print the commands recognized by the text editor.
	printf("Press one of the following keys to issue a ");
	printf("command, then press the Enter key.\n");
	printf("  %c Scroll up one line.\n",
		   SCROLL_LINE_UP_COMMAND);
	printf("  %c Scroll down one line.\n",
		   SCROLL_LINE_DOWN_COMMAND);
	printf("  %c Scroll up one page.\n",
		   SCROLL_PAGE_UP_COMMAND);
	printf("  %c Scroll down one page.\n",
		   SCROLL_PAGE_DOWN_COMMAND);
	printf("  %c Edit text.\n",EDIT_TEXT_COMMAND);
	printf("  %c Quit.\n",QUIT_COMMAND);
	printf("COMMAND>>>");
}

// End PrintMenu
//---------------------------------------------------------



//---------------------------------------------------------
/* 
Function Name:	GetUserCommand
Parameters:
	In:			None.
	Out:		None.
	In/Out:		None.
Return Values:	This function return a single-character
				user command.
Comments:		The GetUserCommand function retrieves a 
				character command from the keyboard. The 
				user must type the character, followed by 
				the Enter key. If the Enter key is not 
				pressed, this function waits until it is.

				After the command is retrieved, there is 
				still a newline character left in the
				keyboard buffer. This function will pull 
				it out and throw it away.
*/

char GetUserCommand(void)
{
	char userInput,dummy;

	// Get the user's command from the keyboard.
	scanf("%c",&userInput);
	userInput = toupper(userInput);	// Convert to upper case

	// Throw away the newline.
	scanf("%c",&dummy);

	// Return the command.
	return (userInput);
}

// End GetUserCommand
//---------------------------------------------------------


// End TEdit.c
//---------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -