GcmLcm *gcmlcm;
gcmlcm = new GcmLcm();
printf("%d と %d の GCM は %d で、LCM は %d です", x,y,gcmlcm->gcm(x,y),gcmlcm->lcm(x,y));
delete gcmlcm;
|
class GcmLcm
{ ・・・
} gcmlcm;
|
class GcmLcm
{ private:
int w1,w2;
public:
int gcm(int v1, int v2)
{ w1 = v1;
w2 = v2;
while(w1 != w2)
{ if (w1 > w2) w1-= w2;
else w2-= w1;
}
return w1;
}
int lcm(int v1, int v2)
{ return v1*v2/gcm(v1,v2);
}
} gcmlcm;
int main()
{ int x = 32;
int y = 24;
printf("%d と %d の GCM は %d で、LCM は %d です", x,y,gcmlcm.gcm(x,y),gcmlcm.lcm(x,y));
return 0;
}
|
char moji = 'A';
char str[10] = "ABCabc123"
|
#include <graph.h>
int main(void)
{ int i;
if (!_setvideomode(_VRES16EXCOLOR)) return(-1);
//「X座標 100,Y座標 50」から 100 ドットを描画
for(i=100; i<200; i++) _setpixel(i,50);
_setcolor(14);
//「X座標 100,Y座標 100」から 100 ドットを描画
for(i=100; i<200; i++) _setpixel(i,100);
_setcolor(12);
//「X座標 100,Y座標 150」から「X座標 200,Y座標 150」にラインを描画
_moveto(100,150);
_lineto(200,150);
_getch();
_setvideomode(_DEFAULTMODE);
}
|
#include <iostream>
using namespace std;
int main(void)
{ int i;
char ans;
for(ans='y',i=1; ans=='Y' || ans=='y'; i++)
{ cout << '\n' << i;
cout << " 処理を続けますか(Y/N)";
cout.flush(); //入力する前にフラッシュ
cin >> ans;
}
return(0);
}
|
#include <stdio.h>
#include <string.h>
int *p_int= NULL;
char *p_char= NULL;
int main()
{
p_int= new int;
p_char= new char[32];
*p_int= 123;
strcpy(p_char,"char[32] を確保");
printf("New でメモリを割り当てる %d %s\n",*p_int,p_char);
if (p_int) delete p_int;
if (p_char) delete [] p_char;
return 0;
}
|
#include <stdio.h>
namespace name1
{ int val= 100; }
namespace name2
{ int val= 200; }
int main(void)
{ using namespace name1;
printf("val= %d\n", val);
printf("name1::val= %d\n", name1::val);
printf("name2::val= %d\n", name2::val);
return 0;
}
|
template<class Type>
Type Max(Type n1, Type n2)
{
if (n1>n2) return n1;
return n2;
}
|
int d1,d2;
d1= 3;
d2= 7;
printf("d1=%d, d2=%d MAX=%d\n",d1,d2,Max(d1,d2));
float f1,f2;
f1= 12.3f;
f2= 234.5f;
printf("f1=%f, f2=%f MAX=%f\n",f1,f2,Max(f1,f2));
|
#include <iostream>
#include <string>
using namespace std;
int main()
{ string s;
s= "12345";
cout << "1番目の文字列: " << s << endl;
s= "abcdefg";
cout << "2番目の文字列: " << s << endl;
s= "ABCDEFGHIJKLMN";
cout << "3番目の文字列: " << s << endl;
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int main()
{ vector<short> v;
short n,s,c;
n= 8;
s=c= 0;
while(s!=8)
{ v.push_back(n);
s= n*2+c;
n= s%10;
c= s/10;
}
for(n=v.size()-1; n>=0; n--) cout << v[n];
cout << endl;
return 0;
}
|
| 文字コード | BOM | 説明 |
|---|---|---|
| utf-16 | FFFE | 先頭2バイトが BOM です |
| utf-8 | EFBBBF | 先頭3バイトが BOM です |
| utf-8(BOM無し) | BOM は格納されていません |
char str1[80];
LPSTR str2 = "LPSTR で宣言";
wchar_t str1[80];
WCHAR str2[80];
LPWSTR str1 = L"LPWSTR で宣言";
|
using namespace System;
class DATA
{ public:
int v1;
int v2;
int sum;
};
void func(DATA* pData)
{ pData->sum = pData->v1+pData->v2; }
int main()
{ DATA data;
data.v1 = 10;
data.v2 = 20;
func(&data);
Console::WriteLine("10 + 20 = {0} です",data.sum);
System::Console::ReadLine();
return 0;
}
|
using namespace System;
ref class DATA
{ public:
int v1;
int v2;
int sum;
};
void func(DATA^ pData)
{ pData->sum = pData->v1+pData->v2; }
int main()
{ DATA data;
data.v1 = 10;
data.v2 = 20;
func(%data);
Console::WriteLine("10 + 20 = {0} です",data.sum);
System::Console::ReadLine();
return 0;
}
|
using namespace System;
value class DATA
{ public:
int v1;
int v2;
int sum;
};
void func(DATA^ pData)
{ pData->sum = pData->v1+pData->v2; }
int main()
{ DATA data;
data.v1 = 10;
data.v2 = 20;
func(%data);
Console::WriteLine("10 + 20 = {0} です",data.sum);
System::Console::ReadLine();
return 0;
}
|
#define SAFE_DELETE(p) { if (p) { delete (p); (p) = nullptr; } }
using namespace System;
ref class GCM
{ private:
int w1,w2;
public:
GCM()
{ Console::WriteLine("Constructor の実行"); }
~GCM()
{ Console::WriteLine("Destructor の実行"); }
int gcm(int v1, int v2)
{ w1 = v1;
w2 = v2;
while(w1 != w2)
{ if (w1 > w2) w1-= w2;
else w2-= w1;
}
return w1;
}
};
void func(GCM^ pGcm)
{ Console::WriteLine("24 と 32 の GCM は {0} です", pGcm->gcm(24,32)); }
int main()
{ GCM^ pGcm = nullptr;
pGcm = gcnew GCM();
func(pGcm);
SAFE_DELETE(pGcm);
return 0;
}
|
array<int>^ array1D = gcnew array<int>(10);
array<int, 2>^ array2D = gcnew array<int, 2>(5, 6);
array<int, 3>^ array3D = gcnew array<int, 3>(2, 3, 4);
|
using namespace System;
void Debug(String^ msg, int v)
{ String^ message = msg + " " + v;
Console::WriteLine(message);
}
int main()
{ String^ str = "2014/09/04";
array<String^>^ ymd;
array<String^>^ sep= {"/", ","};
int yy,mm,dd;
ymd= str->Split(sep, StringSplitOptions::None);
yy= int::Parse(ymd[0]);
mm= int::Parse(ymd[1]);
dd= int::Parse(ymd[2]);
Debug("YY=", yy);
Debug("MM=", mm);
Debug("DD=", dd);
Console::ReadLine();
return 0;
}
|
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
//フォームを作成するクラス
ref class FormClass
{ private:
Form^ form;
public:
//Constructor
FormClass()
{ form = gcnew Form(); }
//Form のキャプションを設定
Form^ SetText()
{ form->Text = "C++/CLI のフォームです♪";
return form;
}
};
int main()
{ MessageBox::Show("C++/CLI で Form を表示します","caption");
FormClass MyForm;
Form^ form = MyForm.SetText();
Application::Run(form);
return 0;
}
|
#using <System.dll>
using namespace System;
delegate void TestDelegate();
void method1()
{ Console::WriteLine("method No1 が呼ばれました"); }
void method2()
{ Console::WriteLine("method No2 が呼ばれました"); }
void method3()
{ Console::WriteLine("method No3 が呼ばれました"); }
void main()
{ TestDelegate ^testdelegate = gcnew TestDelegate(&method1);
TestDelegate ^test2 = gcnew TestDelegate(&method2);
testdelegate += test2;
testdelegate += gcnew TestDelegate(&method3);
testdelegate();
testdelegate -= test2;
Console::WriteLine("※method No2 を削除");
testdelegate();
Console::ReadLine();
}
|
namespace App1
{ ref class GcmClass
{ internal:
GcmClass(int v1, int v2);
private:
int val;
public:
int get_gcm();
};
}
|
#include "pch.h"
#include "GcmClass.h"
namespace App1
{ GcmClass::GcmClass(int v1, int v2)
{ while(v1 != v2)
{ if (v1 > v2) v1-= v2;
else v2-= v1;
}
val= v1;
}
int GcmClass::get_gcm()
{ return val; }
}
|
void App1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{ int d1,d2;
GcmClass^ gcmcls;
d1= _wtoi(data1->Text->Data());
d2= _wtoi(data2->Text->Data());
gcmcls = ref new GcmClass(d1,d2);
gcm->Text= gcmcls->get_gcm().ToString();
}
|
![]()