Stringと構造体とポインタ渡し

9月8日3時間目

文字列型(std::string)

よく使う便利な文字列操作関数
整数の文字列整数値に変換する関数
⇒ std::stoi(整数の文字列)

整数値整数の文字列に変換する関数
⇒ std::to_string(整数)

#include <iostream>
#include <string>
 
using std::cout;
using std::cin;
using std::endl;
//using std::string;
 
//stringの話をするよ
int main()
{
	//8文字の文字配列+'\0'で配列が初期化される
	//'\0'(終端文字)で文字列の終わりを表す
	char cstr[9] = "mojidayo";
 
	std::string str = "mojidayo";
	std::string str2 = "sono2";
	//cout << str+str2 << endl;
	  //cout << str+"mojimoji" << endl;
	//cout << str + str2 + "moji" << endl;
	//cout << str - "dayo" << endl; 引き算はできない
	for (int i = 0; i < 10; i++)
	{
		std::string tmp;
		tmp = str + "_" + std::to_string(i);
		//"mojidaho_0" ~ "mojidayo_9" 
		cout << tmp << endl;
	}
 
	std::string a = "10000";
	std::string b = "10";
	int c;
	//stoi => string to int
	c = std::stoi(a) + std::stoi(b);
	cout << c << endl;
 
}

構造体とポインタとRPG

#include <iostream>
#include <string>
 
 
using std::string;
using std::cin;
using std::cout;
using std::endl;
 
 
//ドラ〇エのキャラクターを考える
 
//HP  生命力:これが尽きると死す
//MP  魔法力:これが尽きると魔法が使えない
//STR  強さ:力の強さ→攻撃力に影響
//ATK  攻撃力:攻撃能力の高さ
//DEF  防御力:防御能力の高さ
//MGK   魔法能力:魔法を操る能力の高さ 
//ING   かしこさ:インテリジェンスの高さ
//LUK  運:運の強さ
 
//僕のヒーローのパラメータ
struct mychar {
	string name;//名前
	int job;//職業番号
	int hp;
	int mp;
	int str;
	float atk;
	float def;
	float mgk;
	int ing;
	int luk;
};
 
void setCharacterStatus(mychar *_mc, 
						string _name,
						int _job,
						int _hp,
						int _mp,
						int _str,
						float _atk,
						float _def,
						float _mgk,
						int _ing,
						int _luk)
{
	//-> アロー演算子:アドレスで渡された構造体のメンバ変数を参照する
	_mc->name = _name;
	_mc->job = _job;
	_mc->hp = _hp;
	_mc->mp = _mp;
	_mc->str = _str;
	_mc->atk = _atk;
	_mc->def = _def;
	_mc->mgk = _mgk;
	_mc->ing = _ing;
	_mc->luk = _luk;
 
}
//素敵なフォーマットでキャラのパラメータを表示する関数
//引数で、キャラクターを表す構造体を渡す(ポインタでアドレス渡し)
void printCharacterStatus(//ここ書く      
)
{
	//ここ書く
	//アドレスで受け取った、キャラクターの構造体の各パラメータを
	//なまえ:XXX
	//HP:XXX
	//みたいな感じでカッコよく表示してみよう!
}
 
int main()
{
	mychar hero;
	setCharacterStatus(&hero, "オルテガ", 0, 100, 10, 50, 100, 80, 10, 5, 20);
	cout << "なまえ:" << hero.name << endl;
 
	return 0;
}