程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 字符串轉整數(C#),

字符串轉整數(C#),

編輯:C#入門知識

字符串轉整數(C#),


using System;
using System.Collections.Generic;

namespace StrToInt_CS
{
    class Program
    {
        static void Main(string[] args)
        {
             string str = "";
             while (!String.IsNullOrEmpty(str = Console.ReadLine()))
             {
                 KeyValuePair<int, bool> ret = Program.StrToInt(str);
                 if (ret.Value)
                 {
                     Console.WriteLine("Number for {0} is {1}", str, ret.Key);
                 }
                 else
                 {
                     Console.WriteLine("The input {0} is invalid", str);
                 }
             }
        }

        public static KeyValuePair<int, bool> StrToInt(string s)
        {
            bool isMinus = false;
            bool isValid = false;
            int num = 0;
            int k = 0;

            // if contain leading space, then invalid
            if (s[k] == ' ')
            {
                new KeyValuePair<int, bool>(num, isValid);
            }

            // if contain operator
            if (s[k] == '-')
            {
                isMinus = true;
                k++;
            }
            if (k < s.Length && s[k] == '+')
            {
                isMinus = false;
                k++;
            } 

            int flag = isMinus ? -1 : 1;

            for (int i = k; i < s.Length; i++)
            {
                if (s[i] >= '0' && s[i] <= '9')
                {
                    try
                    {
                        // check number is not overflow
                        checked
                        {
                            num = num * 10 + flag * (s[i] - '0');
                        }
                    }
                    catch (Exception)
                    {
                        isValid = false;
                        break;
                    }
                }
                else
                {
                    break;
                }

                // if complete traverse the whole string, then is valid 
                if (i + 1 == s.Length) 
                {
                    isValid = true;
                } 
            }

            return new KeyValuePair<int,bool>( num, isValid);
        }
    }
}

Test cases:

null
The input null is invalid
""
The input "" is invalid
" "
The input " " is invalid

The input    is invalid
123
Number for 123 is 123
+123
Number for +123 is 123
-123
Number for -123 is -123
1a3333
The input 1a3333 is invalid
+0
Number for +0 is 0
-0
Number for -0 is 0
2147483647
Number for 2147483647 is 2147483647
2147483648
The input 2147483648 is invalid
-2147483648
Number for -2147483648 is -2147483648
-2147483649
The input -2147483649 is invalid
+
The input + is invalid
-
The input - is invalid
++
The input ++ is invalid
__
The input __ is invalid
--
The input -- is invalid


C語言中怎把一個整數轉換成字符串或字符?

atoi: 把字符串轉換成整型數
itoa:把整數轉換為字符串
#include <stdlib.h>
#include <stdio.h>
{
int main(void)
int number = 12345;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
 

C語言裡,一個把整數轉換成字符數組的問題

輸出是10啊,
#include <stdio.h>
void main()
{
int i=10;
char s[10];
sprintf(s,"%d",i);
// itoa(i,s,10); //效果一樣的
printf("%s",s);
}
你看是不是哪弄錯了?
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved