Advertisement
Guest User

Untitled

a guest
Jun 30th, 2020
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.31 KB | None | 0 0
  1. #pragma warning(disable: 4996)
  2. #include "DxLib.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <random>
  7. #include <vector>
  8.  
  9.  
  10. char String[256];
  11. int InputHandle;
  12. int mozicount = 0;
  13. int gimonnlock = 0;
  14. int gimon = 0;
  15.  
  16. char my_str2(const char* s1, const char* s2)//ここで入力した文字列と用意された文字列を引数として扱う。
  17.  
  18. {
  19.     //s1, s2を比較する関数を使うためだけにs2の文字列のサイズが必要なので、変数aに用意した文字列の情報s2を文字列の長さを測るための関数strlenに引数として渡す。
  20.     const size_t a = strlen(s2);
  21.     //無限ループする。
  22.     for (;;) {
  23.         //関数memcmpの返り値が0の時は一致した時なので、==0とする。
  24.         if (memcmp(s1, s2, a) == 0)
  25.  
  26.             return 1;//入力した文字列にい指定された文字列が入っていた場合は1を返すように設定した。
  27. //入力した文字列が最後の文字まで到達した場合は一致する文字列がないということなので0を返すようにした。
  28.         else if (*s1 == '\0')
  29.  
  30.             return 0;//入っていなかった
  31. //文字列が一致した場合でも一致する文字列がない場合でも入力した文字列の一文字分の文字コードのバイト数?が繰り上がるようにした。
  32.         else
  33.  
  34.             ++s1;
  35.  
  36.     }
  37. }
  38. int memory = 0;
  39. std::string result;
  40. char buffer[256];//★InputHandleに入ったのは文字のデータなので、文字のデータが受け取れる変数の型にする。
  41. char buffer2[256];
  42. char input[256];
  43. char* p;
  44. FILE* outputfile;         // 出力ストリーム
  45. int hyouzi = 0;
  46.  
  47. std::string find_word(const std::string& file, const std::string& buffer) {
  48.     std::string result;
  49.     std::ifstream stream(file);
  50.     if (stream.is_open()) {
  51.         std::vector<std::string> lines;
  52.         std::string line;
  53.         // bufferを含む文字列の集合をlinesに求める
  54.         while (std::getline(stream, line)) {
  55.             if (line.find(buffer) != std::string::npos) {
  56.                 lines.push_back(line);
  57.             }
  58.         }
  59.         // linesが空でなければ、そのうちいずれかをデタラメに返す
  60.         if (!lines.empty()) {
  61.             std::random_device gen;
  62.             std::uniform_int_distribution<int> dist(0, lines.size() - 1);
  63.             result = lines.at(dist(gen));
  64.         }
  65.     }
  66.     return result;
  67. }
  68. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  69. {
  70.     SetGraphMode(1500, 780, 32);         // ウィンドウの大きさを指定
  71.     ChangeWindowMode(TRUE);             // 全画面ではなくウインドウを使用
  72.     // DXライブラリの初期化
  73.     if (DxLib_Init() == -1) return -1;
  74.     SetFontSize(42);                             //サイズを42に変更
  75.     // 描画先を裏にする
  76.     SetDrawScreen(DX_SCREEN_BACK);
  77.     InputHandle = MakeKeyInput(150, FALSE, FALSE, FALSE); //これで総計150バイトの文字データを保持できる。
  78.  
  79.     // 作成したキー入力ハンドルをアクティブにする
  80.     SetActiveKeyInput(InputHandle);
  81.  
  82.     while (ProcessMessage() == 0)
  83.     {
  84.         // 画面の初期化
  85.         ClearDrawScreen();
  86.         // 入力モードを描画
  87.         DrawKeyInputModeString(640, 480);
  88.         // 入力途中の文字列を描画
  89.         DrawKeyInputString(0, 0, InputHandle);//InputHandleはint型とリファレンスに書いてあったんで
  90.          //エンターキーが押された時の部分。
  91.         if (CheckKeyInput(InputHandle) != 0) {
  92.             hyouzi = 0;
  93.             gimon = 0;
  94.             ++mozicount;
  95.             // 入力された文字列を取得、その文字列を数値に変換
  96.             GetKeyInputString(buffer, InputHandle);
  97.             if (memory == 0) {
  98.                 outputfile = fopen("da.txt", "r");  // ファイルを読み込み用にオープン(開く)
  99.                 if (outputfile == NULL) {          // オープンに失敗した場合
  100.                     printf("cannot open\n");         // エラーメッセージを出して
  101.                     exit(1);                         // 異常終了
  102.  
  103.                     while ((p = fgets(buffer2, 256, outputfile)) != NULL){
  104.  
  105.                     // 文字の入力の入るバッファと、メモからの文字が入るバッファ2とで一致する文字が出てきた場合
  106.                     if (my_str2(buffer, buffer2) == 0) {
  107.                         std::string result = find_word("da.txt", buffer);
  108.                         break;
  109.                     }
  110.                     // 文字の入力の入るバッファと、メモからの文字が入るバッファ2とで一致しない文字が出てきた場合
  111.                     if (hyouzi == 0) {
  112.                         if (strcmp(buffer, buffer2) != NULL) {
  113.                             gimon = 1;
  114.                         }
  115.                     }
  116.                 }
  117.                 fclose(outputfile);          // ファイルをクローズ(閉じる)
  118.             }
  119.             //できればエンターキーを押して、次の文章を記憶できるようにしたかったが、文章には必ずと言っていいほど「は」や「とは」
  120.             //が入るし、定義のように覚えさせたいので「覚えて」の後に下のような条件で文章を記憶するようにさせた。
  121.             if (memory == 1 && my_str2(buffer, "とは") or memory == 1 && my_str2(buffer, "は") or memory == 1 && my_str2(buffer, "が")) {
  122.              
  123.                ++mozicount;
  124.                 memory = 2;
  125.             }
  126.             //duration = 1;
  127.             // 再度インプットハンドルをアクティブにする
  128.             SetActiveKeyInput(InputHandle);
  129.             // 入力文字列を初期化する
  130.             SetKeyInputString("", InputHandle);
  131.         }
  132.  
  133.         //エンターキーが押されていないときでの処理
  134.         if (memory == 2) {
  135.             outputfile = fopen("da.txt", "a+");  // ファイルを書き込み用にオープン(開く)
  136.             if (outputfile == NULL) {          // オープンに失敗した場合
  137.                 printf("cannot open\n");         // エラーメッセージを出して
  138.                 exit(1);                         // 異常終了
  139.             }
  140.             fprintf(outputfile, "%s\n", buffer); // ファイルに書く
  141.             fclose(outputfile);          // ファイルをクローズ(閉じる)
  142.             memory = 0;
  143.          
  144.         }
  145.         if (p != NULL or hyouzi == 1) {
  146.             DrawFormatString(100, 500, GetColor(5, 105, 19), " buffer2は%s,resultは%s", buffer2, result.c_str());
  147.         }
  148.         if (hyouzi == 0 && gimon == 1) {
  149.             DrawFormatString(100, 600, GetColor(5, 105, 0), "申し訳ありません%sとは何ですか?", buffer);
  150.         }
  151.         // 裏画面の内容を表画面に反映させる
  152.         ScreenFlip();
  153.     }
  154.     // 用済みのインプットハンドルを削除する
  155.     DeleteKeyInput(InputHandle);
  156.     // 画面の初期化
  157.     ClearDrawScreen();
  158.     // 裏画面の内容を表画面に反映させる
  159.     ScreenFlip();
  160.     // DXライブラリの使用終了
  161.     DxLib_End();
  162.     // 終了
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement