3 Star 2 Fork 0

lo01/introduction_to_programming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
week_9.c 3.07 KB
一键复制 编辑 原始数据 按行查看 历史
lundechen 提交于 2022-05-27 13:19 . master
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
///////////////////////////
///// Fibonacci //////
///////////////////////////
void ex3_4_fibonacci()
{
int n, u_n, u_n_1 = 2, u_n_2 = 1;
printf("type in the value of n\n");
scanf("%d", &n);
if(n == 1)
printf("The fibonacci sequence at position 1 is: 1");
else if(n == 2)
printf("The fibonacci sequence at position 2 is: 2");
else
for(int i = 3; i <= n; i ++)
{
u_n = u_n_1 + u_n_2;
u_n_2 = u_n_1;
u_n_1 = u_n;
}
printf("The fibonacci sequence at position %d is: %d ", n, u_n);
}
int fibo(int n)
{
if(n == 1)
return 1;
if(n == 2)
return 2;
return fibo(n - 1) + fibo(n - 2);
}
void fibo_print()
{
for(int i = 1; i <= 20; i ++)
printf("%d ", fibo(i));
}
///////////////////////////
///// Complex Numbers
///////////////////////////
struct CplxCartisian
{
double re, im;
};
struct CplxPolar
{
double radius, ang;
};
struct Cplx
{
char sys; // C for Cartisian and P for Polar
double param1, param2;
};
double real_part(struct Cplx nb)
{
if(nb.sys == 'C')
return nb.param1;
else
return nb.param1 * cos(nb.param2);
}
void convert_to_polar_wrong(struct Cplx nb)
{
if(nb.sys == 'P')
return;
nb.sys = 'P';
double param1_tmp = nb.param1;
nb.param1 = sqrt( nb.param1 * nb.param1 + nb.param2 * nb.param2 );
nb.param2 = atan(param1_tmp / nb.param2);
}
struct Cplx convert_to_polar_right_1(struct Cplx nb)
{
if(nb.sys == 'P')
return nb;
nb.sys = 'P';
double param1_tmp = nb.param1;
nb.param1 = sqrt( nb.param1 * nb.param1 + nb.param2 * nb.param2 );
nb.param2 = atan(param1_tmp / nb.param2);
return nb;
}
struct Cplx convert_to_polar_right_2(struct Cplx nb)
{
if(nb.sys == 'P')
return nb;
struct Cplx nb_p;
nb_p.sys = 'P';
nb_p.param1 = sqrt( nb.param1 * nb.param1 + nb.param2 * nb.param2 );
nb_p.param2 = atan(nb.param1 / nb.param2);
return nb_p;
}
void convert_to_polar_right_3(struct Cplx* nb)
{
if(nb->sys == 'P')
return;
nb->sys = 'P';
double param1_tmp = nb->param1;
nb->param1 = sqrt( nb->param1 * nb->param1 + nb->param2 * nb->param2 );
nb->param2 = atan(param1_tmp / nb->param2);
}
void ex5_5()
{
struct Cplx a;
a.sys = 'C';
a.param1 = 3;
a.param2 = 4;
printf("\nconvert_to_polar_wrong\n");
convert_to_polar_wrong(a);
printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
printf("\nconvert_to_polar_right_1\n");
struct Cplx b = convert_to_polar_right_1(a);
printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
printf("b: %c %lf %lf\n", b.sys, b.param1, b.param2);
printf("\nconvert_to_polar_right_2\n");
struct Cplx c = convert_to_polar_right_2(a);
printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
printf("c: %c %lf %lf\n", c.sys, c.param1, c.param2);
printf("\nconvert_to_polar_right_3\n");
convert_to_polar_right_3(&a);
printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lo01/introduction_to_programming.git
git@gitee.com:lo01/introduction_to_programming.git
lo01
introduction_to_programming
introduction_to_programming
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385