大鱼吃小鱼的C语言程序
时间: 2023-03-25 18:01:43 浏览: 308
这是一个常见的问题,我可以回答。以下是一个简单的大鱼吃小鱼的C语言程序:
#include <stdio.h>
int main() {
int big_fish = 10;
int small_fish = 5;
printf("大鱼吃小鱼前,大鱼数量为%d,小鱼数量为%d\n", big_fish, small_fish);
big_fish += small_fish;
small_fish = ;
printf("大鱼吃小鱼后,大鱼数量为%d,小鱼数量为%d\n", big_fish, small_fish);
return ;
}
这个程序中,我们定义了两个变量big_fish和small_fish,分别表示大鱼和小鱼的数量。然后,我们输出了大鱼和小鱼的数量,接着让大鱼吃掉了所有的小鱼,最后再次输出了大鱼和小鱼的数量。
相关问题
大鱼吃小鱼C语言程序数据结构顺序撰写
大鱼吃小鱼是一个经典的游戏,它的实现可以采用链表数据结构。下面是一个简单的C语言程序实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义鱼的结构体
struct Fish {
int size; // 鱼的大小
struct Fish *next; // 指向下一个鱼的指针
};
// 创建一条新鱼
struct Fish *createFish(int size) {
struct Fish *fish = (struct Fish*)malloc(sizeof(struct Fish));
fish->size = size;
fish->next = NULL;
return fish;
}
// 吃掉一条鱼
void eatFish(struct Fish *fish, struct Fish **pond) {
struct Fish *prev = NULL;
struct Fish *current = *pond;
// 找到比自己小的鱼
while (current != NULL && current->size > fish->size) {
prev = current;
current = current->next;
}
// 如果找到了比自己小的鱼,吃掉它
if (current != NULL && current->size <= fish->size) {
if (prev != NULL) {
prev->next = current->next;
} else {
*pond = current->next;
}
free(current);
}
}
// 打印池塘中的鱼
void printFish(struct Fish *pond) {
while (pond != NULL) {
printf("%d ", pond->size);
pond = pond->next;
}
printf("\n");
}
int main() {
// 初始化池塘中的鱼
struct Fish *fish1 = createFish(10);
struct Fish *fish2 = createFish(5);
struct Fish *fish3 = createFish(3);
struct Fish *fish4 = createFish(8);
struct Fish *pond = fish1;
fish1->next = fish2;
fish2->next = fish3;
fish3->next = fish4;
// 打印初始状态
printf("初始状态:\n");
printFish(pond);
// 大鱼吃小鱼
eatFish(fish1, &pond);
// 打印最终状态
printf("最终状态:\n");
printFish(pond);
return 0;
}
```
在这个程序中,我们首先定义了一个鱼的结构体,包含鱼的大小和指向下一个鱼的指针。然后,我们实现了三个函数:`createFish`用于创建一条新鱼,`eatFish`用于让大鱼吃小鱼,`printFish`用于打印池塘中的鱼。
在`main`函数中,我们首先初始化池塘中的鱼,然后打印初始状态。接着,我们让大鱼(即第一条鱼)吃小鱼,最后打印最终状态。
给我在网络上搜索一个可以运行的大鱼吃小鱼C语言程序
以下是一个可以在网络上找到的大鱼吃小鱼C语言程序的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define WIDTH 50
#define HEIGHT 20
#define FOOD_NUM 20
typedef struct Node {
int x;
int y;
struct Node* next;
}Node;
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
int Score = 0;
int FoodCount = 0;
int Map[HEIGHT][WIDTH] = {0};
Node* Head = NULL;
Node* Tail = NULL;
Node* Foods[FOOD_NUM] = {NULL};
void DrawMap() {
system("cls");
printf("Score: %d\n", Score);
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (Map[i][j] == 0) {
printf(" ");
} else if (Map[i][j] == 1) {
printf("*");
} else if (Map[i][j] == 2) {
printf("#");
} else if (Map[i][j] == 3) {
printf("@");
}
}
printf("\n");
}
}
void InitSnake() {
Head = (Node*) malloc(sizeof(Node));
Tail = Head;
Head->x = WIDTH / 2;
Head->y = HEIGHT / 2;
Head->next = NULL;
Map[Head->y][Head->x] = 1;
}
void AddTail(int x, int y) {
Node* node = (Node*) malloc(sizeof(Node));
node->x = x;
node->y = y;
node->next = NULL;
Tail->next = node;
Tail = node;
Map[y][x] = 1;
}
void RemoveTail() {
Node* node = Head;
while (node->next != Tail) {
node = node->next;
}
Map[Tail->y][Tail->x] = 0;
free(Tail);
Tail = node;
Tail->next = NULL;
}
void InitFood() {
srand((unsigned)time(NULL));
for (int i = 0; i < FOOD_NUM; i++) {
Foods[i] = (Node*) malloc(sizeof(Node));
Foods[i]->x = rand() % WIDTH;
Foods[i]->y = rand() % HEIGHT;
Foods[i]->next = NULL;
Map[Foods[i]->y][Foods[i]->x] = 2;
FoodCount++;
}
}
int EatFood(int x, int y) {
int score = 0;
for (int i = 0; i < FOOD_NUM; i++) {
if (Foods[i] && Foods[i]->x == x && Foods[i]->y == y) {
score++;
Map[y][x] = 0;
free(Foods[i]);
Foods[i] = NULL;
FoodCount--;
}
}
return score;
}
int MoveSnake(enum Direction dir) {
int new_x = Head->x;
int new_y = Head->y;
switch (dir) {
case UP:
new_y--;
break;
case DOWN:
new_y++;
break;
case LEFT:
new_x--;
break;
case RIGHT:
new_x++;
break;
}
if (new_x < 0 || new_x >= WIDTH || new_y < 0 || new_y >= HEIGHT) {
return 0;
}
if (Map[new_y][new_x] == 1) {
return 0;
}
int score = EatFood(new_x, new_y);
AddTail(new_x, new_y);
if (score == 0) {
RemoveTail();
} else {
Score += score;
}
Head = Tail;
Tail = Tail->next;
Map[new_y][new_x] = 1;
return 1;
}
int main() {
InitSnake();
InitFood();
DrawMap();
while (1) {
if (_kbhit()) {
enum Direction dir = UP;
char c = _getch();
switch (c) {
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'q':
return 0;
}
if (!MoveSnake(dir)) {
printf("Game Over!\n");
return 0;
}
DrawMap();
}
}
return 0;
}
```
注意:这是一个控制台程序,需要在控制台中运行。
阅读全文