## 有没有人做过字符串四则运算的？

```a+a+1  =》 2*a+1
(a+b)-(a-b) =》 2*b
a*(a+b) =》 a*a+a*b
(a+b)*(a+b) =》 a*a+2*a*b+b*b```

0

0

0

0

+1
0

#### 引用来自“jwjiang”的答案

```public class StringComplex
{
public string ResultString { get; set; }
public StringComplex CompA { get; set; }
public StringComplex CompB { get; set; }
public string CompOper { get; set; }
public StringComplex(string str)
{
ResultString = str;
CompOper = "";
}
public static StringComplex operator +(StringComplex sc1, StringComplex sc2)
{
StringComplex sc = new StringComplex(sc1.ResultString + " + " + sc2.ResultString);
sc.CompA = sc1;
sc.CompB = sc2;
sc.CompOper = "+";
return sc;
}
public static StringComplex operator *(StringComplex sc1, StringComplex sc2)
{
if (sc1.CompOper != "" && sc1.CompOper != "*")
{
return GetResult(sc1.CompA * sc2, sc1.CompB * sc2, sc1.CompOper);
}
if (sc2.CompOper != "" && sc2.CompOper != "*")
{
return GetResult(sc1 * sc2.CompA, sc1 * sc2.CompB, sc2.CompOper);
}
StringComplex sc = new StringComplex(sc1.ResultString + " * " + sc2.ResultString);
return sc;
}
public static StringComplex GetResult(StringComplex sc1, StringComplex sc2, string oper)
{
switch (oper)
{
case "+":
return sc1 + sc2;
case "*":
return sc1 * sc2;
default:
return null;
}
}
}```

```StringComplex a = new StringComplex("a");
StringComplex b = new StringComplex("b");
Console.WriteLine("a*(a+b)=" + (a * (a + b)).ResultString);
Console.WriteLine("(a+b)*b=" + ((a + b) * b).ResultString);
Console.WriteLine("(a+b)*(a+b)=" + ((a + b) * (a + b)).ResultString);

0

oh，看来是内行
0

0

0

0