2011/04/01

亂數

#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;
}

2011/02/03

民國轉西元紀年

#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    string InName = "";
    string OutName = "";
    string LineData = "";
    string TempData = "";
    stringstream Final;
    int Data;

    string List[10000][3];
    int Row = 0;
    int Col = 0;

    for(int i=0; i<100; i++)
    {
        for(int j=0; j<3; j++)
        {
            List[i][j] = "-1";
        }
    }

    cout << "Please Input Source File:";
    getline(cin,InName);
    cout << "Please Input Output File:";
    getline(cin,OutName);
    ifstream infile(InName.c_str(),ios::in);
    ofstream outfile(OutName.c_str(),ios::app);

    if(infile)
    {
        while(!infile.eof())
        {
            getline(infile,LineData);
            LineData = LineData + "/";

            for(int i=0; i<LineData.length(); i++)
            {
                if(LineData[i] != '/')
                {
                    TempData = TempData + LineData[i];
                }
                else
                {
                    List[Row][Col] = TempData;
                    Col++;
                    TempData = "";
                }
            }
            Col = 0;
            Row++;
            LineData = "";
        }
    }
    //讀取失敗時提示錯誤
    else
    {
        cout << "WRONG" << endl;
    }
    //進行轉換
    for(int i=0; i<Row; i++)
    {
        if(List[i][0] != "-1")
        {
            Data = atoi(List[i][0].c_str())+1911;
            Final << Data;
            List[i][0] = Final.str();
            Final.str("");
        }
        else
        {
            break;
        }
    }
    //輸出檔案
    for(int i=0; i<Row; i++)
    {
        for(int j=0; j<3; j++)
        {
            outfile << List[i][j];

            if(j<2)
            {
                outfile << "/";
            }
        }
        outfile << endl;
    }

    cout << "DONE." << endl;
    return 0;
}