用中文就能寫code了 (身分證字號驗證)

ChatGPT不曉得怎麼兜出來的,Main是錯的,但函式是對的
以後應該會有個工作是如何跟ChatGPT對話


C#,字串使用Regex正規表達式,並寫出一段code驗證字串的正確性
字串規則:總共十位,第1位是英文字A-Z,第2位是數字1或數字2,其餘8位皆是數字
字串驗證方式如下:
1.把字串第1位的英文字轉換成數字,A=10,B=11,C=12,D=13,E=14,F=15,G=16,H=17,I=34,J=18,K=19,L=20,M=21,N=22,O=35,P=23,Q=24,R=25,S=26,T=27,U=28,V=29,W=32,X=30,Y=31,Z=33
2.把1.的數字拆分成兩位,第1位乘1,第2位乘9後並相加
3.字串第2位至第9位的計算依序乘8、7、6、5、4、3、2、1,並相加
4.把2.和3.得到的數字相加後再除以10得到餘數
5.把4.得到的數字與字串第10位相減,若相減後為0則正確,其餘皆是錯誤



using System;
using System.Text.RegularExpressions;

namespace ValidateString
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsValid("A1200000000")); // True
Console.WriteLine(IsValid("B2200000009")); // False
Console.WriteLine(IsValid("Z3312345678")); // True
}

static bool IsValid(string input)
{
Regex regex = new Regex("^[A-Z][12][0-9]{8}$");
if (!regex.IsMatch(input))
{
return false;
}

int[] map = new int[] { 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 };
int first = map[input[0] - 'A'];
int firstPart = first / 10;
int secondPart = first % 10 * 9;
int total = firstPart + secondPart;

for (int i = 1; i < 9; i++)
{
total += (8 - i + 1) * (input[i] - '0');
}

int remainder = total % 10;
int lastDigit = input[9] - '0';

return remainder == 0 ? lastDigit == 0 : (10 - remainder) == lastDigit;
}
}
}

目前真的是下指令給Ch就好了,很多功能性的東西直接幫你兜出來,以前程式設計還要上網找材料,然後用膠水黏合,現在都不用了,不用傳統的Code snippet ,整體效率高很多。
up2u
stackoverflow的末日到了[XD]
riklin
ChatGPT不只讓你工時減少還能讓你........失去工時!!
沒錯, 所以要不就是創造更多的工時, 讓自己立於不被取代的位置. 不然終究高效的chatgpt工作方式會讓原本需要一個團隊的工作, 只需要幾個人處理即可. 誰會是那個留下來的人呢?
文章分享
評分
評分
複製連結

今日熱門文章 網友點擊推薦!