tail -n 的简易实现版

从《The.C.Programming.Language.2Nd.Ed 》5.13题出发 。Exercise 5-13. Write the program tail, which prints the last n lines of its input. By default, n is set to 10, let us say, but it can be changed by an optional argument so that tail -n prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6 , not in a two-dimensional array of fixed size.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXLINES 10000
#define MAXLENGTH 1000

int main(int argc, char * argv[])
{
    int tail_n = 10;
    char * inputs[MAXLINES];
    char line[MAXLENGTH];

    if( argc > 1 && argv[1] != NULL )
    {
        if( argv[1][0] == '-' )
        {
            tail_n = atoi( &argv[1][1] );
        }
    }

    int i = 0;
    while(fgets(line, MAXLENGTH, stdin) != NULL && i < MAXLINES)
    {
        char * tmp = (char *)malloc( strlen(line) * sizeof(char) + 1 );
        strcpy(tmp, line);
        inputs[i] = tmp;
        i++;
    }

    int j = 0;
    for(; j<i-(tail_n+1); j++){}
    for(; j<i; j++){ printf("%s",inputs[j]);free(inputs[j]); }
    return 0;
}

好的,以下是一个简单的贪吃蛇代码,使用C语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define ROW 20 #define COL 40 int snake[ROW*COL][2]; // 蛇身数组 int food[2]; // 食物位置 int score = 0; // 得分 int head, tail; // 蛇头和蛇尾位置 char direction = 'D'; // 初始方向 void init() { // 初始化蛇身数组 head = tail = 0; snake[head][0] = ROW / 2; snake[head][1] = COL / 2; tail++; snake[tail][0] = ROW / 2; snake[tail][1] = COL / 2 - 1; tail++; // 初始化食物位置 srand(time(NULL)); food[0] = rand() % ROW; food[1] = rand() % COL; } void draw() { system("cls"); // 清屏 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (i == snake[head][0] && j == snake[head][1]) { // 蛇头 printf("O "); } else if (i == food[0] && j == food[1]) { // 食物 printf("* "); } else { int flag = 0; for (int k = tail; k < head; k++) { // 蛇身 if (i == snake[k][0] && j == snake[k][1]) { printf("o "); flag = 1; break; } } if (!flag) { printf(" "); } } } printf("\n"); } printf("Score: %d\n", score); } void move() { int next_row = snake[head][0], next_col = snake[head][1]; switch (direction) { case 'W': next_row--; break; case 'A': next_col--; break; case 'S': next_row++; break; case 'D': next_col++; break; } if (next_row < 0 || next_row >= ROW || next_col < 0 || next_col >= COL) { // 撞墙 printf("Game over!\n"); exit(0); } if (next_row == food[0] && next_col == food[1]) { // 吃到食物 score++; food[0] = rand() % ROW; food[1] = rand() % COL; } else { // 移动 tail++; } head++; snake[head][0] = next_row; snake[head][1] = next_col; } int main() { init(); while (1) { draw(); if (kbhit()) { // 按键检测 char ch = getch(); if ((ch == 'W' && direction != 'S') || (ch == 'A' && direction != 'D') || (ch == 'S' && direction != 'W') || (ch == 'D' && direction != 'A')) { direction = ch; } } move(); for (int i = tail; i < head; i++) { // 撞到自己 if (snake[i][0] == snake[head][0] && snake[i][1] == snake[head][1]) { printf("Game over!\n"); exit(0); } } Sleep(100); // 控制速度 } return 0; } ``` 以上代码是使用Windows API的Sleep函数控制速度,因此需要在Windows下编译运行。如果要在Linux下运行,需要使用头文件`<unistd.h>`中的`usleep()`函数来控制速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Emilin Amy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值