Unity编程之简易计算机
效果如下
实现计算器的主要思路:
将(点击按键)输入的式子存储起来并计算。
计算式子
输入:式子的字符串
输出:计算结果
这是一个经典的队列数据结构应用的问题,具体的解法无需赘述(网络上有很多资源)
核心思想:用两个队列分别维护数字和运算符,对于不同优先级的运算符进行不同的出对入队操作直至队列为空
具体代码:
double Cal(string str){
Stack<char> s = new Stack <char>();
Stack<double> num = new Stack <double>();
double a, b;
char ch;
for(int i = 0 ; i < str.Length; i++){
if(str[i] >= '0' && str[i] <= '9'){
a = 0;
while(str[i] >= '0' && str[i] <= '9'){
a = a*10+(str[i]-'0');
i++;
}
if(str[i] == '.'){
i++;
double pow = 0.1;
while(str[i] >= '0' && str[i] <= '9'){
a = a+ pow*(str[i]-'0');
pow*=0.1;
i++;
}
}
i--;
num.Push(a);
}
else if(str[i] == '*'){
b = 0;
i++;
while(str[i] >= '0' && str[i] <= '9'){
b = b*10+(str[i]-'0');
i++;
}
i--;
a = num.Pop();
a *= b;
num.Push(a);
}
else if(str[i] == '/'){
b = 0;
i++;
while(str[i] >= '0' && str[i] <= '9'){
b = b*10+(str[i]-'0');
i++;
}
i--;
a = num.Pop();
a /= b;
num.Push(a);
}
else if(str[i] == '+' || str[i] == '-'){
if(s.Count > 0){
ch = s.Pop();
a = num.Pop();
b = num.Pop();
if(ch == '+'){
a += b;
}
else{
a = b-a;
}
num.Push(a);
}
s.Push(str[i]);
}
}
while(s.Count > 0){
ch = s.Pop();
a = num.Pop();
b = num.Pop();
if(ch == '+'){
a += b;
}
else{
a = b-a;
}
num.Push(a);
}
return num.Peek();
}
利用IMGUI控件画计算器
主要利用IMGUI中的几种控件:
-
GUI.Label
GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
-
GUI.Box
GUI.Box(new Rect(180, 15, 260, 285), " ");
-
GUI.TextField
string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
如何画出计算器:用一个二维数组存储计算机各个位置的数字或运算符:
private char[, ] num = new char[4, 4]{
{'7', '8', '9', '/'},
{'4', '5', '6', '*'},
{'1', '2', '3', '-'},
{'0', '.', '=', '+'}
};
通过for循环画出每一个Button并绑定相应事件文章来源:https://www.toymoban.com/news/detail-429303.html
-
=:调用
Cal
函数计算输入的式子。 - 非=:存储按钮对应的元素。
具体代码:文章来源地址https://www.toymoban.com/news/detail-429303.html
void OnGUI(){
GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
GUI.Box(new Rect(180, 15, 260, 285), " ");
string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
// int num = 9-(3*j+i+1);
if (GUI.Button (new Rect (210 + i * 50, 80 + j * 50, 50, 50), num[j,i].ToString()))
{
GUI.Label (new Rect (25, 25, 100, 30), num[j,i].ToString());
str += num[j,i];
str1 = str;
if(num[j,i] == '='){
Debug.Log(str);
if(str.Length > 1){
Debug.Log(Cal(str));
a = Cal(str);
}
str1 = a.ToString();
str = "";
}
}
}
}
string text1 = GUI.TextField(new Rect(210, 30, 200, 40), str1);
}
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game : MonoBehaviour
{
string str = "";
string str1 = "";
double a;
// Start is called before the first frame update
private char[, ] num = new char[4, 4]{
{'7', '8', '9', '/'},
{'4', '5', '6', '*'},
{'1', '2', '3', '-'},
{'0', '.', '=', '+'}
};
void Start()
{
// Init();
// OnGUI();
}
// Update is called once per frame
void Update()
{
}
void OnGUI(){
GUI.Label (new Rect (85, 20, 100, 30), "简易计算器");
GUI.Box(new Rect(180, 15, 260, 285), " ");
string text = GUI.TextField(new Rect(210, 30, 200, 40), " ");
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
// int num = 9-(3*j+i+1);
if (GUI.Button (new Rect (210 + i * 50, 80 + j * 50, 50, 50), num[j,i].ToString()))
{
GUI.Label (new Rect (25, 25, 100, 30), num[j,i].ToString());
str += num[j,i];
str1 = str;
if(num[j,i] == '='){
Debug.Log(str);
if(str.Length > 1){
Debug.Log(Cal(str));
a = Cal(str);
}
str1 = a.ToString();
str = "";
}
}
}
}
string text1 = GUI.TextField(new Rect(210, 30, 200, 40), str1);
}
double Cal(string str){
Stack<char> s = new Stack <char>();
Stack<double> num = new Stack <double>();
double a, b;
char ch;
for(int i = 0 ; i < str.Length; i++){
if(str[i] >= '0' && str[i] <= '9'){
a = 0;
while(str[i] >= '0' && str[i] <= '9'){
a = a*10+(str[i]-'0');
i++;
}
if(str[i] == '.'){
i++;
double pow = 0.1;
while(str[i] >= '0' && str[i] <= '9'){
a = a+ pow*(str[i]-'0');
pow*=0.1;
i++;
}
}
i--;
num.Push(a);
}
else if(str[i] == '*'){
b = 0;
i++;
while(str[i] >= '0' && str[i] <= '9'){
b = b*10+(str[i]-'0');
i++;
}
i--;
a = num.Pop();
a *= b;
num.Push(a);
}
else if(str[i] == '/'){
b = 0;
i++;
while(str[i] >= '0' && str[i] <= '9'){
b = b*10+(str[i]-'0');
i++;
}
i--;
a = num.Pop();
a /= b;
num.Push(a);
}
else if(str[i] == '+' || str[i] == '-'){
if(s.Count > 0){
ch = s.Pop();
a = num.Pop();
b = num.Pop();
if(ch == '+'){
a += b;
}
else{
a = b-a;
}
num.Push(a);
}
s.Push(str[i]);
}
}
while(s.Count > 0){
ch = s.Pop();
a = num.Pop();
b = num.Pop();
if(ch == '+'){
a += b;
}
else{
a = b-a;
}
num.Push(a);
}
return num.Peek();
}
}
代码已上传到gitee仓库中 https://gitee.com/lian-zhilu/3d_assignment
到了这里,关于Unity编程之简易计算机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!