角度(度分秒)的四则运算

角度(度分秒)的四则运算

在测量计算(如闭合导线坐标计算),我们可能需要计算比较多的角度。数学中,角度的度与分、分与秒之间一律采用六十进制,完全通过笔算耗时耗力,还容易算错。下面用C语言知识编写一个简单的程序,来实现角度的四则运算。

#include <stdio.h>
#include <stdlib.h>
typedef struct angle
{
    int degree;
    int minute;
    int second;
}ANGLE;
void decrease(ANGLE a[], int i);
void add(ANGLE a[], int i);
void multiply(ANGLE a[], int i, int j);
void divide(ANGLE a[], int i, int j);
int main()
{
    do
    {
    int i = 0, k, j = 0;
    float degree = 0;
    char operation[25];
    ANGLE a[25] = {0};
    printf("本程序可实现角度(度/分/秒)的四则运算,输入时角度的度、分、秒间以一个空格隔开\n");
    printf("角度 %d:",i+1);
    scanf("%d %d %d",&a[i].degree, &a[i].minute, &a[i].second);
    getchar();
    printf("运算符:");
    scanf("%c",&operation[i]);
    do
    {
        if(operation[i] == '+' || operation[i] == '-')
        {
            i++;
            printf("角度 %d:",i+1);
            scanf("%d %d %d",&a[i].degree, &a[i].minute, &a[i].second);
            getchar();
            printf("运算符:");
            scanf("%c",&operation[i]);
        }
        else if(operation[i] == '*')
        {
            printf("  乘数:");
            scanf("%d", &j);
            getchar();
            printf("运算符:");
            scanf("%c",&operation[i+1]);
        }
        else if(operation[i] == '/')
        {
            printf("  除数:");
            scanf("%d", &j);
            getchar();
            printf("运算符:");
            scanf("%c",&operation[i+1]);
        }
    }while(operation[i] != '=' && operation[i+1] != '=');
    for(k = 0; k < i+1; k++)
    {
        if(operation[k] == '+')
        {
            add(a, k+1);
        }
        else if(operation[k] == '-')
        {
            decrease(a, k+1);
        }
        else if(operation[k] == '*')
        {
            multiply(a, k, j);
        }
        else if(operation[k] == '/')
        {
            divide(a, k, j);
        }
        else if(operation[k] != '-' && operation[k] != '+' && operation[k] != '=' && operation[k] != '/' && operation[k] != '*')
        {
            printf("请输入正确的运算符!\n");
        }
    }
    if(a[i].degree < 0 || a[i].minute < 0 || a[i].second < 0)
    {
        printf("结果为:-%d°%d′%d″\n",abs(a[i].degree), abs(a[i].minute), abs(a[i].second));
    }
    else
    {
        printf("结果为:%d°%d′%d″\n",a[i].degree, a[i].minute, a[i].second);
    }
    degree = (float)a[i].degree + (float)a[i].minute / 60 + (float)a[i].second / 3600;
    printf("换算为:%f°\n", degree);
    getchar();
    getchar();
    }while(1);
}
void add(ANGLE a[], int i)
{
    long s1, s2, s3 = 0;
    s1 = a[i-1].degree*60*60 + a[i-1].minute*60 + a[i-1].second;
    s2 = a[i].degree*60*60 + a[i].minute*60 + a[i].second;
    s3 = s1 + s2;
    a[i].degree = s3 / 3600;
    a[i].minute = (s3 - (a[i].degree) * 3600) / 60;
    a[i].second = s3 % 60;
}
void decrease(ANGLE a[], int i)
{
    long s1, s2, s3 = 0;
    s1 = a[i-1].degree*60*60 + a[i-1].minute*60 + a[i-1].second;
    s2 = a[i].degree*60*60 + a[i].minute*60 + a[i].second;
    s3 = s1 - s2;
    a[i].degree = s3 / 3600;
    a[i].minute = (s3 - (a[i].degree) * 3600) / 60;
    a[i].second = s3 % 60;
}
void multiply(ANGLE a[], int i, int j)
{
    long s1, s3 = 0;
    s1 = a[i].degree*60*60 + a[i].minute*60 + a[i].second;
    s3 = s1 * j;
    a[i].degree = s3 / 3600;
    a[i].minute = (s3 - (a[i].degree) * 3600) / 60;
    a[i].second = s3 % 60;
}
void divide(ANGLE a[], int i, int j)
{
    long s1, s3 = 0;
    s1 = a[i].degree*60*60 + a[i].minute*60 + a[i].second;
    s3 = s1 / j;
    a[i].degree = s3 / 3600;
    a[i].minute = (s3 - (a[i].degree) * 3600) / 60;
    a[i].second = s3 % 60;
}

code blocks中运行示例
在这里插入图片描述
如果单次计算的角度多于25个,只需更改这两行代码,改变数组的大小。

    char operation[25];
    ANGLE a[25] = {0};
  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值