【森罗万象C++】消除浮点误差的方法

时间不太够,直接放方法,原理日后完善。

//假设a是一浮点数,希望维持其整数精度
(如需小数精度乘10^x处理后再除回来即可)
result=(int)(a+0.5);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言森罗万象罗盘版时钟是一种基于C语言编写的时钟程序,它通过使用罗盘作为界面,展示出不同的时间和指针效果。下面是一个简单的代码示例,供大家分享和参考: ```c #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <math.h> #define PI 3.1415926 void gotoxy(int x, int y) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(handle, coord); } void drawClock(int x, int y, int radius) { for (int i = 0; i < 60; i++) { double angle = 2 * PI * i / 60.0; int px, py; if (i % 5 == 0) { px = x + radius * sin(angle) * 0.9; py = y - radius * cos(angle) * 0.9; gotoxy(px, py); printf("*"); } else { px = x + radius * sin(angle) * 0.95; py = y - radius * cos(angle) * 0.95; gotoxy(px, py); printf("."); } } } void drawHands(int x, int y, int hour, int minute, int second) { double hAngle = 2 * PI * (hour % 12) / 12.0; double mAngle = 2 * PI * minute / 60.0; double sAngle = 2 * PI * second / 60.0; int hx = x + 0.6 * cos(hAngle); int hy = y - 0.6 * sin(hAngle); int mx = x + 0.8 * cos(mAngle); int my = y - 0.8 * sin(mAngle); int sx = x + 0.9 * cos(sAngle); int sy = y - 0.9 * sin(sAngle); gotoxy(hx, hy); printf("H"); gotoxy(mx, my); printf("M"); gotoxy(sx, sy); printf("S"); } int main() { int x = 30, y = 10, radius = 8; while (1) { SYSTEMTIME t; GetLocalTime(&t); gotoxy(0, 0); printf("当前时间:%02d:%02d:%02d", t.wHour, t.wMinute, t.wSecond); drawClock(x, y, radius); drawHands(x, y, t.wHour, t.wMinute, t.wSecond); Sleep(1000); system("cls"); } return 0; } ``` 这个程序中,我们使用了Windows的Console API来实现在控制台中画图和定位光标的功能。首先使用gotoxy函数来确定字符的位置,然后使用printf函数来输出字符(*或者.)来表示小时和分钟的刻度。然后我们使用drawHands函数根据当前时间来确定时针、分针和秒针的位置,使用"H"、"M"和"S"来表示不同的指针。最后,在一个无限循环中,我们使用系统时间来刷新界面,并且每隔一秒清空控制台屏幕。 这只是一个简单的代码示例,你可以根据自己的需求和喜好进行修改和完善。希望这个代码能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值