Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Back in my calculator post calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Rollback to Revision 2
Source Link
Snowbody
  • 8.7k
  • 25
  • 50

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel == 0- ?1); decimalLevel :// decimalLeveluse "- 1);1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel == 0 ? decimalLevel : decimalLevel - 1);
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Remove major bug not mentioned in any answers.
Source Link
user34073
user34073

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1"== because0 value? incrementsdecimalLevel just: beforedecimalLevel ending- loop1);
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel - 1); // use "- 1" because value increments just before ending loop
}

Please analyze my code as thoroughly as the compiler does.

Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole way and build a double from scratch. I looked through a few streams, but the only one I found that supported reading a double was the BinaryReader. This, unfortunately, reads exactly 8 bytes, and the data is stored as a char (or rather, as an int representation of a char), so I could enter the equation "5+5", and it would crash when it read the "+". If you know of a better solution, I would prefer using it.

private double GetNextNumber(StreamReader dataStream)
{
 double value = 0;
 int decimalLevel = 0;
 while (true)
 {
 if (!"0123456789.".Contains((char)dataStream.Peek()))
 {
 break;
 }
 char token = (char)dataStream.Read();
 if (token == '.')
 {
 if (decimalLevel == 0)
 {
 ++decimalLevel;
 continue;
 }
 throw new InvalidDataException("Invalid number format.");
 }
 int digit = int.Parse(token.ToString());
 value = value * 10 + digit;
 if (decimalLevel != 0)
 {
 ++decimalLevel;
 }
 }
 return value / Math.Pow(10, decimalLevel == 0 ? decimalLevel : decimalLevel - 1);
}

Please analyze my code as thoroughly as the compiler does.

added 13 characters in body
Source Link
user34073
user34073
Loading
Source Link
user34073
user34073
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /