Author Topic: code blocks said NAN while other compilier gave the right answer  (Read 10278 times)

Offline selenaqd

  • Single posting newcomer
  • *
  • Posts: 2
Hi guys,
I am working on a calculator code, while microsoft visual c++ gave the right answer( so i assume the code is right), code block keep saying NAN only. I prefer to use code blocks now, so i wonder if anyone can give me some advise why this is happening? thanks a lot!!!
 
  :-*
/*
    calculator08buggy.cpp
 
    Helpful comments removed.
 
    We have inserted 3 bugs that the compiler will catch and 3 that it won't.
*/
 
#include "std_lib_facilities.h"//see this header code after
 
const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';//t is a number token
const char name = 'a';
const char unit = 'k';
const char func ='f';
 
 
//----------------------------------------------------------------------
struct Token {
    char kind;
    double value;
    string name;
    Token(char ch)              :kind(ch),name(""),value(0) { cout << "Token: '" << ch << "'" << endl; }
    Token(char ch, double val)  :kind(ch),name(""),value(val) { cout << "Token: '" << ch << ",' value: " << val << endl; }
    Token(char ch, string n)    :kind(ch),name(n),value(0) { cout << "Token: '" << ch << ",' name: '" << n << "'" << endl; }
};
//----------------------------------------------------------------------------------
class Token_stream {
public:
//construct
    Token_stream() :full(0), buffer(0) { }
    Token get();
    void unget(Token t) { buffer=t; full=true; }
    void ignore(char);
private:
    bool full;
    Token buffer;
};
//---------------------------------------------------------------------------------------
 
 
Token Token_stream::get()
{
    if (full) {
        full=false;
        return buffer;
    }
 
    char ch;
    cin >> ch;
 
    switch (ch) {
        case ',':
        case '(':
        case ')':
        case '+':
        case '-':
        case '*':
        case '/':
        case '%':
        case ';':
        case '=':
            return Token(ch);
        case 'u':
            return Token(unit, 0.000001);
        case 'm':
            return Token(unit, 0.001);
        case 'c':
            return Token(unit, 0.01);
        case 'd':
            return Token(unit, 10);
        case 'h':
            return Token(unit, 100);
        case 'k':
            return Token(unit, 1000);
        case 'M':
            return Token(unit, 1000000);
        case 'G':
            return Token(unit, 1000000000);
        case 'T':
            return Token(unit, 1000000000000);
        case '.':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        {   
            cin.unget();
            double val;
            cin >> val;
            return Token(number,val);
        }
        default:
            if (isalpha(ch)) {
                string s;
                s += ch;
                while(cin.get(ch) && (isalpha(ch) || isdigit(ch)))
                    s+=ch;
                cin.unget();
                if (s == "let") return Token(let);
                if (s == "quit") return Token(quit);
                if (s == "sqrt") return Token(func, s); // s is sqrt
                if (s == "pow") return Token(func, s); // s is pow
                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;
    while (cin>>ch)
        if (ch==c) return;
}
//-------------------------------------------------------------------------
struct Variable {
    string name;
    double value;
    Variable(string n, double v) :name(n), value(v) { }
};
//------------------------------------------------------------------------
vector<Variable> names;
 
 
//----------------------------------------------------------------------
double get_value(string s)
{
    for (int i = 0; i< (int) names.size(); ++i)
        if (names.name == s) return names.value;
    error("get: undefined name ",s);
}
 
//---------------------------------------------------------------------
void set_value(string s, double d)
{
    for (int i = 0; i<= (int) names.size(); ++i)
        if (names.name == s) {
            names.value = d;
            return;
        }
    error("set: undefined name ",s);
}
//----------------------------------------------------------------------------------
bool is_declared(string s)
{
    for (int i = 0; i<(int)names.size(); ++i)
        if (names.name == s) return true;
    return false;
}
//--------------------------------------------------------------------------------
Token_stream ts;
//-------------------------------------------------------------------------
double expression();
//----------------------------
/*
double sqrt(double y) {
    if(y<0)
        error("only positive number");
    if(y=1 || y=0)
        return y;
    if(y>1)
        return sqrt(y);
    ts.unget(t);
    return 1;
}
*/
 
double getunit() {
    // get a token
    // if the token is a unit token, return the value
    // if not, put the token back and return 1
    Token t = ts.get();
    if(t.kind == unit)
        return t.value;
    ts.unget(t);
    return 1;
}
/*
double sqrt(){
    Token t =ts.get();
    if(t.kind == sqrt && t.kind >0)
        return sqrt(x);
    else if(t.kind == pow)
        return pow(x);
    ts.unget(t);
    return 1;
}
*/
 
vector<double> get_arguments(int num) {
 
    /*
    // This function reads arguments for functions. num is the number of arguments to read.
    // arguments have the form: ( a, b, c, ...)
    //
    // 1. we read '(' first
    // 2. we read an argument expression()
    // 3. if more arguments are coming, we read ',' and repeat 2
    // 4. we read ')' finally
    //
    // the arguments are returned in a vector.
    */
 
    // read '(' or error
    Token t = ts.get();
    if (t.kind != '(')
        error("'(' expected");
 
    // make an empty vector of doubles
    vector<double> getargu;
    // repeat num times
    while(num-->0) {
 
        // read an expression
        double d = expression();
 
        // put result in the vector
        getargu.push_back(d);
   
        // if more arguments are coming
        if(num>0) {
            // read a comma or give an error
            t=ts.get();
            if(t.kind != ',')
                error("syntax error: expected ','");
        }
    }
 
    // read ')' or error
    t = ts.get();
    if (t.kind != ')')
        error("')' expected");
 
    // return the vector
    return getargu;
}
 
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case 'f':
    {
        vector<double> args;
 
        if(t.name=="sqrt") {
            args = get_arguments(1);
            return sqrt(args[0]);
        }
 
        if(t.name == "pow") {
            args = get_arguments(2);
            return pow(args[0], args[1]);
        }
 
        // otherwise throw an error
        cout << "unknown function name: " << t.name << endl;
        error("unknown function name: ", t.name);
    }
    case '(':
    {
        double d = expression();
        t = ts.get();
        if (t.kind != ')')
            error("'(' expected");
        return d;
    }
    case '-':
        return - primary();
    case number:
    {
        cout << "Primary: number: " << t.value << endl;
        return t.value * getunit();
//        return t.value;
    }
    case name:
        return get_value(t.name);
    default:
        error("primary expected");
    }
}
 
