Advanced C Programming Techniques Output,Input,and Graphics

C Language Advanced Programming Techniques

1. Text Output to Screen and Keyboard Input

1.1 Text Output to Screen

In C programming, text output to the screen is a fundamental feature. It typically appears in two forms: text-based and graphical. Text-based output uses characters rather than pixels to display content, and each character's position is indicated by rows and columns. In Turbo C (TC), the default text window spans the entire screen, typically an 80x25 layout. The top-left corner is defined as coordinate (1,1), and the bottom-right is (80,25). The coordinate system uses the X-axis (left to right) and Y-axis (top to bottom). Each character unit includes an ASCII character and properties for color and intensity.

To customize the text window size, TC provides the following function:

void textmode(int newmode);

Here, newmode specifies the new text mode to adjust the window size.

1.2 Keyboard Input

Keyboard input is a crucial interaction method with users. In TC, keyboard input can be handled in several ways:

- Standard Input: Using the standard input stream stdin.

- Instant Input: Using getch() or getche() to get user input. The former does not display characters, while the latter does.

- Special Key Recognition: Using kbhit() to check if a key is pressed, in combination with getch() to detect special keys like the arrow keys.

1.3 Problem Implementation

To create two distinct color text input windows and switch between them with keys, we need to use text window settings, color settings, and keyboard event handling functions.

- Window Setup: Use textmode to define the window size.

- Color Setup: Use textcolor and textbackground to set foreground and background colors.

- Keyboard Event Handling: Use kbhit and getch to detect keys and perform actions.

1.4 Advanced Application - Menu Implementation

To implement a menu in TC, combine text output and keyboard input techniques. The typical steps include:

- Menu Item Output: Display menu items using the gotoxy function for positioning text.

- Highlighting Current Option: Use different colors or backgrounds to highlight the selected menu item when using the arrow keys.

- Handle User Input: Use kbhit and getch to listen for key events and update the menu state.

2. Graphics Display and Mouse Input

In graphical display mode, the screen is divided into pixels, allowing programmers to control each pixel's color to draw graphics. TC provides functions for setting graphic modes, drawing points, lines, and objects, typically included in the graphics.h header. TC also supports mouse input with functions like getmousestate to read the mouse's position and state.

3. Screen Image and Animation Techniques

To implement animations in TC, screen image refreshing is key. Common methods include:

- Simple Implementation: Modify pixel values directly to create animations.

- Dynamic Graphics Viewport: Create extra buffers for images and switch between them to display animations.

- Screen Image Storage and Replay: Draw an image to the screen, then save and restore it to create animation.

- Page Flipping: Use multiple display buffers to quickly switch between frames for animation.

4. Interrupt Technology

Interrupt technology is a more complex part of advanced C programming, involving low-level hardware access and control. By writing custom interrupt handlers, developers can manage timers and hardware interrupts.

5. Sound Technology

TC provides basic sound and music playback capabilities. Specific functions allow the program to play simple sound effects or music.

6. Chinese Character Display Technology

TC supports Chinese character display through special font files and encoding. Developers can call specific functions to display Chinese characters. By mastering these advanced techniques, developers can create powerful, user-friendly applications in TC, covering text output, graphics, animations, and more.

pdf 文件大小:337.25KB