#include < stdio.h >
#include < varargs.h >
int debug_print(va_alist)
va_dcl
{
va_list args;
char *fmt;
char buf[256];
va_start(args);
fmt=va_arg(args,char *);
vsprintf(buf,fmt,args);
va_end(args);
fprintf(stderr,"DEBUG[ %s \n",buf);
return(0);
}
Category: uncategorized
グローバルデータを使う方法
unsigned char Map[ 100 ][ 100 ];
void main()
{
InitMap();
LoadMapData();
SaveMapData();
}
int InitMap()
{
int x, y;
for(y=0;y<100;y++){
for(x=0;x<100;x++){
Map[x][y]=(unsigned char)0;
}
}
return(0);
}
int LoadMapData()
{
int x,y;
for(y=0;y<100;y++){
for(x=0;x<100;x++){
Map[x][y]=getc();
if(feof(stdin)){
return(-1);
}
}
}
return(0);
}
int SaveMapData()
{
int x,y;
for(y=0;y<100;y++){
for(x=0;x<100;x++){
putc(Map[x][y]);
}
}
return(0);
}
関数の実装
#include < stdio.h >
short GetShort();
void main()
{
short num;
while(1){
/* short standard input, insert to num */
num=GetShort();
/* display num binary */
ShortBinPrint(num);
}
}
short GetShort()
{
char buf[80];
short num;
fgets(buf, sizeof(buf)-1,stdin);
num=(short)atoi(buf);
return(0);
}
int ShortBinPrint(num)
short num;
{
char buf[20];
ShortToBinString(num,buf);
printf("%\n",buf);
return(0);
}
int ShortToBingString(num,buf)
short num;
char *ptr;
ptr=buf;
for(i=15;i>=0;i--){
*ptr=(char)(((num>>i)&0x01)+'0');
ptr++;
}
*ptr='\0';
return 0;
}
反復子を利用してベクトルにアクセス
#include < iostream > #includeusing namespace std; int main() { vector v; int i; for(i=0; i<10; i++) v.push_back(i); for(i=0; i<10; i++) cout << v[i] << " "; cout << endl; vector ::iterator p = v.begin(); while(p != v.end()){ cout << *p << " "; p++; } return 0; }
thisポインタ
メンバ関数から同じクラスのほかのメンバを参照する際には、直接参照することができる。
#include < iostream >
#include < cstring >
using namespace std;
class inventory {
char item[20];
double cost;
int on_hand;
public:
inventory(char *i, double c, int o)
{
strcpy(item, i);
cost = c;
on_hand = o;
}
void show();
};
void inventory::show()
{
cout << item;
cout << ": $" << cost;
cout << " stock: " << on_hand << "\n";
}
int main()
{
inventory ob("rench", 4.95, 4);
ob.show();
return 0;
}
オブジェクトの代入
型が同じであれば、1つのオブジェクトをもう1つのオブジェクトに代入することができます。
#includeusing namespace std; class myclass { int a, b; public: void set(int i, int j){ a = i; b = j; } void show() { cout << a << ' ' << b << '\n'; } }; int main() { myclass o1, o2; o1.set(10, 4); o2 = o1; o1.show(); o2.show(); return 0; }