2013年6月9日 星期日

多行寫檔

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

//----------------------------------
// C++檔案輸出範例
//----------------------------------

int main(int argc, char *argv[])
{
string Data; //儲存使用者輸入的資料
ofstream outfile("C:\\output.txt"); //宣告並指定輸出.txt檔的路徑

while(true)
{
cout << "請輸入一段訊息: (輸入*QUIT則結束程式)" << endl;
getline(cin,Data);

if(Data == "*QUIT")
{
break;
}

outfile << Data << endl;
Data = "";
}

    system("PAUSE");
    return EXIT_SUCCESS;
}

2013年6月7日 星期五

亂數產生

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
    bool Find = false;
    int A,B;//表示範圍的起始與結束
    int Change;
    int Num;
    int Temp;
    int Index = 0;

    srand(time(NULL));

    cout << "請輸入範圍起始值:";
    cin >> A;
    cout << "請輸入範圍結束值:";
    cin >> B;
   
    if(A>B)
    {
        Change = A;
        A = B;
        B = Change;
    }
   
    cout << "請輸入亂數個數:";
    cin >> Num;

    int Box[Num];

    for(int i=0; i<Num; i++)
    {
        Box[i] = 0;
    }
    Box[Num-1] = -1;

    while(Box[Num-1] == -1)
    {
        Find = false;//此處旗標需重新設定為false!
        Temp = rand()%B+A;
        for(int j=0; j<Num; j++)
        {
            if(Box[j] == Temp)
            {
                Find = true;
                break;
            }
        }
        //
        if(!Find)
        {
            Box[Index] = Temp;
            Index++;
        }
    }
    //輸出產生的亂數
    cout << "產生的亂數:" << endl;

    for(int k=0; k<Num; k++)
    {
        cout << Box[k] << " ";
    }

    cout << endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

字串轉成數值

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    string num_data; //首先宣告為string型別
    cout << "Please input a number: ";
    cin >> num_data; //這裡的num_data為string型別,不能作運算
 
    int num = atoi(num_data.c_str()); //字串轉為數值,轉換後的num即可進行運算
    cout << ++num;
 
    //解析: c_str(),將string轉為傳統C字元陣列 ; atoi():傳統C字元陣列(即C字串)轉為數值
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

幾個程式語言的參考網站

良葛格學習筆記      http://caterpillar.onlyfun.net/Gossip/
C++學習筆記     http://caterpillar.onlyfun.net/Gossip/CppGossip/CppGossip.html
C語言學習筆記     http://caterpillar.onlyfun.net/Gossip/CGossip/CGossip.html
Java學習筆記(一) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/JavaGossip.htm
Java學習筆記(二) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/JavaGossip2.htm

VB/ VBA/ C#/ Java/ C++ 語言學習筆記  http://www.dotblogs.com.tw/yc421206/Default.aspx
VB/ VBA/ C#/ Java/ C++ 語言學習筆記(備份)  http://yc421206.pixnet.net/blog

指定路徑讀檔

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    string Loc;
    string LineData = "";
   
    cout << "請輸入檔案路徑:";
    getline(cin,Loc);

    ifstream infile(Loc.c_str(),ios::in);
 
    if(infile)
    {
        cout << "Succeeded..." << endl;
        while(!infile.eof())
        {
            getline(infile,LineData);
            cout << LineData << endl;
            LineData = "";          
        }
    }
   
    else
    {
        cout << "Failed..." << endl;
    }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

