C语言 输出角度所在象限

一、题目

003:编写一个程序,要求输入一个角度的大小(度数),输出该角度所在的象限 。
(书例3.19)

二、代码实现

思路:需要对输入的角度进行处理,化为0-360度之间的角度,特别的与坐标轴重合的单独拎出

代码如下:

//003:编写一个程序,要求输入一个角度的大小(度数),输出该角度所在的象限 
#include<stdio.h>
#include<math.h>
int main()
{
    int intangle;//取整处理后的角度
    double angle;//原始角
    scanf("%lf",&angle);
    printf("The given angle is: %g degrees\n",angle);
    if((floor(angle)-angle==0)&&(int)angle%90==0)//如果角度是正好与坐标轴重合
    printf("and coincides with the coordinate axis");
    else
    {
    intangle=floor(angle);//不用int取整是因为负数处理会出错,使用向下取整floor()
    if(intangle>=0)
    intangle%=360;//正角度直接对360取余即可
    else
    intangle=360-(-intangle)%360;//负角度化为正角度
    printf("and lies in ");
    switch(intangle/90)
    {
        case 0:printf("the first");break;
        case 1:printf("the second");break;
        case 2:printf("the third");break;
        case 3:printf("the forth");break;//switch-case判断
    }
    printf(" quadrant\n");
    }
    return 0;
}

运行结果

90.1
The given angle is: 90.1 degrees
and lies in the second quadrant
270
The given angle is: 270 degrees
and coincides with the coordinate axis
-90.1
The given angle is: -90.1 degrees
and lies in the third quadrant
-180
The given angle is: -180 degrees
and coincides with the coordinate axis

说明一下,floor()函数向下取整,ceil()函数向上取整,math.h

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值