//--------------------------------------------------------
double term()
{
    double left = primary();
 
    while(true) {
        Token t = ts.get();
        switch(t.kind) {
        case '*':
            left *= primary();
            break;
        case '/':
        {    double d = primary();
            if (d == 0) error("divide by zero");
            left /= d;
            break;
        }
        default:
            ts.unget(t);
            return left;
        }
    }
}
//-----------------------------------------------------------
double expression()
{
    double left = term();
    while(true) {
        Token t = ts.get();
        switch(t.kind) {
        case '+':
            left += term();
            break;
        case '-':
            left -= term();
            break;
        default:
            ts.unget(t);
            return left;
        }
    }
}
//-------------------------------------------------
double declaration()
{
    Token t = ts.get();
    if (t.kind != 'a') error ("name expected in declaration");
    string name = t.name;
    if (is_declared(name)) error(name, " declared twice");
    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of " ,name);
    double d = expression();
    names.push_back(Variable(name,d));
    return d;
}
//-------------------------------------------------------------------------
double statement()
{
    Token t = ts.get();
    switch(t.kind) {
    case let:
        return declaration();
    default:
        ts.unget(t);
        return expression();
    }
}
//-------------------------------------------------------------------
void clean_up_mess()
{
    ts.ignore(print);
}
 
