2006-11-01から1ヶ月間の記事一覧

文字列の連結

#include <stdio.h> #include <string.h> int main(void) { char s1[] = "Nikkei "; char s2[] = "Software"; char t[20]; strcpy(t, s1); strcat(t, s2); printf("tの内容:%s\n", t); return 0; }sprintf関数を使うこともできる。 #include <stdio.h> int main(void) { char s1[] = "Nik</stdio.h></string.h></stdio.h>…

ポインタを使って文字列をコピーする

#include <stdio.h> void string_copy(char *pdest, char *psrc) { while(*psrc != '\0') *pdest++ = *psrc++; *pdest = '\0'; } int main(void) { char src[] = "Nikkei Software"; char dest[20]; string_copy(dest, src); printf("src = %s\n", src); printf("dest</stdio.h>…

int型のデータをchar*型,short*型,int*型,float型のポインタでアクセス

#include <stdio.h> int main(void) { int n = 0x12345678; char *pc = &n; short *ps = &n; int *pi = &n; float *pf = &n; printf("*pc = %x\n", *pc); printf("*ps = %x\n", *ps); printf("*pi = %x\n", *pi); printf("*pf = %x\n", *pf); return 0; }実行結果 *pc</stdio.h>…

整数型変数の型を決める際の原則

c

ビットフラグなど、ビット単位の情報が重要なものは符号なし整数にする 配列の要素数が多いなど、サイズが重要な場合にはできるだけ小さいデータ型にする さもなければint

intとunsigned intの値を比較する場合。

intがunsigned intに変換されてから比較が行われるため、予期しない結果になることがある。

char型はint型に格上げされてから演算が実行される

#include <stdio.h> int main(void) { char a, b, c, d; a = b = c = d = 100; printf("a + b + c + d = %d\n", a + b + c + d); return 0; }実行結果 a + b + c + d = 400</stdio.h>

整数同士で割り算すると結果は整数になる

オペランド自身をdouble型にキャストする必要がある。

C言語が用意する基本データ型

c

型 サイズ(バイト) 表現できる値の範囲 signed char 1 -128〜127 short int 2 -32768〜32767 int 4 -2147483648〜2147483647 long int 4 -2147483648〜2147483647 unsigned char 1 0〜255 unsigned short int 2 0〜65535 unsigned int 4 0〜4294967295 uns…

演算子のオーバーロード

#include <stdio.h> #include <string.h> class CString { char* s; public: CString(); CString(const char*); CString(const CString&); ~CString(); void Print(); CString& operator=(const CString&); friend CString operator+(const CString&, const CString&); }; CStri</string.h></stdio.h>…

平均点を計算する関数を仮想関数にする

cstudent.h #include <stdio.h> #include "cstudent.h" class CSciStudent : public CStudent //理系生徒クラス { int phys; public: void SetPhys(int pt = 0){phys = pt;} int GetPhys(){return phys;} float CalcAverage(){return (GetEng() + GetMath() + phys) /</stdio.h>…

仮想関数

#include <stdio.h> | #include <stdio.h> | class Base | class Base { | { public: | public: void Who(){printf( | virtual void Who(){printf( "私はBaseです\n");} | "私はBaseです\n");} }; | }; | class Derived : public Base | class Derived : public Base { | { pub</stdio.h></stdio.h>…

派生方法と基底クラスのメンバーが派生クラスでどう振舞うかの関係。

基底クラスの 派生方法 メンバー 私的派生 保護派生 公開派生 private アクセス不可 アクセス不可 アクセス不可 protected private protected prodected public private prodected public

基底クラスから派生クラスを作成する

cstudent.h #ifndef CSTUDENT_H #define CSTUDENT_H class CStudent { int id; int eng; int math; public: void SetID(int i){id = i;} int GetID(){return id;} void SetEng(int pt = 0){eng = pt;} int GetEng(){return eng;} void SetMath(int pt = 0){m…

アクセス演算子 private:

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; private: int total; public: void CalcTotal(); void PrintTotal(){printf("注文番号%d:%d円です\n", number, total);} }; void COrder::CalcTotal() { total = (c</stdio.h>…

スコープ解決演算子

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; int total; void CalcTotal(); void PrintTotal(){printf("注文番号%d:%d円です\n", number, total);} }; void COrder::CalcTotal() { total = (cHamberger * 200 +</stdio.h>…

メンバー関数を追加する

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; int total; void CalcTotal(){total = (cHamberger * 200 +cPotato * 180 + cDrink * 150) * 1.05;} void PrintTotal(){printf("注文番号%d:%d円です\n", number, t</stdio.h>…

参照引数を持つ関数

void swap(int& a, int& b) { int temp; temp = a; a = b; b = temp; }

参照変数の使用例

#include <stdio.h> int main(void) { int a = 0; int& ref_a = a; ref_a = 3; printf("a = %d\n", a); printf("ref_a = %d\n", ref_a); return 0; }ポインタを使って記述↓ #include <stdio.h> int main(void) { int a = 0; int* p_a = &a; *p_a = 3; printf("a = %d\n", a); p</stdio.h></stdio.h>…

関数オーバーロード

#include <stdio.h> int max(int a, int b) { printf("2個のint値を比較します\n"); return (a > b ? a : b); } int max(int a, int b, int c) { printf("3個のint値を比較します\n"); int d = (a > b ? a : b); return (d > c ? d : c); } float max(float a, float </stdio.h>…

奥村晴彦C,C++

http://oku.edu.mie-u.ac.jp/~okumura/c/

デフォルト引数を持つ関数

#include <stdio.h> void func(int a, int b = 1, int c = 2) { printf("a = %d, b= %d, c = %d\n", a, b, c); } int main(void) { func(7, 8, 9); func(7, 8); func(7); return 0; }実行結果 a = 7, b= 8, c = 9 a = 7, b= 8, c = 2 a = 7, b= 1, c = 2 D:\cp></stdio.h>

コンストラクタ・デストラクタ

#include <stdio.h> class CTest { public: int number; CTest(); ~CTest(); void print(void){printf("number = %d\n", number);} }; CTest::CTest() { printf("コンストラクタが呼ばれました\n"); } CTest::~CTest() { printf("デストラクタが呼ばれました\n"); } v</stdio.h>…

基本情報技術者試験合格書届く ^^)v

あらためて感謝 -人-)

大容量ファイルのソート

ƒvƒƒOƒ‰ƒ~ƒ“ƒO

http://wisdom.sakura.ne.jp/programming/index.html クイックリファレンス的

2分探索木

/*---------------------------------------------------------- 2分探索木 -----------------------------------------------------------*/ #include <stdio.h> typedef struct data { int key; struct data *left; struct data *right; } NODE; void print_data(N</stdio.h>…

ハッシュ構造

/*----------------------------------------------------------- ハッシュ構造 -----------------------------------------------------------*/ #include <stdio.h> #include <string.h> #define HASH_SIZE 7 #define OK 0 #define NG -1 typedef struct data { int no; /*番</string.h></stdio.h>…

scanf()による入力バッファに\nが残っている問題

バッファのフラッシュを行う 【書式】 #include <stdio.h> int fflush(FILE *fp); 【説明】 バッファに格納されているデータを吐き出します。 例えば、scanf関数で"%c"変換指示子を指定した場合には、すでに標準入力がある場合には、入力バッファに残っている'\n'を処</stdio.h>…

コマンドプロンプト上でEOFを入力する

Ctrl + Z C:\WINNT>^Z

プリントスクリーンの取り方

アクティブなウィンドウのみを取る場合は Alt + PrintScreen