双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

大虾帮忙看看

lhb_780902 lhb_780902 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是我在MSDN中看到的一段代码,是介绍longjmp的用法的,但在我的机器上编译过不去。
#include "stdafx.h"
#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <string.h>

jmp_buf mark;
int fperr;

void __cdecl fphandler(int sig, int num);
void fpcheck(void);

typedef void (__cdecl * PFUNC)(int);

void main(void)
{
double n1, n2, r;
int jmpret;
PFUNC pFunc = fphandler;

_control87(0, _MCW_EM);

if (signal(SIGFPE, pFunc) == SIG_ERR){
fprintf(stderr, "Couldn't set SIGFPE\n");
abort();
}

jmpret = setjmp(mark);
if (jmpret == 0){
printf("Test for invalid operation - ");
printf("enter two numbers: ");
scanf("%lf %lf", &n1, &n2);

r = n1 / n2;
printf("\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r);

r = n1 * n2;
printf("\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r);
} else{
fpcheck();
}
}

void __cdecl fphandler(int sig, int num)
{
fperr = num;

_fpreset();

longjmp(mark, -1);
}

void fpcheck(void)
{
char fpstr[30];

switch(fperr){
case _FPE_INVALID:
strcpy(fpstr, "Invalid number");
break;
case _FPE_OVERFLOW:
strcpy(fpstr, "Overflow");
break;
case _FPE_UNDERFLOW:
strcpy(fpstr, "Underflow");
break;
case _FPE_ZERODIVIDE:
strcpy(fpstr, "Divide by zero");
break;
default:
strcpy(fpstr, "Other floating point error");
break;
}

printf("Error %d: %s\n", fperr, fpstr);
}
...全文
评论 8
收藏 已收藏
分享
返回顶部
lhb_780902
VC/MFC 16,550 成员 421,605 内容