using UnityEditor;
using UnityEngine;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class BigNumberConverter : EditorWindow
{
private string inputString = "";
private string resultString = "";
private string numberA = ""; // 첫 번째 숫자 입력 필드
private string numberB = ""; // 두 번째 숫자 입력 필드
private string calculationResult = ""; // 계산 결과 필드
private UnityEngine.Vector2 scrollPosition;
private List<string> suffixes = new List<string>(new[] { "" });
[MenuItem("Tools/BigNumberConverter")]
public static void ShowWindow()
{
GetWindow<BigNumberConverter>("Big Number Converter");
}
void OnGUI()
{
GUILayout.Label("Ver. 1.2.0", EditorStyles.boldLabel);
DrawLine();
GUILayout.Label("Big Number Converter", EditorStyles.boldLabel);
GUIStyle textAreaStyle = new GUIStyle(EditorStyles.textArea)
{
wordWrap = true
};
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
inputString = EditorGUILayout.TextArea(inputString, textAreaStyle, GUILayout.ExpandHeight(true));
EditorGUILayout.EndScrollView();
if (GUILayout.Button("Convert"))
{
resultString = ConvertToBigNumberFormat(inputString);
}
EditorGUILayout.LabelField("Converted Number:", resultString);
GUILayout.Space(10);
DrawLine();
GUILayout.Label("Big Number calculator", EditorStyles.boldLabel);
// 사칙연산을 위한 추가 기능
GUILayout.Label("Number A:");
numberA = EditorGUILayout.TextArea(numberA, textAreaStyle);
GUILayout.Label("Number B:");
numberB = EditorGUILayout.TextArea(numberB, textAreaStyle);
GUILayout.BeginHorizontal();
if (GUILayout.Button("+")) PerformCalculation("+");
if (GUILayout.Button("-")) PerformCalculation("-");
if (GUILayout.Button("*")) PerformCalculation("*");
if (GUILayout.Button("/")) PerformCalculation("/");
GUILayout.EndHorizontal();
EditorGUILayout.LabelField("Calculation Result:", calculationResult);
}
private string ConvertToBigNumberFormat(string input)
{
BigInteger number;
if (BigInteger.TryParse(input, out number))
{
return NumberToBigNumberFormat(number);
}
else
{
return "Invalid Input";
}
}
private string NumberToBigNumberFormat(BigInteger number)
{
if (number == 0) return "0";
int unitIndex = 0;
double value = (double)number;
while (value >= 1000)
{
value /= 1000;
unitIndex++;
}
while (unitIndex >= suffixes.Count)
{
ExtendSuffixes();
}
string suffix = unitIndex > 0 ? suffixes[unitIndex - 1] : "";
string format = "0.##";
string result = value.ToString(format) + suffix;
return result;
}
private void PerformCalculation(string operation)
{
BigInteger a, b;
bool isANumber = BigInteger.TryParse(numberA, out a);
bool isBNumber = BigInteger.TryParse(numberB, out b);
if (!isANumber || !isBNumber)
{
calculationResult = "Invalid Input";
return;
}
switch (operation)
{
case "+":
calculationResult = (a + b).ToString();
break;
case "-":
calculationResult = (a - b).ToString();
break;
case "*":
calculationResult = (a * b).ToString();
break;
case "/":
if (b == 0)
calculationResult = "Cannot divide by zero";
else
calculationResult = (a / b).ToString();
break;
default:
calculationResult = "Unknown operation";
break;
}
}
private void ExtendSuffixes()
{
if (suffixes.LastOrDefault() == "")
{
suffixes[0] = "A";
}
else
{
string lastSuffix = suffixes.Last();
char lastChar = lastSuffix.Last();
if (lastChar < 'Z')
{
suffixes.Add(lastSuffix.Substring(0, lastSuffix.Length - 1) + (char)(lastChar + 1));
}
else
{
if (lastSuffix.All(c => c == 'Z'))
{
suffixes.Add(new string('A', lastSuffix.Length + 1));
}
else
{
int index = lastSuffix.Length - 1;
while (lastSuffix[index] == 'Z')
{
index--;
}
string nextSuffix = lastSuffix.Substring(0, index) + (char)(lastSuffix[index] + 1);
nextSuffix = nextSuffix.PadRight(lastSuffix.Length, 'A');
suffixes.Add(nextSuffix);
}
}
}
}
private void DrawLine()
{
var rect = GUILayoutUtility.GetRect(1, 1, GUILayout.ExpandWidth(true));
EditorGUI.DrawRect(rect, Color.gray);
}
}
'메모장' 카테고리의 다른 글
이벤트(Event), 이벤트 핸들러(EventHandler), 그리고 이벤트 기반 프로그래밍(Event-driven programming) (0) | 2024.02.26 |
---|---|
System.Text.RegularExpressions.Regex.IsMatch - 문자열 매칭 메서드 (0) | 2024.02.23 |
Application.dataPath 및 파일 저장 경로 메모 (0) | 2024.02.13 |
DeserializeObject/SerializeObject + 싱글톤 DataManager 메모 (0) | 2024.02.08 |
com.unity.nuget.newtonsoft-json (0) | 2024.02.08 |