1.#QNAN0,gcc vc6.0 的区别

hlyces 2010-01-21 11:34:24
gcc-4.4.2-32

下:[2240] term:return value:1.#QNAN0

vc 6.0

下:[2244] term:return value:1.000000
/* Copyright(C)
* For free
* All right reserved
*
*/
/**
* @file main.cpp
* @brief calculator
* @author hlyces, wsklrw@163.com
* @version 1.0
* @date 2010-01-16
*/
#include "std_lib_facilities.h"

#include <windows.h>

class Token{
public:
char kind;
double value;
string name;
Token(char ch)
:kind(ch),value(0){}
Token(char ch, double val)
:kind(ch),value(val){}
Token( char ch, string n): kind( ch), name( n){}
};

class Token_stream
{
public:
Token_stream( ):full( false),buffer( 0){

}
Token get( );
void putback( Token t);
void ignore( char c);
private:
bool full;
Token buffer;
};

class Variable{
public:
string name;
double value;
Variable( string n, double v):name( n),value( v){}
};

const string prompt = ">";
const string result = "=";
const string declkey= "let";
const char let = 'L';
const char name = 'a';
const char print = ';';
const char quit = 'q';
const char number = '8';
vector <Variable> var_table;
Token_stream ts;



double expression();

double term();

double primary();

double get_value( string s);

void set_value( string s, double d);

double statement( );

bool is_declared( string var);

double define_name( string var, double val);

double declaration( );

void Token_stream::putback( Token t)
{
OutputDebugString("putback..");
if( full) error( "putback( ) into a full buffer");
buffer=t;
full=true;
}

Token Token_stream::get( )
{
if( full)
{
full=false;
return buffer;
}
char ch;
cin >> ch;
switch( ch){
case quit:
case print:
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
OutputDebugString("get operator");
return Token( ch);
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
cin.putback( ch);
double val;
cin >> val;
OutputDebugString("get number");
return Token( number,val);
}
case '=':
OutputDebugString("get '='");
return Token(ch);
default :
if( isalpha( ch))
{
string s;
s+=ch;
while( cin.get( ch) &&( isalpha( ch)||isdigit( ch)))
s+=ch;
if( s == declkey)
{
OutputDebugString("get 'let'");
return Token( let);
}
cin.putback(ch);
return Token( name, s);
}
error( "Bad token");
}
}

void Token_stream::ignore( char c)
{
if( full && c==buffer.kind)
{
full = false;
return;
}
full=false;

char ch=0;
while( cin>>ch)
if( ch==c) return;
}





double get_value( string s)
{
for( int i=0; i<var_table.size( ); ++i)
if( var_table[i].name==s)
{
return var_table[i].value;
}
error( "get : undefined Variable",s);
}

void set_value( string s, double d)
{
for( int i=0; i<var_table.size( );++i)
if( var_table[i].name==s)
{
var_table[i].value=d;
return;
}
error( "set : undefined Variable",s);

}

bool is_declared( string var)
{
for( int i=0; i<var_table.size( ); ++i)
if( var_table[i].name==var) return true;
return false;
}

double define_name( string var, double val)
{
if( is_declared( var)) error( var," declared twice");
var_table.push_back( Variable( var,val));
return val;
}

double declaration( )
{
Token t=ts.get( );
if( t.kind!=name) error( "name expected in declaration");
string var_name=t.name;

Token t2=ts.get( );

if( t2.kind!='=') error( "missing '=' in declaration of ",var_name);

double d = expression( );
define_name( var_name, d);
return d;
}

double statement( )
{
Token t=ts.get( );
switch( t.kind)
{
case let:
return declaration( );
default:
ts.putback( t);
return expression( );
}
}





