0
点赞
收藏
分享

微信扫一扫

C语言 输出角度所在象限

林塬 2022-05-04 阅读 68
c语言

一、题目

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

二、代码实现

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

//003:编写一个程序,要求输入一个角度的大小(度数),输出该角度所在的象限 
#include<stdio.h>
#include<math.h>
int main()
{
    int intangle;//取整处理后的角度
    float angle;//原始角
    scanf("%f",&angle);
    printf("The given angle is: %f 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.099998 degrees
and lies inthe second quadrant
270
The given angle is: 270.000000 degrees
and coincides with the coordinate axis
-90.1
The given angle is: -90.099998 degrees
and lies inthe third quadrant
-180
The given angle is: -180.000000 degrees
and coincides with the coordinate axis

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

举报

相关推荐

0 条评论