大虾帮忙看看
下面是我在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);
}