double expression()
{
double left=term();
Token t=ts.get();
while( true)
{
switch(t.kind)
{
case '+' :
left+=term( );
t=ts.get( );
break;
case '-' :
left-=term( );
t=ts.get( );
break;
default:
ts.putback( t);
char buffer[30];
sprintf(buffer,"expression:return value:%f",left);
OutputDebugString(buffer);
return left;
}
}
}
double term()
{
double left=primary();
Token t=ts.get();
while( true)
{
switch(t.kind)
{
case '*' :
left*=primary( );
t=ts.get( );
break;
case '/' :
{

double d=primary( );
if( d == 0) error( "divide by zero");
left/=d;
t=ts.get( );
break;
}
case '%' :
{
int i1=narrow_cast<int>( left);
int i2=narrow_cast<int>(term());
if( i2==0) error( "%:divide by zero");
left=i1%i2;
t= ts.get( );
break;
}
default:
ts.putback( t);
char buffer[30];
sprintf(buffer,"term:return value:%f",left);
OutputDebugString(buffer);
return left;
}
}
}

double primary()
{
Token t=ts.get();
switch(t.kind)
{
case '(' :
{
double d=expression( );
t=ts.get( );
if( t.kind!=')') error("')' expected") ;
return d;
}
case number :
char buffer[30];
sprintf(buffer,"primary:return value:%f",t.value);
OutputDebugString(buffer);
return t.value;
case '-':
return - primary( );
case '+':
return primary( );
default:
error( "primary expected");
}

}



void clean_up_mess( )
{
ts.ignore( print);
//system("cls");
}

void calculate( )
{
while(cin)
{
try{
cout << prompt;
Token t = ts.get();
while( t.kind == print) t=ts.get( );
if( t.kind == quit)
{
return ;
}
ts.putback( t);
cout << result << statement( ) << endl;
}
catch( exception & e)
{
cerr << e.what( ) << endl;
clean_up_mess( );
}
}
}



int main()
{
try
{
calculate( );
keep_window_open( );
return 0;
}
catch( exception & e)
{
cerr << e.what( ) << endl;
keep_window_open( "~~");
return 1;
}
catch(...)
{
cerr << "exception \n" ;
keep_window_open( "~~");
return 2;
}
}



...全文
338 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cocat 2010-01-21
  • 打赏
  • 举报
回复
好长噢~帮顶
_JeffreyWu 2010-01-21
  • 打赏
  • 举报
回复
哪里越界嘞
不晓得,你试了下喽
hlyces 2010-01-21
  • 打赏
  • 举报
回复
...

我在VC下能得到正解


gcc下却说越界??


是不是double 要写成long double?
_JeffreyWu 2010-01-21
  • 打赏
  • 举报
回复
百度一下,你就知道
Google一下,你就知道的太多了
hlyces 2010-01-21
  • 打赏
  • 举报
回复

/*
simple "Programming: Principles and Practice using C++" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.

Students: please don't try to understand the details of headers just yet:
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.
*/

#ifndef H112
#define H112 201001L

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
#include<stdexcept>
using namespace std;

const string s="";
template<class T> string to_string(const T& t)
{
ostringstream os;
os << t;
return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
int index;
Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};




struct Exit : runtime_error {
Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
error(s+s2);
}

inline void error(const string& s, int i)
{
ostringstream os;
os << s <<": " << i;
error(os.str());
}



template<class T> char* as_bytes(T& i) // needed for binary I/O
{
void* addr = &i; // get the address of the first byte
// of memory used to store the object
return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
// cin.clear();
// cout << "Please enter a charcter to exit\n";
system("pause");
// char ch;
// cin >> ch;
return;
}

inline void keep_window_open(string s)
{
if (s=="") return;
cin.clear();
cin.ignore(120,'\n');
for (;;) {
cout << "Please enter " << s << " to exit\n";
string ss;
while (cin >> ss && ss!=s)
cout << "Please enter " << s << " to exit\n";
return;
}
}

// make std::min() and std::max() accessible:
#undef min
#undef max

#include<iomanip>
inline ios_base& general(ios_base& b) // to augment fixed and scientific
{
b.setf(ios_base::fmtflags(0),ios_base::floatfield);
return b;
}

// run-time checked narrowing cast (type conversion):
template<class R, class A> R narrow_cast(const A& a)
{
R r = a;
if (A(r)!=a) error(string("info loss"));
return r;
}


inline int randint(int max) { return rand()%max; }

inline int randint(int min, int max) { return randint(max-min)+min; }

#endif
hlyces 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 laowu_ 的回复:]
哪里越界嘞
不晓得,你试了下喽
[/Quote]
在gcc下
你有没试??
输入1+2;
会得到=nan
而在VC下是
=3

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