Skip to content

Instantly share code, notes, and snippets.

@oyakodon
Created July 5, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyakodon/3e91262e648e4f7d3bbdb6f443285bee to your computer and use it in GitHub Desktop.
Save oyakodon/3e91262e648e4f7d3bbdb6f443285bee to your computer and use it in GitHub Desktop.
2017年度一年生C++勉強会用 / NITSC ProClub
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
n = n * 2;
cout << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a[4];
a[0] = 334;
a[1] = 3 + 3 + 4; // 10
a[2] = 3 * 3 * 4; // 36
a[3] = 1919;
cout << a[0] << " , " << a[2] << endl;
for (int i = 0; i < 4; i++)
{
cout << a[i] << endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, str1, str2, str3;
// 文字列の入力
cout << "文字列を入力 > ";
cin >> str;
cout << "入力された文字列は " << str << " です。" << endl;
// 文字列の連結
str1 = "oyako";
str2 = "don";
str3 = str1 + str2;
cout << "str1 + str2 = " << str3 << endl;
// i番目の文字と文字列の長さ
cout << "3番目の文字は、" << str3[2] << "で文字列の長さは、" << str3.length() << "です。" << endl;
// forとif
if (str == "oyakodon")
{
cout << "文字列strはoyakodonです。" << endl;
}
else
{
cout << "文字列strはoyakodonではありません(´・ω・`)" << endl;
}
for (int i = 0; i < str.length(); i++)
{
cout << "str[" << i << "] = " << str[i] << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "n > ";
cin >> n;
switch (n)
{
case 0:
cout << "入力された値は 0 です。" << endl;
break;
case 1:
case 2:
cout << "入力された値は 1 または 2 です。" << endl;
break;
case 3:
cout << "入力された値は 3 です。" << endl;
case 4:
cout << "入力された値は 4 です。" << endl;
break;
default:
cout << "入力された値は 0-4 以外の値です。" << endl;
break;
}
return 0;
}
#include <iostream>
using namespace std;
int sum(int x, int y)
{
int z;
cout << "関数sumが呼び出されました。" << endl;
cout << "引数x : " << x << " , 引数y : " << y << endl;
z = x + y;
cout << x << " + " << y << " = " << z << endl;
cout << "返り値を返します。" << endl;
return z;
// returnから下の行は実行されない
}
int main()
{
int a, b;
// 入力
cout << "a > "; cin >> a;
cout << "b > "; cin >> b;
// 関数の呼び出し
int result;
cout << "関数sumを呼び出します。" << endl;
cout << "--------------------" << endl;
result = sum(a, b);
cout << "--------------------" << endl;
cout << "関数mainに戻ってきました。" << endl;
cout << "返り値 : " << result << endl;
return 0;
}
// 関数を使う所(今はmain)より下に書くとエラーが出る
// ex) int sum (int x, int y) ... はエラー
#include <iostream>
#include <string>
using namespace std;
int sum(int x, int y)
{
return x + y;
}
void func1()
{
cout << "わーい!引数も返り値もない関数のフレンズだよ" << endl;
}
void func2(string str)
{
cout << "引数1つ、返り値無しの関数みたいだよ、サーバルちゃん。" << endl;
cout << str << "が得意なフレンズなんだね!すっごーい!" << endl;
cout << "// ここでfunc2()の中でstrの値を書き換える。" << endl;
str = "サーバルキャットのサーバルだよ!";
}
int func3()
{
cout << "引数なし、返り値1つの関数を作るのです。" << endl;
cout << "頭を使うには関数が必要なのです。我々はかしこいので。" << endl;
int curry, rice, curry_and_rice;
curry = 10;
rice = 5;
cout << "// sum(curry, rice)が呼ばれました。" << endl;
curry_and_rice = sum(curry, rice);
cout << "(関数の中で関数を・・・、あれはやはりヒトですよ)" << endl;
return curry_and_rice;
}
int main()
{
string str = "プログラミング";
cout << "func1()" << endl;
cout << "--------------------" << endl;
func1();
cout << "--------------------" << endl;
cout << "func2(str)" << endl;
cout << "--------------------" << endl;
func2(str);
cout << "--------------------" << endl;
cout << "str = " << str << endl;
cout << "func3()" << endl;
cout << "--------------------" << endl;
int meal = func3();
cout << "--------------------" << endl;
cout << "func3()の返り値は、" << meal << "らしいっす、心配っす・・・。" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment