落書きノート

ふと自分が気になった事を書いてます

C++で問題を解く

最近、Gentoozshを本格的に使い始めました。便利ですね。もう少しtmuxと組み合わせて使い込んで行きたいと思います。新たな使い方が分かってくるかもしれませんし…。

#include <iostream>

using namespace std;

namespace English {
    int x = 1;

    void print_x(){
        cout << x << endl;
    }

    void hello(){
        cout << "Hello!" << endl;
    }
}

namespace Japanese {
    int x = 2;

    void print_x(){
        cout << x << endl;
    }

    void hello(){
        cout << "こんにちは!" << endl;
    }

    namespace Osaka {
        void print_x(){
            cout << "変数xの値は" << x << "でっせ。" << endl;
        }

        void hello(){
            cout << "もうかってまっか?" << endl;
        }
    }
}

int main(void){
    cout << "English::x = " << English::x << endl;
    English::print_x();
    English::hello();

    cout << "Japanese::x = " << Japanese::x << endl;
    Japanese::print_x();
    Japanese::hello();

    Japanese::Osaka::print_x();
    Japanese::Osaka::hello();

    return 0;
}
#include <iostream>

using namespace std;

namespace English {
    int x = 1;
    void print_x();
    void hello();
}

namespace Japanese {
    int x = 2;
    void print_x();
    void hello();
    namespace Osaka {
        void print_x();
        void hello();
    }
}

void English::print_x(){
    cout << x << endl;
}

void English::hello(){
    cout << "Hello!" << endl;
}

void Japanese::print_x(){
    cout << x << endl;
}

void Japanese::hello(){
    cout << "こんにちは!" << endl;
}

void Japanese::Osaka::print_x(){
    cout << x << endl;
}

void Japanese::Osaka::hello(){
    cout << "もうかってまっか?" << endl;
}

int main(void){
    using Japanese::hello;

    cout << English::x << endl;
    English::print_x();
    English::hello();

    cout << Japanese::x << endl;
    Japanese::print_x();
    hello();

    Japanese::Osaka::print_x();
    Japanese::Osaka::hello();

    return 0;
}
//kazuate.h
void initialize();
void gen_no();
int judge(int cand);
int answer();
int input(int left);
bool confirm_retry();
extern int max_no;

#include <ctime>
#include <cstdlib>
#include "kazuate.h"

using namespace std;

namespace {
    int kotae = 0;
}

void initialize(){
    srand(time(NULL));
}

void gen_no(){
    kotae = rand() % (max_no + 1);
}

int judge(int cand){
    if(cand == kotae)
        return 0;
    else if(cand > kotae)
        return 1;
    else
        return 2;
}

int answer(){
    return kotae;
}


#include <iostream>
#include "kazuate.h"

using namespace std;

namespace {
    static void prompt(){
        cout << "0~" << max_no << "の数 : ";
    }
}

int input(int left){
    int val;
    cout << "残り" << left << "回" << endl;
    do {
        prompt();
        cin >> val;
    } while(val < 0 || val > max_no);
    return val;
}

bool confirm_retry(){
    int cont;
    cout << "もう一度しますか?" << endl << "<Yes...1/No...0> : ";
    cin >> cont;
    return static_cast<bool>(cont);
}



#include <iostream>
#include "kazuate.h"

using namespace std;

int max_no = 999;

int main(void){
    initialize();
    cout << "数当てゲーム開始!" << endl;

    int max = 10;

    do {
        gen_no();
        int left = max;
        int hantei;
        do {
            hantei = judge(input(left));
            if(hantei == 1)
                cout << "\aもっと小さいですよ。" << endl;
            else if(hantei == 2)
                cout << "\aもっと大きいですよ。" << endl;
            left--;
        } while(hantei != 0 && left > 0);
        if(left > 0)
            cout << (max - left) << "回で正解しました。" << endl;
        else
            cout << "残念でした。正解は" << answer() << "でした。" << endl;
    } while(confirm_retry());

    return 0;
}