Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
find the currency symbol in given paragraph on command line prompt..and i trying output answer replace currency put #c symbols.
I trying that one..
in this project i get all functions.i trying create main function for this project.


/////////////////////////////////first function////////////////////////////////////////

C#
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;

		// Modified by Kewei, using symbols instead of ascii code to represent currency
		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;
	};

/////////////////////////////////second function////////////////////////////////////////

C#
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;
	};



/////////////////////////////////third function////////////////////////////////////////

C#
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;
	};

/////////////////////////////////fourth function////////////////////////////////////////

C#
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;
	};

/////////////////////////////////fifth function////////////////////////////////////////

C#
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;
	};

};
Posted
Updated 30-Nov-13 1:01am
v6
Comments
OriginalGriff 30-Nov-13 5:32am    
That is just a code dump. And an unformatted one at that.
Edit your question, use pre tags to format the code, and cut it down to just the relevant fragment.
Then cut your subject down to a brief overview and put the body of your question in the actual question where it doesn't get truncated and we can read it all. Hopefully,m there will then be an actual question in there!

Help us to help you!
phil.o 30-Nov-13 5:33am    
Please
- do not use the title to ask the actual question
- ask a rather precise question
- format the code
Member 8748841 30-Nov-13 6:51am    
i format it
CHill60 1-Dec-13 8:44am    
Still haven't asked a question or explained your problem though

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