最大公因數、最小公倍數(非遞迴版)

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int max,min,tmpm,tmpn,temp;
 
    cout << "請輸入第一個數:";
    cin >> max;
    cout << "請輸入第二個數:";
    cin >> min;
 
    if(max < min)
    {
        temp = max;
        max = min;
        min = temp;
    }
 
    tmpm = max;
    tmpn = min;
 
    if(max % min == 0)
    {
        cout << "GCD = " << min << endl;
        cout << "LCM = " << max << endl;
    }
    //
    else
    {
        while(min > 0)
        {
            max = max % min;
            min = min % max;
            if(min == 0)
            {
                cout << "GCD = " << max << endl;
                cout << "LCM = " << max*(tmpm/max)*(tmpn/max) << endl;
            }
        }
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

最大公因數、最小公倍數(遞迴版)

#include <cstdlib>
#include <iostream>

using namespace std;

int GCD(int a,int b)
{
    if(b == 0)
    {
        return a;
    }
    else
    {
        return GCD(b, a%b);
    }
}
int main(int argc, char *argv[])
{
    int m, n;
    cout << "請輸入第一個數:";
    cin >> m;
    cout << "請輸入第二個數:";
    cin >> n;

    int GetGcd = GCD(m,n);
    cout << "GCD: " << GetGcd << endl;
    cout << "LCD: " << GetGcd*(m/GetGcd)*(n/GetGcd) << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

猜數字

C++寫的猜數字程式
猜一個四位數字(千位數不為0,且沒有數字重複)
它會提示有幾A幾B
ex: 2A1B
2A代表有2個數值猜對且位置正確
1B代表有1個數值猜對但位置不正確

以下為code

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <time.h>

using namespace std;

//把整數轉為字串的副程式IntToString
string IntToString (int N)
{
    string Get;
    stringstream ss;
    ss << N;
    ss >> Get;
    return Get;
}

int main()
{
    string Answer; //儲存答案
    string Guess; //儲存使用者猜的數字
    int Num; //儲存由rand()產生的亂數
    int A = 0;
    int B = 0;
    int Time = 5; //一個回合可以猜的次數
    int Round = 1; //回合數 初始值 = 1

    srand(time(NULL));

    while(true)
    {
         Answer = IntToString(rand()%9+1); //先將答案的千位數字指定為1~9之間的任意數字

        for (int i=1; i<4; i++)
        {
            //產生謎底
            Num = rand()%10;

            if(Answer.find(IntToString(Num)) == -1)
            {
                Answer = Answer + IntToString(Num);
            }

            else
            {
                i--;
            }
        }

        cout << "----------------------" << endl;
        cout << "Round " << Round << endl; //提示目前回合
        cout << endl;

        while(true)
        {
            //將A與B值初始化
            A = 0;
            B = 0;

            //使用者猜數字
            cout << "請猜一個四位數字: ";
            getline(cin,Guess);

            //判斷幾A幾B
            for (int i=0; i<4; i++)
            {
                for (int j=0; j<4; j++)
                {
                    if((Answer[i] == Guess[j])&&(i == j))
                    {
                        A++; //值吻合且位置相同
                    }

                    else if((Answer[i] == Guess[j])&&(i != j))
                    {
                        B++; //值吻合但位置不同
                    }
                }
            }

            //輸出提示
            if (A == 4)
            {
                cout << "恭喜答對!" << endl;
                break;
            }

            else if((A != 4)&&(Time > 1))
            {
                Time--;
                cout << A << "A" << B << "B" << endl;
                cout << "尚餘 " << Time << " 次機會..." << endl;
                cout << endl;
            }

            else
            {
                break;
            }

        }

        //
        cout << A << "A" << B << "B" << endl;
        cout << endl;
        cout << "正確解答: " << Answer << endl;
        Round++;
        Answer = "";
        Guess = "";
        Time = 5;
    }

    return 0;
}

取出字串中的單字

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    //取出字串中的單字,其中單字與單字間以一個或數個空白隔開
    string str;
    string Temp = "";
 
    cout << "請輸入一段文字:";
    getline(cin,str);
    str = str + " "; //加入空白讓取值時判斷是否到達字串邊界
 
    for(int i=0; i<str.length(); i++)
    {
        if(str[i] != ' ')
        {
            Temp = Temp + str[i];
        }
        else if((str[i] == ' ')&&(str[i+1] != ' ')) //抓到最後一個空白才輸出
        {
            cout << Temp << endl;
            Temp = ""; //清空Temp的值讓迴圈進行下一次取新的值
        }
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

猜數字

C++寫的猜數字程式
猜一個四位數字(千位數不為0,且沒有數字重複)
它會提示有幾A幾B
ex: 2A1B
2A代表有2個數值猜對且位置正確
1B代表有1個數值猜對但位置不正確

以下為code



#include <iostream>
#include <cstdlib>
#include <sstream>
#include <time.h>

using namespace std;

//把整數轉為字串的副程式IntToString
string IntToString (int N)
{
    string Get;
    stringstream ss;
    ss << N;
    ss >> Get;
    return Get;
}

int main()
{
    string Answer; //儲存答案
    string Guess; //儲存使用者猜的數字
    int Num; //儲存由rand()產生的亂數
    int A = 0;
    int B = 0;
    int Time = 5; //一個回合可以猜的次數
    int Round = 1; //回合數 初始值 = 1

    srand(time(NULL));

    while(true)
    {
         Answer = IntToString(rand()%9+1); //先將答案的千位數字指定為1~9之間的任意數字

        for (int i=1; i<4; i++)
        {
            //產生謎底
            Num = rand()%10;

            if(Answer.find(IntToString(Num)) == -1)
            {
                Answer = Answer + IntToString(Num);
            }

            else
            {
                i--;
            }
        }

        cout << "----------------------" << endl;
        cout << "Round " << Round << endl; //提示目前回合
        cout << endl;

        while(true)
        {
            //將A與B值初始化
            A = 0;
            B = 0;

            //使用者猜數字
            cout << "請猜一個四位數字: ";
            getline(cin,Guess);

            //判斷幾A幾B
            for (int i=0; i<4; i++)
            {
                for (int j=0; j<4; j++)
                {
                    if((Answer[i] == Guess[j])&&(i == j))
                    {
                        A++; //值吻合且位置相同
                    }

                    else if((Answer[i] == Guess[j])&&(i != j))
                    {
                        B++; //值吻合但位置不同
                    }
                }
            }

            //輸出提示
            if (A == 4)
            {
                cout << "恭喜答對!" << endl;
                break;
            }

            else if((A != 4)&&(Time > 1))
            {
                Time--;
                cout << A << "A" << B << "B" << endl;
                cout << "尚餘 " << Time << " 次機會..." << endl;
                cout << endl;

                //Debug過程用
                //cout << Answer << endl;
            }

            else
            {
                break;
            }

        }

        //
        cout << A << "A" << B << "B" << endl;
        cout << endl;
        cout << "正確解答: " << Answer << endl;
        Round++;
        Answer = "";
        Guess = "";
        Time = 5;
    }

    return 0;
}

關鍵字搜尋

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    string Data = "";
    string KeyWord = "";
    int Counter = 0;
    int Index = 0;

    cout << "Please input message: ";
    getline(cin,Data);
    cout << "Please input keyword: ";
    getline(cin,KeyWord);

    while(Index < Data.length())
    {
        if(Data.find(KeyWord,Index) != std::string::npos)
        {
            cout << "Find " << "\"" << KeyWord << "\"" << " at [" << Data.find(KeyWord,Index)  << "]."<< endl;
            Counter++;
            Index = Data.find(KeyWord,Index)+KeyWord.length() ;
        }

        else
        {
            break;
        }
    }

    cout << "\"" << KeyWord << "\"" <<  " appears "  << Counter << " times." << endl;

    system(("PAUSE"));
    return 0;
}