1 Star 0 Fork 1

Chobits/C Code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
coordinate_conversions.c 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
Chobits 提交于 2023-03-17 02:03 . refine coordinate_conversions.c
#include <math.h>
#include <stdio.h>
#define PI 3.1415926
struct rect
{
double x;
double y;
};
struct polar
{
double radius;
double angle;
};
struct polar rect_to_polar(struct rect r)
{
struct polar p;
p.radius = sqrt(r.x * r.x + r.y * r.y);
p.angle = (180 / PI) * atan2(r.y, r.x);
return p;
}
struct rect polar_to_rect(struct polar p)
{
struct rect r;
r.x = p.radius * cos(p.angle * PI / 180);
r.y = p.radius * sin(p.angle * PI / 180);
return r;
}
int main(void)
{
int choice;
printf("Rectangular to polar coordinates: 1\n");
printf("Polar to rectangular coordinates: 2\n");
printf("Your choice: ");
scanf("%d", &choice);
if (choice == 1)
{
struct rect input;
printf("(x, y) = ");
scanf("%lf %lf", &input.x, &input.y);
struct polar result = rect_to_polar(input);
printf("(radius, angle(degree)) = (%.2lf, %.2lf)\n", result.radius, result.angle);
}
else if (choice == 2)
{
struct polar input;
printf("(radius, angle(degree)) = ");
scanf("%lf %lf", &input.radius, &input.angle);
struct rect result = polar_to_rect(input);
printf("(x, y) = (%.2lf, %.2lf)\n", result.x, result.y);
}
else
{
printf("Invalid choice.\n");
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ChobitsY/C-Code.git
git@gitee.com:ChobitsY/C-Code.git
ChobitsY
C-Code
C Code
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385