Mã:
using System;using System.Windows.Forms; namespace MayTinhCaNhan{ public partial class maytinh : Form { public maytinh() { InitializeComponent(); } String[] PhepTinh = new String[] {"%", "*", "/", "+", "-" }; private String iif(Boolean DieuKien, String NeuDung, String NeuSai) { if (DieuKien) return NeuDung; else return NeuSai; } private Decimal iif(Boolean DieuKien, Decimal NeuDung, Decimal NeuSai) { if (DieuKien) return NeuDung; else return NeuSai; } private void btnNaoDo_Click(object sender, EventArgs e) { Button btn = (Button)sender; tbxKQ.Text += btn.Text; } private void btnBS_Click(object sender, EventArgs e) { tbxKQ.Text = tbxKQ.Text.Substring(0, tbxKQ.Text.Length - 1); } private void btnCE_Click(object sender, EventArgs e) { tbxKQ.Text = ""; } private void ketqua_Click(object sender, EventArgs e) { tbxKQ.Text = ThucHien(SuaLoi(tbxKQ.Text)); } private String SuaLoi(String str) { return str .Replace("++", "+") .Replace("+-", "-") .Replace("-+", "-") .Replace("--", "+") .Replace("**", "*") .Replace("//", "/") .Replace("\\", "") .Replace("(","") .Replace(")",""); } private String ThucHien(String str) { while (str.Contains("("))//Tính khi có ngoặc { String tam = ChuoiTrongNgoac(str); String strDau = str.Substring(0, str.IndexOf(tam)); String strCuoi = str.Substring(str.IndexOf(tam) + tam.Length); str = SuaLoi(strDau + Tinh(ChuoiTrongNgoac(tam)) + strCuoi); } return Tinh(str); } private String ChuoiTrongNgoac(String str) { String KQ = ""; if (str.Contains("(")) if (str.Contains(")")) { Int32 BatDau = str.LastIndexOf("(") , KetThuc = str.IndexOf(")") + 1; if (BatDau < KetThuc) KQ = str.Substring(BatDau, KetThuc - BatDau); } return KQ; } private String Tinh(String str) { String tam = str; for (Int16 i = 0; i < PhepTinh.Length; i++) while (tam.LastIndexOf(PhepTinh[i])>0) tam = SuaLoi(Tinh(tam, PhepTinh[i])); return tam;//.Replace("(", "").Replace(")", ""); } private String Tinh(String BieuThuc, String PhepTinh) { String So1 = "", So2 = ""; Int32 BatDau = 0, KetThuc = BieuThuc.Length; for (Int32 i = BieuThuc.LastIndexOf(PhepTinh) - 1; i >= 0; i--) if ("0123456789.".Contains(BieuThuc[i].ToString())) So1 = BieuThuc[i] + So1; else if (BieuThuc[i] == '-') { So1 = BieuThuc[i] + So1; BatDau = i; break; } else { if (BieuThuc[i] == '-') So1 = BieuThuc[i] + So1; BatDau = i + 1; break; } for (Int32 i = BieuThuc.LastIndexOf(PhepTinh) + 1; i < BieuThuc.Length; i++) if ("0123456789.".Contains(BieuThuc[i].ToString())) So2 += BieuThuc[i]; else { KetThuc = i; break; } return BieuThuc.Substring(0, BatDau) + Tinh(So1, PhepTinh, So2) + BieuThuc.Substring(KetThuc); } private String Tinh(String So1, String PhepTinh, String So2) { if (So1 == "") return PhepTinh + So2; else { Decimal a = Convert.ToDecimal(So1), b = Convert.ToDecimal(So2); return iif(PhepTinh == "*", a * b , iif(PhepTinh == "/", a / b , iif(PhepTinh == "+", a + b , iif(PhepTinh == "-", a - b , iif(PhepTinh == "%", a % b, 0))))).ToString(); } } }}
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...