Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
C++
#include "stdafx.h"
#include <iostream>
#include<stdio.h>
#include <string>
#include<cctype>
#include<stdlib.h>
#include<fstream>
#include<iterator>
#include<list>
#include <functional>
#include <algorithm>
#include<vector>
 
using namespace std;

class sample
{
public:
  static auto checkForCurrency = [](char *&prevPtr, char *&p)->bool
  {
    char *origPrevPtr = prevPtr;
    char *origPtr = p;

    static const unsigned char CentSymbol = 155;
    //static const unsigned char EuroSymbol = ??; //doesn't seem to be an ASCII char
    static const unsigned char PoundSymbol = 156;
    static const unsigned char YenYuanSymbol = 157;

    bool retVal = false;

    if (*p == '$' || *p == '€' || *p == '£' || *p == '¥' || *p == '¢') {
      advance(prevPtr, p, 1);
      if (isNumber(prevPtr, p)) {
        retVal = true;
      } else if (*p == TheSpace) {
        char *tmpPtr = p;
        advance(prevPtr, p, 1);
        if (isNumber(prevPtr, p)) {
          retVal = true;
        } else {
          p = tmpPtr;
        }
      }
    } else if(isNumber(prevPtr, p)) {
      if (*p == CentSymbol) {
        advance(prevPtr, p, 1);
        retVal = true;
      } else if (isCurrency(prevPtr, p)) {
        retVal = true;
      } else if (*p == TheSpace) {
        char *tmpPtr = p;
        advance(prevPtr, p, 1);
        if (*p == CentSymbol) {
          advance(prevPtr, p, 1);
          retVal = true;
        } else if(isCurrency(prevPtr, p)) { 
          retVal = true;
        } else {
          p = tmpPtr;
        }
      }
    }

    if (retVal) {
      memset(origPtr, TheSpace, p - origPtr);
      *(origPtr + 0) = '#';
      *(origPtr + 1) = 'c';
    } else {
      prevPtr = origPrevPtr;
      p = origPtr;
    }

    return retVal;
  };

 static auto isCurrency = [](char *&prevPtr, char *&p)->bool
  {
    // Modified by Kewei, added several new currency units
    static const string currencies[] = {  // make sure the longer version is first
      "dollars", "dollar", "cents", "cent",
      "euros", "euro", "yen", "yuan", "pounds", "pound", "pence",
      "usd", "eur", "aud", "cad", "cny",
    };

    for (const string ¤cy : currencies) {
      const string::size_type lenCurrency = currency.length();
      if (strncmp(p, currency.c_str(), lenCurrency) == 0) {
        if (*prevPtr == TheSpace) {
          char *const pEndOfCurrency = p + lenCurrency;
          if (*pEndOfCurrency == TheSpace) {
            advance(prevPtr, p, lenCurrency);
            return true;
          }
        }
      }
    }

    return false;
  };


static auto isNumber = [](char *&prevPtr, char *&p)->bool
  {
    bool retVal = false;

    if (*p == '-' || *p == '+') {
      advance(prevPtr, p, 1);
      if (isDigit(prevPtr, p)) {
        retVal = true;
        for (;;) {
          while (isDigit(prevPtr, p)) {}
          if (*p == ',') {
            advance(prevPtr, p, 1);
            continue;
          }
          break;
        }
        if (*p == '.') {
          advance(prevPtr, p, 1);
          while (isDigit(prevPtr, p)) {}
          if (*p == 'e') {
            advance(prevPtr, p, 1);
            if (*p == '-' || *p == '+') {
              advance(prevPtr, p, 1);
            }
            while (isDigit(prevPtr, p)) {}
          }
        } else if (*p == 'e') {
          advance(prevPtr, p, 1);
          if (*p == '-' || *p == '+') {
            advance(prevPtr, p, 1);
          }
          while (isDigit(prevPtr, p)) {}
        } else if (*p == '/') {		// Modified by Kewei, added a case for fraction
          advance(prevPtr, p, 1);
          for (;;) {
            while (isDigit(prevPtr, p)) {}
            if (*p == ',') {
              advance(prevPtr, p, 1);
              continue;
            }
            break;
          }
        }
      }
    } else if (isDigit(prevPtr, p)) {
      retVal = true;
      for (;;) {
        while (isDigit(prevPtr, p)) {}
        if (*p == ',') {
          advance(prevPtr, p, 1);
          continue;
        }
        break;
      }
      if (*p == '.') {
        advance(prevPtr, p, 1);
        while (isDigit(prevPtr, p)) {}
        if (*p == 'e') {
          advance(prevPtr, p, 1);
          if (*p == '-' || *p == '+') {
            advance(prevPtr, p, 1);
          }
          while (isDigit(prevPtr, p)) {}
        }
      } else if (*p == 'e') {
        advance(prevPtr, p, 1);
        if (*p == '-' || *p == '+') {
          advance(prevPtr, p, 1);
        }
        while (isDigit(prevPtr, p)) {}
      } else if (*p == '/') {		// Modified by Kewei, added a case for fraction
        advance(prevPtr, p, 1);
        for (;;) {
          while (isDigit(prevPtr, p)) {}
          if (*p == ',') {
            advance(prevPtr, p, 1);
            continue;
          }
          break;
        }
      }
    }

    return retVal;
  };


  static auto advance = [](char *&prevPtr, char *&p, const size_t offset)->void
  {
    if (offset == 0) return;  // safety measure
    prevPtr = p + (offset - 1);
    p = prevPtr + 1;
  };

  static auto isDigit = [](char *&prevPtr, char *&p)->bool
  {
    if (*p >= '0' && *p <= '9') {  // There is a <ctype> function isdigit(), but it converts char to int
      advance(prevPtr, p, 1);
      return true;
    }
    return false;
  };

};

int main()
{
	sample smpl;
	smpl.checkForCurrency = [](char *&prevPtr, char *&p)->bool;
	smpl.isCurrency = [](char *&prevPtr, char *&p)->bool;
	smpl.isNumber = [](char *&prevPtr, char *&p)->bool;
	smpl.advance = [](char *&prevPtr, char *&p, const size_t offset)->void;
    smpl. isDigit = [](char *&prevPtr, char *&p)->bool;
	cin.ignore();
	system("pause");
	return 0;
}
Posted
Updated 22-Nov-13 1:13am
v2
Comments
CPallini 22-Nov-13 7:48am    
So, what is the problem with this code? (leaving aside the code itself)
Aescleal 22-Nov-13 10:06am    
Take note people, this is the comedy you get when you try and pretend that C++ is Javascript.

At least I assume that's what the author of the code's trying for 'cause it's pretty weird otherwise.

1 solution

please help me. clear the errors in this C++ program. rply for my email xxxxxx
 
Share this answer
 
v2
Comments
nv3 22-Nov-13 7:12am    
You should not post a question as a solution. Use Improve question to amend you original question, instead.

And never post your e-mail address in any entries, except you like to get tons of spam e-mail. I have removed it for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900