2006.10.13 Friday
17:06 | posted by
gardener
#define | マクロの定義 |
#undef | 定義の無効化 |
#include | ファイルの取り込み |
#if | 条件コンパイル |
#elif | 条件コンパイル |
#else | 条件コンパイル |
#ifdef | 条件コンパイル |
#ifndef | 条件コンパイル |
#line | 行制御 |
#error | エラー生成 |
#pragma | 厳密にはコンパイラ制御文 |
2006.10.13 Friday
15:22 | posted by
gardener
「エラトステネスのふるい」のサンプル
#include <stdio.h>
#define NUM 1000
void main()
{
int prime[ NUM ], i, j;
for ( i = 2; i < NUM; i++ )
prime[ i ] = 1; // 1を立てておく
for ( i = 2; i < NUM; i++ ) {
if ( prime[ i ] == 1 ) { // 1ならそれは最初にみつかった素数
for ( j = 2; i * j < NUM; j++ )
prime[ i * j ] = 0; // i以外のiの倍数を全て倒す
}
}
printf( "素数:1 " );
for ( i = 2; i < NUM; i++ ) {
if ( prime[ i ] == 1 )
printf( "%d ", i );
}
}
結果は出力してのお楽しみだ。
2006.10.13 Friday
14:31 | posted by
gardener
ビットパターンを表示するサンプル
#include <stdio.h>
void bitpat( int x )
{
int i;
for ( i = 31; i>=0; i-- )
printf( "%d", ( x >> i ) & 0x00000001 );
printf( "\n" );
}
void main()
{
int a;
while ( scanf( "%d", &a ) != EOF )
bitpat( a );
}
2006.10.13 Friday
13:25 | posted by
gardener
優先順位 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
関数、かっこ | () | | | | | | | | | | | | | | |
配列 | [] | | | | | | | | | | | | | | |
構造体 | .、-> | | | | | | | | | | | | | | |
型 | | sizeof | | | | | | | | | | | | | |
ポインタ | | *、& | | | | | | | | | | | | | |
インクリメント・ディクリメント | | ++、-- | | | | | | | | | | | | | |
算術 | | - | *、/、% | +、- | | | | | | | | | | | |
関係 | | | | | | <、<=、>、>= | ==、!= | | | | | | | | |
ビット | | ~ | | | <<、>> | | | & | ^ | | | | | | | |
論理 | | ! | | | | | | | | | && | || | | | |
条件 | | | | | | | | | | | | | ?: | | |
代入 | | | | | | | | | | | | | | =、+=、*=、etc | |
カンマ | | | | | | | | | | | | | | | , |
2006.10.13 Friday
11:21 | posted by
gardener
#include <stdio.h>
void dispA( char** s )
{
int i = 0;
printf( "sのアドレス%p:格納アドレス%p\n", &s, s );
while ( s[ i ] != NULL ) {
printf( "s[%d]のアドレス%p:格納アドレス%p:%s\n", i, &s[ i ], s[ i ], s[ i ] );
i++;
}
printf( "sのアドレス%p:格納アドレス%p\n\n", &s, s );
}
void dispB( char** s )
{
printf( "sのアドレス%p:格納アドレス%p\n", &s, s );
while ( *s != NULL ) {
printf( "sのアドレス%p:格納アドレス%p:%s\n", &s, *s, *s );
s++;
}
printf( "sのアドレス%p:格納アドレス%p\n", &s, s );
}
void main()
{
char *name[] = { "Candy", "Rolla", "Nancy", "Ann", "Eluza", NULL };
dispA( name );
dispB( name );
}
実行結果
sのアドレス0012FF18:sの格納アドレス0012FF68
s[0]のアドレス0012FF68:s[0]の格納アドレス0042201C:Candy
s[1]のアドレス0012FF6C:s[1]の格納アドレス00423030:Rolla
s[2]のアドレス0012FF70:s[2]の格納アドレス00422FBC:Nancy
s[3]のアドレス0012FF74:s[3]の格納アドレス00422FB4:Ann
s[4]のアドレス0012FF78:s[4]の格納アドレス00422FAC:Eluza
sのアドレス0012FF18:sの格納アドレス0012FF68
sのアドレス0012FF18:sの格納アドレス0012FF68
sのアドレス0012FF18:sの格納アドレス0042201C:Candy
sのアドレス0012FF18:sの格納アドレス00423030:Rolla
sのアドレス0012FF18:sの格納アドレス00422FBC:Nancy
sのアドレス0012FF18:sの格納アドレス00422FB4:Ann
sのアドレス0012FF18:sの格納アドレス00422FAC:Eluza
sのアドレス0012FF18:sの格納アドレス0012FF7C
2006.10.13 Friday
11:04 | posted by
gardener
#include <stdio.h>
int sum( int* a )
{
int i = 0, sum = 0;
while ( *a != -999 ) {
sum += *a;
a++;
}
return sum;
}
void main()
{
int a[][4] = { { 1, 2, 3, 0 }
, { 4, 5, 6, 0 }
, { 7, 8, 9, -999 } };
printf( "%d\n", sum( a[0] ) );
}
2006.10.13 Friday
10:42 | posted by
gardener
#include <stdio.h>
int sumA( int* a )
{
int sum = 0;
while ( *a != -999 ) {
sum += *a;
a++;
}
return sum;
}
int sumB( int* a )
{
int i = 0, sum = 0;
while ( a[ i ] != -999 ) {
sum += a[ i ];
i++;
}
return sum;
}
void main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -999 };
printf( "%d\n", sumA( a ) );
printf( "%d\n", sumB( a ) );
}
1/1
- Calendar
<< October 2006 >> |
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | | | | |
- Categories
- Selected Entries
- Archives
- Links
page top