Lập trinh C++ code máy tính bỏ túi

20 1.7K 1
Lập trinh C++ code máy tính bỏ túi

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //lyvantuan//mangmaytinh namespace MayTinh2 { public partial class Form1 : Form { public bool clickBang = false; public string ketQua=""; public double x, y,z, x1, x2,min; public double a1 = 0, b1 = 0, c1 = 0,d1=0, a2 = 0, b2 = 0, c2 = 0,d2=0,a3=0,b3=0,c3=0,d3=0; public double a = 0, b = 0, c = 0; public int mode = 1; public int ngoacTrai = 0; public int ngoacPhai = 0; public const double PI = 3.141592654; public const double E = 2.718281828; public Form1() { InitializeComponent(); } public double tinhTich(string str) { double tich = 0; int l = 0; string s1 = str.Substring(l, 1); string s2 = ""; string dau = ""; while (s1 != "*" && s1 != "/" && l < str.Length) { s2 = s2 + str.Substring(l, 1); l++; if (l == str.Length) break; s1 = str.Substring(l, 1); } tich = double.Parse(s2); //dau=str.Substring(l,1); while (l < str.Length) { string s3 = ""; dau = str.Substring(l, 1); l++; s1 = str.Substring(l, 1); while (s1 != "*" && s1 != "/" && l < str.Length) { s3 = s3 + str.Substring(l, 1); l++; if (l == str.Length) break; s1 = str.Substring(l, 1); } if (dau == "*") { tich = tich * double.Parse(s3); continue; } if (dau == "/") { tich = tich / double.Parse(s3); continue; } } return tich; } public double tinhKetQua(string str) { int l = str.Length; double result = 0; while (l > 0) { string s1 = ""; string dau = ""; string s = str.Substring(l - 1, 1); while (s != "+" && s != "-" && s != "*" && s != "/" && l > 0) { s1 = str.Substring(l - 1, 1) + s1; l ; if (l == 0) break; s = str.Substring(l - 1, 1); } if (l == 0) dau = ""; else dau = str.Substring(l - 1, 1); if (dau == "") { result += double.Parse(s1); } if (dau == "+") { result = result + double.Parse(s1); l ; continue; } if (dau == "-") { result = result - double.Parse(s1); l ; continue; } if (dau == "*" || dau == "/") { string str_s1 = str.Substring(l - 1, 1); string str_s2 = s1; string d = "";//dấu + or - trước tích:-(3*3*5) while (str_s1 != "+" && str_s1 != "-" && l > 0) { str_s2 = str.Substring(l - 1, 1) + str_s2; l ; if (l == 0) break; str_s1 = str.Substring(l - 1, 1); } if (l == 0) d = "+"; else d = str.Substring(l - 1, 1); if (d == "+") { result += tinhTich(str_s2); l ; } if (d == "-") { result -= tinhTich(str_s2); l ; } } } return result; } public string DaoDau(string str) { string result = ""; int i = str.Length - 1; while (i >= 0) { result += str.Substring(i, 1); i ; } return result; } public string chuoiCongVoiNgoacCoDauTru(string str) { string result = ""; int i = 0; while (i < str.Length) { //Xu ly 9 if (str.Substring(i, 1) == "-" && str.Substring(i + 1, 1) == "-") { result += "+"; i = i + 2; } //Xu ly +-9 if (str.Substring(i, 1) == "+" && str.Substring(i + 1, 1) == "-") { result += "-"; i = i + 2; } //Xu ly ++9 if (str.Substring(i, 1) == "+" && str.Substring(i + 1, 1) == "+") { result += "+"; i = i + 2; } else { result += str.Substring(i, 1); i++; } } return result; } public string chuoiNhanVoiNgoacCoDauTru(string str) { if (str.Substring(0, 1) != "+" && str.Substring(0, 1) != "-") str = "+" + str; string result = ""; int i = str.Length - 1; while (i >= 0) { if (str.Substring(i, 1) == "-" && i > 0 && (str.Substring(i - 1, 1) == "*" || str.Substring(i - 1, 1) == "/")) { string chuoitam = str.Substring(i - 1, 1);//Lay dau nhan hoac chia int vitridaunhan = i - 1; i = i - 2;//Vuot qua dau tru,nhan //Tim vi tri dau + or -gan nhat // while ((str.Substring(i, 1) != "+" && str.Substring(i, 1) != "-") && i > 0) { chuoitam += str.Substring(i, 1); i ; } //MessageBox.Show("Vi tri do la " + i); //Khi gap dau +/- string dau; if (str.Substring(i, 1) == "-") dau = "+"; else dau = "-"; chuoitam += dau; //Vuot qua dau doi i ; result += chuoitam; } else { result += str.Substring(i, 1); i ; } } return (DaoDau(result)); } public string chinhLaiDau(string str) { string s1, s2; s2 = chuoiCongVoiNgoacCoDauTru(str); s1 = chuoiNhanVoiNgoacCoDauTru(s2); return s1; } public string XuLyNgoac(string str, int l) { string s = ""; string ketQua = ""; while (l > 0) { if (str.Substring(l - 1, 1) == ")") { l ; s = XuLyNgoac(str, l); str = s + ketQua; l = str.Length; ketQua = ""; } else { if (str.Substring(l - 1, 1) == "(") { return (str.Substring(0, l - 1) + tinhKetQua(chinhLaiDau(ketQua)).ToString()); } ketQua = str.Substring(l - 1, 1) + ketQua; l ; } } return chinhLaiDau(ketQua); } public string XuLyChuoiCoSin(string str) { string result = ""; int i = 0; while (i < str.Length) { //Xu ly Sin(9+3) string chuoitam = ""; double sin, s; if (str.Substring(i, 1) == "s" && str.Substring(i + 3, 1) == "(") { //vuot qua sin( i = i + 4; int daumongoac = 1; int daudongngoac = 0; while (daumongoac != daudongngoac) { chuoitam += str.Substring(i, 1); if (str.Substring(i, 1) == "(") daumongoac++; if (str.Substring(i, 1) == ")") daudongngoac++; i++; } //Lui lai sau i ; string u, v; u = XuLyNgoac(chuoitam, chuoitam.Length); v = chinhLaiDau(u); s = tinhKetQua(v); //Doi qua do s = s * Math.PI / 180; //u = double.Parse(s); sin = Math.Sin(s); result += sin.ToString(); //vuot qua ) i++; } else { result += str.Substring(i, 1); i++; } } return result; } public string XuLyChuoiCoCos(string str) { string result = ""; int i = 0; while (i < str.Length) { //Xu ly Sin(9+3) string chuoitam = ""; double sin, s; if (str.Substring(i, 1) == "c" && str.Substring(i + 3, 1) == "(") { //vuot qua sin( i = i + 4; int daumongoac = 1; int daudongngoac = 0; while (daumongoac != daudongngoac) { chuoitam += str.Substring(i, 1); if (str.Substring(i, 1) == "(") daumongoac++; if (str.Substring(i, 1) == ")") daudongngoac++; i++; } //Lui lai sau i ; string u, v; u = XuLyNgoac(chuoitam, chuoitam.Length); v = chinhLaiDau(u); s = tinhKetQua(v); //Doi qua do s = s * Math.PI / 180; //u = double.Parse(s); sin = Math.Cos(s); result += sin.ToString(); //vuot qua ) i++; } else { result += str.Substring(i, 1); i++; } } return result; } public string XuLyChuoiCoTan(string str) { string result = ""; int i = 0; while (i < str.Length) { //Xu ly Sin(9+3) string chuoitam = ""; double sin, s; if (str.Substring(i, 1) == "t" && str.Substring(i + 3, 1) == "(") { //vuot qua sin( i = i + 4; int daumongoac = 1; int daudongngoac = 0; while (daumongoac != daudongngoac) { chuoitam += str.Substring(i, 1); if (str.Substring(i, 1) == "(") daumongoac++; if (str.Substring(i, 1) == ")") daudongngoac++; i++; } //Lui lai sau i ; string u, v; u = XuLyNgoac(chuoitam, chuoitam.Length); v = chinhLaiDau(u); s = tinhKetQua(v); //Doi qua do s = s * Math.PI / 180; //u = double.Parse(s); sin = Math.Tan(s); result += sin.ToString(); //vuot qua ) i++; } else { result += str.Substring(i, 1); i++; } } return result; } public string XuLyChuoiCoSinCosTan(string str) { string u, v, result; u = XuLyChuoiCoSin(str); v = XuLyChuoiCoCos(u); result = XuLyChuoiCoTan(v); return result; } public bool KiemTraLoiChuoi(string chuoi) { string s1; string s2; if (txt_pheptinh.Text == "") return true; if (ngoacTrai != ngoacPhai) return true; int l = chuoi.Length; s1 = chuoi.Substring(l-1, 1); if (s1 == "+" || s1 == "-" || s1 == "/" || s1 == "*") return true; for (int i = 0; i < l - 1; i++) { s1 = chuoi.Substring(i, 1); s2 = chuoi.Substring(i + 1, 1); if(s1=="1"||s1=="2"||s1=="3"||s1=="4"||s1=="5"||s1=="6"|| s1=="7"||s1=="8"||s1=="9"||s1=="0") if (s2 == "s" || s2 == "c" || s2 == "t") return true; if (s1 == ")") if (s2 == "s" || s2 == "c" || s2 == "t") return true; if(s1=="(") if(s2==")") return true; if (s1 == "/" || s1 == "*" || s1 == "+" || s1 == "-" || s1 == ".") if (s2 == "/" || s2 == "*" || s2 == "+" || s2 == "-" || s2 == ".") return true; } return false; } public string XuLyChuoi(string str) { string u, v, k; k = XuLyChuoiCoSinCosTan(str); u = XuLyNgoac(k, k.Length); v = chinhLaiDau(u); return v; } public double giaiHePhuongTrinhBacNhat2An(double a1, double b1, double c1, double a2, double b2, double c2) { double d, d1, d2; d = a1 * b2 - a2 * b1; d1 = c1 * b2 - c2 * b1; d2 = a1 * c2 - a2 * c1; if ((d == 0) && (d1 == 0) && (d2 == 0)) return 1; if ((d == 0) && (d1 != 0) && (d2 == 0)) return 0; if ((d == 0) && (d1 == 0) && (d2 != 0)) return 0; if (d != 0) { x = d1 / d; y = d2 / d; } return 2; } public double giaiHePhuongTrinhBacNhat3An(double a1, double b1, double c1, double d1, double a2, double b2, double c2, double d2, double a3, double b3, double c3, double d3) { double d, dx, dy, dz; d = a1 * b2 * c3 - a1 * c2 * b3 + b1 * a2 * c3 - b1 * c2 * a3 + c1 * a2 * b3 - c1 * a3 * b2; dx = d1 * b2 * c3 - d1 * c2 * b3 + b1 * d2 * c3 - b1 * c2 * d3 + c1 * d2 * b3 - c1 * d3 * b2; dy = a1 * d2 * c3 - a1 * c2 * d3 + d1 * a2 * c3 - d1 * c2 * a3 + c1 * a2 * d3 - c1 * a3 * d2; dz = a1 * b2 * d3 - a1 * d2 * b3 + b1 * a2 * d3 - b1 * d2 * a3 + d1 * a2 * b3 - d1 * a3 * b2; if (d == 0 && dx == 0 && dy == 0 && dz == 0) return 0; if (d == 0) if (dx != 0 || dy != 0 || dz != 0) return 1; if (d != 0) { x = dx / d; y = dy / d; z = dz / d; } return 2; } public double giaiPhuongTrinhBacHai(double a, double b, double c) { double denta; denta = b * b - 4 * a * c; if (denta == 0) { x1 = -b / (2 * a); return 1; } if (denta < 0) return 0; if (denta > 0) { x1 = -b + Math.Sqrt(denta) / (2 * a); x2 = -b - Math.Sqrt(denta) / (2 * a); } return 2; } private void bt_0_Click(object sender, EventArgs e) { txt_pheptinh.Text = txt_pheptinh.Text + "0"; clickBang = false; } private void bt_1_Click(object sender, EventArgs e) { txt_pheptinh.Text = txt_pheptinh.Text + "1"; clickBang = false; } private void bt_2_Click(object sender, EventArgs e) { txt_pheptinh.Text = txt_pheptinh.Text + "2"; clickBang = false; } private void bt_3_Click(object sender, EventArgs e) [...]... txt_pheptinh.Text = ""; } if (giaiPhuongTrinhBacHai(a, b, c) == 1) { txt_pheptinh.Text = "PT co 1 nghiem X1"; txt_ketqua.Text = x1.ToString(); } if (giaiPhuongTrinhBacHai(a, b, c) == 0) { txt_pheptinh.Text = "PT vo nghiem"; } if (giaiPhuongTrinhBacHai(a, b, c) == 2) { txt_pheptinh.Text = "PT Co 2 Nghiem x1,x2:"; txt_ketqua.Text = x1.ToString() + " " + x2.ToString(); } } //Giai he phuong trinh bac nhat 3 an if (mode... textBox4.Text = "HPT bac nhat 3 an"; txt_ketqua.Text = "nhap a1"; txt_pheptinh.Text = ""; } } private void Form1_Load(object sender, EventArgs e) { textBox2.Text = "Kết quả"; textBox4.Text = "CHƯƠNG TRÌNH MÁY TÍNH BỎ TÚI"; } private void bt_doiDau_Click(object sender, EventArgs e) { txt_pheptinh.Text += "-"; } private void bt_ac_Click(object sender, EventArgs e) { clickBang = false; if (mode == 1) { txt_pheptinh.Text... (txt_ketqua.Text == "nhap d3") { d3 = double.Parse(txt_pheptinh.Text); } if (giaiHePhuongTrinhBacNhat3An(a1, b1, c1, d1, a2, d2, a3, b3, c3, d3) == 0) txt_pheptinh.Text = "HPT co vo so nghiem"; if (giaiHePhuongTrinhBacNhat3An(a1, b1, c1, d1, a2, d2, a3, b3, c3, d3) == 1) txt_pheptinh.Text = "HPT vo nghiem"; if (giaiHePhuongTrinhBacNhat3An(a1, b1, c1, d1, a2, d2, a3, b3, c3, d3) == 2) { txt_pheptinh.Text =... double.Parse(txt_pheptinh.Text); txt_pheptinh.Text = ""; txt_ketqua.Text = ""; } if (giaiHePhuongTrinhBacNhat2An(a1, b1, c1, a2, b2, c2) == 2) { txt_pheptinh.Text = "nghiem X,Y:"; txt_ketqua.Text = x + " " + y; } if (giaiHePhuongTrinhBacNhat2An(a1, b1, c1, a2, b2, c2) == 1) { txt_pheptinh.Text = "PT Co Vo So Nghiem"; } if (giaiHePhuongTrinhBacNhat2An(a1, b1, c1, a2, b2, c2) == 0) { txt_pheptinh.Text = "PT Vo Nghiem";... (KiemTraLoiChuoi(txt_pheptinh.Text) == true) { txt_ketqua.Text = "Error"; return; } else { txt_ketqua.Text = tinhKetQua(XuLyChuoi(txt_pheptinh.Text)).ToString(); ketQua = txt_ketqua.Text; clickBang = true; } } //giai he phuong trinh 2 an if(mode==2) { if (txt_ketqua.Text == "nhap a1") { if (txt_pheptinh.Text == "") { a1 = 0; return; } a1 = double.Parse(txt_pheptinh.Text); txt_ketqua.Text = "nhap b1"; txt_pheptinh.Text = "";... txt_pheptinh_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) { } private void textBox4_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { MessageBox.Show( "Lập trình bởi :Lý Văn Tuấn\n\rPhone: 0979585949/0988585949\n\rEmail:lyvantuan91@gmail.com ", "Thông tin"); this.txt_pheptinh.Focus(); } } } . EventArgs e) { textBox2.Text = "Kết quả"; textBox4.Text = "CHƯƠNG TRÌNH MÁY TÍNH BỎ TÚI"; } private void bt_doiDau_Click(object sender, EventArgs e) { txt_pheptinh.Text. str.Substring(i, 1); if (str.Substring(i, 1) == "(") daumongoac++; if (str.Substring(i, 1) == ")") daudongngoac++; i++; } //Lui lai sau i ; string u, v; u = XuLyNgoac(chuoitam,. str.Substring(i, 1); if (str.Substring(i, 1) == "(") daumongoac++; if (str.Substring(i, 1) == ")") daudongngoac++; i++; } //Lui lai sau i ; string u, v; u = XuLyNgoac(chuoitam,

Ngày đăng: 09/06/2015, 09:41

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan