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

📄 main.c

📁 一个ascii 码值计算原代码的例子
💻 C
📖 第 1 页 / 共 4 页
字号:
	COORD coord = {0};



	// limits
	if ( (x >= 0) && (x <= 78) && (y >= 0) && (y <= 24) )
	{
		coord.X = (short) x;
		coord.Y = (short) y;
		SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
	}
	else
	{
		if ( !((x >= 0) && (x <= 78)) )
		{
			ErrorMsg ("Error occured in function GotoXY (file \"main.c\").   Value x is not between 0 and 78 (x = %02d).", x);
		}
		if ( !((y >= 0) && (y <= 24)) )
		{
			ErrorMsg ("Error occured in function GotoXY (file \"main.c\").   Value y is not between 0 and 24 (y = %02d).", y);
		}
	}
}
/*
 * Calculate ASCII table for certain codepage and print it on the screen
 */
void ASCII_Table (unsigned int CodePage)
{
	short i = 0, j = 0, SpaceBetweenNumbers = 0;

	// set codepage for ASCII table
	SetConsoleCP (CodePage);
	SetConsoleOutputCP (CodePage);
	// print it on the screen
	for (i = 0, SpaceBetweenNumbers = 0; i < 256; i++, SpaceBetweenNumbers += 2)
	{
		if (i % 16 == 0) { SpaceBetweenNumbers = 0; j++; }
		Print (hX + SpaceBetweenNumbers, PRINT_ASCII_Y + j, BACK_LIGHTGREY, "%c", i);
	}
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    TimeAndDate
 * Function version: 1.0
 * Input parameters: none
 * Return value:     char *
 * Description:      gets current time and date
 * Example:          char *GetTimeAndDate = NULL;
 *                   if ((GetTimeAndDate = (char *) malloc (sizeof (char) * 80)) != NULL
 *                   {
 *						sprintf (GetTimeAndDate, TimeAndDate);
 *                      free (GetTimeAndDate);
 *                      GetTimeAndDate = NULL;
 *                   } else ErrorMsg ("Error occured");
 * Limits:           none
 * Warnings:         none
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
char *TimeAndDate ()
{
	char *TD = NULL;
	time_t Now = 0;



	if ((TD = (char *) malloc (sizeof (char) * 80)) != NULL)
	{
		Now = time (NULL);
		// get time and date
		strftime (TD, strlen (TD) + 1, "%x %X", localtime (&Now));
	}
	else
	{
		ErrorMsg ("Error occured while allocating space for TD variable in function TimeAndDate (file \"main.c\").");
	}
	return TD;
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    ClrScr
 * Function version: 1.0
 * Input parameters: - x (short)
 *                   - y (short)
 *                   - EraseLength (short)
 *                   - NoOfLines (short)
 *                   - EraseColor (WORD)
 *                   - ErasePattern (char)
 * Return value:     none
 * Description:      clearing the specified region on the screen
 * Example:          ClrScr (0, 0, 80, 25, FRONT_LIGHTGREY, ' ');
 * Limits:           0 <= x <= 79
 *                   0 <= y <= 24
 *                   x+EraseLength <= 80
 *                   y+NoOfLines <= 25
 * Warnings:         Read the GotoXY function, section Warnings, for more details.
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
void ClrScr (short x, short y, short EraseLength, short NoOfLines, WORD EraseColor, char ErasePattern)
{
	short i	= 0, j = 0;
	char Erase[26][81] = {0};



	// limits
	if ( (x >= 0) && (x <= 79) && (x+EraseLength <= 80) &&
		 (y >= 0) && (y <= 24) && (y+NoOfLines <= 25) )
	{
		for	(i = 0; i < NoOfLines; i++)
		{
			for	(j = 0; j < EraseLength; j++)
			{
				Erase[i][j] = ErasePattern;
			}
			Erase[i][j] = '\0';
			Print (x, y+i, EraseColor, "%s", Erase);
		}
	}
	else
	{
		if ( !((x >= 0) && (x <= 79)) )
		{
			ErrorMsg ("Error occured in function ClrScr (file \"main.c\").   Value x is not between 0 and 79 (x = %02d)", x);
		}
		if ( !((y >= 0) && (y <= 24)) )
		{
			ErrorMsg ("Error occured in function ClrScr (file \"main.c\").   Value y is not between 0 and 24 (y = %02d)", y);
		}
		if ( !(x+EraseLength <= 80) )
		{
			ErrorMsg ("Error occured in function ClrScr (file \"main.c\").   Value x+EraseLength is greater than 80              (x+EraseLength = %02d)", x+EraseLength);
		}
		if ( !(y+NoOfLines <= 25) )
		{
			ErrorMsg ("Error occured in function ClrScr (file \"main.c\").   Value y+NoOfLines is greater than 25                (y+NoOfLines = %02d)", y+NoOfLines);
		}
	}
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    SetColor
 * Function version: 1.0
 * Input parameters: - Color (WORD)
 * Return value:     BOOL
 * Description:      setting the console color
 * Example:          Color (FRONT_WHITE | BACK_LIGHTGREY);
 *                   You'll find color names in the header file color.h
 * Limits:           none
 * Warnings:         none
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
BOOL SetColor (WORD Color)
{
	return SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), Color);
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    ConsoleTitle
 * Function version: 1.0
 * Input parameters: - String (char *)
 *                   - ...
 * Return value:     none
 * Description:      setting the console title
 * Example:          ConsoleTitle ("MyTitle");
 * Limits:           none
 * Warnings:         none
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
void ConsoleTitle (char *String, ...)
{
	va_list List;
	char FormattedString[80] = {0};


	
    va_start (List, String);
		vsprintf (FormattedString, String, List);
	va_end (List);
	SetConsoleTitle (FormattedString);
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    Print
 * Function version: 1.0
 * Input parameters: - x      (short)
 *                   - y      (short)
 *                   - Color  (WORD)
 *                   - String (char *)
 *                   - ...
 * Return value:     none
 * Description:      prints text on the screen
 * Example:          Print (0, 0, FRONT_LIGHTGREY, "MyText");
 * Limits:           none
 * Warnings:         none
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
void Print (short x, short y, WORD Color, char *String, ...)
{
	COORD   screenPosition = {0};
    short   i = 0, FormattedStringLength = 0;
    va_list List;
	char    FormattedString[100] = {0};
	HANDLE hOutput;
	DWORD  NoOfCharsWritten = 0, NoOfAttrsWritten = 0;

	

    va_start (List, String);
		vsprintf (FormattedString, String, List);
		FormattedStringLength = (short) strlen (FormattedString);
	va_end (List);
	if ( (x >= 0) && (x <= 79) && (y >= 0) && (x+FormattedStringLength <= 80) && (y <= 25) )
	{
		hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
		for (i = 0; i < FormattedStringLength; i++)
		{
			screenPosition.X = (short) (x + i);
			screenPosition.Y = (short) y;
			WriteConsoleOutputCharacter (hOutput, &FormattedString[i], 1, screenPosition, &NoOfCharsWritten);
			WriteConsoleOutputAttribute (hOutput, &Color, 1, screenPosition, &NoOfAttrsWritten);
		}
		if (NoOfAttrsWritten != NoOfCharsWritten)
		{
			ErrorMsg ("Error occured while writting characters and         attributes on the screen in function Print (file    \"main.c\"). NoOfCharsWritten = %02d ; NoOfAttrsWritten = %02d.", NoOfCharsWritten, NoOfAttrsWritten);
		}
	}
	else
	{
		if ( !(x >= 0 && x <= 79) )
		{
			ErrorMsg ("Error occured in function Print (file \"main.c\").    Value x is not between 0 and 79 (x = %02d).", x);
		}
		if ( !(y >= 0 && y <= 24)  )
		{
			ErrorMsg ("Error occured in function Print (file \"main.c\").    Value y is not between 0 and 24 (y = %02d).", y);
		}
		if ( !(x+FormattedStringLength <= 80) )
		{
			ErrorMsg ("Error occured in function Print (file \"main.c\").    Value x+FormattedStringLength is over 80            (x+FormattedStringLength = %02d).", x+FormattedStringLength);
		}
	}
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    ShowScreenCursor
 * Function version: 1.0
 * Input parameters: - Show (BOOL)
 * Return value:     none
 * Description:      setting the console screen cursor
 * Example:          ShowScreenCursor (TRUE); // this will turn on the screen cursor
 * Limits:           none
 * Warnings:         none
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
BOOL ShowScreenCursor (BOOL Show)
{
	CONSOLE_CURSOR_INFO cci = {0};
	BOOL StatusCCI = 0;



	if (Show) // show screen cursor
	{
		cci.dwSize   = 30;
		cci.bVisible = 1;
		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);		
	}
	else // hide screen cursor
	{
		cci.dwSize   = 100;
		cci.bVisible = 0;
		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
	}
	if (!StatusCCI)
	{
		ErrorMsg ("Error occured in function ShowScreenCursor (file    \"main.c\"). Error code number = %u.", GetLastError ());
	}
	return StatusCCI;
}
/* !! DO NOT REMOVE THIS COMMENT !!
 *
 * Function name:    ErrorMsg
 * Function version: 1.0
 * Input parameters: - Message (char *)
 *                   - ...
 * Return value:     none
 * Description:      displays error message
 * Example:          ErrorMsg ("Error No.: %d", 5);
 * Limits:           dbErrMsg->Height+10+MsgCharY-1 < 25 --> look at the code for more information
 * Warnings:         programmer must properly format the text, otherwise some characters might get lost :(
 *
 * Updates:
 *   - (!! here you write the updates - do not remove this line !!)
 *
 */
void ErrorMsg (char *Message, ...)
{
	// input variables
	HANDLE Stdin = 0;
	INPUT_RECORD ir = {0};
	DWORD Read = 0;
	// component variables
	DIALOGBOX_COMPONENT *dbErrMsg = NULL;
	// common variables
	short i = 0, j = 0; // counters
	short MsgCharY = 0, MsgCharX2 = 0, MsgCharX1 = 0, MsgCharCount = 0; // variables used for formatting the text
	short HelpIsDisplayed = 0; // flag which becomes true if help text is displayed in the status line
	short xItemNameLength = 0, MsgLength = 0;
	va_list List;
	char FormattedMessage[1000] = {0};
	// variables for menu buttons (Close, Save To File and Exit)
	short ErrMsgDialogLoop = 0, xStartMenu[ERRMSG_MENU_NO_OF_ITEMS] = {0}, ErrMsgMenuValue = ERRMSG_MENU_DEFAULT_SELECTED;
	short xEndMenu[ERRMSG_MENU_NO_OF_ITEMS] = {0}, yStartMenu[ERRMSG_MENU_NO_OF_ITEMS] = {0};
	short WriteMenu = 0, MarkMenu = 0, MenuIndex = 0, MenuClicked = 0;
	short MenuTempIndex = ERRMSG_MENU_DEFAULT_SELECTED, MenuSelected = 0;
	char *ErrMsgMenuItems[ERRMSG_MENU_NO_OF_ITEMS] = {0}, *ErrMsgMenuHelp[ERRMSG_MENU_NO_OF_ITEMS] = {0};
	// options
	WORD ItemColor = 0, ItemHighLightColor = 0, DisabledItemColor = 0, DisabledItemHighLightColor = 0;
	WORD HelpColor = 0;
	short DisabledItem[3] = {0}, ShowHelp = 0;
	// Save To File variables
	FILE *FileSave = NULL;
	// time and date
	char *DateAndTime = NULL;



	// reload menu item colors
	ItemColor                  = FRONT_LIGHTGREY;
	ItemHighLightColor         = FRONT_WHITE;
	DisabledItemColor          = FRONT_DARKGREEN;
	DisabledItemHighLightColor = FRONT_LIGHTGREEN;
	HelpColor                  = FRONT_WHITE | BACK_LIGHTGREY;
	// enable button Close and Save To File
	DisabledItem[0] = FALSE;
	DisabledItem[1] = FALSE;
	DisabledItem[2] = FALSE;
	ShowHelp        = TRUE;
	Stdin           = GetStdHandle (STD_INPUT_HANDLE);
	va_start (List, Message);
		vsprintf (FormattedMessage, Message, List);
	va_end (List);
	// initialize ErrMsg dialog
	if ((dbErrMsg = (DIALOGBOX_COMPONENT *) malloc (sizeof (DIALOGBOX_COMPONENT))) != NULL)
	{
		MsgLength = (short) strlen (FormattedMessage);
		dbErrMsg->Length = 65;
		dbErrMsg->x = ((CONSOLE_LENGTH - 1 - dbErrMsg->Length) / 2);
		dbErrMsg->y = 6;
		dbErrMsg->Height = 0;
		// analyzing text
		MsgCharY = 0;
		for (i = 0; i < MsgLength; i++)
		{
			if (MsgCharCount + dbErrMsg->x + 8 >= dbErrMsg->Length - dbErrMsg->x + 8)
			{
				MsgCharX1++;
				MsgCharY++;
				MsgCharX2 = -1;
				MsgCharCount = -1;
			}
			MsgCharX2++;
			MsgCharCount++;
		}
		if (dbErrMsg->Height+10+MsgCharY-1 < 25) dbErrMsg->Height = 10+MsgCharY-1;
		else
		{
			Print (0, 0, FRONT_LIGHTGREY, "Error message is too long!");
			Print (0, 2, FRONT_LIGHTGREY, "Press any key to exit to the system ...");
			
		}
		i = MsgCharY = MsgCharX2 = MsgCharX1 = MsgCharCount = 0;
		dbErrMsg->BorderColor        = FRONT_WHITE;
		dbErrMsg->IconColor          = FRONT_LIGHTGREY;
		dbErrMsg->IconHighLightColor = FRONT_WHITE;
		dbErrMsg->TitleColor         = FRONT_WHITE | BACK_DARKBLUE;
		dbErrMsg->ShadowColor        = BACK_DARKGREY;
		dbErrMsg->Active             = TRUE;
		dbErrMsg->EnableExitIcon     = TRUE;
		dbErrMsg->EnableHelpIcon     = FALSE;
		dbErrMsg->DoubleLineBorder   = FALSE;
		dbErrMsg->ExitIconClicked    = FALSE;
		dbErrMsg->HelpIconClicked    = FALSE;
		dbErrMsg->IsInitialized      = FALSE;
		dbErrMsg->Pattern            = ' ';
		dbErrMsg->TitlePattern       = ' ';
		dbErrMsg->ShadowPattern      = ' ';
		dbErrMsg->LeftAlignedTitle   = TRUE;
		dbErrMsg->CenterAlignedTitle = FALSE;
		dbErrMsg->RightAlignedTitle  = FALSE;
		dbErrMsg->EnableShadow       = TRUE;
		dbErrMsg->InputCodePage      = 1250;
		dbErrMsg->OutputCodePage     = 1250;
		sprintf (dbErrMsg->Title, "Error message");
		dbErrMsg = DialogBoxComponent (dbErrMsg, ir, TRUE);
	}
	else
	{
		Print (0, 0, FRONT_LIGHTGREY, "Error occured while allocating space for dbErrMsg variable");
		Print (0, 1, FRONT_LIGHTGREY, "in function ErrorMsg (file \"main.c\").");
		Print (0, 3, FRONT_LIGHTGREY, "Press any key to exit to the system ...");

⌨️ 快捷键说明

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