zoukankan      html  css  js  c++  java
  • 一些函数

    今天写了一些函数(有些是直接嫁接过来的),为了防止以后找不到,就发出来吧

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <cstring>
    #include <conio.h>
    #include <stdlib.h>
    #include <cmath>
    #include <ctime>
    #include <vector>
    #include <list>
    #include <deque>
    #include <malloc.h>
    #include <map>
    #include <windows.h>
    using namespace std;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord;
    void hide()//隐藏光标
    {
        CONSOLE_CURSOR_INFO cursor_info={1,0};
        SetConsoleCursorInfo(hout, &cursor_info);
    }
    /**********************************************************************************************/
    
    bool breorcon()//一旦键入值,返回 false
    {
        if(_kbhit())//_kbhit()返回缓存区是否为空,空返回 0 ,非空返回 1
        {
            cin.sync();//cin.sync()清空缓存区
            cin.clear();//cin.clear()修复输入流
            return false;
        }
        return true;
    
    }
    /**********************************************************************************************/
    
    
    void SetCurPos(const int x, const int y)//设置光标位置
    {
        COORD position;
        position.X = x;
        position.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
    }
    
    /**********************************************************************************************/
    
    void SetColor(int colorID)//设置文本颜色
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorID);
    }
    
    
    
    void SetBackColor()//设置文本背景色
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                                FOREGROUND_BLUE |
                                BACKGROUND_BLUE |
                                BACKGROUND_GREEN |
                                BACKGROUND_RED );//这里是白色
    }
    
    /**********************************************************************************************/
    
    
    string generatetime()//利用WindowsAPI获取系统时间
    {
        SYSTEMTIME sys;
        GetLocalTime(&sys);
        int pl;
        string m="";
        pl=sys.wYear;   m+=to_string(pl);
        pl=sys.wMonth;  if(pl<10)m+="0";    m+=to_string(pl);
        pl=sys.wDay;    if(pl<10)m+="0";    m+=to_string(pl);
        pl=sys.wHour;   if(pl<10)m+="0";    m+=to_string(pl);
        pl=sys.wMinute; if(pl<10)m+="0";    m+=to_string(pl);
        pl=sys.wSecond; if(pl<10)m+="0";    m+=to_string(pl);
        return m;
    }
    /**********************************************************************************************/
    
    void scanall()//打开.txt文件(其他类型文件暂不明),一次性读取并输出
    {
        string n;
        cin>>n;
        string s;
        ifstream in(n);
        ostringstream tmp;
        tmp<<in.rdbuf();
        s=tmp.str();
        cout<<s;
    }
    
    /**********************************************************************************************/
    void scanfilebyline(string n)//按行读取文件数据(假如一行用空格分为 4 部分)并输出
    {
        string s,a,b,c,d;
        ifstream input(n);
        if(!input)
            cout<<"error | no data ?!"<<endl;
        else
            while(getline(input,s))
            {
                istringstream scin(s);
                scin>>a>>b>>c>>d;
                cout<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
            }
    }
    
    
    /**********************************************************************************************/
    
    int randomnum(int a,int b)//产生随机数(有范围)
    {
        srand((unsigned)time(nullptr));
        int c=rand()%(b-a)+a;
        return c;
    }
    
    
    /**********************************************************************************************/
    void actviewlefthide
        (const int posx,
         const int posy,
         string* strgather,
         int height)//语句组向左隐藏于一点
    {
        int lenght=strgather[0].size();
        for(int i=0;i<lenght;i++)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx,posy+j);
                string active=strgather[j].substr(i);
                cout<<active;
            }
            Sleep(10);
            char a[lenght-i];
            memset(a,' ',sizeof(a));
            a[lenght-i]='';
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx,posy+j);
                cout<<a;
            }
        }
    }
    void actviewleftout
        (const int posx,
         const int posy,
         string* strgather,
         int height)//语句自左方一点展开
    {
        int lenght=strgather[0].size();
        for(int i=lenght-1;i>=0;i--)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx,posy+j);
                string active=strgather[j].substr(i);
                cout<<active;
            }
            Sleep(10);
        }
    }
    
    void actviewrighthide
        (const int posx,
         const int posy,
         string* strgather,
         int height)//语句组向右隐藏于一点
    {
        int lenght=strgather[0].size();
        for(int i=0;i<lenght;i++)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx-lenght+i,posy+j);
                string active=strgather[j].substr(0,lenght-i);
                cout<<active;
            }
            Sleep(10);
            char a[lenght-i];
            memset(a,' ',sizeof(a));
            a[lenght-i]='';
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx-lenght+i,posy+j);
    
                cout<<a;
            }
        }
    }
    void actviewrightout
        (const int posx,
         const int posy,
         string* strgather,
         int height)//语句自右方一点展开
    {
        int lenght=strgather[0].size();
        for(int i=lenght-1;i>=0;i--)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(posx-lenght+i,posy+j);
                string active=strgather[j].substr(0,lenght-i);
                cout<<active;
            }
            Sleep(10);
        }
    }
    
    
    
    void actviewleftoright
        (const int posx1,//语句前端初坐标 x
         const int posx2,//语句前端末坐标 x
         const int posy,
         string* strgather,
         int height)//语句自左方向右方滑动
    {
        int lenght=strgather[0].size();
        for(int i=posx1;i<posx2;i++)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(i,posy+j);
                cout<<strgather[j];
            }
            SetCurPos(i+lenght-1,posy+height);
            Sleep(10);
            char a[lenght];
            memset(a,' ',lenght);
            a[lenght]='';
            for(int j=0;j<height;j++)
            {
                SetCurPos(i,posy+j);
                cout<<a;
            }
        }
        for(int j=0;j<height;j++)
        {
            SetCurPos(posx2,posy+j);
            cout<<strgather[j];
        }
    
    }
    
    void actviewrightoleft
                (const int posx1,//语句前端初坐标 x
                 const int posx2,//语句前端末坐标 x
                 const int posy,
                 string* strgather,
                 int height)//语句自右方向左方滑动
    {
        int lenght=strgather[0].size();
        for(int i=posx1;i>=posx2;i--)
        {
            for(int j=0;j<height;j++)
            {
                SetCurPos(i,posy+j);
                cout<<strgather[j];
            }
            SetCurPos(i+lenght-1,posy+height);
            Sleep(10);
            char a[lenght];
            memset(a,' ',lenght);
            a[lenght]='';
            for(int j=0;j<height;j++)
            {
                SetCurPos(i,posy+j);
                cout<<a;
            }
        }
        for(int j=0;j<height;j++)
        {
            SetCurPos(posx2,posy+j);
            cout<<strgather[j];
        }
    }
    
    
    
    
    /**
     *以下为用于测试上述函数功能的函数
     *
     */
    void testactview()
    {
        hide();
        string a[3]={"dcsoivsovn.zdrv   xdknl.vnljdxasscusdu",
                     "osvudnjsxaczcsdczsd   cszdczxaoifsiorv",
                     "pok   zsdczsdczsdczsdvczdfvd   ghyjfdz"};
        while(1)
        {
            actviewlefthide(2,3,a,3);Sleep(70);
            actviewleftout(2,3,a,3);
            actviewleftoright(2,50,3,a,3);
            actviewrighthide(88,3,a,3);Sleep(70);
            actviewrightout(88,3,a,3);
            actviewrightoleft(50,2,3,a,3);
    
        }
    }
    /****************************************************************************************/
    
    //由键值选择
    //left arrow : 75
    //right arrow : 77
    //up arrow : 72
    //down arrow :80
    const string testSen[5]={" 飞剑 "," 宝刀 "," 匕首 "," 长枪 "," 强弩 "};
    int SitionSen[5]={0};
    string activepr[5];
    int maxnum=0;
    
    void remakemenu
                (const string* pl,
                 int *io,
                 int d,
                 string* activepl,
                 int *fr)//重构菜单
    {
        int k=0;
        vector<int> m;
        for(int i=0;i<d;i++)
        {
            activepl[i]="";//首先,清空 active pl
            if(SitionSen[i]>0)
            {
                m.push_back(i);//将参数写入 m
                k++;
            }
        }
        for(int h=0;h<k;h++)
            activepl[h]=testSen[m[h]];
        fr[0]=k;
    }
    
    
    
    void arrowchoose()//建立菜单并选择
    {
        //
        for(int i=0;i<5;i++)
            SitionSen[i]=1;
    
        remakemenu(testSen,SitionSen,5,activepr,&maxnum);
    
    
    
    
        SetCurPos(10,5);
        SetBackColor();
        cout<<activepr[0];
        for(int i=1;i<maxnum;i++)
        {
            SetColor(3);
            cout<<activepr[i];
        }
    
    
    
    
        int pos=10;
        int key=0;
        char ch;
        hide();
        while((ch=getch()))
        {
            switch(ch)
            {
                case 13:
                    SetCurPos(10,7);
                    SetColor(4);
                    cout<<"已选择"<<testSen[key];
                    break;
                case 75:
                    if(key>0)
                    {
                        SetCurPos(pos,5);
                        SetColor(3);
                        cout<<activepr[key];
                        pos-=6;
                        key--;
                        SetCurPos(pos,5);
                        SetBackColor();
                        cout<<activepr[key];
                    }
                    break;
                case 77:
                    if(key<maxnum-1)
                    {
                        SetCurPos(pos,5);
                        SetColor(3);
                        cout<<activepr[key];
                        pos+=6;
                        key++;
                        SetCurPos(pos,5);
                        SetBackColor();
                        cout<<activepr[key];
                    }
                    break;
                default:break;
            }
            Sleep(50);
        }
    }
    
    /**********************************************************************************************/
    
    
    void getkeynum()//获取键码
    {
        char ch;
        while((ch=getch()))
        {
            switch(ch)
            {
                case 0:cout<<0<<endl;break;
                case 1:cout<<1<<endl;break;
                case 2:cout<<2<<endl;break;
                case 3:cout<<3<<endl;break;
                case 4:cout<<4<<endl;break;
                case 5:cout<<5<<endl;break;
                case 6:cout<<6<<endl;break;
                case 7:cout<<7<<endl;break;
                case 8:cout<<8<<endl;break;
                case 9:cout<<9<<endl;break;
                case 10:cout<<10<<endl;break;
                case 11:cout<<11<<endl;break;
                case 12:cout<<12<<endl;break;
                case 13:cout<<13<<endl;break;
                case 14:cout<<14<<endl;break;
                case 15:cout<<15<<endl;break;
                case 16:cout<<16<<endl;break;
                case 17:cout<<17<<endl;break;
                case 18:cout<<18<<endl;break;
                case 19:cout<<19<<endl;break;
                case 20:cout<<20<<endl;break;
                case 21:cout<<21<<endl;break;
                case 22:cout<<22<<endl;break;
                case 23:cout<<23<<endl;break;
                case 24:cout<<24<<endl;break;
                case 25:cout<<25<<endl;break;
                case 26:cout<<26<<endl;break;
                case 27:cout<<27<<endl;break;
                case 28:cout<<28<<endl;break;
                case 29:cout<<29<<endl;break;
                case 30:cout<<30<<endl;break;
                case 31:cout<<31<<endl;break;
                case 32:cout<<32<<endl;break;
                case 33:cout<<33<<endl;break;
                case 34:cout<<34<<endl;break;
                case 35:cout<<35<<endl;break;
                case 36:cout<<36<<endl;break;
                case 37:cout<<37<<endl;break;
                case 38:cout<<38<<endl;break;
                case 39:cout<<39<<endl;break;
                case 40:cout<<40<<endl;break;
                case 41:cout<<41<<endl;break;
                case 42:cout<<42<<endl;break;
                case 43:cout<<43<<endl;break;
                case 44:cout<<44<<endl;break;
                case 45:cout<<45<<endl;break;
                case 46:cout<<46<<endl;break;
                case 47:cout<<47<<endl;break;
                case 48:cout<<48<<endl;break;
                case 49:cout<<49<<endl;break;
                case 50:cout<<50<<endl;break;
                case 51:cout<<51<<endl;break;
                case 52:cout<<52<<endl;break;
                case 53:cout<<53<<endl;break;
                case 54:cout<<54<<endl;break;
                case 55:cout<<55<<endl;break;
                case 56:cout<<56<<endl;break;
                case 57:cout<<57<<endl;break;
                case 58:cout<<58<<endl;break;
                case 59:cout<<59<<endl;break;
                case 60:cout<<60<<endl;break;
                case 61:cout<<61<<endl;break;
                case 62:cout<<62<<endl;break;
                case 63:cout<<63<<endl;break;
                case 64:cout<<64<<endl;break;
                case 65:cout<<65<<endl;break;
                case 66:cout<<66<<endl;break;
                case 67:cout<<67<<endl;break;
                case 68:cout<<68<<endl;break;
                case 69:cout<<69<<endl;break;
                case 70:cout<<70<<endl;break;
                case 71:cout<<71<<endl;break;
                case 72:cout<<72<<endl;break;
                case 73:cout<<73<<endl;break;
                case 74:cout<<74<<endl;break;
                case 75:cout<<75<<endl;break;
                case 76:cout<<76<<endl;break;
                case 77:cout<<77<<endl;break;
                case 78:cout<<78<<endl;break;
                case 79:cout<<79<<endl;break;
                case 80:cout<<80<<endl;break;
                case 81:cout<<81<<endl;break;
                case 82:cout<<82<<endl;break;
                case 83:cout<<83<<endl;break;
                case 84:cout<<84<<endl;break;
                case 85:cout<<85<<endl;break;
                case 86:cout<<86<<endl;break;
                case 87:cout<<87<<endl;break;
                case 88:cout<<88<<endl;break;
                case 89:cout<<89<<endl;break;
                case 90:cout<<90<<endl;break;
                case 91:cout<<91<<endl;break;
                case 92:cout<<92<<endl;break;
                case 93:cout<<93<<endl;break;
                case 94:cout<<94<<endl;break;
                case 95:cout<<95<<endl;break;
                case 96:cout<<96<<endl;break;
                case 97:cout<<97<<endl;break;
                case 98:cout<<98<<endl;break;
                case 99:cout<<99<<endl;break;
                case 100:cout<<100<<endl;break;
                case 101:cout<<101<<endl;break;
                case 102:cout<<102<<endl;break;
                case 103:cout<<103<<endl;break;
                case 104:cout<<104<<endl;break;
                case 105:cout<<105<<endl;break;
                case 106:cout<<106<<endl;break;
                case 107:cout<<107<<endl;break;
                case 108:cout<<108<<endl;break;
                case 109:cout<<109<<endl;break;
                case 110:cout<<110<<endl;break;
                case 111:cout<<111<<endl;break;
                case 112:cout<<112<<endl;break;
                case 113:cout<<113<<endl;break;
                case 114:cout<<114<<endl;break;
                case 115:cout<<115<<endl;break;
                case 116:cout<<116<<endl;break;
                case 117:cout<<117<<endl;break;
                case 118:cout<<118<<endl;break;
                case 119:cout<<119<<endl;break;
                case 120:cout<<120<<endl;break;
                case 121:cout<<121<<endl;break;
                case 122:cout<<122<<endl;break;
                case 123:cout<<123<<endl;break;
                case 124:cout<<124<<endl;break;
                case 125:cout<<125<<endl;break;
                case 126:cout<<126<<endl;break;
                case 127:cout<<127<<endl;break;
                default:break;
            }
        }
    }
    
    
    
    
    int main()
    {
        return 0;
    }
    

    这些函数是为实现另一个程序的一部分功能而写的

    希望我能写好这个程序。。。

    路过的大神们,如果知道怎样在控制台中画出矩形(不是由什么'#'****组成的,而是真正的矩形,
    并且是纯C++环境<不过系统环境例外,调用WindowsAPI是可以的>请在评论区留言,****谢谢)

  • 相关阅读:
    HTTP报文
    Linux命令行下快捷键
    ruby离线安装整理
    Tomcat启动时卡在 INFO HostConfig.deployDirectory Deploy
    ruby在线安装整理
    python_控制台输出带颜色的文字方法
    http proxy模块参数
    upstream模块调度算法
    upstream模块介绍
    nginx的upstream目前支持5种方式的分配
  • 原文地址:https://www.cnblogs.com/BuluGuy/p/9184747.html
Copyright © 2011-2022 走看看