This question already has answers here:
Convert a text fraction to a decimal
(5 answers)
Parse Math Expression [duplicate]
(9 answers)
Closed 3 years ago.
I have created a method that splits a string a^b and converts a and b to doubles. However, if I input the value of either a or b using a fraction (for example 3/5 instead of 0.6) the method doesn't work; it only allows me to input it like 0.6. Why is this, and is it possible to fix it?
The code is shown below:
public static double Coefficient()
{
while (true)
{
string input = Console.ReadLine();
string[] items = input.Split('^');
if (items.Length == 1)
{
if (double.TryParse(items[0], out double A))
return A;
}
else if (items.Length == 2)
{
if (double.TryParse(items[0], out double A) &
double.TryParse(items[1], out double B))
return Math.Pow(A, B);
}
Console.WriteLine("\nPlease follow the specified input form.");
}
}
You're trying to parse expressions with methods that only know how to handle specifically formatted numbers as inputs. You need to either write an equation parser or split your inputs appropriately. You are already doing this by splitting ^ - you can do the same with /.
In fact, there are libraries that already do this.
Related
This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 3 years ago.
I can't get the string of text_box.text to convert to an int then compare to another int.
I also need to check to see if the string of text_box.text can be turned into an int.
I've tried .ToInt32, which I have always used and it has been fine. I have no idea how to test if the string of text_box can be turned into an int.
public static void before (int bS)
{
beforeScore = bS;
}
//some space later
if (score_bet_text_box.Text.ToInt32() > beforeScore)
{
MessageBox.Show("You can't bet more than you have", "game");
}
I expect it to convert the string of text_box into an int, then compare it to the other int.
I also hope to test if it can be converted into an int, but have no clue how so it is not shown in the code above.
ToInt32 isn't a method on a string unless you have an extension method somewhere. You want to use the TryParse method as follows...
if(int.TryParse(score_bet_text_box.Text, out int result))
{
if(result > beforeScore)
{
MessageBox.Show("You can't bet more than you have", "game");
}
}
If you are using an older version of C# you'll have to define result outside the if as follows:
int result;
if(int.TryParse(score_bet_text_box.Text, out result))
This question already has answers here:
Convert String to int in C#
(5 answers)
Closed 4 years ago.
I have been learning the basics of C# with a Console Application. I was wondering if anyone knew how to use an IF statement with a String instead of an integer. It's a bit annoying and I need it so I can compare a value the user has outputted to the console. This is because the Console.ReadLine(); only likes Strings and not Integers. Below is my code:
string num = Console.ReadLine();
if (num == 9)
{
Console.WriteLine("Ooh my number is 9");
}
Any help is appreciated!
You should always validate integer user input with TryParse
int.TryParse
Converts the string representation of a number to its 32-bit signed
integer equivalent. A return value indicates whether the operation
succeeded.
string value = Console.ReadLine();
if(!int.TryParse(value, out var num))
{
Console.WriteLine("You had one job!");
}
else if (num == 9)
{
Console.WriteLine("Ooh my number is 9");
}
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I have the following using MathNet library where child1 is -4.09 and child2 is -4.162. The result after Expression.Real((double1 - double2)) returns 0.072000000000000064. It should be 0.072. Can anyone please help me understand what is going on?
private static Expression GetSimplifiedExpression(Expression child1, Expression child2, LaTeXTokenType tokenType)
{
double double1 = Evaluate.Evaluate(null, child1).RealValue;
double double2 = Evaluate.Evaluate(null, child2).RealValue;
return Expression.Real((double1 - double2));
}
First, let's convert decimal to binary:
-4.09 = -100.00010111000010100011110101110000101000111101011100001010001111...
-4.162 = -100.00101001011110001101010011111101111100111011011001000101101000...
Then, subtract those two binaries. The result in binary is:
0.00010010011011101001011110001101010011111101111100111011011001...
which is approximately equal to decimal 0.07199999999999999998.
It is not exactly 0.072000000000000064, but I think you could get the idea behind it. If you want the exact result, you could cast double to decimal:
var decimal1 = (decimal) double1;
var decimal2 = (decimal) double2;
This question already has answers here:
How can I check if a string is a number?
(25 answers)
Closed 8 years ago.
I have a string list array that contains strings. Either the values will be a name or number. For example the contents could be:
stringList[0]="Mary"
stringList[1]="John"
stringList[2]="4564321"
stringList[3]="Steven"
I want to append the contents of the list to a string which I have done through a simple loop but if a number is encountered I want that number to be popped out and handled in a different method and then have the original loop continue looking for strings and appending. Essentially I want to append the non numbers and take the numbers and do something else with them. What functions or tricks can I do so when it is going through the list it will be able to identify a string as a number?
As long as the string is always a number (not numbers and letters mixed), you an use one of the various TryParse methods. I'll use int in my example, but you can use whichever fits your needs:
int value;
foreach(var s in stringList)
{
if(int.TryParse(s, out value))
{
// s was a number, the parsed result is in value
}
else
{
// s was something else
}
}
You can use int.TryParse to determine if it's a number and get its value.
int intValue;
if (int.TryParse(str, out intValue))
// handle as int
else
// handle as string
This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 9 years ago.
I was translating some C++ to C# code and I saw the below definiton:
#define x 'liaM'
First, what does this single quoted constant mean? Do I make it a string constant in c#?
Second, this constant is assigned as value to a uint variable in C++. How does that work?
uint m = x;
This is sometimes called a FOURCC. There's a Windows API that can convert from a string into a FOURCC called mmioStringToFOURCC and here's some C# code to do the same thing:
public static int ChunkIdentifierToInt32(string s)
{
if (s.Length != 4) throw new ArgumentException("Must be a four character string");
var bytes = Encoding.UTF8.GetBytes(s);
if (bytes.Length != 4) throw new ArgumentException("Must encode to exactly four bytes");
return BitConverter.ToInt32(bytes, 0);
}