BÀI 2 : Viết chương trình rắn săn mồi using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace ransanmoi {... public FoodRand
Trang 1BÀI 1: Viết chương trình bảng tính đơn giản
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Bảng_Tính_Đơn_Giản
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double kq = 0;
private int dem1 = 0;
private int dem2 = 0;
private int dem3 = 0;
private string tinh1 = "", tinh2 = "", x = "0", y="0"; private string pheptinh = "0";
private void bt1_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "1";
}
else
{
x += 1;
lbkq.Text += 1;
Trang 2}
}
}
private void bt2_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "2";
}
else
{
x += 2;
lbkq.Text += 2;
}
}
}
private void bt3_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "3";
}
else
{
x += 3;
lbkq.Text += 3;
}
}
}
Trang 3private void bt4_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "4";
}
else
{
x += 4;
lbkq.Text += 4;
}
}
}
private void bt5_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "5";
}
else
{
x += 5;
lbkq.Text += 5;
}
}
}
private void bt6_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
Trang 4}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "6";
}
else
{
x += 6;
lbkq.Text += 6;
}
}
}
private void bt7_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "3";
}
else
{
x += 3;
lbkq.Text += 3;
}
}
}
private void bt8_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
Trang 5{
pheptinh = x = lbkq.Text = "8";
}
else
{
x += 8;
lbkq.Text += 8;
}
}
}
private void bt9_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "9";
}
else
{
x += 9;
lbkq.Text += 9;
}
}
}
private void bt0_Click(object sender, EventArgs e) {
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
}
else
{
if (lbkq.Text == "0" || x == "0")
{
pheptinh = x = lbkq.Text = "0";
}
else
Trang 6{
x += 0;
lbkq.Text += 0;
}
}
}
private void btpoint_Click(object sender, EventArgs e) {
if (x == "")
x = "0";
try
{
if (Convert.ToInt32(x) >= 0)
{
lbkq.Text += ".";
x += ".";
}
}
catch { }
}
private int tam;
private void btcong_Click(object sender, EventArgs e) {
try
{
Convert.ToDouble(pheptinh);
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
lbtinh.Text += "+";
}
else
{
lbtinh.Text += lbkq.Text + "+";
}
tinh1 = tinh2;
tinh2 = "+";
tinh(tinh1);
pheptinh = "+";
x = "0";
Trang 7}
catch
{
tinh2 = "+";
tinh1 = lbtinh.Text;
tam = tinh1.Length;
tinh1 = tinh1.Remove(tam - 1, 1);
lbtinh.Text = tinh1 + tinh2;
x = "0";
}
dem1 = dem2 = dem3 = 0;
}
private void bttru_Click(object sender, EventArgs e) {
try
{
Convert.ToDouble(pheptinh);
Convert.ToDouble(pheptinh);
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
lbtinh.Text += "-";
}
else
{
lbtinh.Text += lbkq.Text + "-";
}
tinh1 = tinh2;
tinh2 = "-";
tinh(tinh1);
pheptinh = "-";
x = "0";
}
catch
{
tinh2 = "-";
tinh1 = lbtinh.Text;
tam = tinh1.Length;
tinh1 = tinh1.Remove(tam - 1, 1);
lbtinh.Text = tinh1 + tinh2;
x = "0";
}
Trang 8dem1 = dem2 = dem3 = 0;
}
private void btnhan_Click(object sender, EventArgs e) {
try
{
Convert.ToDouble(pheptinh);
Convert.ToDouble(pheptinh);
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/") {
lbtinh.Text += "*";
}
else
{
lbtinh.Text += lbkq.Text + "*";
}
tinh1 = tinh2;
tinh2 = "*";
tinh(tinh1);
pheptinh = "*";
x = "0";
}
catch
{
tinh2 = "*";
tinh1 = lbtinh.Text;
tam = tinh1.Length;
tinh1 = tinh1.Remove(tam - 1, 1);
lbtinh.Text = tinh1 + tinh2;
x = "0";
}
dem1 = dem2 = dem3 = 0;
}
private void btchia_Click(object sender, EventArgs e) {
try
{
Convert.ToDouble(pheptinh);
Convert.ToDouble(pheptinh);
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/")
Trang 9{
lbtinh.Text += "/";
}
else
{
lbtinh.Text += lbkq.Text + "/";
}
tinh1 = tinh2;
tinh2 = "/";
tinh(tinh1);
pheptinh = "/";
x = "0";
}
catch
{
tinh2 = "/";
tinh1 = lbtinh.Text;
tam = tinh1.Length;
tinh1 = tinh1.Remove(tam - 1, 1);
lbtinh.Text = tinh1 + tinh2;
x = "0";
}
dem1 = dem2 = dem3 = 0;
}
private void btbang_Click(object sender, EventArgs e) {
tinh1 = tinh2;
if (tinh1 == "")
{
lbkq.Text = x;
}
else
{
tinh(tinh2);
tinh2 = "=";
}
pheptinh = x = "0";
lbtinh.Text = "";
tinh1 = tinh2 = "";
dem1 = dem2 = dem3 = 0;
}
Trang 10public void tinh(string a)
{
if(a=="")
{
kq = Convert.ToDouble(lbkq.Text);
lbkq.Text = "" + kq;
}
if(a=="+")
{
kq += Convert.ToDouble(x);
lbkq.Text = "" + kq;
}
if (a == "-")
{
kq -= Convert.ToDouble(x);
lbkq.Text = "" + kq;
}
if (a == "*")
{
kq *= Convert.ToDouble(x);
lbkq.Text = "" + kq;
}
if (a == "/")
{
kq /= (1.0*Convert.ToDouble(x));
lbkq.Text = "" + kq;
}
}
private void btresetall_Click(object sender, EventArgs e) {
lbtinh.Text = "";
lbkq.Text = "0";
tinh1 = "";
tinh2 = "";
pheptinh = x = "0";
kq = 0;
}
private void btdelete_Click(object sender, EventArgs e) {
Trang 11if (x == "0")
{
}
else
{
tam = x.Length;
x = x.Remove(tam - 1, 1);
if (x == "")
{
lbkq.Text = "0";
}
else
{
lbkq.Text = x;
}
}
}
private void btbinhphuong_Click(object sender, EventArgs e)
{
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/")
{
if (Convert.ToDouble(lbkq.Text) == 0)
{
lbtinh.Text = "";
}
else
{
tam = lbtinh.Text.Length;
lbtinh.Text = lbtinh.Text.Insert(tam dem1 * 5 dem2 * 3 -dem3 * 4 - y.Length, "sqr(") + ")";
}
}
else
{
y = x;
lbtinh.Text += "sqr(" + y + ")";
}
x = "" + Convert.ToDouble(lbkq.Text) *
Convert.ToDouble(lbkq.Text);
lbkq.Text =""+ x;
//x = "0";
Trang 12pheptinh = "0";
tinh1 = "sqr";
dem1++;
}
private void btcanbachai_Click(object sender, EventArgs e)
{
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/")
{
if (Convert.ToDouble(lbkq.Text)==0)
{
lbtinh.Text = "";
}
else
{
//thêm vào chuỗi Insert(vị trí,"chuỗi cần thêm");
tam = lbtinh.Text.Length;
//bắt đầu thêm với căn
lbtinh.Text = lbtinh.Text.Insert(tam dem1 * 5 dem2 * 3 -dem3 * 4 - y.Length, "√(") + ")";
}
}
else
{
y = x;
lbtinh.Text += "√(" + y + ")";
}
x = "" + Math.Sqrt(Convert.ToDouble(lbkq.Text));
lbkq.Text = x;
pheptinh = "0";
tinh1 = "√";
dem2++;
}
private void btnghichdao_Click(object sender, EventArgs e)
{
if (tinh1 == "sqr" || tinh1 == "√" || tinh1 == "1/")
{
if (Convert.ToDouble(lbkq.Text) == 0)
{
lbtinh.Text = "";
Trang 13}
else
{
tam = lbtinh.Text.Length;
lbtinh.Text = lbtinh.Text.Insert(tam dem1 * 5 dem2 * 3 -dem3 * 4 - y.Length, "1/(") + ")";
}
}
else
{
y = x;
lbtinh.Text += "√(" + y + ")";
}
x = "" + 1.0 / Convert.ToDouble(lbkq.Text);
lbkq.Text = "" + x;
//x = "0";
pheptinh = "0";
tinh1 = "1/";
dem3++;
}
private void btcongtru_Click(object sender, EventArgs e)
{
if(Convert.ToDouble(x)>=0)
{
x = "-" + x;
}
else
{
x = "" + Math.Abs(Convert.ToDouble(x));
}
lbkq.Text = x;
}
private void btresetkq_Click(object sender, EventArgs e)
{
x = "0";
lbkq.Text = "" + x;
}
}
Trang 14
BÀI 2 : Viết chương trình rắn săn mồi using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Drawing;
namespace ransanmoi
{
Trang 15class Dragon
{
int x, y, h, w;
SolidBrush br;
Rectangle[] dragon;
public Dragon()
{
x = 20;
y = 20;
h = 10;
w = 10;
br = new SolidBrush(Color.Blue);
dragon = new Rectangle[4];
for (int i = 0; i < dragon.Length-1; i++)
{
this.dragon[i] = new Rectangle(x + 10, y, w, h); }
}
public void DrawDragon(Graphics g)
{
foreach (Rectangle i in dragon)
{
g.FillEllipse(br,i);
}
}
public void RunDragon()
{
for (int i =dragon.Length-1; i >0; i )
{
dragon[i] = this.dragon[i - 1];
}
}
public void LeftRun()
{
RunDragon();
this.dragon[0].X -= 10;
}
public void RightRun()
Trang 16{
RunDragon();
this.dragon[0].X += 10;
}
public void UpRun()
{
RunDragon();
this.dragon[0].Y -= 10;
}
public void DownRun()
{
RunDragon();
this.dragon[0].Y += 10;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ransanmoi
{
class Food
{
int x, y, w, h;
Rectangle food;
SolidBrush br;
public Food()
{
br = new SolidBrush(Color.Red);
x = 20; y = 20;
w = 10; h = 10;
food = new Rectangle(x, y, w, h); }
public void Drawfood(Graphics g) { food.X = x;
food.Y = y;
g.FillEllipse(br, food);
}
Trang 17public Food(Random rd)
{
}
}
}
BÀI 3: Viết chương trình xem ảnh using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Multiple_Document_Interface_01 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Trang 18}
private void openToolStripMenuItem_Click(object sender, EventArgs e) {
OpenFileDialog openfiledialog = new OpenFileDialog();
openfiledialog.Title = "Mở ảnh";
openfiledialog.Filter = "Bitmap file|*.bmp;|Jpeg file|*.jpg;";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
Form2 fr = new Form2(openfiledialog.FileName);
fr.MdiParent = this;
fr.Show();
}
}
private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
LayoutMdi(MdiLayout.Cascade);
}
private void tileHorizToolStripMenuItem_Click(object sender, EventArgs e) {
LayoutMdi(MdiLayout.TileHorizontal);
}
private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e) {
LayoutMdi(MdiLayout.TileVertical);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Multiple_Document_Interface_01
{