Write a C# Sharp program to convert a given string value to a 32-bit signed integer
- برمجة سي شارب
- برمجة
- 2021-05-31
- ahmadghneem
الأجوبة
using System;
namespace exercises {
class Program {
static void Main(string[] args) {
String str1;
str1 = "123456";
Console.WriteLine("Original String value: " + str1);
Console.WriteLine("Convert the said string to integer value:");
Console.WriteLine(str_to_integer(str1));
str1 = "+3456";
Console.WriteLine("Original String value: " + str1);
Console.WriteLine("Convert the said string to integer value:");
Console.WriteLine(str_to_integer(str1));
str1 = "-123456";
Console.WriteLine("Original String value: " + str1);
Console.WriteLine("Convert the said string to integer value:");
Console.WriteLine(str_to_integer(str1));
str1 = "a1234";
Console.WriteLine("Original String value: " + str1);
Console.WriteLine("Convert the said string to integer value:");
Console.WriteLine(str_to_integer(str1));
str1 = "123a456";
Console.WriteLine("Original String value: " + str1);
Console.WriteLine("Convert the said string to integer value:");
Console.WriteLine(str_to_integer(str1));
}
public static int str_to_integer(String str1)
{
if (string.IsNullOrWhiteSpace(str1)) { return 0; }
var flag = false;
var index_num = 0;
while (index_num < str1.Length && str1[index_num] == ' ') { index_num++; }
if (str1[index_num] == '-')
{
flag = true;
index_num++;
}
else if (str1[index_num] == '+')
{
index_num++;
}
var max1 = int.MaxValue / 10;
var max2 = int.MaxValue % 10;
var result = 0;
for (; index_num < str1.Length; index_num++)
{
var str_digit = str1[index_num] - '0';
if (str_digit < 0 || str_digit > 9) { break; }
if (result > max1 ||
(result == max1 && str_digit > max2))
{
return flag ? int.MinValue : int.MaxValue;
}
result = result * 10 + str_digit;
}
return flag ? -result : result;
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال