//修订号比较,s1大返回1,s2大返回-1,相等返回0;如果发生错误返回错误 func compareCor(s1, s2 string) (int,error ){ var i int //去除前导0 for { if i >= len(s1){ s1 = "0" break } if s1[i] == '0' { i++ } else { s1 = s1[i:] break } } i = 0 for { if i >= len(s2){ s2 = "0" break } if s2[i] == '0' { i++ } else { s2 = s2[i:] break } } if len(s1) == 0{ if len(s2) == 0{ return 0,nil }else{ return -1,nil } } s1Int,err := strconv.Atoi(s1) if err != nil { // log.Println("") return 0,err } s2Int,err := strconv.Atoi(s2) if err != nil { return 0,err } if s1Int == s2Int { return 0,nil }else if s1Int > s2Int{ return 1,nil }else{ return -1,nil } } //版本号比较,s1大返回1,s2大返回-1,相等返回0;发生错误则返回错误 func compare(version1 string, version2 string) (int,error ){ // write code here arr1 := strings.Split(version1, ".") arr2 := strings.Split(version2, ".") log.Println("arr1:",arr1) log.Println("arr2:",arr2) i := 0 for ;i < len(arr1) && i < len(arr2);i++{ res,err := compareCor(arr1[i],arr2[i]) if err != nil { log.Println("compareCor ERROR:",err) return 0,err } if res != 0 { return res,nil } } if len(arr1) == len(arr2) { return 0,nil } if len(arr1) > len(arr2){ for ;i < len(arr1);i++{ res,err := compareCor(arr1[i],"0") if err != nil { return 0,err }else{ if res != 0{ return res,nil } } } return 0,nil }else{ for ;i < len(arr2);i++{ res,err := compareCor("0",arr2[i]) if err != nil { return 0,err }else{ if res != 0{ return res,nil } } } return 0,nil } }这是go 语言的代码,转为C#代码
时间: 2024-04-09 14:33:09 浏览: 149
compare_gan:比较GAN代码
以下是将Go语言代码转换为C#代码的版本较函数:
```csharp
using System;
using System.Linq;
public class Program
{
// 修订比较,s1大1,s2大返回-1,相等返回0;如果发生错误返回错误
public static (int, Exception) CompareCor(string s1, string s2)
{
var i = 0;
// 去除前导0
while (i < s1.Length && s1[i] == '0')
{
i++;
}
if (i == s1.Length)
{
s1 = "0";
}
else
{
s1 = s1.Substring(i);
}
i = 0;
while (i < s2.Length && s2[i] == '0')
{
i++;
}
if (i == s2.Length)
{
s2 = "0";
}
else
{
s2 = s2.Substring(i);
}
if (s1.Length == 0)
{
if (s2.Length == 0)
{
return (0, null);
}
else
{
return (-1, null);
}
}
int s1Int, s2Int;
if (!int.TryParse(s1, out s1Int))
{
return (0, new Exception("Error parsing s1"));
}
if (!int.TryParse(s2, out s2Int))
{
return (0, new Exception("Error parsing s2"));
}
if (s1Int == s2Int)
{
return (0, null);
}
else if (s1Int > s2Int)
{
return (1, null);
}
else
{
return (-1, null);
}
}
// 版本号比较, s1大返回1,s2大返回-1,相等返回0;发生错误则返回错误
public static (int, Exception) Compare(string version1, string version2)
{
var arr1 = version1.Split('.');
var arr2 = version2.Split('.');
int i = 0;
for (; i < arr1.Length && i < arr2.Length; i++)
{
var (res, err) = CompareCor(arr1[i], arr2[i]);
if (err != null)
{
return (0, err);
}
if (res != 0)
{
return (res, null);
}
}
if (arr1.Length == arr2.Length)
{
return (0, null);
}
if (arr1.Length > arr2.Length)
{
for (; i < arr1.Length; i++)
{
var (res, err) = CompareCor(arr1[i], "0");
if (err != null)
{
return (0, err);
}
else
{
if (res != 0)
{
return (res, null);
}
}
}
return (0, null);
}
else
{
for (; i < arr2.Length; i++)
{
var (res, err) = CompareCor("0", arr2[i]);
if (err != null)
{
return (0, err);
}
else
{
if (res != 0)
{
return (res, null);
}
}
}
return (0, null);
}
}
public static void Main()
{
var version1 = "1.2.3";
var version2 = "1.2.4";
var (res, err) = Compare(version1, version2);
if (err != null)
{
Console.WriteLine("Error: " + err.Message);
}
else
{
Console.WriteLine("Comparison Result: " + res);
}
}
}
```
这段C#代码实现了与原始Go代码相同的逻辑,可以用于进行版本号比较。请注意,C#代码中的错误处理方式可能与Go代码不同,这是为了适应C#语言的异常处理机制。
阅读全文