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