//----------------------------------------------------------
const string prompt = "> ";
const string result = "= ";
//-----------------------------------------------------
void calculate()
{
    while(true) try {
        cout << prompt;
        Token t = ts.get();
        while (t.kind == print) t=ts.get();
        if (t.kind == quit) return;
        ts.unget(t);
        cout << result << statement() << endl;
    }
    catch(runtime_error& e) {
        cerr << e.what() << endl;
        clean_up_mess();
    }
}
//-------------------------------------------------
 
int _tmain(int argc, _TCHAR* argv[])
{   
    try {
        calculate();
        keep_window_open();
        return 0;
    }
    catch (exception& e) {
        cerr << "exception: " << e.what() << endl;
        char c;
        while (cin >>c&& c!=';') ;
        return 1;
    }
    catch (...) {
        cerr << "exception\n";
        char c;
        while (cin>>c && c!=';');
        return 2;
    }
}


 // "std_lib_facilities.h" :)

/*
   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.

   Revised April 25, 2010: simple_error() added
*/

#ifndef H112
#define H112 201004L

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
#include<stdexcept>

//------------------------------------------------------------------------------

#ifdef _MSC_VER
#include <hash_map>
using stdext::hash_map;
#else
#include <ext/hash_map>
using __gnu_cxx::hash_map;

namespace __gnu_cxx {

    template<> struct hash<std::string>
    {
        size_t operator()(const std::string& s) const
        {
            return hash<char*>()(s.c_str());
        }
    };

} // of namespace __gnu_cxx
#endif

//------------------------------------------------------------------------------

#define unordered_map hash_map

//------------------------------------------------------------------------------

typedef long Unicode;

//------------------------------------------------------------------------------

using namespace std;

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) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
   typedef typename std::vector<T>::size_type size_type;

   Vector() { }
   explicit Vector(size_type n) :std::vector<T>(n) {}
   Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
   template <class I>
   Vector(I first, I last) :std::vector<T>(first,last) {}

   T& operator[](unsigned int i) // rather than return at(i);
   {
      if (i<0||this->size()<=i) throw Range_error(i);
      return std::vector<T>::operator[](i);
   }
   const T& operator[](unsigned int i) const
   {
      if (i<0||this->size()<=i) throw Range_error(i);
      return std::vector<T>::operator[](i);
   }
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
   
   String() { }
   String(const char* p) :std::string(p) {}
   String(const string& s) :std::string(s) {}
   template<class S> String(S s) :std::string(s) {}
   String(int sz, char val) :std::string(sz,val) {}
   template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }

   char& operator[](unsigned int i) // rather than return at(i);
   {
      if (i<0||size()<=i) throw Range_error(i);
      return std::string::operator[](i);
   }

   const char& operator[](unsigned int i) const
   {
      if (i<0||size()<=i) throw Range_error(i);
      return std::string::operator[](i);
   }
};

#ifndef _MSC_VER
namespace __gnu_cxx {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace __gnu_cxx
#endif


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());
}

#if _MSC_VER<1500
   // disgusting macro hack to get a range checked string:
   #define string String
   // MS C++ 9.0 have a built-in assert for string range check
   // and uses "std::string" in several places so that macro substitution fails
#endif

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 character to exit\n";
   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;
   }
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)   // write ``error: s?? and exit program
{
   cerr << "error: " << s << '\n';
   keep_window_open();      // for some Windows environments
   exit(1);
}

// 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 = 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; }

inline double sqrt(int x) { return sqrt(double(x)); }   // to match C++0x

#endif

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: code blocks said NAN while other compilier gave the right answer
« Reply #1 on: October 04, 2012, 07:09:27 pm »
Please reread the rules carefully !
You might alos want to read the FAQ, especially: http://wiki.codeblocks.org/index.php?title=FAQ-General#Q:_What_Code::Blocks_is_not.3F .

When you registered here, you accepted to follow the forum rules, please do it in the future.