竹園論壇

標題: 跑馬燈程式 [打印本頁]

作者: 林宇翔    時間: 2014-8-29 16:28
標題: 跑馬燈程式
請各位大大幫我看一下這程式
請問要如何讓它變成
自碰到視窗邊緣後
後面的字自動跑來前面
例如
我輸入123
然後
                          123碰到視窗邊緣了變成
3                          12再來
23                          1再來
123                          
[C++] 純文本查看 復制代碼
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<windows.h>
using namespace std;
int main()
{
        string a;
        int b=0,c=0;
        cin>>a;
        b=a.size();
        for(int num=1;num>0;num++)
        {
                for(int e=0;e<num;e++)
                {
                        cout<<" ";
                }
                cout<<a;
                Sleep(80);
                system("cls");
                if(num==70)
                {
                        num=1;
                }
        }
        return 0;
}

作者: Sylveon    時間: 2014-8-29 19:35
給你參考

[C++] 純文本查看 復制代碼
#include<cstdlib>
#include<iostream>
#include<string>
#include<deque>
#include<algorithm>
#include<Windows.h>
#define BUFFERMAX 80

//不用 using namespace std;
//就要一個一個列舉要用std的是哪一些
using std::string;
using std::cout;
using std::deque;

int main()
{
        deque<char> show;
        string res = "Hello World!";
        //補滿80字
        while( res.size() < BUFFERMAX )
                res.push_back(' ');
        //複製到 deque(雙向佇列) show
        copy( res.begin() , res.end() , std::back_inserter(show) );

        while(true)
        {
                system("cls");
                //顯示 有C++11的寫法 但怕你不會編譯,舊式寫法也支援
                //use -std=c++11 to enable C++11
                #if __cplusplus < 201103LL
                for(int p=0;p<show.size();++p){//C++
                        char i =show[p];
                #else
                for(char &i : show){//C++11
                #endif
                        cout<<i;
                }
                //把最後一個字複製到前面來
                show.push_front(show.back());
                //把最後一個字刪除
                show.pop_back();
                Sleep(150);
        }
}


作者: 林宇翔    時間: 2014-8-31 16:50
//不用 using namespace std;
//就要一個一個列舉要用std的是哪一些
using std::string;
using std::cout;
using std::deque;

using是啥
std是啥
作者: jd3    時間: 2014-8-31 22:53
本帖最後由 jd3 於 2014-8-31 22:56 編輯

一些函式是在命名空間(namespace)底下的
像是 cout, cin , string 這些以及大量標準函式褲的東西都是在std底下的
要使用時應該告知是什麼namespace的東西才找得到定義
例如std::cout<<"Hello\n";
使用using 表示要使用他(也就是可以省略到命名空間裡找的步驟)

(如果有講錯請幫我糾正...)





歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2