Visual Studio 2008 Express Edition で三択ゲームを作ろう
Excelエクセルのcsvファイルを読み込み問題を出題する
Excelで問題を作りcsv形式で保存する
c++起動
新規作成 プロジェクト Windowsアプリケーション
上記のようにFormデザインをするButton RadioButton TextBox
OpenFileDailogとmenuStripをFormの上にドラックする
Load と書きいれ ここをクリック
| 説c++ステートメント明 | Basicとの違い | |
| コメント分 | //実行されない | rem 実行されない文 |
| 改行 | ; セミコロン |
エンターキー |
if文 ==2個で= |
if(a==b) c=10; | if a=b then c=10 |
| if文 | if(a==b) c=10; |
if a=b then |
| 乱数 | x=rand(); |
x=rand(); 0から0.9999999まで |
| radioButton | if(radioButton->Cheaked==true) x=1; | if OptionButton1=True Then x=1 |
| 配列 宣言 使用法 |
整数配列 1次元 |
整数・実数ともに |
| 配列 宣言 使用法 |
整数配列 2次元 int b[10][10]; a=b[x][y]; |
整数・実数ともに dim b(10,10) a=b(x,y) |
配列 宣言
|
array<int>^b = gcnew array<int>(10); x=b(y); |
整数・実数ともに dim b(10) a=b(y) |
| 配列 宣言 使用法 |
array<int,2>^c = gcnew array<int,2>(2, 5); a=b(x,y); |
整数・実数ともに dim b(10,10) a=b(x,y) |
array<array<String^>^>^ b=gcnew array<array<String^>^>(10);
|
dim a$(10,10) a$="aaaaa" |
|
次のプログラムを入れる 赤字が入力したところ
tdafx.h
// stdafx.h : 標準のシステム インクルード ファイルのインクルード ファイル、または
// 参照回数が多く、かつあまり変更されない、プロジェクト専用のインクルード ファイル
// を記述します。
#pragma once
// TODO: プログラムに必要な追加ヘッダーをここで参照してください。
#include <stdlib.h>
// 乱数で必要
Form1
#pragma once
namespace santaku {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
// yh
using namespace System::IO;
//yh end
/// <summary>
/// Form1 の概要
///
/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
/// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
/// 変更する必要があります。この変更を行わないと、
/// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
/// 正しく相互に利用できなくなります。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクタ コードを追加します
//
}
// yh
public:
String^ fname;
array<array<String^>^>^ TableData;
int data_no,data_x;
//yh end
private: System::Windows::Forms::Button^ button2;
public:
protected:
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::RadioButton^ radioButton1;
private: System::Windows::Forms::RadioButton^ radioButton2;
private: System::Windows::Forms::RadioButton^ radioButton3;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ loadToolStripMenuItem;
private:
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->loadToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->button2 = (gcnew System::Windows::Forms::Button());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 46);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(597, 22);
this->textBox1->TabIndex = 0;
//
// button1
//
this->button1->Location = System::Drawing::Point(71, 398);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(173, 65);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// radioButton1
//
this->radioButton1->AutoSize = true;
this->radioButton1->Location = System::Drawing::Point(45, 89);
this->radioButton1->Name = L"radioButton1";
this->radioButton1->Size = System::Drawing::Size(110, 19);
this->radioButton1->TabIndex = 2;
this->radioButton1->TabStop = true;
this->radioButton1->Text = L"radioButton1";
this->radioButton1->UseVisualStyleBackColor = true;
//
// radioButton2
//
this->radioButton2->AutoSize = true;
this->radioButton2->Location = System::Drawing::Point(44, 145);
this->radioButton2->Name = L"radioButton2";
this->radioButton2->Size = System::Drawing::Size(110, 19);
this->radioButton2->TabIndex = 3;
this->radioButton2->TabStop = true;
this->radioButton2->Text = L"radioButton2";
this->radioButton2->UseVisualStyleBackColor = true;
//
// radioButton3
//
this->radioButton3->AutoSize = true;
this->radioButton3->Location = System::Drawing::Point(46, 206);
this->radioButton3->Name = L"radioButton3";
this->radioButton3->Size = System::Drawing::Size(110, 19);
this->radioButton3->TabIndex = 4;
this->radioButton3->TabStop = true;
this->radioButton3->Text = L"radioButton3";
this->radioButton3->UseVisualStyleBackColor = true;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(397, 89);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(191, 22);
this->textBox2->TabIndex = 5;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(395, 143);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(193, 22);
this->textBox3->TabIndex = 6;
//
// openFileDialog1
//
this->openFileDialog1->FileName = L"openFileDialog1";
this->openFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::openFileDialog1_FileOk);
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->loadToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(633, 31);
this->menuStrip1->TabIndex = 7;
this->menuStrip1->Text = L"menuStrip1";
//
// loadToolStripMenuItem
//
this->loadToolStripMenuItem->Name = L"loadToolStripMenuItem";
this->loadToolStripMenuItem->Size = System::Drawing::Size(57, 27);
this->loadToolStripMenuItem->Text = L"Load";
this->loadToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::loadToolStripMenuItem_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(333, 402);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(183, 57);
this->button2->TabIndex = 8;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(633, 515);
this->Controls->Add(this->button2);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->radioButton3);
this->Controls->Add(this->radioButton2);
this->Controls->Add(this->radioButton1);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void openFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
fname="santaku.csv";
String^ FileName = fname; // ファイル名を指定
String^ CodeType = "shift_jis"; // 文字コードは、シフトJISコードを指定
System::Text::Encoding^ encod = System::Text::Encoding::GetEncoding(CodeType);
System::IO::StreamReader^ reader = gcnew System::IO::StreamReader(gcnew System::IO::FileStream(FileName, System::IO::FileMode::Open), encod);
String^ TextData = reader->ReadToEnd();
reader->Close();
array<String^>^ LineData = TextData->Split(gcnew array<String^>{"\r", "\r", "\r"}, StringSplitOptions::RemoveEmptyEntries);
TableData = gcnew array<array<String^>^>(LineData->Length);
for(int i = 0; i < LineData->Length; i++){
TableData[i] = LineData[i]->Split(',');
}
/*for(int i = 0; i < TableData->Length; i++){
for(int j = 0; j < TableData[i]->Length; j++){
// TableData[i][j]を用いた処理
}
}*/
//textBox1->Text=TableData[i][j];
data_no=TableData->Length;
}
private: System::Void loadToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
OpenFileDialog^ ofdlg=gcnew OpenFileDialog();
ofdlg->Filter="Dataファイル(*.csv)|*.csv";
if(ofdlg->ShowDialog()!=System::Windows::Forms::DialogResult::OK) return;
fname=ofdlg->FileName;
String^ FileName = fname; // ファイル名を指定
String^ CodeType = "shift_jis"; // 文字コードは、シフトJISコードを指定
System::Text::Encoding^ encod = System::Text::Encoding::GetEncoding(CodeType);
System::IO::StreamReader^ reader = gcnew System::IO::StreamReader(gcnew System::IO::FileStream(FileName, System::IO::FileMode::Open), encod);
String^ TextData = reader->ReadToEnd();
reader->Close();
array<String^>^ LineData = TextData->Split(gcnew array<String^>{"\r", "\r", "\r"}, StringSplitOptions::RemoveEmptyEntries);
TableData = gcnew array<array<String^>^>(LineData->Length);
for(int i = 0; i < LineData->Length; i++){
TableData[i] = LineData[i]->Split(',');
}
/*for(int i = 0; i < TableData->Length; i++){
for(int j = 0; j < TableData[i]->Length; j++){
// TableData[i][j]を用いた処理
}
}*/
//textBox1->Text=TableData[i][j];
data_no=TableData->Length;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
// x=rand()*data_no/32767; //stdafx.hに #include <stdlib.h>必要
// x=rand()%data_no; //stdafx.hに #include <stdlib.h>必要
// %であまりの数を求めて問題番号としてもよい
data_x=rand()*10/32767; //stdafx.hに #include <stdlib.h>必要
// textBox1->Text=data_x.ToString();
textBox1->Text=TableData[data_x][0];
textBox2->Text=TableData[data_x][5];
radioButton1->Text=TableData[data_x][1];
radioButton2->Text=TableData[data_x][2];
radioButton3->Text=TableData[data_x][3];
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
int x,n;
if(radioButton1->Checked == true) x=1;
if(radioButton1->Checked == true) x=2;
if(radioButton1->Checked == true) x=3;
n = Convert::ToInt32(TableData[data_x][4]);
if(n==x) textBox1->Text="正解です";
else textBox1->Text = TableData[data_x][n];
}
};
}
/*
abiko_tetuさん より一部引用
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1122345816
*/



