设计函数将实型角度转化为度分秒输出

其中应该怎么设计函数
输入40.1875
输出40度11分15秒

SAngleconvert(double a1, SAngle a2){

}
int main()
{
double angle;
SAngle tAngle;
scanf("%lf", &angle);
convert(angle, &tAngle);
printf("%d°%d′%d″", tAngle.h, tAngle.m, tAngle.s);
return 0;
}

一度有60分钟,一分钟有60秒。如果您有40.1875 度,你可以看到你的测量值是 40整数度加上 0.1875 度。

以分钟为单位计算十进制 0.1875 度, 0.1875 *60 得到 11.25分钟。现在你有11整分钟加上 0.25 分钟。

十进制 0.25 分钟乘以60, 0.25*60 得到15 秒。
结果:40度11分15秒

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>

using namespace std;

double maxlimit = 360.0;

int main()
{
    cout <<"请输入你要转换的角度";
    double num;
    cin >> num;
    cout<<"即将转换的角度是 = "<<fixed<<setprecision(6)<<num<<endl;

    double maxnum = max(num,maxlimit);
    
    if( maxnum != maxlimit  ||  num < 0)
    {
        cout <<"支持转换的角度数值是正数且小于等于360度"<<endl;
        return 0;
    }
    
    
    double x1,y1;
    y1 = modf (num , &x1);
    
    if(y1 == 0)
    {
        cout<<"转换结果是"<<x1<<"度"<<endl;
        return 0;
    }
    else
    {
        y1 = y1 * 60;
        double x2,y2;
        y2 = modf(y1,&x2);
        if(y2 == 0)
        {
            cout<<"转换结果是"<<x1<<"度"<<x2<<"分"<<endl;
        }
        else
        {
            y2 = y2 * 60;
            double x3,y3;
            y3 = modf(y2,&x3);
            cout<<"y2 = "<<y2<<"  x3 = "<<x3<<"  y3 = "<<y3<<endl;
            cout<<"转换结果是"<<x1<<"度"<<x2<<"分"<<x3<<"秒"<<endl;
        }
    }
    
    return 0;
}

大概是这个样子吧,自己测了测感觉还行,太细节的可能就考虑不周全了
https://www.23bei.com/tool-477.html 这个网站是角度转换工具 大家可以参考一下

要先了解你的逻辑,你输入的是个什么数值?如何转换为时分秒?

我理解你想要的就是弧度转化成度分秒


#include<stdio.h>
#define pi 3.1415926

struct SAngle
{
    SAngle(){h=0;m=0;s=0;}

    int h;
    int m;
    int s;
};
void convert(double x,SAngle& tAngle)
{
    double t;
    int d,m,s;
    t=x/pi*180;
    d=t;
    t=t-d;
    t=t*60;
    m=t;
    t=t-m;
    t=t*60;
    s=t;
    
    tAngle.h = d;
    tAngle.m = m; 
    tAngle.s = s;
}

int main(int argc,char*argv[])
{
    double x;
    scanf("%lf",&x);
    SAngle tAngle;
    convert(x,tAngle);
    printf("%5d° %5d′ %5d′′",tAngle.h,tAngle.m,tAngle.s);
    return 0;
}

#include <iostream>
using namespace std;

const double PI = 3.14159;

//角度转换为弧度
double angle_to_radian(double degree, double min, double second)
{
    double flag = (degree < 0)? -1.0 : 1.0;            //判断正负
    if(degree<0)
    {
        degree = degree * (-1.0);
    }
    double angle = degree + min/60 + second/3600;
    double result =flag * (angle * PI)/180;
    return result;
    //cout<<result<<endl;
}
//弧度转换为角度
void radian_to_angle(double rad, double ang[])
{
    double flag = (rad < 0)? -1.0 : 1.0;
    if(rad<0)
    {
        rad = rad * (-1.0);
    }
    double result = (rad*180)/PI;            
    double degree = int(result);
    double min =(result - degree)*60;
    double second = (min - int(min)) * 60;    
    ang[0] = flag * degree;
    ang[1] = int(min);
    ang[2] = second;
}

int main()
{
    /* 1弧度=180/π度,1度=π/180弧度 */
    double ans[3] = {0};    // 度分秒
    radian_to_angle(1,ans);    // 1弧度 转为 角度值
   cout << ans[0] << "°" << ans[1] << "′" << ans[2] << "″";
   return 0;
}

img

https://www.csdn.net/tags/MtTaIg3sMDA4NTY0LWJsb2cO0O0O.html

通过取整取余可以实现