003:编写一个程序,要求输入一个角度的大小(度数),输出该角度所在的象限 。
(书例3.19)
代码如下:
//003:编写一个程序,要求输入一个角度的大小(度数),输出该角度所在的象限 #include运行结果#include int main() { int intangle;//取整处理后的角度 float angle;//原始角 scanf("%f",&angle); printf("The given angle is: %f degreesn",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(" quadrantn"); } 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



