アフィリエイト広告を利用しています

広告

この広告は30日以上更新がないブログに表示されております。
新規記事の投稿を行うことで、非表示にすることが可能です。
posted by fanblog

2021年02月14日

10進数から2進数に変換するプログラム

<PR>






10進数から2進数に変換するプログラムです。

#include
#include
#define BIT_SIZE 8

int main(void)
{
char buf[256];
int n, binary_num[BIT_SIZE];
int quotient, remainder;
int i, cp;

printf("10進数を2進数に変換します.\n");

//キーボードから整数を入力する
printf("変換する10進数を入力してください:");
fgets(buf, 256, stdin);
n = atoi(buf);
cp = n;

//2進数に変換する
for(i = BIT_SIZE - 1;i >= 0;i--)
{
quotient = n / 2;
remainder = n % 2;

binary_num[i] = remainder;
n = n / 2;
}

//結果を表示する
printf("%dを2進数に変換すると\n", cp);
for(i = 0;i < BIT_SIZE;i++)
{
printf("%d", binary_num[i]);
}
printf("\n");

return 0;
}

<PR>





2017年11月25日

じゃんけんプログラム

<PR>






じゃんけんプログラム


乱数を使ってじゃんけんをするプログラムです。

#include<stdio.h>
#include<stdlib.h> //rand(), srand()関数を使用するため
#include<time.h> //time()関数を使用するため
#define ROCK 1
#define SCISSORS 2
#define PAPER 0

//関数のプロトタイプ宣言
int get_num(void);
void print_hand(int );
void decide_win(int , int);

int main(void)
{
int com_hand, play_hand;

//あいこの限り繰り返す
do
{
//自分の手を入力
printf("あなたの手を入力してください.(グー: 1, チョキ: 2, パー: 3)\n");
printf("あなたの手:");
play_hand = get_num() % 3;

//相手の手を乱数を使って入手する
srand(time(NULL));
com_hand = rand() % 3;

printf("ジャンケンポン\n");

//それぞれの手を表示する
printf("あなたの手: ");
print_hand(play_hand);
printf("相手の手: ");
print_hand(com_hand);

//結果を表示する
decide_win(play_hand, com_hand);

}while(com_hand == play_hand);

return 0;
}
//キーボードから入力された数字を入手する
int get_num(void)
{
char buf[128];

fgets(buf, 128, stdin);

return atoi(buf);
}
//手を表示する
void print_hand(int hand)
{
switch(hand)
{
case ROCK:
printf("グー\n");
break;
case SCISSORS:
printf("チョキ\n");
break;
case PAPER:
printf("パー\n");
break;
}
}
//勝敗を表示する
void decide_win(int play_hand, int com_hand)
{
int result = play_hand - com_hand;

if(play_hand == com_hand)
printf("あいこです.\n");
else if(result == -1 || result == 2)
printf("あなたの勝ち!\n");
else if(result == 1 || result == -2)
printf("あなたの負け\n");
}



実行例


% ./a.out

あなたの手を入力してください.(グー: 1, チョキ: 2, パー: 3)

あなたの手:3

ジャンケンポン

あなたの手: パー

相手の手: チョキ

あなたの負け

%



*青字はキーボードから入力


<PR>





2017年11月19日

log()関数の自作

<PR>






log()関数の自作


*マクローリンの定理を用いてlog()関数を作っています.


*オリジナルに比べると引数が2~3を超えたあたりから急激に精度が落ちます.


*精度を上げるにはlog4=2log2のように積の関係を使用する方法があります.






double my_log(double x)
{
int i;
double result, result1, result2;

x -= 1;
result1 = 0;
result2 = 0;

for(i = 1;i <= 40;i++)
{
if(i % 2 == 1)
result1 += my_pow(x, i) / i;
else

result2 += my_pow(x, i) / i;
}

return result1 - result2;
}

double my_pow(double x, int n)
{
int i;
double pow_result = 1;

if(n == 0)
return 1;
else
{
for(i = 0;i < n;i++)
{
pow_result *= x;
}
return pow_result;
}
}




<PR>





平方根計算関数sqrt()の自作

<PR>






sqrt()関数の自作


今回はC言語のmat.hライブラリのsqrt関数の自作ソースコードを
載せたいと思います。

sqrt()関数の自作


*ニュートン法を用いてsqrt()関数を作っています.






double my_sqrt(double x)
{
int i;
double y, z, result;

if(x == 0)
return 0;
else
{
y = 1;

for(i = 0;i <= 10;i++)
{
z = x / y;
result = (y + z) / 2;
y = result;
}
return result;
}
}





<PR>





exp関数の自作

<PR>






exp()関数の自作


*マクローリンの定理を用いてexp()関数を作っています.


*オリジナルに比べると引数が2~3を超えたあたりから急激に精度が落ちます.


精度を上げるには引数が1以上の時はeを後から掛け算し引数を1以下にする方法などがあります.






double my_exp(double x)
{
int i;
double result = 0;

//ループの回数(ここでは25)を増やせば精度は上がります.
for(i = 1;i <= 25; i++)
{
result += my_pow(x, i) / fact(i);
}

return result + 1;
}

//xのn乗を計算する関数
double my_pow(double x, int n)
{
int i;
double pow_result = 1;

if(n == 0)
return 1;
else
{
for(i = 0;i < n;i++)
{
pow_result *= x;
}
return pow_result;
}
}

//nの階乗を計算する関数
int fact(int n)
{
int i, result = 1;

if(n == 0)
return 1;
else
{
for(i = 1;i <= n;i++)
{
result *= i;
}
return result;
}
}



<PR>





cos関数の自作

<PR>






cos()関数の自作


今回はC言語のmath.hライブラリのsin関数の自作ソースコードを
載せたいと思います。

*マクローリンの定理を用いてsin()関数を作っています.


*オリジナルに比べると引数が2~3を超えたあたりから急激に精度が落ちます.


*精度を上げるにはcos(2x)=2cos^2(x)-1とループ文を使う方法があります。






double my_cos(double x)
{
double result1, result2, result;
int n;

result1 = 0;
result2 = 0;

for(n = 1;n <= 5;n++)
{
result1 += my_pow(x, 4 * n - 2) / fact(4 * n - 2);
}

for(n = 1; n <= 5;n++)
{
result2 += my_pow(x, 4 * n) / fact(4 * n);
}

result = 1 - result1 + result2;

return result;
}

double my_pow(double x, int n)
{
int i;
double pow_result = 1;

if(n == 0)
return 1;
else
{
for(i = 0;i < n;i++)
{
pow_result *= x;
}
return pow_result;
}
}

int fact(int n)
{
int i, result = 1;

if(n == 0)
return 1;
else
{
for(i = 1;i <= n;i++)
{
result *= i;
}
return result;
}
}






<PR>





sin関数の自作

<PR>






sin()関数の自作


今回はC言語のmath.hライブラリのsin関数の自作ソースコードを
載せたいと思います。

*マクローリンの定理を用いてsin()関数を作っています.


*オリジナルに比べると引数が2~3を超えたあたりから急激に精度が落ちます.


*精度を上げるにはcos関数と組み合わせてsin(2x)=2sin(x)cos(x)とループ文を使う方法があります。







double my_sin(double x)
{
double result1, result2, result;
int n;

result1 = 0;
result2 = 0;

//ループを回す回数(ここでは5)を大きくすると精度が上がります
for(n = 1;n <= 5;n++)
{
result1 += my_pow(x, 4 * n - 3) / fact(4 * n - 3);
}


//ループを回す回数(ここでは5)を大きくすると精度が上がります
for(n = 1; n <= 5;n++)
{
result2 += my_pow(x, 4 * n - 1) / fact(4 * n - 1);
}

result = result1 - result2;

return result;
}

