筆記本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 檔案ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
private void 開啟舊檔ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
String text = fileToText(openFileDialog1.FileName);
richTextBox1.Text = text;
}
}
public static String fileToText(String filePath)
{
StreamReader file = new StreamReader(filePath);
String text = file.ReadToEnd();
file.Close();
return text;
}
public static void textToFile(String filePath, String text)
{
StreamWriter file = new StreamWriter(filePath);
file.Write(text);
file.Close();
}
private void 儲存檔案ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
String text = richTextBox1.Text;
textToFile(saveFileDialog1.FileName, text);
}
}
private void 另存新檔ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
String text = richTextBox1.Text;
textToFile(saveFileDialog1.FileName, text);
}
}
private void 說明主題ToolStripMenuItem_Click(object sender, EventArgs e)
{
Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.Arguments = "www.google.com.tw";
proc.Start();
proc.WaitForExit();
}
private void 關於記事本ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:文胤臣\n2011 年 12 月 6 日視窗作業");
}
}
}
(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello!!");
int i, j;
for (i = 1; i < =9; i++)
for (j = 1; j <=9; j++)
Console.WriteLine(i + "*" + j + "=" + (j * i));
}
}
}
(2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int i = 1;
for (i=1; i<=1000; i++)
{
if ((i % 3) != 0 || (i % 5) != 0 || (i % 7) != 0)
{
Console.WriteLine(i);
sum += i;
}
else
{
continue;
}
}
Console.WriteLine(sum);
}
}
}
作業一、二:內積 矩陣的加法 矩陣的乘法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayTest
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 3, 3, 3, 3, 3 };
int[] c = new int[1];
int[,] a1 = { { 1, 2 }, { 3, 4 } };
int[,] b1 = { { 3, 4 }, { 5, 6 } };
int[,] d = new int[2, 2];
int[,] e = new int[2, 2];
for (int i = 0; i < a.Length; i++)
{
c[0] += a[i] * b[i];
}
Console.Write("內積\n");
printArray(a);
printArray(b);
printArray(c);
Console.Write("加法\n");
arrayA(a1);
arrayA(b1);
arrayAdd2(a1, b1, d);
Console.Write("乘法\n");
arrayA(a1);
arrayA(b1);
arrayAdd(a1, b1, e);
}
static void printArray(int[] x)
{
for (int i = 0; i < x.Length; i++)
Console.Write(x[i] + " ");
Console.WriteLine();
}
static void arrayA(int[,] x)
{
for (int i = 0; i < x.Rank; i++)
for (int j = 0; j < x.Rank; j++)
{
Console.Write(x[i, j] + "\t ");
}
Console.WriteLine("");
}
static void arrayAdd2(int[,] x, int[,] y, int[,] z)
{
for (int i = 0; i < x.Rank; i++)
for (int j = 0; j < x.Rank; j++)
{
z[i,j] = x[i,j] + y[i,j];
Console.Write(z[i,j] + "\t ");
}
Console.WriteLine("");
}
static void arrayAdd(int[,] x, int[,] y, int[,] z)
{
for (int i = 0; i < x.Rank; i++)
for (int j = 0; j < x.Rank; j++)
for (int k = 0; k < x.Rank; k++)
z[i, j] = x[i, k] * y[k, j] + z[i, j];
for (int i = 0; i < x.Rank; i++)
for (int j = 0; j < x.Rank; j++)
{
Console.Write(z[i, j] + "\t ");
}
Console.WriteLine("");
}
}
}
計算圓面積
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
class circle
{
public double radius;
public circle (double r)
{
radius = r;
}
public double area()
{
return 3.14 * radius * radius;
}
public void print()
{
Console.WriteLine(" radius=" + radius + " area()=" + area());
}
}
class Program
{
static void Main(string[] args)
{
circle r = new circle(3.0);
r.print();
circle r2 = new circle(6.0);
r2.print();
}
}
}
Shape, Circle, Rectangle 加入周長函數 perimeter,並進行多型呼叫。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Home_Work
{
class Rectangle
{
public double width, height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public double area()
{
return width * height;
}
public void print()
{
Console.WriteLine("width=" + width + " height=" + height + " area()=" + area());
}
}
class Circle
{
public double r;
public Circle(double r)
{
this.r = r;
}
public double area()
{
return 3.14 * r * r;
}
public void print()
{
Console.WriteLine("r=" + r + " area()=" + area());
}
}
class perimeter
{
public double p;
public perimeter(double p)
{
this.p = p;
}
public double length()
{
return 3.14 * p ;
}
public void print()
{
Console.WriteLine("perimeter=" + p + " length()=" + length());
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(5.0, 3.0);
r.print();
Rectangle r2 = new Rectangle(4.0, 6.0);
r2.print();
Circle c1 = new Circle(1.0);
c1.print();
Circle c2 = new Circle(2.0);
c2.print();
perimeter p = new perimeter(5.0);
p.print();
}
}
}
請設計一個文字型計算機。
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double a = Double.Parse(textBox1.Text);
string co = comboBox1.Text;
double result =0.0;
double b = Double.Parse(textBox2.Text);
switch (co)
{
case "+" : result = a + b;break;
case "-": result = a - b; break;
case "*": result = a * b; break;
case "/": result = a / b; break;
}
textBox3.Text = result.ToString();
}
}
}