注册 登录
编程论坛 C++教室

想了3天还是compile不可以

zfan85 发布于 2010-09-28 00:35, 717 次点击
请各位大大分享一下是那里错了,我跟另外2个同学想了一个星期了
下面是Binomial.h里的内容
#ifndef BINOMIAL_H
#define BINOMIAL_H

#define    SUCCESS    0
#define    FAILURE    -1

enum    CALL_PUT    {Call, Put};
enum    AMER_EURO    {European, American};

#define OPTION_MULTIPLIER(x)    ((x) == Call ? 1.0 : -1.0)
#define MAX(a,b) (((a) > (b)) ? (a) : (b))

/*-----------------------------------------------------------------------------
** FUNCTION:    CRRBinomialTree.
**
** DESCRIPTION:    Calculates the price of an option using the Cox, Ross and Rubinstein
**                binomial tree model.
**
** RETURNS:        SUCCESS and the premium, otherwise FAILURE.                                 
**
**---------------------------------------------------------------------------*/
int CRRBinomialTree(
    CALL_PUT    callOrPut,                /* (I) put or call flag (use Call or Put) */
    AMER_EURO    amerOrEuro,                /* (I) option type (use European, American) */
    double        spotPrice,                /* (I) spot price of underlying */
    double        strike,                    /* (I) strike price of option */
    double        maturity,                /* (I) maturity of option in years */
    double        vol,                    /* (I) annual volatility of underlying */
    double        rate,                    /* (I) annual continuous compounded discount rate */
    int            nStep,                    /* (I) number of steps in tree */
    double        *value);                /* (O) option value */

#endif



这是Binomial.cpp的内容

#include<iostream>
#include"Binomial.h"
#include"cmath"

using namespace std;

int CRRBinomialTree(CALL_PUT callOrPut, AMER_EURO amerOrEuro, double spotPrice, double strike, double maturity, double vol, double rate, int nStep,double *value)
{        
    double u,d,p,discount;
    int i;
    u=exp(vol*sqrt(maturity/nStep));d=1/u;p=(exp(rate*maturity/nStep)-d)/(u-d);discount=exp(-rate*maturity/nStep);
    if(amerOrEuro==European){
        for(i=0;i<=nStep;i++)
            value[i]=MAX(OPTION_MULTIPLIER(callOrPut)*(spotPrice*pow(u,nStep-2i)-strike),0);
        while(nStep>0){
            for(i=0;i<=nStep-1;i++)
            value[i]=discount*(p*value[i]+(1-p)*value[i+1]);
            nStep--;}
    }
    else if(amerOrEuro==American){
        for(i=0;i<=nStep;i++)
            value[i]=MAX(OPTION_MULTIPLIER(callOrPut)*(spotPrice*pow(u,nStep-2i)-strike),0);
        while(nStep>0){
            for(i=0;i<=nStep-1;i++)
            value[i]=MAX(discount*(p*value[i]+(1-p)*value[i+1]),MAX(OPTION_MULTIPLIER(callOrPut)*(spotPrice*pow(u,nStep-2i)-strike),0));
            nStep--;}
    }
    else
        return FAILURE;

return SUCCESS;


}


当我compile的时候出现
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(14) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(14) : error C2146: syntax error : missing ')' before identifier 'i'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(14) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(14) : error C2059: syntax error : ')'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(22) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(22) : error C2146: syntax error : missing ')' before identifier 'i'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(22) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(22) : error C2059: syntax error : ')'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2146: syntax error : missing ')' before identifier 'i'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2059: syntax error : 'bad suffix on number'
1>c:\users\zhang fan\desktop\新建文件夹\binomial.cpp(25) : error C2059: syntax error : ')'


根据我的分析,错误是相同的,可是就是不知道是那里错?
3 回复
#2
pbreak2010-09-28 14:59
2i 是什么意思?
#3
weble2010-09-28 21:23
变量名不能以数字开头
#4
zfan852010-09-28 23:17
谢谢 我知道了
1