//xのn乗を計算する関数
double my_pow(double x, int n)
{
int i;
double pow_result = 1;

if(n == 0)
return 1;
else
{
for(i = 0;i < n;i++)
{
pow_result *= x;
}
return pow_result;
}
}

//xの階乗を計算する関数

int fact(int n)
{
int i, result = 1;

if(n == 0)
return 1;
else
{
for(i = 1;i <= n;i++)
{
result *= i;
}
return result;
}
}





<PR>





文字列→実数変換関数atof()の自作

<PR>






atoi()関数の自作


今回はC言語のstdlib.hライブラリのatoi関数の自作ソースコードを
載せたいと思います。

atoi()関数の自作






double my_atof(char array[])
{
int i, j, temp1, temp2, sa;
int figure1, figure2, number1 = 0;
double number, number2 = 0;

sa = '0' - 0;
figure1 = 0;
figure2 = 0;

for(i = 0;array[i] != '\0';i++)
{
if(array[i] >= '0' && array[i] <= '9')
figure1++;
else
break;
}

temp1 = figure1;

if(array[i] == '.')
{
for(i += 1;array[i] != '\0';i++)
{
if(array[i] >= '0' && array[i] <= '9')
figure2++;
else
break;
}
}

temp2 = figure2;

for(i = 0;i < figure1;i++, temp1--)
{
array[i] -= sa;
number1 += array[i] * pow_10(temp1 - 1);
}

for(i = figure1 + 1 , j = 1;i < figure1 + figure2 + 1;i++, j++)
{
array[i] -= sa;
number2 += array[i] * pow_10_inverse(j);
}

number = number1 + number2;

return number;
}

double pow_10_inverse(int n)
{
int i;
double result = 1;

if(n == 0)
return 1;
else
{
for(i = 0; i < n;i++)
{
result *= 0.1;
}
return result;
}
}
int pow_10(int n)
{
int i, result = 1;

if(n == 0)
return 1;
else
{
for(i = 0; i < n;i++)
{
result *= 10;
}
return result;
}
}




<PR>





文字列→整数変換関数atoi()の自作

<PR>






atoi()関数の自作


今回はC言語のstdlib.hライブラリのatoi関数の自作ソースコードを
載せたいと思います。

atoi()関数の自作






int my_atoi(char array[])
{
int i, j, k, sa;
int figure, number = 0;

sa = '0' - 0;

for(figure = 0;array[figure] != '\0';)
{
if(array[figure] >= '0' && array[figure] <= '9')
figure++;
else
break;
}

j = figure;

for(i = 0;i < j;i++, figure--)
{
array[i] -= sa;
number += array[i] * pow_10(figure - 1);
}

return number;
}

int pow_10(int n)
{
int i, result = 1;

if(n == 0)
return 1;
else
{
for(i = 0; i < n;i++)
{
result *= 10;
}
return result;
}
}




<PR>





文字列検索関数strstr()関数の自作

<PR>






strstr()関数の自作


今回はC言語のstring.hライブラリのstrstr関数の自作ソースコードを
載せたいと思います。

strstr()関数の自作





char * my_strstr(char *str1, char *str2)
{
int i, j, k;

if(*str2 == '\0')
return str1;
else
{
for(i = 0;*(str1 + i) != '\0';i++)
{
if(*(str1 + i) == *str2)
{
for(j = i, k = 0;*(str1 + j) == *(str2 + k);j++, k++);
if(*(str2 + k) == '\0')
return str1 + i;
}
}
return NULL;
}
}


<PR>





ファン
検索
<< 2021年02月 >>
  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            
最新記事
写真ギャラリー
最新コメント
タグクラウド
カテゴリーアーカイブ
月別アーカイブ
プロフィール
日別アーカイブ
×

この広告は30日以上新しい記事の更新がないブログに表示されております。