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);
Trang 2l++;
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);
Trang 3string 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
Trang 4if (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;
}
Trang 5else
{
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)
Trang 6{
//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 = "";
Trang 7double 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) ==
"(")
Trang 8{
//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);
Trang 9if (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;
Trang 10dx = 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)
Trang 11{
txt_pheptinh.Text = txt_pheptinh.Text + "3";
clickBang = false;
}
private void bt_4_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "4";
clickBang = false;
}
private void bt_5_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "5";
clickBang = false;
}
private void bt_6_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "6";
clickBang = false;
}
private void bt_7_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "7";
clickBang = false;
}
private void bt_8_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "8";
clickBang = false;
}
private void bt_9_Click(object sender, EventArgs e) {
txt_pheptinh.Text = txt_pheptinh.Text + "9";
clickBang = false;
}
private void bt_cong_Click(object sender, EventArgs e) {
if (clickBang == true)
{
txt_pheptinh.Text = ketQua;
clickBang = false;
}
txt_pheptinh.Text += "+";
}
private void bt_nhan_Click(object sender, EventArgs e) {
if (clickBang == true)
{
txt_pheptinh.Text = ketQua;
clickBang = false;