C++实现tail的方法(还不完善)

基本思想是,从文件末尾向前Seek N个字符,读入内存,如果达到了tail n行则读取结束,如果不够n行再从文件末尾向前Seek N+N*2^1个字符,并读取N*2^1个字符,加上之前的行数,如果还不够tail n行,继续直到N+N*2^1+...+N*2^m以满足tail n为止。

 

#include <iostream> #include <fstream>
#include
<string>
#include
<list>

int tail(const std::string & fname, std::list<std::string> & ls, int lines, int bytes=100)
{
    std::fstream fs(fname.c_str());
   
if(!fs)
       
return -1;
   
   
int status=0;
   
    fs.seekg(
0,std::ios_base::end);
    std::streamoff old_pos
=fs.tellg();
    std::streamoff last_pos
=old_pos-bytes*lines;
    fs.seekg(last_pos,std::ios_base::beg);
       
    std::
string line;
    std::list
<std::string>::const_iterator cit=ls.begin();
   
while(true)
    {
        std::getline(fs,line);
        cit
=ls.insert(cit,line);
       
++cit;
       
if(status==0)
        {
           
if(fs.eof())
            {
               
if(ls.size()>lines)
                   
break;
                old_pos
=last_pos;
                last_pos
>>=1;
                fs.clear();
                fs.seekg(last_pos,std::ios_base::beg);
                std::streamoff temp
=fs.tellg();
                ls.erase(
--cit);
                cit
=ls.begin();
                status
=1;
            }
        }
       
else if(status > 0)
        {
           
if(fs.tellg() >= old_pos)
            {
               
if(ls.size() > lines)
                   
break;
                old_pos
=last_pos;
                last_pos
>>=1;
                fs.seekg(last_pos,std::ios_base::beg);
                ls.erase(
--cit);
                cit
=ls.begin();
            }
        }
    }
   
   
if(ls.size()>lines)
    {
       
int i=ls.size()-lines;
       
for(;i>0;--i)
            ls.erase(ls.begin());
    }
   
return ls.size();
}
   
int main(int argc, char * argv[])
{
    std::list
<std::string> ls;
  tail(argv[
1],ls,atoi(argv[2]));
  std::copy(ls.begin(),ls.end(),std::ostream_iterator
<std::string>(std::cout,"/n"));
 
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的贪吃蛇游戏的实现: ```cpp #include <iostream> #include <conio.h> //用于控制台输入 #include <Windows.h> //用于控制台窗口大小和光标位置 using namespace std; const int width = 20; //游戏地图宽度 const int height = 20; //游戏地图高度 int x, y, fruitX, fruitY, score; //蛇头坐标、水果坐标、分数 int tailX[100], tailY[100]; //蛇身坐标数组 int nTail; //蛇身长度 enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; //定义方向枚举类型 eDirection dir; //蛇的方向 void Setup() { srand(time(NULL)); //设置随机数种子,使每次生成的随机数不同 dir = STOP; //初始化方向为静止 x = width / 2; //初始化蛇头坐标 y = height / 2; fruitX = rand() % width; //随机生成水果坐标 fruitY = rand() % height; score = 0; //初始化分数为0 } void Draw() { system("cls"); //清屏 for (int i = 0; i < width + 2; i++) //绘制游戏地图上边界 cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) //绘制游戏地图左边界 cout << "#"; if (i == y && j == x) //绘制蛇头 cout << "O"; else if (i == fruitY && j == fruitX) //绘制水果 cout << "F"; else { bool printTail = false; for (int k = 0; k < nTail; k++) //绘制蛇身 { if (tailX[k] == j && tailY[k] == i) { cout << "o"; printTail = true; } } if (!printTail) //如果该位置没有蛇身,则输出空格 cout << " "; } if (j == width - 1) //绘制游戏地图右边界 cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) //绘制游戏地图下边界 cout << "#"; cout << endl; cout << "Score:" << score << endl; //显示分数 } void Input() { if (_kbhit()) //判断是否有按键输入 { switch (_getch()) //获取按键输入 { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': dir = STOP; break; } } } void Logic() { int prevX = tailX[0]; //记录蛇尾坐标,用于更新蛇身 int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; //更新蛇头坐标 tailY[0] = y; for (int i = 1; i < nTail; i++) //更新蛇身坐标 { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) //根据方向移动蛇头 { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } if (x >= width) //如果撞到墙壁,游戏结束 x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) //如果撞到自己的身体,游戏结束 { if (tailX[i] == x && tailY[i] == y) dir = STOP; } if (x == fruitX && y == fruitY) //如果吃到水果,蛇身长度加1,分数加10,并重新生成水果 { nTail++; score += 10; fruitX = rand() % width; fruitY = rand() % height; } } int main() { Setup(); while (dir != STOP) { Draw(); Input(); Logic(); Sleep(50); //延迟50毫秒,控制蛇移动速度 } return 0; } ``` 注意:以上代码仅是一个简单的贪吃蛇游戏实现,可能存在一些不足之处,可以根据自己的需求进行修改和完善

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值