| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 659 人关注过本帖
标题:角度值得实现
只看楼主 加入收藏
小赵q1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:4
帖 子:492
专家分:777
注 册:2011-8-26
收藏
得分:0 
你说的是宏定义吧,用#define就可以实现。
2012-03-22 06:42
迷上编程
Rank: 2
等 级:论坛游民
帖 子:140
专家分:86
注 册:2012-3-11
收藏
得分:0 
同学提醒我,把数据转换成60进制的。
2012-03-22 19:12
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:4 
测试主程序:
程序代码:
/*
    程序功能:自定义角度数据类型(Angle)使用演示
          头:头文件Angle.h及实现文件Angle.cpp
    变量声明:Angle a(d, m, s, pn);  其中d为度,m为分,s为秒,pn为正负标识,均带默认值
      运算符:重载了加、减、乘、除运算符
        方法:wchar_t* ToString(wchar_t* str);    以宽字符数组格式返回角度值表达式
              std::wstring ToString();            以宽字符string类型返回角度值表达式
              double ToRadian();                  返回角的弧度值
        注释:所有运算及赋值,自动把角度调整到±360°范围内。
*/

#include <cstdio>
#include <cstdlib>
#include <locale>
#include <conio.h>
#include "Angle.h"

// 函数原型
void Quit(void);

// 程序入口
void main(void)
{
    setlocale(LC_ALL, "chs");

    wchar_t buffer[20];
    Angle angle;

    Angle a1(10, 20, 30);
    wprintf_s(L"a1 = %s(%.4f)\n", a1.ToString(buffer), a1.ToRadian());
    Angle a2(30, 5, 37, Angle::negative);
    wprintf_s(L"a2 = %s(%.4f)\n", a2.ToString(buffer), a2.ToRadian());
    angle = a1 + a2;
    wprintf_s(L"a1 + a2 = %s(%.4f)\n", angle.ToString(buffer), angle.ToRadian());
    angle = a1 - a2;
    wprintf_s(L"a1 - a2 = %s(%.4f)\n", angle.ToString(buffer), angle.ToRadian());
    double proportion(4.0);
    angle = a2 * proportion;
    wprintf_s(L"a2 * %.4f = %s(%.4f)\n", proportion, angle.ToString(buffer), angle.ToRadian());
    angle = a2 / proportion;
    wprintf_s(L"a2 / %.4f = %s(%.4f)\n", proportion, angle.ToString(buffer), angle.ToRadian());

    Quit();
}

// 结束程序
#define K_ENTER     0x000D
void Quit(void)
{
    wprintf_s(L"\n按Enter键结束程序...");
    while (_getwch() != K_ENTER)
    {
        ;
    }
    exit(EXIT_SUCCESS);
}
#undef K_ESC


头文件(Angle.h):
程序代码:
#pragma once

#include <string>

class Angle
{
public:

    static enum SIGN { positive = 1, negative = -1 };

    Angle(const unsigned int degree = 0,
          const unsigned int minute = 0,
          const unsigned int second = 0,
          const SIGN sign = positive)
        : Degree(degree), Minute(minute), Second(second), Sign(sign)
        { Adjust(); }

    Angle operator+(const Angle& other) const;
    Angle operator-(const Angle& other) const;
    Angle operator*(const double proportion) const;
    Angle operator/(const double proportion) const;

    wchar_t* ToString(wchar_t* str) const;
    std::wstring ToString(void) const;
    double ToRadian(void) const ;

private:

    SIGN Sign;
    unsigned int Degree;
    unsigned int Minute;
    unsigned int Second;

    void Adjust(void);
    long GetReal(void) const;
    Angle GetAngle(long real) const;
};


类实现文件(Angle.cpp):
程序代码:
#include "Angle.h"
#include <string>

void Angle::Adjust(void)
{
    Minute += Second / 60;
    Second %= 60;
    Degree += Minute / 60;
    Minute %= 60;
    Degree %= 360;
}

long Angle::GetReal(void) const
{
    return Sign * (Second + Minute * 60 + Degree * 60 * 60);
}

Angle Angle::GetAngle(long real) const
{
    SIGN sign(Angle::positive);
    if (real < 0)
    {
        sign = Angle::negative;
        real = -real;
    }
    unsigned int second(real % 60);
    real /= 60;
    unsigned int minute(real % 60);
    unsigned int degree(real / 60);
    return Angle(degree, minute, second, sign);
}

Angle Angle::operator+(const Angle& other) const
{
    return GetAngle(GetReal() + other.GetReal());
}

Angle Angle::operator-(const Angle& other) const
{
    return GetAngle(GetReal() - other.GetReal());
}

Angle Angle::operator*(const double proportion) const
{
    return GetAngle((long)(GetReal() * proportion));
}

Angle Angle::operator/(const double proportion) const
{
    return GetAngle((long)(GetReal() / proportion));
}

wchar_t* Angle::ToString(wchar_t* str) const
{
    swprintf_s(str, 20, L"%d\x00B0%d\x2032%d\x2033", Sign * Degree, Minute, Second);
    return str;
}

std::wstring Angle::ToString(void) const
{
    wchar_t buffer[20];
    swprintf_s(buffer, 20, L"%d\x00B0%d\x2032%d\x2033", Sign * Degree, Minute, Second);
    return std::wstring(buffer);
}

double Angle::ToRadian(void) const
{
    const double PI = 3.1415926535;
    return Sign * (Degree + Minute / 60.0 + Second / 3600.0) * PI / 180;
}


授人以渔,不授人以鱼。
2012-03-22 20:21
快速回复:角度值得实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034661 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved