你说的是宏定义吧,用#define就可以实现。
/* 程序功能:自定义角度数据类型(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
#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; };
#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; }