I have written a function to check if the number is fraction in either X/X or X X/X format where X is double.
Here is the code. -
-
bool IsFraction(const CComBSTR bucketSize, double &result)
-
{
-
int nToken=1;
-
HRESULT hr = S_OK;
-
CComBSTR tempBucketSize(bucketSize);
-
//Count the token. must be 2 or 3
-
wcstok(tempBucketSize.m_str, _T(" /"));
-
while(wcstok(NULL, _T(" /")))
-
{
-
++nToken;
-
}
-
-
//If number token == 2, the number must be in the form X/X
-
if(nToken == 2)
-
{
-
CComBSTR szToken = wcstok(bucketSize.m_str, _T("/"));
-
-
CComVariant vtNumerator = szToken;
-
hr = vtNumerator.ChangeType( VT_R8 );
-
//if szToken is not number return false
-
if(FAILED(hr) || (vtNumerator.vt != VT_R8) || (vtNumerator.dblVal < 0.0))
-
return false;
-
-
//if there's no string left or the second string is not number or it's zero, return false
-
szToken = wcstok(NULL, _T("/"));
-
CComVariant vtDenominator = szToken;
-
hr = vtDenominator.ChangeType(VT_R8);
-
-
if(FAILED(hr) || (vtDenominator.vt != VT_R8) || (vtDenominator.dblVal <= 0.0))
-
return false;
-
-
//otherwise calculate number and return true
-
result = vtNumerator.dblVal / vtDenominator.dblVal;
-
return true;
-
}
-
//If number token == 3, the number must be in the form X X/X
-
else if(nToken == 3)
-
{
-
CComBSTR szToken = wcstok(bucketSize.m_str, _T(" "));
-
-
CComVariant vtNumber = szToken;
-
hr = vtNumber.ChangeType(VT_R8);
-
//if szToken is not number return false
-
if(FAILED(hr) || (vtNumber.vt != VT_R8) || (vtNumber.dblVal < 0.0))
-
return false;
-
-
//if there's no string left or the second string is not number, return false
-
szToken = wcstok(NULL, _T("/"));
-
CComVariant vtNumerator = szToken;
-
hr = vtNumerator.ChangeType(VT_R8);
-
-
if(FAILED(hr) || (vtNumerator.vt != VT_R8) || (vtNumerator.dblVal < 0.0))
-
return false;
-
-
//if there's no string left or the third string is not number or it's zero, return false
-
szToken = wcstok(NULL, _T(" /"));
-
CComVariant vtDenominator = szToken;
-
hr = vtDenominator.ChangeType(VT_R8);
-
-
if(FAILED(hr) || (vtDenominator.vt != VT_R8) || (vtDenominator.dblVal <= 0.0))
-
return false;
-
-
//otherwise calculate number and return true
-
result = vtNumber.dblVal + vtNumerator.dblVal / vtDenominator.dblVal;
-
return true;
-
-
}
-
else
-
return false;
-
}
-
It's very long but I can't think of another algorithm to deal with it. So if you have an idea to optimize it, please give me your suggestions or comments.
Thank
5 2917
Is 1.25 3.67/0.87 a fraction too, like 1 1/4 is ? If yes then ok, without decreasing number of checks it probably can't be reduced. What is VT_R8, btw?
1 1/4 is not a fraction. It's a mixed number. The actual number is 5/4 which is a rational number.
1.25 3.67/0.87 is incorrect syntax. The 3.67/0.87 is a real number.
Note that 5/4 maps to 5.0/4.0 making the rational numbers a subset of the real numbers.
If you use the form X/X where X is a floating point number, you will have a correct representation.
kouisawang ...
What precise forms do you need to recognize as fractions? - 100 whole number (implied denominator of 1)
-
1/4 ratio of integers
-
0.45 decimal fraction (denominator implied by number of digits)
-
2.67 mixed number (decimal fraction)
-
2 1/2 mixed number (ratio, whitespace separator)
-
2-1/2 mixed number (ratio, dash separator)
-
1.5/7.8 ratio of real numbers
-
1.2 1.5/7.8 funky mixed number
-
4,5 European decimal point
How many ways do you need to support negative numbers?
-100, -1/4, 1/-4, -2 1/2, -(2 1/2), etc
My intention is to have X/X and X X/X as a valid input format. X is double. however, it only supports positive number. Maybe using "fraction" is not right coz I do count numerator number with decimal point as valid input but ... you get my intention.
btw, vt_r8 is a double variant type
You might consider a VT_I4 or a VT_UI4 rather than a
VT_R8 for your VARIANT.
You can always create a X X/X from a X/X and you can convert an input X X/X to an X/X. So the common format X/X seems you best bet.
Further, numerators and denominators are integers and not reals. Hcne, I recommend the VT_I4 or for positives only, a VT_UI4.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: EricLin |
last post by:
Dear all:
I am just concerned about how to use a function with variable number
of arguments. for example:
int TestFunction(arguments)
{
....
Testing contents...
}
|
by: G Patel |
last post by:
Hi, I'm wondering if anyone knows if the following function will
function properly as a set-bit counter on non 2s complement machines
(as K&R2 implies).
| int bitcount(unsigned x)
| {
| ...
|
by: Nimmi Srivastav |
last post by:
Consider two functions A and B, both of which accept a variable number
of arguments (va_start, va-arg, va_end). Is there an easy way for
arguments passed to A to be, in turn, passed to B? (For...
|
by: Luke Wu |
last post by:
Is there a C function that returns the number of digits in an input
int/long?
example:
numdigits(123) returns 3
numdigits(1232132) returns 7
|
by: pocmatos |
last post by:
Hi all,
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits + and keep them in an int, however if the
number is bigger than int, I should output error....
|
by: Richard Hollenbeck |
last post by:
How can a numeric field display results in fractions? If my recipe data
requires 1/2 cup of something, for example, I'm willing to punch in 0.5 in
the quantity field then select cup in the unit...
|
by: John Friedland |
last post by:
My problem: I need to call (from C code) an arbitrary C library
function, but I don't know until runtime what the function name is,
how many parameters are required, and what the parameters are. I...
|
by: sonic |
last post by:
I am fairly new to C++. I was wondering how would one go about checking to see if a value entered by the user was negative or positive. Is this something you might use modulo(%) math for?
...
|
by: eadavid |
last post by:
HI,
I am a beginner to perl and I need to write perl code for checking a good die in the wafermap.
Here is the wafermap look like in file
....XXXX111111111X1111XXXX.......
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |