C++: finding a specific digit in a number - c++

Someone please guide me how to check whether a specific digit exists in an integer or not. for the sake of code optimization I am trying to avoid the use of strings or any kind of loops to iterate over all the digits of the integer.
If I need to find out whether 4 exists the in an integer or not, input and the output samples are given below:
Sample Input:
154
Sample Output:
true
Desired Code:
bool ifExists(int digit, int number)
{
if()
{
return true;
}
else
{
return false;
}
}
Possible Logic:
I believe there must be a mathematical approach which will do the job in the if condition, however I am unable to find such method in cmath library.

Convert integer to string, do a string search for digit.
The "mathematical" method would have to do the same, compute the digit sequence by division/remainder by 10 and compare with the given digit.

A mathematical approach would be possible i think. By using log(x) in some crazy way.
But ones for surey you should use string or a loop by dividing by 10. log(x) requires far more resources than the way you want to use.
Also string or dividing by 10 is far more easy to read than a mathimatical solution.
For the mathimatical solution.
I suggest you try to transform the decimal number in a binary representation of it. Then it could be possible to create a filter for the digit you look for. By joining the filter and the transformed value through a bitwise & you could get what you seek. Im not sure if that realy is possible but that would be my first idea for an approach.
But as i said before. This method will be very expensive to the cpu and will be hard to read.

Related

How to implement bitset binary exclusion used to find code?

I am new to C++, and I need to write a code that can find a binary code by checking a bitset against inputs and excluding unnecessary options.
Example:
input-000000000 = 6 correct
This implies that there must be 3 ones present.
I need the code to only use strings that contain 6 zeros and three ones and print one attempting to narrow down the code.
Example:
000000111 = 5 correct
This must mean that two of those ones are correct and one must be a zero with a one somewhere in the first six digits.
How do I approach this problem?
I have so far the function:
string index2code (int i)
{
return bitset <9>(i).to_string();
}
But I am still struggling to think of the logic, and how to use it.

string consisting of positive integer or positive decimal, convert toI number format

I was given the following assignment, but I do not understand exactly what is the problem exactly...
Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the numbers to the number format.
from my perspective what is saying is that I need to read a string consisting of x integer/decimal and convert it to an integer or a double...is that right? it seems so easy because I can just use strtol() and strtod().
It depends on whether you are allowed to use the standard library functions or not. If so, then, yes, it is just as easy as you described. If not, you will have to parse the string looking for decimal points, minus signs, etc. and convert by your own algorithms.
I'm not writing an answer for you, but you basically your assignment asks to convert strings of form say :
"123"
"-321"
"+3219871230"
"0123"
to corresponding numbers.
Also, since this is an assignment your professor shouldn't be interested in seeing solutions using library functions strtod, strtol etc.

How to know value in the std::string/Cstring is number or not

I am getting command line argument when i launch the application.
I am getting four parameter from command line.
after parsing I store them in four std::string/CString(mfc) variable now i need to know whether the value is decimal or not.because these parameter is going to be used in some mathematical calculation.
Can anybody help me on this.
here you can find how to determine if a string is a numeric, in 70 languages!
I'm not sure if all the solutions check the same thing. The C++ implementation checks if the input is a positive/negative integer/floating-point for base 10, or if it a is positive/negative integer for base 8/16. Is that what you want? Do you need to support only positive numbers? Do you need to support floating-points?
Probably you'll need to convert your input strings to numeric value, so there is no reason to do it in two steps (to check, then to convert). Better do it in a single step.
One more thing: If the input string is too long, for example "32525252332912461984612491264912649126129319312931279171295127951275129" - you normally won't want to consider it as a valid input.
Look at every character in the string, and if you find something that is not a digit, or a '.', then it is not a number.
Just use a string->number conversion function that unambiguously reports failure.
e.g. strtod and not atof

Determining whether a string is a math formula?

So I'm processing math formulas from strings using the shunting-yard algorithm. I pass every string through a function which processes specific strings into values, but occasionally I check against strings that are just strings that should just get passed by - read skip the shunting-yard pass. Should I just use a regular expression to test for all of the symbols and numbers? Or is there a simpler way I might test for this? I guess the inverse would be checking if there are still any letters left in the string?
While I'm sure there is a better answer, I've decided to use the following until something better comes up, I'm working in AS3:
if ( !String( value ).match( /[a-zA-Z]/ ) && !String( value ).match( /^(-?\d+)$/ ) && String( value ).match( /[()\\*+-]/ ) )
{
value = MathParser.parse( value );
}
The first expression verifies that there are no characters - my shunting yard implementation doesn't process special characters or operations, it just does basic math, so if there are any characters remaining, something is wrong.
The second verifies that the string contains mathmatical symbols. There's no point in attempting the operation if there's no math to process. I assume this is unnecessary if I wanted to handle errors inside the shunting-yard processes.
And finally I verify that the value isn't just a standalone number of positive or negative value.
Comments for improvement appreciated.

Is this an acceptable use of "ASCII arithmetic"?

I've got a string value of the form 10123X123456 where 10 is the year, 123 is the day number within the year, and the rest is unique system-generated stuff. Under certain circumstances, I need to add 400 to the day number, so that the number above, for example, would become 10523X123456.
My first idea was to substring those three characters, convert them to an integer, add 400 to it, convert them back to a string and then call replace on the original string. That works.
But then it occurred to me that the only character I actually need to change is the third one, and that the original value would always be 0-3, so there would never be any "carrying" problems. It further occurred to me that the ASCII code points for the numbers are consecutive, so adding the number 4 to the character "0", for example, would result in "4", and so forth. So that's what I ended up doing.
My question is, is there any reason that won't always work? I generally avoid "ASCII arithmetic" on the grounds that it's not cross-platform or internationalization friendly. But it seems reasonable to assume that the code points for numbers will always be sequential, i.e., "4" will always be 1 more than "3". Anybody see any problem with this reasoning?
Here's the code.
string input = "10123X123456";
input[2] += 4;
//Output should be 10523X123456
From the C++ standard, section 2.2.3:
In both the source and execution basic character sets, the value of each character after 0 in the
above list of decimal digits shall be one greater than the value of the previous.
So yes, if you're guaranteed to never need a carry, you're good to go.
The C++ language definition requres that the code-point values of the numerals be consecutive. Therefore, ASCII Arithmetic is perfectly acceptable.
Always keep in mind that if this is generated by something that you do not entirely control (such as users and third-party system), that something can and will go wrong with it. (Check out Murphy's laws)
So I think you should at least put on some validations before doing so.
It sounds like altering the string as you describe is easier than parsing the number out in the first place. So if your algorithm works (and it certainly does what you describe), I wouldn't consider it premature optimization.
Of course, after you add 400, it's no longer a day number, so you couldn't apply this process recursively.
And, <obligatory Year 2100 warning>.
Very long time ago I saw some x86 processor instructions for ASCII and BCD.
Those are AAA (ASCII Adjust for Addition), AAS (subtraction), AAM (mult), AAD (div).
But even if you are not sure about target platform you can refer to specification of characters set you are using and I guess you'll find that first 127 characters of ASCII is always have the same meaning for all characters set (for unicode that is first characters page).