Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active April 29, 2025 00:25
Show Gist options
  • Save sdkfz181tiger/308adb41a0a348645df38a54c28b4caa to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/308adb41a0a348645df38a54c28b4caa to your computer and use it in GitHub Desktop.
C/C++_定番ハンズオン7選_2025
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int main(int argc, char* argv[]){
cout << "== Hello, World!! ==" << endl;
string input;
cout << "名前を入力してください" << endl;
if(cin >> input){
cout << "こんにちは, " << input << "さん!!" << endl;
}
cout << "年齢を入力してください" << endl;
if(cin >> input){
const int num = stoi(input);
if(num < 13){
cout << "少年です!!" << endl;
}else if(num < 18){
cout << "青年です!!" << endl;
}else if(num < 30){
cout << "成人です!!" << endl;
}else if(num < 65){
cout << "中年です!!" << endl;
}else{
cout << "高齢です!!" << endl;
}
}
cout << "1 ~ x までの値の合計値を計算します" << endl;
if(cin >> input){
const int num = stoi(input);
int total = 0;
for(int i=0; i<num; i++){
total += i + 1;
}
cout << "合計値は" << total << "です" << endl;
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
void dice(int index){
if(index == 1){
cout << "+-------+" << endl;
cout << "| |" << endl;
cout << "| ● |" << endl;
cout << "| |" << endl;
cout << "+-------+" << endl;
return;
}
if(index == 2){
cout << "+-------+" << endl;
cout << "| ● |" << endl;
cout << "| |" << endl;
cout << "| ● |" << endl;
cout << "+-------+" << endl;
return;
}
if(index == 3){
cout << "+-------+" << endl;
cout << "| ● |" << endl;
cout << "| ● |" << endl;
cout << "| ● |" << endl;
cout << "+-------+" << endl;
return;
}
if(index == 4){
cout << "+-------+" << endl;
cout << "| ● ● |" << endl;
cout << "| |" << endl;
cout << "| ● ● |" << endl;
cout << "+-------+" << endl;
return;
}
if(index == 5){
cout << "+-------+" << endl;
cout << "| ● ● |" << endl;
cout << "| ● |" << endl;
cout << "| ● ● |" << endl;
cout << "+-------+" << endl;
return;
}
if(index == 6){
cout << "+-------+" << endl;
cout << "| ● ● |" << endl;
cout << "| ● ● |" << endl;
cout << "| ● ● |" << endl;
cout << "+-------+" << endl;
return;
}
}
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
const int total = 6;
cout << "== サイコロ ==" << endl;
while(true){
cout << "SPACE: サイコロを振る, q: 終了" << endl;
// Keyboard
const char key = getch();
if(key == 'q') break;
// Dice
const int num = rand() % total + 1;
dice(num);
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
const string OMIKUJI[] = {"大吉", "中吉", "小吉", "吉", "末吉", "凶", "大凶"};
const int total = sizeof(OMIKUJI) / sizeof(string);
cout << "== おみくじ ==" << endl;
while(true){
cout << "SPACE: おみくじを引く, q: 終了" << endl;
// Keyboard
const char key = getch();
if(key == 'q') break;
// Omikuji
const int index = rand() % total;
cout << "結果: " << OMIKUJI[index] << endl;
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int str2num(const string &str){
int num = -1;
try{
num = stoi(str);
}catch(const invalid_argument& e){
return num;
}catch(const out_of_range& e){
return num;
}
return num;
}
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
int counter = 0;
int numMin = 0;
int numMax = 10;
int numAnswer = numMin + rand() % (numMax - numMin);
//cout << "答え: " << numAnswer << endl;
cout << "== 数当てゲーム ==" << endl;
while(true){
counter++;
cout << "数字を当ててください: " << counter << "回目" << endl;
cout << "ヒント: " << numMin << "から" << numMax << "までの範囲です" << endl;
string strInput;
if(cin >> strInput){
const int numInput = str2num(strInput);
if(numInput < numMin) continue;
if(numMax < numInput) continue;
if(numMin < 0) continue;
if(numAnswer < numInput){
cout << "残念、" << numInput << "より小さい数字です。" << endl;
numMax = numInput - 1;
continue;
}
if(numInput < numAnswer){
cout << "残念、" << numInput << "より大きい数字です。" << endl;
numMin = numInput + 1;
continue;
}
if(numInput == numAnswer){
cout << "正解です: " << numInput << endl;
cout << "チャレンジ回数: " << counter << "回" << endl;
break;
}
}
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int str2num(const string &str){
int num = -1;
try{
num = stoi(str);
}catch(const invalid_argument& e){
return num;
}catch(const out_of_range& e){
return num;
}
return num;
}
void pyramidA(int height){
cout << "== A:" << height << " ==" << endl;
for(int r=0; r<height; r++){
for(int c=0; c<r+1; c++){
cout << "*";
}
cout << endl;
}
}
void pyramidB(int height){
cout << "== B:" << height << " ==" << endl;
for(int r=0; r<height; r++){
for(int c=0; c<height-r-1; c++){
cout << " ";
}
for(int c=0; c<r+1; c++){
cout << "*";
}
cout << endl;
}
}
void pyramidC(int height){
cout << "== C:" << height << " ==" << endl;
const int width = height * 2 + 1;
for(int r=0; r<height; r++){
const int len = r * 2 + 1;// 1, 3, 5, 7...
for(int c=0; c<(width-len)/2-1; c++){
cout << " ";
}
for(int c=0; c<len; c++){
cout << "*";
}
cout << endl;
}
}
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
cout << "== ピラミッド ==" << endl;
while(true){
cout << "ピラミッドの高さを入力してください, q: 終了" << endl;
string input;
if(cin >> input){
if(input.compare("q") == 0) break;
const int height = str2num(input);
if(height < 0) continue;
pyramidA(height);
pyramidB(height);
pyramidC(height);
}
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int str2num(const string &str){
int num = -1;
try{
num = stoi(str);
}catch(const invalid_argument& e){
return num;
}catch(const out_of_range& e){
return num;
}
return num;
}
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
const string HANDS[] = {"グー", "チョキ", "パー"};
const int total = sizeof(HANDS) / sizeof(string);
int nWin = 0;
int nLose = 0;
int nEven = 0;
cout << "== ジャンケンゲーム ==" << endl;
while(true){
cout << "選択してください" << endl;
cout << "1: グー, 2: チョキ, 3: パー, q: 終了" << endl;
// Keyboard
const char key = getch();
if(key == 'q') break;
// Janken
if(key == '1' || key == '2'|| key == '3'){
const int nYou = key - '0';
const int nCom = rand() % total + 1;
cout << "You: " << HANDS[nYou - 1] << " vs ";
cout << "Com: " << HANDS[nCom - 1] << endl;
const int result = (nCom - nYou + 3) % 3;
if(result == 1){
cout << "結果: 勝ちました" << endl;
nWin++;
}else if(result == 2){
cout << "結果: 負けました" << endl;
nLose++;
}else{
cout << "結果: あいこです" << endl;
nEven++;
}
cout << "成績: ";
cout << "Win(" << nWin << "), ";
cout << "Lose(" << nLose << "), ";
cout << "Even(" << nEven << ")" << endl;
}
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <ctime>
#include <iostream>
#include "conio.h"
/*
1, $brew install cmake
2, $cmake --version
3, $ cd your_project_folder
4, $mkdir build
5, $cd build
6, $cmake ../
7, $make
8, $./test
*/
using namespace std;
int main(int argc, char* argv[]){
srand((unsigned)time(NULL));
const string words[] = {"abc", "def", "ghi", "jkl", "mno"};
const int total = sizeof(words) / sizeof(string);
int index = 0;
int score = 0;
cout << "== タイピングゲーム ==" << endl;
cout << "文字を入力してください, q: 終了" << endl;
while(true){
cout << "[問" << index+1 << "/" << total << "] " << words[index] << ": ";
string input;
if(cin >> input){
if(input.compare("q") == 0) break;
if(input.compare(words[index]) == 0){
cout << "GOOD!!" << endl;
score++;
}else{
cout << "BAD..." << endl;
}
}
index++;
if(index < total) continue;
break;
}
cout << "結果: " << score << "/" << total << "点" << endl;
return EXIT_SUCCESS;
}
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
/* reads from keypress, doesn't echo */
int getch(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/* reads from keypress, echoes */
int getche(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment