c++メモ(一)基本データ型と式

24079 ワード

いくつかのc++プログラミングの例題を始めて自分のプログラミング能力を回復します
蓄積したばらばらな知識を記録する
1.整数関数
floor()は負の無限大に切り込まれ、floor(-10.5)=-11である.Ceil()は正無限大に丸められ、ceil(-10.5)=-10である.
2.大文字と小文字の変換
小文字ascII?大文字より32大きい.変換式はchar=char-0 x 20//char=char-‘a’+‘A’//(-32)
3.stringクラス
1)プログラムでstringタイプを使用するためには、ここではstringではないことに注意するヘッダファイルを含む必要がある.h,string.hはC文字列ヘッダファイルである.2)==、>、=、<=、および!=文字列を比較すると、+または+=オペレータで2つの文字列を接続でき、[]で特定の文字を取得できます.3)特性関数int capacity()const;//現在の容量(stringでメモリを増やさずに格納できる要素の数)int max_size()constを返します.//stringオブジェクトに格納可能な最大文字列の長さint size()constを返します//現在の文字列のサイズint length()constを返します//現在の文字列の長さbool empty()constを返します//現在の文字列が空のvoid resize(int len,char c);//文字列の現在のサイズをlenに設定し、多く補完し、多くの文字cが不足している部分を埋める4)常用関数string&insert(int p,const string&s);p位置に文字列s string&replace(int p,int n,const char*s);//pから始まるn文字を削除し、pにシリアルs string&erase(int p,int n)//を挿入します.pから始まるn文字を削除し、修正した文字列string substr(int pos=0、int n=npos)const;//posから始まるn文字の文字列void swap(string&s 2)//を返します.現在の文字列とs 2の値string&append(const char*s);//文字列sを現在の文字列の末尾void push_に接続するback(char c)/現在の文字列の末尾に文字cを付ける
const char data()const;//nullで終了しないc文字配列を返します.data():とc_str()と同様に、string回転const charで返される配列は空の文字で終了しません.
const char c_str()const;//nullで終了したc文字列、すなわちc_を返します.str()関数は、本string列と同じ内容の正規C文字列を指すポインタを返し、string回転const charに使用します.
size_type find( const basic_string &str, size_type index );//strが文字列に最初に表示された場所(indexから検索)を返し、見つからない場合はstring::npos size_type find(const char*str、size_type index)//を返します.同上size_type find( const char *str, size_type index, size_type length );//strが文字列に最初に現れた位置(indexから検索し、長さはlength)を返し、見つからなかったらstring::npos size_type find(char ch,size_type index);//文字列に最初に出現したchの位置を返し(indexから検索)、見つからない場合string::nposを返します.
4.setw()関数
この関数は、出力間隔を制御するための後続の出力(例えばcout<デフォルトはスペースです。setfill()を使用して、他の文字を設定して入力できます。)である
5.一般的なマニピュレータ
dec 10進数表示整数入出力hex 16進数表示整数入出力oct 8進数表示整数入出力wsプリアンブルスキップ入力endl出力改行文字を出力ストリーム出力ends出力空文字('0')をリフレッシュストリーム出力setprecision(int p)設定精度ビット数p出力setw(int w)をリフレッシュフィールド幅をw出力に設定注意:I/Oストリームクラスライブラリの特定のマニピュレータを使用する場合は、プログラムの上部に前処理コマンドincludeを追加する必要があります.
6.<
<左シフト記号の意味もあり、a<<8は左シフト8ビット同理可得>>
7.ファイルの読み書き
ヘッダファイル#includeファイルの3つの操作ofstream書き込みifstream読み込み操作fstream読み書き操作
ファイルの読み書きの手順:1、含むヘッダファイル:#include 2、ストリームの作成3、ファイルを開く(ファイルとストリームの関連付け)4、読み書き(書き込み操作:<>、get()、getline()、read()5、ファイルを閉じる:バッファデータを完全にファイルに書き込み、ファイル終了フラグを追加し、ストリームオブジェクトと外部ファイルの接続を切断する
書き込みファイル:1)<動作単位で書き込む2)put()でメモリから1バイトファイルに書き込む3)関数write()機能:bufが指す内容をnバイト取ってファイルに書き込む関数宣言:ostream&ostream::write(char*buf,int n);パラメータ説明:bufはメモリに書き込むアドレスを表し、パラメータを渡すときにアドレスを取ります.nバイトを読み込む長さを示す
ファイルを読む:1)>>常にスペース、Tab、リターンで終わるので、行為単位で読むのではなく、単語単位で読むことはできません.2)getline()行単位でメモリを読み込み、1行3)get()ファイルから1バイトをメモリ4に読み込むread()機能:ファイルからnバイトのデータを抽出し、bufが指す場所に関数宣言を書き込む:istream&read(char*buf,int n);
練習のコードを添付します.
#include "stdafx.h"
#include
#include
#include

using namespace std;


void t21()
{
	char c;
	cin >> c;
	c = c - 'a' + 'A';
	cout << c << endl;
	
}

void t22()
{
	int n;
	cin >> n;
	n = n % 100;
	n = n / 10;
	cout << n << endl;
	

}

void t23()
{
	float a, b, h, s,bs,roll;
	cout << "    :";
	cin >> a;
	cout << "    :";
	cin >> b;
	cout << "    :";
	cin >> h;
	cout << "      ";
	cin >> bs;

	s = (a + b) * 2 * h;
	roll = s / bs;
	roll = ceil(roll);
	cout <<"     :"<< roll << endl;

}
void t24()
{
	const int HFEE = 100;
	const int RFEE = 1800;
	string sname;
	int hour, total;
	cout << "    :";
	cin >> sname;
	cout << "    :";
	cin >> hour;
	total = hour * HFEE + RFEE;

	cout << sname << ":" << total << endl;

}
void t25()
{
	int year;
	cin >> year;
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
		cout << year << "    ";
	else
		cout << year << "     ";
}
void t26()
{
	typedef unsigned long u_long;
	u_long b1, b2, b3, b4;
	u_long addr = 0;
	char ch;
	cin >> b4 >> ch >> b3 >> ch >> b2 >> ch >> b1;
	addr = addr | b4;
	addr = (addr << 8) | b3;
	addr = (addr << 8) | b2;
	addr = (addr << 8) | b1;
	cout << hex << addr << endl;
}
void t27()
{
	ofstream ofile;
	ofile.open("odata.txt");
	if (ofile.is_open())
	{
		cout << "     ctrl+z  " << endl;
		char ch;
		while ((ch = cin.get()) != EOF)
		{
			if ((ch >= 'a') && (ch <= 'z'))
				ch = 'a' + (ch + 4 - 'a') % 26;
			else if ((ch >= 'A') && (ch <= 'Z'))
				ch = 'A' + (ch + 4 - 'A') % 26;
			ofile << ch;
		}
		ofile.close();
	}
	else {
		cout << "      ";
	}
}
void t28()
{
	ifstream ifile;
	ifile.open("odata.txt");
	if (ifile.is_open())
	{
		cout << "    :" << endl;
		char ch;
		while ((ch = ifile.get()) != EOF)
		{
			if ((ch >= 'a') && (ch <= 'z'))
				ch = 'z' - ('z' - ch + 4) % 26;
			else if ((ch >= 'A') && (ch <= 'Z'))
				ch = 'Z' - ('Z' - ch + 4) % 26;
			cout << ch;
		}
		ifile.close();
	}
	else {
		cout << "      ";
	}
	system("pause");
}
void t29()
{
	int n;
	cin >> n;
	n = n | 0x0f;
	cout << n << endl;

}
int main()
{
	//t21();
	t29();
	system("pause");
	return 0;
	
}