C# winApp จะแก้ตัวเลขยกกำลังแบบใหญ่บ้าง เล็กบ้าง ให้มีขนาดเท่ากันได้ยังไง บ้างครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp5
{
public static class SomeClass
{
private static readonly string superscripts = @"⁰¹²³⁴⁵⁶⁷⁸⁹";
public static string ToSuperscriptNumber_(this int @this)
{
var sb = new StringBuilder();
Stack<byte> digits = new Stack<byte>();
do
{
var digit = (byte)(@this % 10);
digits.Push(digit);
@this /= 10;
} while (@this != 0);
while (digits.Count > 0)
{
var digit = digits.Pop();
sb.Append(superscripts[digit]);
}
return sb.ToString();
}
}
public static class SuperscriptNumber
{
//https://stackoverflow.com/questions/15042334/how-can-i-add-superscript-power-operators-in-c-sharp-winforms
//https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
//https://stackoverflow.com/questions/19682459/superscript-label-or-form-name
private static readonly string superscripts = @"0123456789+-";
private static string ToSuperscriptNumber(this int @this)
{
var sb = new StringBuilder();
var digits = @this.ToString().ToArray();
foreach (var digit in digits)
{
if (digit == '0')
{
sb.Append(("\u2070"));
}
else if (digit == '1')
{
sb.Append(("\u00B9"));
}
else if (digit == '2')
{
sb.Append(("\u00B2"));
}
else if (digit == '3')
{
sb.Append(("\u00B3"));
}
else if (digit == '4')
{
sb.Append(("\u2074"));
}
else if (digit == '5')
{
sb.Append(("\u2075"));
}
else if (digit == '6')
{
sb.Append(("\u2076"));
}
else if (digit == '7')
{
sb.Append(("\u2077"));
}
else if (digit == '8')
{
sb.Append(("\u2078"));
}
else if (digit == '9')
{
sb.Append(("\u2079"));
}
else if (digit == '+')
{
sb.Append(("\u207A"));
}
else if (digit == '-')
{
sb.Append(("\u207B"));
}
}
return sb.ToString();
}
public static string ToSuperScript(this int number)
{
if (number == 0 ||
number == 1)
return "";
const string SuperscriptDigits =
"\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
string Superscript = "";
if (number < 0)
{
//Adds superscript minus
Superscript = ((char)0x207B).ToString();
number *= -1;
}
Superscript += new string(number.ToString()
.Select(x => SuperscriptDigits[x - '0'])
.ToArray()
);
return Superscript;
}
public static string ToSuperscriptNumber(this string @this)
{
var a = @this.Trim().Split('^');
string s = "";
if (a.Length == 2)
{
string _a = a[0].Trim();
int _b = int.Parse(a[1].Trim());
s = _a + _b.ToSuperscriptNumber();
}
return s;
}
}
}
Code (C#)
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int yC = 20;
for (int i = 1; i < 20; i++)
{
e.Graphics.DrawString($"{random .Next(1,10) + random.Next(1,10).ToSuperScript()} + {random.Next(1, 10) + random.Next(1, 10).ToSuperScript()} - {random.Next(1, 10) + random.Next(1, 10).ToSuperScript()}= " ,
new Font("Cordia New", 20), new SolidBrush(Color.Black), 50, yC);
yC += 50;
}
}
Tag : .NET, Win (Windows App), C#
Date :
2022-10-11 15:35:44
By :
lamaka.tor
View :
706
Reply :
8
ผมลองไล่ดูตั้งแต่ 0-10 แล้วได้แบบนี้ ครับ
Code (C#)
string ToSuperScript( int number)
{
if (number == 0 ||
number == 1)
return "";
const string SuperscriptDigits =
"\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
string Superscript = "";
if (number < 0)
{
//Adds superscript minus
Superscript = ((char)0x207B).ToString();
number *= -1;
}
Superscript += new string(number.ToString()
.Select(x => SuperscriptDigits[x - '0'])
.ToArray()
);
return Superscript;
}
Code (C#)
private void Form1_Paint(object sender, PaintEventArgs e)
{
int y = 30;
string s = "";
for (int i = 0; i < 10; i++)
{
s = "";
for (int a = 0; a < 11; a++)
{
s +=a + ToSuperScript(i) + ",";
}
e.Graphics.DrawString(s, new Font("Cordia New", 20), new SolidBrush(Color.Black), 50, y + (35 * i));
}
}
Date :
2022-10-11 21:06:13
By :
lamaka.tor
Load balance : Server 02