C++問題集_基本速習編
※記述内容の保証はできかねます。
※言葉の概念や定義などが間違っているかもしれませんがご了承ください。
基本
問題1
「Hello World!」と表示せよ。
$ HellWorld Hello World! $
【答え】
#include<iostream> using namespace std; int main(void){ cout << "Hello World!" << endl; return(0); }
問題2
aが1、bが2のとき、aとbの和を出力せよ。
$ a_tasu_b
a と b の和は 3です。
$
【答え】
#include<iostream> using namespace std; int main(void){ int a = 1; int b = 2; int c; c = a + b; cout << "a と b の和は " << c << "です。" << endl; return(0); }
問題3−1
a、bの値をそれぞれキーボード入力で受け付け、aとbの和を出力せよ。
$ a_tasu_b_in a の値を入力--> 7 b の値を入力--> 5 a と b の和は 12です。 $
【答え】
#include<iostream> using namespace std; int main(void){ int a; int b; int c; cout << "a の値を入力--> "; cin >> a; cout << "b の値を入力--> "; cin >> b; c = a + b; cout << "a と b の和は " << c << "です。" << endl; return(0); }
問題3−2
底辺と高さの値をそれぞれキーボード入力で受け付け、三角形の面積を出力するプログラムを作成せよ。
$ delta_practice 三角形の面積を求めます。 底辺 --> 5 高さ --> 8 三角形の面積は 20 です。 $ |sh|< 【答え】 >|cpp| #include <iostream> using namespace std; int main(void){ double bottom; double height; double menseki; cout << "三角形の面積を求めます。" << endl; cout << "底辺 --> "; cin >> bottom; cout << "高さ --> "; cin >> height; menseki = ( bottom * height ) / 2; cout << "三角形の面積は " << menseki << " です。" << endl; return 0; }
問題3−3
キーボード入力で5科目の入力を受け付け、それらの合計点と平均点を出力するプログラムを作成せよ。
$ five_sum_average 5科目の点数入力せよ 科目1 -->7 科目2 -->4 科目3 -->5 科目4 -->9 科目5 -->2 合計点:27 平均点:5.4 $
【答え】
#include <iostream> using namespace std; int main(void){ double score[5]; double sum,average; cout << "5科目の点数入力せよ" << endl; for ( int i = 1; i <= 5; i++ ) { cout << "科目" << i << " -->"; cin >> score[i]; sum += score[i]; } average = sum / 5; cout << "合計点:" << sum << endl; cout << "平均点:" << average << endl; return 0; }
問題3ー4 動作未確認
必要な数だけの要素数を持った配列をユーザ入力によって用意し、その配列に任意の数(ユーザ入力)を格納し、その後それぞれを2倍にして出力せよ。
#include <iostream> using namespace std; int main(void){ int num; cout << "いくつの数を入力しますか?\n"; cin >> num; int *p p = new int[num]; cout << num << "個の数を入力してください。\n"; for ( int i = 0; i < num; i++ ){ cin >> *(p + i); } cout << "あなたが入力した数を2倍にして出力します。\n"; for ( int i = 0; i < num; i++ ){ cout << *(p + i) * 2 << endl; } return 0; }
問題4
「abc」、「123456」を20桁右寄せ、「abc」+「123456」を左寄せで出力せよ。
$ str_practice
abc
123456
abc123456
$
【答え】
#include<iostream> #include<iomanip> using namespace std; int main(void){ char str1[] = "abc"; char str2[] = "123456"; cout << setw(20) << str1 << endl << setw(20) << str2 << endl; cout << left << str1 << str2 << endl; return(0); }
※別解
#include<iostream> #include<iomanip> using namespace std; int main(void){ char *str1 = "abc"; char *str2 = "123456"; cout << setw(20) << str1 << endl << setw(20) << str2 << endl; cout << left << str1 << str2 << endl; return(0); }
問題5
int型の変数x、yに数値を入力し、xがyより大きい場合に“xはyより大きい”、等しい場合に"xはyと同じ"、xがyより小さい場合に"xはyより小さい"という文を表示せよ。
$ xy_compare x --> 5 y --> 4 xはyより大きい $ xy_compare x --> 13 y --> 13 xはyと同じ $ xy_compare x --> 8 y --> 11 xはyより小さい $
【答え】
#include<iostream> using namespace std; int main(void){ int x,y; cout << "x --> "; cin >> x; cout << "y --> "; cin >> y; if ( x > y ){ cout << "xはyより大きい" << endl; } else if(x == y){ cout << "xはyと同じ" << endl; } else { cout << "xはyより小さい" << endl; } return(0); }
構造体
問題1
Studentという名前の型(名前、身長、体重のデータを持てる)を定義し、main関数にてその構造体に値(名前:Taro、身長:172.5cm、体重:67Kg)をセットし、出力せよ。
$ struct_practice
名前:Taro
身長:172.5
体重:67
$
【答え】
#include <iostream> using namespace std; struct Student{ // char[] name; エラー。 // char name[]; エラー。初期化時のみ使用可 // char *name; char name[20]; double height; double weight; }; int main(void){ Student s = { "Taro", 172.5, 67 }; cout << "名前:" << s.name << endl; cout << "身長:" << s.height << endl; cout << "体重:" << s.weight << endl; return 0; }
問題2
Studentという名前の型(名前、身長、体重のデータを持てる)を定義し、main関数にてその構造体をsという名前で宣言し、StuFuncという関数に参照型で渡し、その関数内で構造体各メンバに値(名前:Jiro、身長:168.4cm、体重:72.3Kg)をセットし、main関数で出力せよ。
$ struct_practice2 名前:Jiro 身長:168.4cm 体重:72.3kg
【答え】
#include <iostream> using namespace std; struct Student{ char name[20]; double height; double weight; }; void StuFunc(Student&); int main(void){ Student s; StuFunc(s); cout << "名前:" << s.name << endl; cout << "身長:" << s.height << "cm" << endl; cout << "体重:" << s.weight << "kg" << endl; return 0; } void StuFunc(Student& s){ // s.name = "Taro"; エラー // 配列への後からの代入不可 strcpy( s.name, "Jiro" ); s.height = 168.4; s.weight = 72.3; }
※別解
#include <iostream> using namespace std; struct Student{ <span style="color: #ff0097">string name;</span> // C++ではこれが使える double height; double weight; }; void StuFunc(Student&); int main(void){ Student s; StuFunc(s); cout << "名前:" << s.name << endl; cout << "身長:" << s.height << "cm" << endl; cout << "体重:" << s.weight << "kg" << endl; return 0; } void StuFunc(Student& s){ // s.name = "Jiro"; エラー // 配列への後からの代入不可 <span style="color: #ff009b">s.name = "Jiro";</span> s.height = 168.4; s.weight = 72.3; }
問題3
Studentという名前の構造体(名前、身長、体重のメンバを持て、さらにその構造体の中に「メンバに値(名前:Saburo、身長:156.2cm、体重:58Kg)をセットするためのdatesetという名の関数」を定義する。「メンバにセットした値を出力するためのoutputという名の関数」については構造体内で宣言のみ行う。main関数にてその構造体をsという名前で宣言し、構造体sのメンバ関数を呼び出して、メンバ関数によってデータセット・データ出力を行え。
$ struct_practice3 名前:Saburo 身長:156.2cm 体重:58kg $
【答え】
#include <iostream> using namespace std; struct Student{ char name[20]; // データメンバ double height; double weight; void dataset(void){ // void strcpy( name, "Saburo" ); // 構造体内で定義したメンバを使用するときは // 「s.」のような語句不要 height = 156.2; weight = 58; } void output(void); // 構造体の外でメンバ関数定義する場合、 // 普通の関数同様、プロトタイプ宣言のみ }; void Student::output(void){ // 構造体の外で定義する場合は構造体型を // 「Student::〜」のように明示 cout << "名前:" << name << endl; cout << "身長:" << height << "cm" << endl; cout << "体重:" << weight << "kg" << endl; } int main(void){ Student s; s.dataset(); s.output(); return 0; }
問題4
3つのメンバa,b,sumを持つ構造体を定義し、構造体の宣言・a,bの値の設定・aとbの足し算をsumに設定、sumの出力をmain関数で行え。
$ struct_practice4
a 足す b は 15
$
#include <iostream> using namespace std; struct Data{ int a; int b; int sum; }; int main(void){ Data d = { 5, 10, }; // 以下でも可能 // Data d; // d.a=6; // d.b=10; d.sum = d.a + d.b; cout << "a 足す b は " << d.sum << endl; return 0; }
クラス
構造体structとクラスclassはほとんど同じようなものであるらしい。
構造体はクラスの一種とも言われる。
BohYoh.com-C/C++ FAQ structとclassの違いは何ですか。
ポイントは、
データメンバ:private
メンバ関数:public
にすること.
問題1
Studentという名前のクラス(名前、身長、体重のメンバを持て、さらにそのクラスの中に「メンバに値をセットするためのdatesetという名の関数」の宣言のみ行う。「メンバにセットした値を出力するためのoutputという名の関数」についてはクラス内で定義を行う。main関数にてそのクラスからsという名前のインスタンスを生成し、インスタンスsのメンバ関数を呼び出して、メンバ関数datasetにて値(名前:Ichiro、身長:166.8cm、体重:54Kg)のデータセット・データ出力を行え。
$ class_practice 名前:Ichiro 身長:156.2cm 体重:58kg $
【答え】
#include <iostream> using namespace std; class Student{ private: char name[20]; // データメンバ double height; double weight; public: // void dataset(char, double,double); エラー // void dataset( char*, double, double ); エラー void dataset(); // mainで呼び出す形と同じ(ここでは引数なし)にする必要がある void output(void){ // 構造体の外で定義する場合は構造体型を cout << "名前:" << name << endl; cout << "身長:" << height << "cm" << endl; cout << "体重:" << weight << "kg" << endl; } }; //void Student::dataset( char* name, double height, double weight ){ エラー // 自分のクラスが持つメンバなので引数に渡す必要がない void Student::dataset(){ // strcpy( *name, "Saburo" ); エラー strcpy( name, "Ichiro" ); // アドレスはアドレスに height = 156.2; weight = 58; } int main(void){ Student s; s.dataset(); s.output(); return 0; }
問題2
ユーザから入力を受け付けるinputクラスを実行し、1からユーザ入力された数までの和を求めるCalcクラスを実行し、main関数でその結果をGoukeiクラスを用いて出力せよ。ただし、ユーザ入力によって受け付けた数はmain関数内では保持せずクラスのメンバとして保持すること。また、インスタンス生成時の初期化とクラス呼び出し終了時のメモリ解放を行うこと。
$ class_practice2 コンストラクタ実行 数を入力 --> 6 合計は21です。 デコンストラクタ実行 $
【答え】
#include <iostream> using namespace std; class Souwa{ private: long input; long goukei; public: Souwa(long x = 0, long y = 0){ // コンストラクタ。インスタンス生成時に実行 input = x; goukei = y; cout << "コンストラクタ実行" << endl; } ~Souwa(){ // デストラクタ。クラス呼び出し終了時に実行 cout << "デコンストラクタ実行" << endl; } void Setnum(void){ // cout << "数を入力 --> " エラー // cin >> input >>endl; cout << "数を入力 --> " ; // coutとcinは連続しては使えない // cin >> input >> endl; cinはendlnしない cin >> input; } void Calc(void){ // for (int i = 0; i <= input; i++ ){ エラー for (long i = 0; i <= input; i++ ){ goukei += i; } } long Goukei(void){ return goukei; } }; int main(void){ Souwa s; s.Setnum(); s.Calc(); // cout << "1から" << s.n << "までの合計は" << s.Goukei() << "です。" << endl; // 関数を使わないとprivateなメンバにはアクセスできない cout << "合計は" << s.Goukei() << "です。" << endl; return 0; }
ファイル処理
問題1
char型文字列「あいうえお」とint型数値「123」をファイルabc.txtに出力せよ。
$ file_practice
ファイル「abc.txt」を作成しました。
$ cat abc.txt
あいうえお
123
$
【答え】
#include <iostream> #include <fstream> using namespace std; int main(void){ char word[20] = "あいうえお"; int num = 123; ofstream ofile; // ファイルのインスタンス生成 ofile.open( "abc.txt", ios::out ); // ファイルオープン // 上記2行は1行でも書ける // ofstream ofile( "abc.txt" ); // ofile.open( "abc.txt" ); 新規作成(一旦ファイル内容消去)時は第二引数省略可 // ofile.open( "abc.txt", ios::app ); 追記(ファイル内容消去しない)時 if(!ofile){ // C言語とは異なり、インスタンスの存在でオープン処理判定 cout << "ファイルオープンエラー" << endl; return 1; } ofile << word << endl; // ファイル出力:インスタンス「ofile」へ ofile << num << endl; ofile.close(); // ファイルクローズ cout << "ファイル「abc.txt」を作成しました。" << endl; return 0; }
問題2
「かきくけこ
456」
と書かれたファイルdef.txtを事前に用意し、def.txtの内容を読み込んで出力せよ。
$ cat def.txt かきくけこ 456 $ file_practice2 かきくけこ 456 $
【答え】
#include <iostream> #include <fstream> using namespace std; int main(void){ char word[20]; int num; //ifstream ifile; //ifile.open("def.txt"); // 1行でも書ける ifstream ifile("def.txt"); if(!ifile){ cout << "ファイルオープンエラー" << endl; return 1; } ifile >> word >> num; cout << word << endl; cout << num << endl; return 0; }
問題3
「さしすせそたちつてとなにぬねの
My name is Yamada Taro.
I am 19 years old.
To understand what recursion is, you must first understand recursion.」
と書かれたファイルghi.txtを事前に用意し、def.txtの内容を読み込んで出力せよ。
$ cat ghi.txt さしすせそたちつてとなにぬねの My name is Yamada Taro. I am 19 years old. To understand what recursion is, you must first understand recursion. $ $ file_practice3 さしすせそたちつてとなにぬねの My name is Yamada Taro. I am 19 years old. To understand what recursion is, you must first understand recursion. $
【答え】
#include <iostream> #include <fstream> using namespace std; int main(){ ifstream ifile; ifile.open( "ghi.txt", ios::in ); if(!ifile){ cout << "ファイルオープンエラー" << endl; return 1; } char sent[256]; while(ifile.getline(sent, 256), !ifile.eof()){ // スペースを含む文字列に「ifile >>」は使えない // C言語で書いた場合以下のようになるだろう(インスタンスとポインタは別物だが) // while( fgets( sent, 256, ifile ) != NULL ){ cout << sent << endl; } ifile.close(); return 0; }
※ちなみに、
「To understand what recursion is, you must first understand recursion.」とは、
「再帰とは何かを理解するためには、まず再帰を理解する必要がある。」というの意味のプログラマーの言葉遊び。