LoginSignup
1
0

More than 3 years have passed since last update.

ローマ字入力を平仮名に変換する

Last updated at Posted at 2019-09-22

改良中。。。
覚書としてのこします。

main.cpp
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>

const std::vector<std::uint8_t> sDictionary1 = {
    ' ','k','s','t','n','h','m','y','r','w','g','z','d','b','p','f'
};

const std::vector<std::uint8_t> sDictionary2 = {
    'a','i','u','e','o' 
};

const std::vector<std::string> sDictionary3 = {
    "あ", "い", "う", "え", "お",
    "か", "き", "く", "け", "こ",
    "さ", "し", "す", "せ", "そ",
    "た", "ち", "つ", "て", "と",
    "な", "に", "ぬ", "ね", "の",
    "は", "ひ", "ふ", "へ", "ほ",
    "ま", "み", "む", "め", "も",
    "や", "  ", "ゆ", "  ", "よ",
    "ら", "り", "る", "れ", "ろ",
    "わ", "  ", "を", "  ", "ん",
    "が", "ぎ", "ぐ", "げ", "ご",
    "ざ", "じ", "ず", "ぜ", "ぞ",
    "だ", "ぢ", "づ", "で", "ど",
    "ば", "び", "ぶ", "べ", "ぼ",
    "ぱ", "ぴ", "ぷ", "ぺ", "ぽ",
    "ふぁ", "ふぃ", "ふ", "ふぇ", "ふぉ", 
};

const std::vector<std::string> sDictionary4 = {
    "あ", "い", "う", "え", "お",
    "きゃ", "きぃ", "きゅ", "きぇ", "きょ",
    "しゃ", "しぃ", "しゅ", "しぇ", "しょ",
    "ちゃ", "ちぃ", "ちゅ", "ちぇ", "ちょ",
    "にゃ", "にぃ", "にゅ", "にぇ", "にょ",
    "ひゃ", "ひぃ", "ひゅ", "ひぇ", "ひょ",
    "みゃ", "みぃ", "みゅ", "みぇ", "みょ",
    "や", "  ", "ゆ", "  ", "よ",
    "りゃ", "りぃ", "りゅ", "りぇ", "りょ",
    "わ", "うぃ", "うぅ", "うぇ ", "を",
    "ぎゃ", "ぎぃ", "ぎゅ", "ぎぇ", "ぎょ",
    "じゃ", "じぃ", "じゅ", "じぇ", "じょ",
    "ぢゃ", "ぢぃ", "ぢゅ", "ぢぇ", "ぢょ",
    "びゃ", "びぃ", "びゅ", "びぇ", "びょ",
    "ぴゃ", "ぴぃ", "ぴゅ", "ぴぇ", "ぴょ",
    "ふぁ", "ふぃ", "ふ", "ふぇ", "ふぉ",
};

int main()
{
    std::string aInput;
    std::cout << "アルファベットを入力する:" << std::endl;
    std::cin >> aInput;

    // 文字サイズ
    auto aSize = aInput.size();

    // アルファベット判定
    for (auto i = 0u; i < aSize; ++i) {
        if ((aInput[i] >= 0x41) && (aInput[i] <= 0x5A)) {
            aInput[i] += 0x20; // 大文字→小文字
        }
        else if ((aInput[i] >= 0x61) && (aInput[i] <= 0x7A)) {
            // Do Nothing;
        }
        else {
            std::cout << "Not Alphabet!!" << std::endl;
            std::exit(1);
        }
    }

    std::string aOutput;

    auto aPoss = aInput.begin();
    auto aEndPoss = aInput.end();

    do {
        auto aIndexItr = std::find(sDictionary1.begin(), sDictionary1.end(), *aPoss);
        auto aOyaIndex = 0u;
        if (aIndexItr != sDictionary1.end()) {
            aOyaIndex = aIndexItr - sDictionary1.begin(); // 1文字目が子音だった場合

            if (*aPoss == *(aPoss + 1)) {
                aOutput += "っ";
                ++aPoss;
            }
            ++aPoss;
        }

        auto aFlag = false;
        if (*aPoss == 'y') {
            aFlag = true;
            ++aPoss;
        }

        // 1文字目が母音だった場合
        auto aTempIndexItr = std::find(sDictionary2.begin(), sDictionary2.end(), *aPoss);
        auto aKodomoIndex = 0u;
        if (aTempIndexItr != sDictionary2.end()) {
            aKodomoIndex = aTempIndexItr - sDictionary2.begin(); // 1文字目が子音だった場合
        }

        auto aTable = aFlag ? sDictionary4 : sDictionary3;
        aOutput += aTable.at(aOyaIndex * 5 + aKodomoIndex);
        ++aPoss;
    }
    while (aPoss != aEndPoss);

    std::cout << aOutput << std::endl;

    return 0;
}
void foo(const char* iStr, std::weak_ptr<std::ostream> oPtr)
{
    // wpの監視対象であるspが、有効なリソースを保持している状態なら処理する。
    if (std::shared_ptr<std::ostream> r = oPtr.lock()) {
        *r << iStr << std::endl;
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0