c 編寫小軟件有哪些
2025.05.21 02:15 9
使用C語言可以編寫各種各樣的小軟件,以下是一些常見的類型:
控制臺應用程序
- 簡單計算器
- 功能:實現基本的加、減、乘、除、取余運算,用戶輸入算式,程序輸出結果。
- 示例代碼:
#include <stdio.h>
int main() { double num1, num2; char op; printf("請輸入算式(如 3 + 5):"); scanf("%lf %c %lf", &num1, &op, &num2);
switch (op) {
case '+':
printf("結果是:%.2lf\n", num1 + num2);
break;
case '-':
printf("結果是:%.2lf\n", num1 - num2);
break;
case '*':
printf("結果是:%.2lf\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("結果是:%.2lf\n", num1 / num2);
} else {
printf("除數不能為0\n");
}
break;
case '%':
if (num2 != 0) {
printf("結果是:%.0lf\n", (int)num1 % (int)num2);
} else {
printf("除數不能為0\n");
}
break;
default:
printf("無效的運算符\n");
}
return 0;
**文件統計工具**
- 功能:統計一個文本文件中字符數、單詞數和行數。
- 示例代碼:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("無法打開文件");
return 1;
}
int charCount = 0, wordCount = 0, lineCount = 0;
int inWord = 0;
int c;
while ((c = fgetc(file)) != EOF) {
charCount++;
if (isspace(c)) {
inWord = 0;
} else if (!inWord) {
inWord = 1;
wordCount++;
}
if (c == '\n') {
lineCount++;
}
}
fclose(file);
printf("字符數:%d\n", charCount);
printf("單詞數:%d\n", wordCount);
printf("行數:%d\n", lineCount);
return 0;
}
圖形界面應用程序(借助圖形庫)
- 簡單繪圖程序(使用Windows控制臺圖形模式或Linux下的ncurses庫等)
- Windows控制臺圖形模式示例(繪制矩形):
#include <windows.h>
- Windows控制臺圖形模式示例(繪制矩形):
void drawRectangle(int x, int y, int width, int height) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos;
for (int i = 0; i < height; i++) {
pos.Y = y + i;
for (int j = 0; j < width; j++) {
pos.X = x + j;
SetConsoleCursorPosition(hConsole, pos);
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
printf("*");
} else {
printf(" ");
}
}
}
int main() { drawRectangle(10, 5, 20, 10); return 0; }
- **ncurses庫示例(在Linux下繪制簡單圖形)**:
```c
#include <ncurses.h>
int main() {
initscr();
noecho();
cbreak();
move(10, 20);
addch('*');
refresh();
getch();
endwin();
return 0;
}
- 小游戲(如貪吃蛇)
- 功能:實現經典的貪吃蛇游戲,蛇可以在屏幕上移動、吃食物并增長身體,撞到墻壁或自己身體則游戲結束。
- 示例代碼(部分關鍵代碼):
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <time.h>
define WIDTH 80
define HEIGHT 20
typedef struct { int x, y; } Point;
typedef struct { Point *body; int length; int direction; } Snake;
typedef struct { Point pos; } Food;
void setup(Snake snake, Food food); void draw(Snake snake, Food food); void input(Snake snake); void logic(Snake snake, Food *food);
int main() { Snake snake; Food food; setup(&snake, &food);
while (1) {
draw(&snake, &food);
input(&snake);
logic(&snake, &food);
Sleep(100);
}
free(snake.body);
return 0;
void setup(Snake snake, Food food) { snake->length = 5; snake->body = (Point )malloc(snake->length sizeof(Point)); snake->direction = 0; // 0:右, 1:下, 2:左, 3:上
for (int i = 0; i < snake->length; i++) {
snake->body[i].x = WIDTH / 2 - i;
snake->body[i].y = HEIGHT / 2;
}
srand(time(NULL));
food->pos.x = rand() % WIDTH;
food->pos.y = rand() % HEIGHT;
void draw(Snake snake, Food food) { system("cls"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { int isSnake = 0; for (int k = 0; k < snake->length; k++) { if (snake->body[k].x == j && snake->body[k].y == i) { isSnake = 1; break; } } if (isSnake) { printf("O"); } else if (i == food->pos.y && j == food->pos.x) { printf("X"); } else { printf(" "); } } printf("\n"); } }
void input(Snake *snake) { if (GetAsyncKeyState(VK_UP) && snake->direction != 1) { snake->direction = 3; } else if (GetAsyncKeyState(VK_DOWN) && snake->direction != 3) { snake->direction = 1; } else if (GetAsyncKeyState(VK_LEFT) && snake->direction != 0) { snake->direction = 2; } else if (GetAsyncKeyState(VK_RIGHT) && snake->direction != 2) { snake->direction = 0; } }
void logic(Snake snake, Food food) { for (int i = snake->length - 1; i > 0; i--) { snake->body[i] = snake->body[i - 1]; }
switch (snake->direction) {
case 0:
snake->body[0].x++;
break;
case 1:
snake->body[0].y++;
break;
case 2:
snake->body[0].x--;
break;
case 3:
snake->body[0].y--;
break;
}
if (snake->body[0].x < 0 || snake->body[0].x >= WIDTH || snake->body[0].y < 0 || snake->body[0].y >= HEIGHT) {
exit(0);
}
for (int i = 1; i < snake->length; i++) {
if (snake->body[0].x == snake->body[i].x && snake->body[0].y == snake->body[i].y) {
exit(0);
}
}
if (snake->body[0].x == food->pos.x && snake->body[0].y == food->pos.y) {
snake->length++;
snake->body = (Point *)realloc(snake->body, snake->length * sizeof(Point));
srand(time(NULL));
food->pos.x = rand() % WIDTH;
food->pos.y = rand() % HEIGHT;
}
以上只是C語言編寫小軟件的一些簡單示例,C語言還可以用于編寫更復雜的系統工具、網絡程序等。