fmod
double fmod( double x, double y )
浮動小数点数の剰余を計算する。
zは0.9になる。
浮動小数点数の剰余を計算する。
z = fmod( 10.5, 3.2 );
zは0.9になる。
http://gardener.hustle.ne.jp/programming/
C言語関連の要点まとめ(更新停止中)
z = fmod( 10.5, 3.2 );
printf( "%4.2f\n", ceil(12.11) );
printf( "%4.2f\n", ceil(-5.68) );
13.00
-5.00
printf( "%4.2f\n", floor(12.11) );
printf( "%4.2f\n", floor(-5.68) );
12.00
-6.00
srand( ( unsigned )time( NULL ) );
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int i;
srand( ( unsigned )time( NULL ) );
// 0〜32767の整数乱数
for ( i=0; i<=10; i++ ) printf( "%d\n", rand() );
// 0〜1の実数乱数
for ( i=0; i<=10; i++ ) printf( "%f\n", rand() / 32767.0 );
// 0〜1未満の実数乱数
for ( i=0; i<=10; i++ ) printf( "%f\n", rand() / 32767.1 );
// このように書いているのもある
for ( i=0; i<=10; i++ ) printf( "%f\n", rand() / 32768.0 );
// 1〜6の整数乱数
for ( i=0; i<=10; i++ ) printf( "%d\n", ( int )( ( rand() / 32767.1 * 6 ) + 1 ) );
// これだと商が1.0の場合に7がでてしまう
for ( i=0; i<=10; i++ ) printf( "%d\n", ( int )( ( rand() / 32767.0 * 6 ) + 1 ) );
}
( rand() % ( 上限 - ( 下限 ) + 1 ) ) + 下限
rand() % 10
( rand() % 6 ) + 1
( rand() % 7 ) - 3
値 | str1 と str2 の関係 |
< 0 | str1 は str2 より小さい |
0 | str1 と str2 は等しい |
> 0 | str1 は str2 より大きい |
if ( strcmp( str, "aaa" ) == 0 ) {
・・・
}
strcpy( s, "strcpy test" );
< 0 | 昇順:elem1はelem2より小さい、降順:elem1はelem2より大きい |
0 | elem1 は elem2 に等しい |
> 0 | 昇順:elem1はelem2より大きい、降順:elem1はelem2より小さい |
#include
#include// qsort用
#include// strcmp用
int intcmp( const void *, const void * );
int intrcmp( const void *, const void * );
int dblcmp( const void *, const void * );
int mystrcmp( const void *, const void * );
void main()
{
int id[10] = { 45, 65, 23, 87, 53, 67, 32, 19, 73, 66 };
double dd[10] = { 3.56, 7.43, 5.76, 3.67, 9.65, 5.64, 2.75, 4.55, 6.32, 4.78 };
char *pstr[5] = { "apple", "orange", "banana", "lemon", "peach" };
int i;
printf( "--int昇順--\n" );
qsort( id, 10, sizeof( int ), intcmp );
for ( i=0; i<10; i++ ) printf( "%d ", id[i] );
puts( "\n" );
printf( "--int降順--\n" );
qsort( id, 10, sizeof( int ), intrcmp );
for ( i=0; i<10; i++ ) printf( "%d ", id[i] );
puts( "\n" );
printf( "--double昇順--\n" );
qsort( dd, 10, sizeof( double ), dblcmp );
for ( i=0; i<10; i++ ) printf( "%4.2f ", dd[i] );
puts( "\n" );
printf( "--文字列昇順--\n" );
qsort( pstr, 5, sizeof( char* ), mystrcmp );
for ( i=0; i<5; i++ ) printf( "%s ", pstr[i] );
puts( "\n" );
}
int intcmp( const void *a, const void *b )
{
return( *(int*)a - *(int*)b );
}
int intrcmp( const void *a, const void *b )
{
return( *(int*)b - *(int*)a );
}
int dblcmp( const void *a, const void *b )
{
if ( *(double*)a < *(double*)b )
return -1;
else if ( *(double*)a == *(double*)b )
return 0;
else
return 1;
}
int mystrcmp( const void *a, const void *b )
{
return strcmp( *(char**)a, *(char**)b );
}
--int昇順--
19 23 32 45 53 65 66 67 73 87
--int降順--
87 73 67 66 65 53 45 32 23 19
--double昇順--
2.75 3.56 3.67 4.55 4.78 5.64 5.76 6.32 7.43 9.65
--文字列昇順--
apple banana lemon orange peach
#include <stdio.h>
void main()
{
char str[256];
sprintf( str, "%c%d", 65, 123 );
puts( str );
}
A123
strcat( str, "xyz" );
char *p;
char s[] = "strchr test";
p = strchr( s, 'c' );
puts( p );
chr tst
n = strlen( "abcde" );
strncat( s1, s2, 10 );
戻り値 | 説明 |
< 0 | string1 の部分文字列は string2 の部分文字列より小さい |
0 | string1 の部分文字列は string2 の部分文字列と同じ |
> 0 | string1 の部分文字列は string2 の部分文字列より大きい |
if ( strncmp( str, "abc", 3 ) == 0 ) {
・・・
}
strncpy( s1, s2, 10 );
char *p;
char s[] = "strstr test";
p = strchr( s, "te" );
puts( p );
test
#include <stdio.h>
#include <string.h>
void main()
{
char s[] = "123,456.789 012/,.:345:678";
const char delim[] = "/: ,.";
char *p;
p = strtok( s, delim );
printf( "%s\n", p );
while ( ( p = strtok( NULL, delim ) ) != NULL )
printf( "%s\n", p );
}
123
456
789
012
345
678