ช่วยให้คำแนะนำหนูหน่อยคะ เกี่ยวกับ array list ขอบคุณมากๆนะค่ะ
หูย คิดยากทำยากด้วย มาตายเอาน้ำตื้นซะได้
ทำไมไม่ ส่ง string "343+250-30" เข้า method แล้วให้มันออกเป็น double 563 เลยล่ะ
Code (C#)
using System;
using System.Reflection;
using System.CodeDom.Compiler;
using System.Text;
using Microsoft.CSharp;
namespace CSharpMath
{
class Calculator
{
public static double EvaluateExpression(string expression)
{
// สร้างโค้ด class และ method เก็บใส่ตัวแปร string
StringBuilder Sb = new StringBuilder();
Sb.AppendLine("public static class Cal");
Sb.AppendLine("{");
Sb.AppendLine("\t public static double cal()");
Sb.AppendLine("\t {");
Sb.AppendFormat("\t\t return {0};", expression);
Sb.AppendLine("\t }");
Sb.AppendLine("}");
// ส่งโค้ดไป compile
string code = Sb.ToString();
CompilerResults compilerResults = CompileScript(code);
if (compilerResults.Errors.HasErrors)
{
throw new InvalidOperationException("Expression has a syntax error.");
}
// สั่งรัน class และ method ที่ compile ไว้แล้ว return ค่ากลับไป
Assembly assembly = compilerResults.CompiledAssembly;
MethodInfo method = assembly.GetType("Cal").GetMethod("cal");
return (double)method.Invoke(null, null);
}
// compile โค้ดแล้วเก็บไว้ใน object
private static CompilerResults CompileScript(string source)
{
CompilerParameters parms = new CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.IncludeDebugInformation = false;
CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
return compiler.CompileAssemblyFromSource(parms, source);
}
}
}
ป้อน
string "1.5+3+5"
จะได้
double 9.5
ประวัติการแก้ไข 2010-08-20 14:28:54 2010-08-20 14:32:01
Date :
2010-08-20 14:26:19
By :
tungman
พี่ค่ะคือตอนแรก พูด สาม มันจะออกมา 3 พูดร้อย ออก 100 แบบนี้คะ หนูadd ว่า พูด หนึ่ง =1 สอง=2 สิบ=10 ร้อย=100 อยากได้ 343 ก็เอามารวมกันแบบนี้ค่ะ
เลยเอามาเข้า arraylist ทีละคำแล้วถึงเอามารวมได้ค่ะแต่ตอนรวมอะค่ะตอนรวมเป็น 300 + พจน์กลาง+พจน์ท้าย ตอนรวมเป็นพจกลางกับพจน์ท้ายหนูทำไม่เป็น ตอนพจน์แรกตามข้างต้นอะค่ะ ช่วยด้วยนะค่ะ
Date :
2010-08-20 14:44:11
By :
พิมพ์
เก็บเป็น char array เสร็จแล้วแปลงเป็น string เสร็จแล้วส่งเข้า class ที่ให้
พูด หนึ่ง --> c[0] = '1'
พูด สอง --> c[1] = '2'
พูด บวก --> c[2] = '+'
พูด สาม --> c[3] = '3'
char array ของเราจะได้ --> char[] c = { '1', '2', '+', '3'}
เอามาแปลงเป็น string --> "12+3"
จับส่งเข้าคำนวณใน class --> 15
จบ ง่ายมะ
Date :
2010-08-20 14:50:19
By :
tungman
พี่ค่ะคือจารย์บอกว่า ให้พูดได้ถึงแสนนะค่ะ เช่น สองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ด เรากำหนดไว้แค่ 1 2 3 4 5 6 7 8 9 10 100 1000 10000 100000 +-*/ คือพูดตามนี้แล้วมันจะออกมาเป็นตัวเลข จากนั้น หนูเอามาเข้าสมการ ตัวอย่างนะคะ สองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ด จะได้ 2*100000+3*10000+4*1000+5*100+6*10+7 ประมานี้อะคะ
พจน์แรกทำได้แล้วแต่เมื่อมีพจน์ต่อๆ(หลังเครื่องหมาย+-*/)ไปนำมาคำนวณไม่เป็นค่ะ ช่วยที่นะคะ
Date :
2010-08-20 15:02:56
By :
พิมพ์
define
พูด ศูนย์ = "0"
พูด หนึ่ง,เอ็ด = "1"
พูด สอง,ยี่ = "2"
พูด สาม = "3"
พูด สี่ = "4"
พูด ห้า = "5"
พูด หก = "6"
พูด เจ็ด = "7"
พูด แปด = "8"
พูด เก้า = "9"
----------------------------
พูด สิบ = "*10"
พูด ร้อย = "*100"
พูด พัน = "*1000"
พูด หมื่น = "*10000"
พูด แสน = "*100000"
--------------------------------
พูด บวก = "+"
พูด ลบ = "-"
พูด คูณ = "*"
พูด หาร = "/"
---------------------------
โจทย์ 234567+234567
เก็บด้วย StringBuilder Sb = new StringBuilder()
พูด สอง --> Sb.Append("2")
พูด แสน --> Sb.Append("*100000")
พูด สาม --> Sb.Append("3")
พูด หมื่น --> Sb.Append("*10000")
พูด สี่ --> Sb.Append("4")
พูด พัน --> Sb.Append("*1000")
พูด ห้า --> Sb.Append("5")
พูด ร้อย --> Sb.Append("*100")
พูด หก --> Sb.Append("6")
พูด สิบ --> Sb.Append("*10")
พูด เจ็ด --> Sb.Append("7")
พูด บวก --> Sb.Append("+")
พูด สอง --> Sb.Append("2")
พูด แสน --> Sb.Append("*100000")
พูด สาม --> Sb.Append("3")
พูด หมื่น --> Sb.Append("*10000")
พูด สี่ --> Sb.Append("4")
พูด พัน --> Sb.Append("*1000")
พูด ห้า --> Sb.Append("5")
พูด ร้อย --> Sb.Append("*100")
พูด หก --> Sb.Append("6")
พูด สิบ --> Sb.Append("*10")
พูด เจ็ด --> Sb.Append("7")
้เสร็จแล้วกแปลงเป็น string --> string expresstion = Sb.ToString()
จะได้ออกเป็นแบบนี้ expresstion = "2*100000+3*10000+4*1000+5*100+6*10+7+2*100000+3*10000+4*1000+5*100+6*10+7"
ซึ่งส่งไปคำนวณไน class ที่ให้ก็จะได้ = 469134
Date :
2010-08-20 15:18:42
By :
tungman
พี่ค่ะคือว่าหนูมีบัคอยู่คะช่วยแก้หน่อยนะคะถ้าพูด สิบสี่ หนึ่งร้อยสิบ แบบนี้อะค่ะ ขอบคุณค่ะ
Date :
2010-08-20 15:31:43
By :
พิมพ์
เงื่อนไขเยอะเหมือนกันนะ สงสัยต้องไปคิด method แปลง คำพูดเป็นตัวเลขก่อนแล้ว
จะแก้จาก input เลยหรือว่าจะปรับที่ string ที่รับมาดีเนี่ย
Date :
2010-08-20 15:56:00
By :
tungman
ช่วยหน่อยนะค่ะ หนูคิดมานานคิดไม่ออกตามวิธีหนูได้ไหมค่ะ
ใช้แยกเป็นพจน์ๆอะค่ะ
_______+___________+_________
หรืออาจมีหลายพจน์
_______+........................+_________
แยกแล้วค่อยมาคิดอะค่ะ
Date :
2010-08-20 16:01:38
By :
พิมพ์
คิดออกอีกวิธีนึงคือ รับคำอ่านมาให้จบก่อนแล้วค่อยแปลงเป็นตัวเลขทีเดียวจะดีกว่าไหม เช่น
รับ สองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ด เสร็จแล้วค่อยแปลงเป็นตัวเลขที่เดียว
เดี๋ยวจะลองกลับไปลองดู
Date :
2010-08-20 16:04:43
By :
tungman
ขอบคุณมากๆนะค่ะ ช่วยหนูด้วยนะค่ะ ขอบคุณค่ะ
Date :
2010-08-20 16:19:36
By :
พิมพ์
ขอโค้ด input หน่อยก็ดีนะ ว่าต้องพูดอะไรบ้าง
ตอนนี้รู้ว่าต้องพูด
ศูนย์ หนึ่ง,เอ็ด สอง,ยี่ สาม สี่ ห้า หก เจ็ด แปด เก้า
สิบ ร้อย พัน หมื่น แสน
บวก ลบ คูณ หาร
มีอะไรอีกไหมนอกเหนือจากนี้
Date :
2010-08-20 16:25:00
By :
tungman
ไม่มีค่ะ มีแต่พูดเท่ากับผลก็ออกมานะคะ
Date :
2010-08-20 16:41:16
By :
พิมพ์
ส่วนรับข้อมูลเสียงค่ะพูดเข้าไปอะค่ะกำหนดไว้คำที่ใกล้เคียงกับภาษาอักกิตในไลบารีวิสต้าหรือ7ค่ะ
Code (C#)
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;
using System.IO;
using System.Speech;
using System.Speech.Recognition;
using System.Collections;
namespace WindowsFormsApplication2
{
public partial class Main : Form
{
#region Local Members
private SpeechRecognitionEngine recognizer = null;
ArrayList arrCommand = new ArrayList();
ArrayList arrPoint = new ArrayList();
int arrPointSize = 0;
int arrCommandSize = 0;
#endregion
public Main()
{
InitializeComponent();
recognizer = new SpeechRecognitionEngine();
InitializeSpeechRecognitionEngine();
}
private void InitializeSpeechRecognitionEngine()
{
recognizer.SetInputToDefaultAudioDevice();
Grammar customGrammar = CreateCustomGrammar();
recognizer.UnloadAllGrammars();
recognizer.LoadGrammar(customGrammar);
recognizer.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
}
private Grammar CreateCustomGrammar()
{
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices("yee","lob","koon","han","saan","mern","pun","roy","it", "non", "song", "ponlaub", "Some", "si", "Her", "Hold", "Get", "Bat", "Gaow", "Sib", "gee", "booug"));
return new Grammar(grammarBuilder);
}
private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
try
{
GuessText(e.Result.Text);
}
catch
{ }
}
private void GuessText(string guess)
{
try
{
this.richTextBox1.ForeColor = Color.Blue;
switch (guess)
{
case "han":
arrCommand.Add("/");
break;
case "koon":
arrCommand.Add("*");
break;
case "lob":
arrCommand.Add("-");
break;
case "non":
arrCommand.Add("1");
break;
case "it":
arrCommand.Add("1");
break;
case "booug":
arrCommand.Add("+");
break;
case "song":
arrCommand.Add("2");
break;
case "yee":
arrCommand.Add("2");
break;
case "Some":
arrCommand.Add("3");
break;
case "si":
arrCommand.Add("4");
break;
case "Her":
arrCommand.Add("5");
break;
case "Hold":
arrCommand.Add("6");
break;
case "Get":
arrCommand.Add("7");
break;
case "Bat":
arrCommand.Add("8");
break;
case "Gaow":
arrCommand.Add("9");
break;
case "Sib":
arrCommand.Add("10");
break;
case "roy":
arrCommand.Add("100");
break;
case "pun":
arrCommand.Add("1000");
break;
case "mern":
arrCommand.Add("10000");
break;
case "saan":
arrCommand.Add("100000");
break;
default:
break;
}
}
}
Date :
2010-08-20 16:47:07
By :
พิมพ์
ผม layout โปรแกรมแบบนี้โดยสมมุติว่ากดปุ่มแทนคำพูดที่ input เข้าไป (ไม่ต้อง layout ตามหรอกนะ แค่ก็อปโค้ดด้านล่างให้ถูกก็พอ)
1. สร้างโปรเจ็คใหม่ตั้งชื่อว่า MyCalculator ให้ดับเบิลคลิกที่ไฟล์ Form1.Designer.cs แล้วเอาโค้ดด้านล่างไปวาง
Form1.Designer.cs
namespace MyCalculator
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBoxOutputString = new System.Windows.Forms.TextBox();
this.textBoxInputString = new System.Windows.Forms.TextBox();
this.buttonOne = new System.Windows.Forms.Button();
this.buttonTwo = new System.Windows.Forms.Button();
this.buttonThree = new System.Windows.Forms.Button();
this.buttonFour = new System.Windows.Forms.Button();
this.buttonFive = new System.Windows.Forms.Button();
this.buttonSix = new System.Windows.Forms.Button();
this.buttonSeven = new System.Windows.Forms.Button();
this.buttonEight = new System.Windows.Forms.Button();
this.buttonNine = new System.Windows.Forms.Button();
this.textBoxInputDebug = new System.Windows.Forms.TextBox();
this.textBoxOutputDebug = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBoxInput = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxOutput = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.buttonHundredThousand = new System.Windows.Forms.Button();
this.buttonTenThousand = new System.Windows.Forms.Button();
this.buttonThousand = new System.Windows.Forms.Button();
this.buttonHundred = new System.Windows.Forms.Button();
this.buttonTen = new System.Windows.Forms.Button();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.buttonDivide = new System.Windows.Forms.Button();
this.buttonMinus = new System.Windows.Forms.Button();
this.buttonMultiply = new System.Windows.Forms.Button();
this.buttonPlus = new System.Windows.Forms.Button();
this.buttonExecute = new System.Windows.Forms.Button();
this.buttonBack = new System.Windows.Forms.Button();
this.buttonClear = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.groupBox5.SuspendLayout();
this.SuspendLayout();
//
// textBoxOutputString
//
this.textBoxOutputString.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutputString.Location = new System.Drawing.Point(10, 73);
this.textBoxOutputString.Multiline = true;
this.textBoxOutputString.Name = "textBoxOutputString";
this.textBoxOutputString.ReadOnly = true;
this.textBoxOutputString.Size = new System.Drawing.Size(234, 93);
this.textBoxOutputString.TabIndex = 0;
//
// textBoxInputString
//
this.textBoxInputString.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInputString.Location = new System.Drawing.Point(10, 71);
this.textBoxInputString.Multiline = true;
this.textBoxInputString.Name = "textBoxInputString";
this.textBoxInputString.ReadOnly = true;
this.textBoxInputString.Size = new System.Drawing.Size(234, 93);
this.textBoxInputString.TabIndex = 0;
//
// buttonOne
//
this.buttonOne.Location = new System.Drawing.Point(8, 19);
this.buttonOne.Name = "buttonOne";
this.buttonOne.Size = new System.Drawing.Size(75, 23);
this.buttonOne.TabIndex = 1;
this.buttonOne.Text = "หนึ่ง/เอ็ด";
this.buttonOne.UseVisualStyleBackColor = true;
this.buttonOne.Click += new System.EventHandler(this.buttonOne_Click);
//
// buttonTwo
//
this.buttonTwo.Location = new System.Drawing.Point(90, 18);
this.buttonTwo.Name = "buttonTwo";
this.buttonTwo.Size = new System.Drawing.Size(75, 23);
this.buttonTwo.TabIndex = 2;
this.buttonTwo.Text = "สอง/ยี่";
this.buttonTwo.UseVisualStyleBackColor = true;
this.buttonTwo.Click += new System.EventHandler(this.buttonTwo_Click);
//
// buttonThree
//
this.buttonThree.Location = new System.Drawing.Point(172, 17);
this.buttonThree.Name = "buttonThree";
this.buttonThree.Size = new System.Drawing.Size(75, 23);
this.buttonThree.TabIndex = 3;
this.buttonThree.Text = "สาม";
this.buttonThree.UseVisualStyleBackColor = true;
this.buttonThree.Click += new System.EventHandler(this.buttonThree_Click);
//
// buttonFour
//
this.buttonFour.Location = new System.Drawing.Point(8, 49);
this.buttonFour.Name = "buttonFour";
this.buttonFour.Size = new System.Drawing.Size(75, 23);
this.buttonFour.TabIndex = 4;
this.buttonFour.Text = "สี่";
this.buttonFour.UseVisualStyleBackColor = true;
this.buttonFour.Click += new System.EventHandler(this.buttonFour_Click);
//
// buttonFive
//
this.buttonFive.Location = new System.Drawing.Point(90, 49);
this.buttonFive.Name = "buttonFive";
this.buttonFive.Size = new System.Drawing.Size(75, 23);
this.buttonFive.TabIndex = 5;
this.buttonFive.Text = "ห้า";
this.buttonFive.UseVisualStyleBackColor = true;
this.buttonFive.Click += new System.EventHandler(this.buttonFive_Click);
//
// buttonSix
//
this.buttonSix.Location = new System.Drawing.Point(172, 48);
this.buttonSix.Name = "buttonSix";
this.buttonSix.Size = new System.Drawing.Size(75, 23);
this.buttonSix.TabIndex = 6;
this.buttonSix.Text = "หก";
this.buttonSix.UseVisualStyleBackColor = true;
this.buttonSix.Click += new System.EventHandler(this.buttonSix_Click);
//
// buttonSeven
//
this.buttonSeven.Location = new System.Drawing.Point(8, 79);
this.buttonSeven.Name = "buttonSeven";
this.buttonSeven.Size = new System.Drawing.Size(75, 23);
this.buttonSeven.TabIndex = 7;
this.buttonSeven.Text = "เจ็ด";
this.buttonSeven.UseVisualStyleBackColor = true;
this.buttonSeven.Click += new System.EventHandler(this.buttonSeven_Click);
//
// buttonEight
//
this.buttonEight.Location = new System.Drawing.Point(90, 78);
this.buttonEight.Name = "buttonEight";
this.buttonEight.Size = new System.Drawing.Size(75, 23);
this.buttonEight.TabIndex = 8;
this.buttonEight.Text = "แปด";
this.buttonEight.UseVisualStyleBackColor = true;
this.buttonEight.Click += new System.EventHandler(this.buttonEight_Click);
//
// buttonNine
//
this.buttonNine.Location = new System.Drawing.Point(172, 79);
this.buttonNine.Name = "buttonNine";
this.buttonNine.Size = new System.Drawing.Size(75, 23);
this.buttonNine.TabIndex = 9;
this.buttonNine.Text = "เก้า";
this.buttonNine.UseVisualStyleBackColor = true;
this.buttonNine.Click += new System.EventHandler(this.buttonNine_Click);
//
// textBoxInputDebug
//
this.textBoxInputDebug.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInputDebug.Location = new System.Drawing.Point(10, 19);
this.textBoxInputDebug.Name = "textBoxInputDebug";
this.textBoxInputDebug.ReadOnly = true;
this.textBoxInputDebug.Size = new System.Drawing.Size(234, 20);
this.textBoxInputDebug.TabIndex = 10;
//
// textBoxOutputDebug
//
this.textBoxOutputDebug.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutputDebug.Location = new System.Drawing.Point(10, 20);
this.textBoxOutputDebug.Name = "textBoxOutputDebug";
this.textBoxOutputDebug.ReadOnly = true;
this.textBoxOutputDebug.Size = new System.Drawing.Size(234, 20);
this.textBoxOutputDebug.TabIndex = 11;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBoxInput);
this.groupBox1.Controls.Add(this.textBoxInputDebug);
this.groupBox1.Controls.Add(this.textBoxInputString);
this.groupBox1.Location = new System.Drawing.Point(12, 11);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(254, 176);
this.groupBox1.TabIndex = 12;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Input";
//
// textBoxInput
//
this.textBoxInput.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInput.Location = new System.Drawing.Point(10, 45);
this.textBoxInput.Name = "textBoxInput";
this.textBoxInput.ReadOnly = true;
this.textBoxInput.Size = new System.Drawing.Size(234, 20);
this.textBoxInput.TabIndex = 11;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBoxOutput);
this.groupBox2.Controls.Add(this.textBoxOutputDebug);
this.groupBox2.Controls.Add(this.textBoxOutputString);
this.groupBox2.Location = new System.Drawing.Point(12, 193);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(254, 176);
this.groupBox2.TabIndex = 13;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Output";
//
// textBoxOutput
//
this.textBoxOutput.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutput.Location = new System.Drawing.Point(10, 47);
this.textBoxOutput.Name = "textBoxOutput";
this.textBoxOutput.ReadOnly = true;
this.textBoxOutput.Size = new System.Drawing.Size(234, 20);
this.textBoxOutput.TabIndex = 12;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.buttonOne);
this.groupBox3.Controls.Add(this.buttonTwo);
this.groupBox3.Controls.Add(this.buttonThree);
this.groupBox3.Controls.Add(this.buttonNine);
this.groupBox3.Controls.Add(this.buttonFour);
this.groupBox3.Controls.Add(this.buttonEight);
this.groupBox3.Controls.Add(this.buttonFive);
this.groupBox3.Controls.Add(this.buttonSeven);
this.groupBox3.Controls.Add(this.buttonSix);
this.groupBox3.Location = new System.Drawing.Point(272, 56);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(256, 110);
this.groupBox3.TabIndex = 14;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "เลข";
//
// groupBox4
//
this.groupBox4.Controls.Add(this.buttonHundredThousand);
this.groupBox4.Controls.Add(this.buttonTenThousand);
this.groupBox4.Controls.Add(this.buttonThousand);
this.groupBox4.Controls.Add(this.buttonHundred);
this.groupBox4.Controls.Add(this.buttonTen);
this.groupBox4.Location = new System.Drawing.Point(273, 173);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(90, 197);
this.groupBox4.TabIndex = 15;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "หลัก";
//
// buttonHundredThousand
//
this.buttonHundredThousand.Location = new System.Drawing.Point(7, 160);
this.buttonHundredThousand.Name = "buttonHundredThousand";
this.buttonHundredThousand.Size = new System.Drawing.Size(75, 29);
this.buttonHundredThousand.TabIndex = 4;
this.buttonHundredThousand.Text = "แสน";
this.buttonHundredThousand.UseVisualStyleBackColor = true;
this.buttonHundredThousand.Click += new System.EventHandler(this.buttonHundredThousand_Click);
//
// buttonTenThousand
//
this.buttonTenThousand.Location = new System.Drawing.Point(7, 125);
this.buttonTenThousand.Name = "buttonTenThousand";
this.buttonTenThousand.Size = new System.Drawing.Size(75, 29);
this.buttonTenThousand.TabIndex = 3;
this.buttonTenThousand.Text = "หมื่น";
this.buttonTenThousand.UseVisualStyleBackColor = true;
this.buttonTenThousand.Click += new System.EventHandler(this.buttonTenThousand_Click);
//
// buttonThousand
//
this.buttonThousand.Location = new System.Drawing.Point(7, 90);
this.buttonThousand.Name = "buttonThousand";
this.buttonThousand.Size = new System.Drawing.Size(75, 29);
this.buttonThousand.TabIndex = 2;
this.buttonThousand.Text = "พัน";
this.buttonThousand.UseVisualStyleBackColor = true;
this.buttonThousand.Click += new System.EventHandler(this.buttonThousand_Click);
//
// buttonHundred
//
this.buttonHundred.Location = new System.Drawing.Point(7, 55);
this.buttonHundred.Name = "buttonHundred";
this.buttonHundred.Size = new System.Drawing.Size(75, 29);
this.buttonHundred.TabIndex = 1;
this.buttonHundred.Text = "ร้อย";
this.buttonHundred.UseVisualStyleBackColor = true;
this.buttonHundred.Click += new System.EventHandler(this.buttonHundred_Click);
//
// buttonTen
//
this.buttonTen.Location = new System.Drawing.Point(7, 20);
this.buttonTen.Name = "buttonTen";
this.buttonTen.Size = new System.Drawing.Size(75, 29);
this.buttonTen.TabIndex = 0;
this.buttonTen.Text = "สิบ";
this.buttonTen.UseVisualStyleBackColor = true;
this.buttonTen.Click += new System.EventHandler(this.buttonTen_Click);
//
// groupBox5
//
this.groupBox5.Controls.Add(this.buttonDivide);
this.groupBox5.Controls.Add(this.buttonMinus);
this.groupBox5.Controls.Add(this.buttonMultiply);
this.groupBox5.Controls.Add(this.buttonPlus);
this.groupBox5.Location = new System.Drawing.Point(369, 173);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(159, 140);
this.groupBox5.TabIndex = 16;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "เครื่องหมาย";
//
// buttonDivide
//
this.buttonDivide.Location = new System.Drawing.Point(80, 78);
this.buttonDivide.Name = "buttonDivide";
this.buttonDivide.Size = new System.Drawing.Size(69, 53);
this.buttonDivide.TabIndex = 1;
this.buttonDivide.Text = "หาร";
this.buttonDivide.UseVisualStyleBackColor = true;
this.buttonDivide.Click += new System.EventHandler(this.buttonDivide_Click);
//
// buttonMinus
//
this.buttonMinus.Location = new System.Drawing.Point(80, 20);
this.buttonMinus.Name = "buttonMinus";
this.buttonMinus.Size = new System.Drawing.Size(69, 53);
this.buttonMinus.TabIndex = 1;
this.buttonMinus.Text = "ลบ";
this.buttonMinus.UseVisualStyleBackColor = true;
this.buttonMinus.Click += new System.EventHandler(this.buttonMinus_Click);
//
// buttonMultiply
//
this.buttonMultiply.Location = new System.Drawing.Point(6, 78);
this.buttonMultiply.Name = "buttonMultiply";
this.buttonMultiply.Size = new System.Drawing.Size(69, 53);
this.buttonMultiply.TabIndex = 0;
this.buttonMultiply.Text = "คูณ";
this.buttonMultiply.UseVisualStyleBackColor = true;
this.buttonMultiply.Click += new System.EventHandler(this.buttonMultiply_Click);
//
// buttonPlus
//
this.buttonPlus.Location = new System.Drawing.Point(6, 20);
this.buttonPlus.Name = "buttonPlus";
this.buttonPlus.Size = new System.Drawing.Size(69, 53);
this.buttonPlus.TabIndex = 0;
this.buttonPlus.Text = "บวก";
this.buttonPlus.UseVisualStyleBackColor = true;
this.buttonPlus.Click += new System.EventHandler(this.buttonPlus_Click);
//
// buttonExecute
//
this.buttonExecute.Location = new System.Drawing.Point(370, 319);
this.buttonExecute.Name = "buttonExecute";
this.buttonExecute.Size = new System.Drawing.Size(158, 50);
this.buttonExecute.TabIndex = 17;
this.buttonExecute.Text = "เท่ากับ";
this.buttonExecute.UseVisualStyleBackColor = true;
this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click);
//
// buttonBack
//
this.buttonBack.Location = new System.Drawing.Point(272, 15);
this.buttonBack.Name = "buttonBack";
this.buttonBack.Size = new System.Drawing.Size(124, 35);
this.buttonBack.TabIndex = 18;
this.buttonBack.Text = "ถอย";
this.buttonBack.UseVisualStyleBackColor = true;
this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
//
// buttonClear
//
this.buttonClear.Location = new System.Drawing.Point(404, 15);
this.buttonClear.Name = "buttonClear";
this.buttonClear.Size = new System.Drawing.Size(124, 35);
this.buttonClear.TabIndex = 19;
this.buttonClear.Text = "ล้าง";
this.buttonClear.UseVisualStyleBackColor = true;
this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(540, 387);
this.Controls.Add(this.buttonClear);
this.Controls.Add(this.buttonBack);
this.Controls.Add(this.buttonExecute);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "My Calculator";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TextBox textBoxOutputString;
private System.Windows.Forms.TextBox textBoxInputString;
private System.Windows.Forms.Button buttonOne;
private System.Windows.Forms.Button buttonTwo;
private System.Windows.Forms.Button buttonThree;
private System.Windows.Forms.Button buttonFour;
private System.Windows.Forms.Button buttonFive;
private System.Windows.Forms.Button buttonSix;
private System.Windows.Forms.Button buttonSeven;
private System.Windows.Forms.Button buttonEight;
private System.Windows.Forms.Button buttonNine;
private System.Windows.Forms.TextBox textBoxInputDebug;
private System.Windows.Forms.TextBox textBoxOutputDebug;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button buttonHundredThousand;
private System.Windows.Forms.Button buttonTenThousand;
private System.Windows.Forms.Button buttonThousand;
private System.Windows.Forms.Button buttonHundred;
private System.Windows.Forms.Button buttonTen;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.Button buttonDivide;
private System.Windows.Forms.Button buttonMinus;
private System.Windows.Forms.Button buttonMultiply;
private System.Windows.Forms.Button buttonPlus;
private System.Windows.Forms.Button buttonExecute;
private System.Windows.Forms.Button buttonBack;
private System.Windows.Forms.Button buttonClear;
private System.Windows.Forms.TextBox textBoxInput;
private System.Windows.Forms.TextBox textBoxOutput;
}
}
2. เปิดไฟล์ชื่อ Form1.cs แล้วก็อปโค้ดด้านล่างไปวาง
Form1.cs
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;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonOne_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "1";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTwo_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "2";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonThree_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "3";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonFour_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "4";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonFive_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "5";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonSix_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "6";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonSeven_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "7";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonEight_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "8";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonNine_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "9";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTen_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "A";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonHundred_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "B";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "C";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTenThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "D";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonHundredThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "E";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonPlus_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "+";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "-";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "*";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonDivide_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "/";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonBack_Click(object sender, EventArgs e)
{
if (textBoxInputDebug.Text != string.Empty)
textBoxInputDebug.Text = textBoxInputDebug.Text.Substring(0, textBoxInputDebug.Text.Length - 1);
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text = string.Empty;
textBoxInput.Text = string.Empty;
textBoxInputString.Text = string.Empty;
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonExecute_Click(object sender, EventArgs e)
{
textBoxOutputDebug.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutput.Text = Convert.ToString(EvaluateExpression(textBoxOutputDebug.Text));
}
private string ConvertToText(string Expression)
{
StringBuilder Sb = new StringBuilder();
char[] chr = Expression.ToCharArray();
for (int i = 0; i < chr.Length; i++)
{
switch (chr[i])
{
case '1':
if (i > 0 && i < chr.Length - 1)
Sb.Append((!IsOperator(chr[i + 1]) || IsOperator(chr[i - 1])) ? "หนึ่ง" : "เอ็ด");
else
{
if (i == 0)
Sb.Append("หนึ่ง");
else
Sb.Append((IsOperator(chr[i - 1])) ? "หนึ่ง" : "เอ็ด");
}
break;
case '2':
if (i < chr.Length - 1)
Sb.Append((chr[i + 1] != 'A') ? "สอง" : "ยี่");
else
Sb.Append("สอง");
break;
case '3':
Sb.Append("สาม");
break;
case '4':
Sb.Append("สี่");
break;
case '5':
Sb.Append("ห้า");
break;
case '6':
Sb.Append("หก");
break;
case '7':
Sb.Append("เจ็ด");
break;
case '8':
Sb.Append("แปด");
break;
case '9':
Sb.Append("เก้า");
break;
case 'A':
Sb.Append("สิบ");
break;
case 'B':
Sb.Append("ร้อย");
break;
case 'C':
Sb.Append("พัน");
break;
case 'D':
Sb.Append("หมื่น");
break;
case 'E':
Sb.Append("แสน");
break;
case '+':
Sb.Append(" บวก ");
break;
case '-':
Sb.Append(" ลบ ");
break;
case '*':
Sb.Append(" คูณ ");
break;
case '/':
Sb.Append(" หาร ");
break;
}
}
return Sb.ToString();
}
private string ConvertToNumeric(string Expression)
{
StringBuilder Sb = new StringBuilder();
char[] chr = Expression.ToCharArray();
for (int i = 0; i < chr.Length; i++)
{
if (i == 0)
Sb.Append("(");
switch (chr[i])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (i > 0 && i < chr.Length - 1)
{
if (IsOperator(chr[i - 1]))
Sb.AppendFormat("({0}.0f", chr[i].ToString());
else if (!IsNumeric(chr[i - 1]))
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
else if (!IsNumeric(chr[i - 1]))
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
if (IsOperator(chr[i + 1]))
Sb.Append(")");
}
else
{
if (i == 0)
Sb.AppendFormat("({0}.0f", chr[i].ToString());
else
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
if (i == chr.Length - 1)
Sb.Append(")");
}
break;
case 'A':
if (i > 0 && i < chr.Length - 1)
{
if (IsNumeric(chr[i - 1]))
Sb.Append("*10.0f)");
else if (chr[i - 1] == 'B' || chr[i - 1] == 'C' || chr[i - 1] == 'D' || chr[i - 1] == 'E')
Sb.Append("+(10.0f)");
else if (chr[i - 1] == '+' || chr[i - 1] == '-' || chr[i - 1] == '*' || chr[i - 1] == '/')
Sb.Append("(10.0f)");
else if (chr[i + 1] == '+' || chr[i + 1] == '-' || chr[i + 1] == '*' || chr[i + 1] == '/')
Sb.Append("+(10.0f)");
}
else
{
if (i == 0)
Sb.Append("(10.0f)");
else if (i == chr.Length - 1)
Sb.Append("+(10.0f)");
}
break;
case 'B':
Sb.Append("*100.0f)");
break;
case 'C':
Sb.Append("*1000.0f)");
break;
case 'D':
Sb.Append("*10000.0f)");
break;
case 'E':
Sb.Append("*100000.0f)");
break;
case '+':
case '-':
case '*':
case '/':
Sb.AppendFormat("){0}(", chr[i].ToString());
break;
}
if (i == chr.Length - 1)
Sb.Append(")");
}
return Sb.ToString();
}
// IsNumeric Function
private static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
bool isNum;
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
double retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
private static bool IsOperator(object Expression)
{
string obj = Expression.ToString();
return (obj == "+" || obj == "-" || obj == "*" || obj == "/") ? true : false;
}
public static double EvaluateExpression(string expression)
{
// สร้างโค้ด class และ method เก็บใส่ตัวแปร string
StringBuilder Sb = new StringBuilder();
Sb.AppendLine("public static class Calculator");
Sb.AppendLine("{");
Sb.AppendLine("\t public static double Execute()");
Sb.AppendLine("\t {");
Sb.AppendFormat("\t\t return {0};", expression);
Sb.AppendLine("\t }");
Sb.AppendLine("}");
// ส่งโค้ดไป compile
string code = Sb.ToString();
CompilerResults compilerResults = CompileScript(code);
if (compilerResults.Errors.HasErrors)
{
throw new InvalidOperationException("Expression has a syntax error.");
}
// สั่งรัน class และ method ที่ compile ไว้แล้ว return ค่ากลับไป
Assembly assembly = compilerResults.CompiledAssembly;
MethodInfo method = assembly.GetType("Calculator").GetMethod("Execute");
return (double)method.Invoke(null, null);
}
// compile โค้ดแล้วเก็บไว้ใน object
private static CompilerResults CompileScript(string source)
{
CompilerParameters parms = new CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.IncludeDebugInformation = false;
CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
return compiler.CompileAssemblyFromSource(parms, source);
}
}
}
3. รันดู
ปล. textbox ที่ว่างใน group input กะว่าจะแปลงให้เป็นตัวเลขภาษามนุษย์
กับ textbox ที่ว่างใน group output ก็กะว่าจะแปลง output ออกเป็นคำอ่าน แต่พอดีขี้เกียจทำก็เลยปล่อยว่างไว้
ปล.2 เลขหลัก ที่คุณใช้ 10 100 1000..... ผมเปลี่ยนไปใช้
10 = A
100 = B
1000 = C
10000 = D
100000 = E
ลองเอาไปดูเป็นแนวทางแล้วกันนะ
ประวัติการแก้ไข 2010-08-20 23:53:22 2010-08-21 07:31:11 2010-08-21 07:48:33 2010-08-21 09:43:51
Date :
2010-08-20 23:24:39
By :
tungman
การทำงานของมันคือ
input --------> 2E3D4C5B6A7/2A1 --------> ((2.0f*100000.0f)+(3.0f*10000.0f)+(4.0f*1000.0f)+(5.0f*100.0f)+(6.0f*10.0f)+(7.0f))/((2.0f*10.0f)+(1.0f)) --------> 11169.8571428571
Date :
2010-08-20 23:41:40
By :
tungman
ขอบคุณมากๆค่ะ เดี่ยวหนูลองก่อนนะค่ะ ทำงัยหนูจะเก่งเหมือนพี่ค่ะ แนะนำหนูที สามารถเอาไปแปลงใช้กับที่รับเสียงได้เลยมะค่ะ
ขอบคุณมากๆจิงๆค่ะ
Date :
2010-08-20 23:49:14
By :
พิมพ์
ก็เปลี่ยน input จากกดปุ่มไปเป็น input โดยเสียง ก็น่าจะแทนกันได้แล้วแหละ
สำคัญคือ ตอนนี้ผมไม่ได้ validate input ดังนั้นเวลา input ต้องเป็นไปตาม format ของจำนวน
เช่น สองร้อยสิบเอ็ด แบบนี้ได้ แต่ ประเภท หนึ่งร้อยหนึ่งสิบสอง หรือ หนึ่งพันสเก้าหมื่นสิบสอง แบบนี้ไม่ได้
Date :
2010-08-21 00:02:18
By :
tungman
พี่ช่วยแยกเป็นพจน์ให้หนูดูหน่อยคะหนูทำไม่เป็น
สมมุติว่าเราพูดได้แบบนี้มาคะ ช่วยเขียนโคดแยกว่าจะเอามารวมเป็นพจน์ๆยังงัยอะค่ะ
คื่อช่วงตั้งแต่แรกถึงเครื่องหมาย + ช่วงกลาง และช่วงท้าย
และถ้าไม่สามารถรู้ว่ามันจะมีกี่พจน์( มี 2 พจน์ขึ่นไปอยู่แล้ว) จะกำหนดยังงัยค่ะ
รบกวนอีกครั้งนะค่ะ
ขอบคุณค่ะ
Date :
2010-08-21 00:33:26
By :
พิมพ์
ตามรูปนี้ดีกว่าจะได้เห็นชัดเจนนะค่ะ
คือหนูตอนแรกเอาเข้า array list ก่อนนะคะ
แล้วแสกนหา +-*/ จากนั้นนำมารวามเป็นพจน์ พจแรกตั้งแต่เริ่มต้นถึง เครื่องหมาย แล้วก็พจน์กลางแล้วก็พจน์สุดท้าย
หนูก็แสกนว่า จากตำแหน่งงแรกถึง เครื่องหมายมีกี่ตัว
ถ้าเป็นคู่ทำสมการ (X1*X2)+(X3*X4)+........+(Xn*Xn+1)
ถ้าเป็นคี่ ทำสมการ ข้างล้างเลยคะตามโค้ดค่ะ
ไม่ทราบว่าจะเขียนออกมายังงัยตรงพจน์กลางและท้ายค่ะ
พจน์แรกได้ประมาณรี้นะคะ
int x =0;
int y = 0;
int z = 0;
if (((int)arrPoint[0])%2 == 0)//อันนี้ถ้าจำนวนมีจำนวนเป็นคู
{
for (int i = 0; i < ((int)arrPoint[0] - 1); i++)
{
x = Convert.ToInt32(arrCommand[i]);
y = Convert.ToInt32(arrCommand[i+1]);
z = z + (x * y);
i++;
}
MessageBox.Show(z.ToString());
}
else
{
for (int i = 0; i < ((int)arrPoint[0] - 2); i++)
{
x = Convert.ToInt32(arrCommand[i]);
y = Convert.ToInt32(arrCommand[i + 1]);
z = z + (x * y);
i++;
}
z += Convert.ToInt32(arrCommand[(int)arrPoint[0]-1]);
MessageBox.Show(z.ToString());
}
Date :
2010-08-21 00:50:38
By :
พิมพ์
คุณ Tungman สุดยอดไปเลย
ทำให้ผมได้ความรู้ใหม่
มันสามารถ compile ตอน runtime ได้เลยเหรอครับเนี่ย
สุดยอดๆ
ขอฝากตัวเป็นศิษย์จะได้ไหมครับ
Date :
2010-08-21 10:54:51
By :
nickyshox99
ขอบคุณมากๆ คะพี่ Tungman
Date :
2010-08-21 12:10:57
By :
พิมพ์
Final ตัวเต็ม version ลูกทุ่ง (none oop)
แก้บักแล้ว เอาเงื่อนไขไปใช้ได้เลย
Form1.Designer.cs
namespace MyCalculator
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBoxOutputString = new System.Windows.Forms.TextBox();
this.textBoxInputString = new System.Windows.Forms.TextBox();
this.buttonOne = new System.Windows.Forms.Button();
this.buttonTwo = new System.Windows.Forms.Button();
this.buttonThree = new System.Windows.Forms.Button();
this.buttonFour = new System.Windows.Forms.Button();
this.buttonFive = new System.Windows.Forms.Button();
this.buttonSix = new System.Windows.Forms.Button();
this.buttonSeven = new System.Windows.Forms.Button();
this.buttonEight = new System.Windows.Forms.Button();
this.buttonNine = new System.Windows.Forms.Button();
this.textBoxInputDebug = new System.Windows.Forms.TextBox();
this.textBoxOutputDebug = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBoxInput = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxOutput = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.buttonHundredThousand = new System.Windows.Forms.Button();
this.buttonTenThousand = new System.Windows.Forms.Button();
this.buttonThousand = new System.Windows.Forms.Button();
this.buttonHundred = new System.Windows.Forms.Button();
this.buttonTen = new System.Windows.Forms.Button();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.buttonDivide = new System.Windows.Forms.Button();
this.buttonMinus = new System.Windows.Forms.Button();
this.buttonMultiply = new System.Windows.Forms.Button();
this.buttonPlus = new System.Windows.Forms.Button();
this.buttonExecute = new System.Windows.Forms.Button();
this.buttonBack = new System.Windows.Forms.Button();
this.buttonClear = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.groupBox5.SuspendLayout();
this.SuspendLayout();
//
// textBoxOutputString
//
this.textBoxOutputString.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutputString.Location = new System.Drawing.Point(10, 73);
this.textBoxOutputString.Multiline = true;
this.textBoxOutputString.Name = "textBoxOutputString";
this.textBoxOutputString.ReadOnly = true;
this.textBoxOutputString.Size = new System.Drawing.Size(234, 93);
this.textBoxOutputString.TabIndex = 0;
//
// textBoxInputString
//
this.textBoxInputString.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInputString.Location = new System.Drawing.Point(10, 71);
this.textBoxInputString.Multiline = true;
this.textBoxInputString.Name = "textBoxInputString";
this.textBoxInputString.ReadOnly = true;
this.textBoxInputString.Size = new System.Drawing.Size(234, 93);
this.textBoxInputString.TabIndex = 0;
//
// buttonOne
//
this.buttonOne.Location = new System.Drawing.Point(8, 19);
this.buttonOne.Name = "buttonOne";
this.buttonOne.Size = new System.Drawing.Size(75, 23);
this.buttonOne.TabIndex = 1;
this.buttonOne.Text = "หนึ่ง/เอ็ด";
this.buttonOne.UseVisualStyleBackColor = true;
this.buttonOne.Click += new System.EventHandler(this.buttonOne_Click);
//
// buttonTwo
//
this.buttonTwo.Location = new System.Drawing.Point(90, 18);
this.buttonTwo.Name = "buttonTwo";
this.buttonTwo.Size = new System.Drawing.Size(75, 23);
this.buttonTwo.TabIndex = 2;
this.buttonTwo.Text = "สอง/ยี่";
this.buttonTwo.UseVisualStyleBackColor = true;
this.buttonTwo.Click += new System.EventHandler(this.buttonTwo_Click);
//
// buttonThree
//
this.buttonThree.Location = new System.Drawing.Point(172, 17);
this.buttonThree.Name = "buttonThree";
this.buttonThree.Size = new System.Drawing.Size(75, 23);
this.buttonThree.TabIndex = 3;
this.buttonThree.Text = "สาม";
this.buttonThree.UseVisualStyleBackColor = true;
this.buttonThree.Click += new System.EventHandler(this.buttonThree_Click);
//
// buttonFour
//
this.buttonFour.Location = new System.Drawing.Point(8, 49);
this.buttonFour.Name = "buttonFour";
this.buttonFour.Size = new System.Drawing.Size(75, 23);
this.buttonFour.TabIndex = 4;
this.buttonFour.Text = "สี่";
this.buttonFour.UseVisualStyleBackColor = true;
this.buttonFour.Click += new System.EventHandler(this.buttonFour_Click);
//
// buttonFive
//
this.buttonFive.Location = new System.Drawing.Point(90, 49);
this.buttonFive.Name = "buttonFive";
this.buttonFive.Size = new System.Drawing.Size(75, 23);
this.buttonFive.TabIndex = 5;
this.buttonFive.Text = "ห้า";
this.buttonFive.UseVisualStyleBackColor = true;
this.buttonFive.Click += new System.EventHandler(this.buttonFive_Click);
//
// buttonSix
//
this.buttonSix.Location = new System.Drawing.Point(172, 48);
this.buttonSix.Name = "buttonSix";
this.buttonSix.Size = new System.Drawing.Size(75, 23);
this.buttonSix.TabIndex = 6;
this.buttonSix.Text = "หก";
this.buttonSix.UseVisualStyleBackColor = true;
this.buttonSix.Click += new System.EventHandler(this.buttonSix_Click);
//
// buttonSeven
//
this.buttonSeven.Location = new System.Drawing.Point(8, 79);
this.buttonSeven.Name = "buttonSeven";
this.buttonSeven.Size = new System.Drawing.Size(75, 23);
this.buttonSeven.TabIndex = 7;
this.buttonSeven.Text = "เจ็ด";
this.buttonSeven.UseVisualStyleBackColor = true;
this.buttonSeven.Click += new System.EventHandler(this.buttonSeven_Click);
//
// buttonEight
//
this.buttonEight.Location = new System.Drawing.Point(90, 78);
this.buttonEight.Name = "buttonEight";
this.buttonEight.Size = new System.Drawing.Size(75, 23);
this.buttonEight.TabIndex = 8;
this.buttonEight.Text = "แปด";
this.buttonEight.UseVisualStyleBackColor = true;
this.buttonEight.Click += new System.EventHandler(this.buttonEight_Click);
//
// buttonNine
//
this.buttonNine.Location = new System.Drawing.Point(172, 79);
this.buttonNine.Name = "buttonNine";
this.buttonNine.Size = new System.Drawing.Size(75, 23);
this.buttonNine.TabIndex = 9;
this.buttonNine.Text = "เก้า";
this.buttonNine.UseVisualStyleBackColor = true;
this.buttonNine.Click += new System.EventHandler(this.buttonNine_Click);
//
// textBoxInputDebug
//
this.textBoxInputDebug.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInputDebug.Location = new System.Drawing.Point(10, 19);
this.textBoxInputDebug.Name = "textBoxInputDebug";
this.textBoxInputDebug.ReadOnly = true;
this.textBoxInputDebug.Size = new System.Drawing.Size(234, 20);
this.textBoxInputDebug.TabIndex = 10;
//
// textBoxOutputDebug
//
this.textBoxOutputDebug.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutputDebug.Location = new System.Drawing.Point(10, 20);
this.textBoxOutputDebug.Name = "textBoxOutputDebug";
this.textBoxOutputDebug.ReadOnly = true;
this.textBoxOutputDebug.Size = new System.Drawing.Size(234, 20);
this.textBoxOutputDebug.TabIndex = 11;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBoxInput);
this.groupBox1.Controls.Add(this.textBoxInputDebug);
this.groupBox1.Controls.Add(this.textBoxInputString);
this.groupBox1.Location = new System.Drawing.Point(12, 11);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(254, 176);
this.groupBox1.TabIndex = 12;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Input";
//
// textBoxInput
//
this.textBoxInput.BackColor = System.Drawing.SystemColors.Window;
this.textBoxInput.Location = new System.Drawing.Point(10, 45);
this.textBoxInput.Name = "textBoxInput";
this.textBoxInput.ReadOnly = true;
this.textBoxInput.Size = new System.Drawing.Size(234, 20);
this.textBoxInput.TabIndex = 11;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBoxOutput);
this.groupBox2.Controls.Add(this.textBoxOutputDebug);
this.groupBox2.Controls.Add(this.textBoxOutputString);
this.groupBox2.Location = new System.Drawing.Point(12, 193);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(254, 176);
this.groupBox2.TabIndex = 13;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Output";
//
// textBoxOutput
//
this.textBoxOutput.BackColor = System.Drawing.SystemColors.Window;
this.textBoxOutput.Location = new System.Drawing.Point(10, 47);
this.textBoxOutput.Name = "textBoxOutput";
this.textBoxOutput.ReadOnly = true;
this.textBoxOutput.Size = new System.Drawing.Size(234, 20);
this.textBoxOutput.TabIndex = 12;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.buttonOne);
this.groupBox3.Controls.Add(this.buttonTwo);
this.groupBox3.Controls.Add(this.buttonThree);
this.groupBox3.Controls.Add(this.buttonNine);
this.groupBox3.Controls.Add(this.buttonFour);
this.groupBox3.Controls.Add(this.buttonEight);
this.groupBox3.Controls.Add(this.buttonFive);
this.groupBox3.Controls.Add(this.buttonSeven);
this.groupBox3.Controls.Add(this.buttonSix);
this.groupBox3.Location = new System.Drawing.Point(272, 56);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(256, 110);
this.groupBox3.TabIndex = 14;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "เลข";
//
// groupBox4
//
this.groupBox4.Controls.Add(this.buttonHundredThousand);
this.groupBox4.Controls.Add(this.buttonTenThousand);
this.groupBox4.Controls.Add(this.buttonThousand);
this.groupBox4.Controls.Add(this.buttonHundred);
this.groupBox4.Controls.Add(this.buttonTen);
this.groupBox4.Location = new System.Drawing.Point(273, 173);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(90, 197);
this.groupBox4.TabIndex = 15;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "หลัก";
//
// buttonHundredThousand
//
this.buttonHundredThousand.Location = new System.Drawing.Point(7, 160);
this.buttonHundredThousand.Name = "buttonHundredThousand";
this.buttonHundredThousand.Size = new System.Drawing.Size(75, 29);
this.buttonHundredThousand.TabIndex = 4;
this.buttonHundredThousand.Text = "แสน";
this.buttonHundredThousand.UseVisualStyleBackColor = true;
this.buttonHundredThousand.Click += new System.EventHandler(this.buttonHundredThousand_Click);
//
// buttonTenThousand
//
this.buttonTenThousand.Location = new System.Drawing.Point(7, 125);
this.buttonTenThousand.Name = "buttonTenThousand";
this.buttonTenThousand.Size = new System.Drawing.Size(75, 29);
this.buttonTenThousand.TabIndex = 3;
this.buttonTenThousand.Text = "หมื่น";
this.buttonTenThousand.UseVisualStyleBackColor = true;
this.buttonTenThousand.Click += new System.EventHandler(this.buttonTenThousand_Click);
//
// buttonThousand
//
this.buttonThousand.Location = new System.Drawing.Point(7, 90);
this.buttonThousand.Name = "buttonThousand";
this.buttonThousand.Size = new System.Drawing.Size(75, 29);
this.buttonThousand.TabIndex = 2;
this.buttonThousand.Text = "พัน";
this.buttonThousand.UseVisualStyleBackColor = true;
this.buttonThousand.Click += new System.EventHandler(this.buttonThousand_Click);
//
// buttonHundred
//
this.buttonHundred.Location = new System.Drawing.Point(7, 55);
this.buttonHundred.Name = "buttonHundred";
this.buttonHundred.Size = new System.Drawing.Size(75, 29);
this.buttonHundred.TabIndex = 1;
this.buttonHundred.Text = "ร้อย";
this.buttonHundred.UseVisualStyleBackColor = true;
this.buttonHundred.Click += new System.EventHandler(this.buttonHundred_Click);
//
// buttonTen
//
this.buttonTen.Location = new System.Drawing.Point(7, 20);
this.buttonTen.Name = "buttonTen";
this.buttonTen.Size = new System.Drawing.Size(75, 29);
this.buttonTen.TabIndex = 0;
this.buttonTen.Text = "สิบ";
this.buttonTen.UseVisualStyleBackColor = true;
this.buttonTen.Click += new System.EventHandler(this.buttonTen_Click);
//
// groupBox5
//
this.groupBox5.Controls.Add(this.buttonDivide);
this.groupBox5.Controls.Add(this.buttonMinus);
this.groupBox5.Controls.Add(this.buttonMultiply);
this.groupBox5.Controls.Add(this.buttonPlus);
this.groupBox5.Location = new System.Drawing.Point(369, 173);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(159, 140);
this.groupBox5.TabIndex = 16;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "เครื่องหมาย";
//
// buttonDivide
//
this.buttonDivide.Location = new System.Drawing.Point(80, 78);
this.buttonDivide.Name = "buttonDivide";
this.buttonDivide.Size = new System.Drawing.Size(69, 53);
this.buttonDivide.TabIndex = 1;
this.buttonDivide.Text = "หาร";
this.buttonDivide.UseVisualStyleBackColor = true;
this.buttonDivide.Click += new System.EventHandler(this.buttonDivide_Click);
//
// buttonMinus
//
this.buttonMinus.Location = new System.Drawing.Point(80, 20);
this.buttonMinus.Name = "buttonMinus";
this.buttonMinus.Size = new System.Drawing.Size(69, 53);
this.buttonMinus.TabIndex = 1;
this.buttonMinus.Text = "ลบ";
this.buttonMinus.UseVisualStyleBackColor = true;
this.buttonMinus.Click += new System.EventHandler(this.buttonMinus_Click);
//
// buttonMultiply
//
this.buttonMultiply.Location = new System.Drawing.Point(6, 78);
this.buttonMultiply.Name = "buttonMultiply";
this.buttonMultiply.Size = new System.Drawing.Size(69, 53);
this.buttonMultiply.TabIndex = 0;
this.buttonMultiply.Text = "คูณ";
this.buttonMultiply.UseVisualStyleBackColor = true;
this.buttonMultiply.Click += new System.EventHandler(this.buttonMultiply_Click);
//
// buttonPlus
//
this.buttonPlus.Location = new System.Drawing.Point(6, 20);
this.buttonPlus.Name = "buttonPlus";
this.buttonPlus.Size = new System.Drawing.Size(69, 53);
this.buttonPlus.TabIndex = 0;
this.buttonPlus.Text = "บวก";
this.buttonPlus.UseVisualStyleBackColor = true;
this.buttonPlus.Click += new System.EventHandler(this.buttonPlus_Click);
//
// buttonExecute
//
this.buttonExecute.Location = new System.Drawing.Point(370, 319);
this.buttonExecute.Name = "buttonExecute";
this.buttonExecute.Size = new System.Drawing.Size(158, 50);
this.buttonExecute.TabIndex = 17;
this.buttonExecute.Text = "เท่ากับ";
this.buttonExecute.UseVisualStyleBackColor = true;
this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click);
//
// buttonBack
//
this.buttonBack.Location = new System.Drawing.Point(272, 15);
this.buttonBack.Name = "buttonBack";
this.buttonBack.Size = new System.Drawing.Size(124, 35);
this.buttonBack.TabIndex = 18;
this.buttonBack.Text = "ถอยลบ";
this.buttonBack.UseVisualStyleBackColor = true;
this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
//
// buttonClear
//
this.buttonClear.Location = new System.Drawing.Point(404, 15);
this.buttonClear.Name = "buttonClear";
this.buttonClear.Size = new System.Drawing.Size(124, 35);
this.buttonClear.TabIndex = 19;
this.buttonClear.Text = "ล้าง";
this.buttonClear.UseVisualStyleBackColor = true;
this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(540, 387);
this.Controls.Add(this.buttonClear);
this.Controls.Add(this.buttonBack);
this.Controls.Add(this.buttonExecute);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "My Calculator";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TextBox textBoxOutputString;
private System.Windows.Forms.TextBox textBoxInputString;
private System.Windows.Forms.Button buttonOne;
private System.Windows.Forms.Button buttonTwo;
private System.Windows.Forms.Button buttonThree;
private System.Windows.Forms.Button buttonFour;
private System.Windows.Forms.Button buttonFive;
private System.Windows.Forms.Button buttonSix;
private System.Windows.Forms.Button buttonSeven;
private System.Windows.Forms.Button buttonEight;
private System.Windows.Forms.Button buttonNine;
private System.Windows.Forms.TextBox textBoxInputDebug;
private System.Windows.Forms.TextBox textBoxOutputDebug;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button buttonHundredThousand;
private System.Windows.Forms.Button buttonTenThousand;
private System.Windows.Forms.Button buttonThousand;
private System.Windows.Forms.Button buttonHundred;
private System.Windows.Forms.Button buttonTen;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.Button buttonDivide;
private System.Windows.Forms.Button buttonMinus;
private System.Windows.Forms.Button buttonMultiply;
private System.Windows.Forms.Button buttonPlus;
private System.Windows.Forms.Button buttonExecute;
private System.Windows.Forms.Button buttonBack;
private System.Windows.Forms.Button buttonClear;
private System.Windows.Forms.TextBox textBoxInput;
private System.Windows.Forms.TextBox textBoxOutput;
}
}
Form1.cs
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;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonOne_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "1";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTwo_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "2";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonThree_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "3";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonFour_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "4";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonFive_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "5";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonSix_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "6";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonSeven_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "7";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonEight_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "8";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonNine_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "9";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTen_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "A";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonHundred_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "B";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "C";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonTenThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "D";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonHundredThousand_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "E";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonPlus_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "+";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonMinus_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "-";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "*";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonDivide_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text += "/";
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxInput.Text = ConvertToNumeric(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonBack_Click(object sender, EventArgs e)
{
if (textBoxInputDebug.Text != string.Empty)
textBoxInputDebug.Text = textBoxInputDebug.Text.Substring(0, textBoxInputDebug.Text.Length - 1);
textBoxInputString.Text = ConvertToText(textBoxInputDebug.Text);
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBoxInputDebug.Text = string.Empty;
textBoxInput.Text = string.Empty;
textBoxInputString.Text = string.Empty;
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
}
private void buttonExecute_Click(object sender, EventArgs e)
{
textBoxOutputDebug.Text = ConvertToEquation(textBoxInputDebug.Text);
textBoxOutput.Text = Convert.ToString(EvaluateExpression(textBoxOutputDebug.Text));
textBoxOutputString.Text = ConvertToEquivalent(textBoxOutput.Text);
}
private string ConvertToNumeric(string Expression)
{
string changeText = Expression.Replace("+", ",").Replace("-", ",").Replace("*", ",").Replace("/", ",");
string operatorText = Expression.Replace("1", string.Empty).Replace("2", string.Empty).Replace("3", string.Empty).Replace("4", string.Empty).Replace("5", string.Empty).Replace("6", string.Empty).Replace("7", string.Empty).Replace("8", string.Empty).Replace("9", string.Empty).Replace("A", string.Empty).Replace("B", string.Empty).Replace("C", string.Empty).Replace("D", string.Empty).Replace("E", string.Empty);
char[] splitOperator = operatorText.ToCharArray();
string[] splitText = changeText.Split(new char[] { ',' });
string outputText = string.Empty;
int i = 0;
foreach (string str in splitText)
{
if (str != string.Empty)
{
double result = EvaluateExpression(ConvertToEquation(str));
if (result != 0.0f)
{
outputText += Convert.ToString(result);
if (splitOperator.Length > 0 && i < splitOperator.Length)
{
outputText += splitOperator[i].ToString();
i++;
}
}
else
{
textBoxInputDebug.Text = string.Empty;
textBoxInput.Text = string.Empty;
textBoxInputString.Text = string.Empty;
textBoxOutputDebug.Text = string.Empty;
textBoxOutput.Text = string.Empty;
textBoxOutputString.Text = string.Empty;
break;
}
}
}
return outputText;
}
private string ConvertToEquivalent(string Expression)
{
string[] NumberWord;
string[] NumberWord2;
string Num3 = "";
char cha1 = '.';
NumberWord = Expression.Split(cha1);
cha1 = ',';
NumberWord2 = NumberWord[0].Split(cha1);
for (int i = 0; i <= NumberWord2.Length - 1; i++)
{
Num3 = Num3 + NumberWord2[i];
}
string ProcessValue = SplitWord(Num3);
if (NumberWord.Length > 1)
{
if (Int64.Parse(NumberWord[1]) > 0)
{
ProcessValue = ProcessValue + "จุด" + SplitWord2(NumberWord[1]);
}
}
return ProcessValue;
}
private string ConvertToText(string Expression)
{
StringBuilder Sb = new StringBuilder();
char[] chr = Expression.ToCharArray();
for (int i = 0; i < chr.Length; i++)
{
switch (chr[i])
{
case '1':
if (i != 0 && i != chr.Length - 1)
Sb.Append((!IsOperator(chr[i + 1]) || IsOperator(chr[i - 1])) ? "หนึ่ง" : "เอ็ด");
else
{
if (i == 0)
{
if (chr.Length > 1)
Sb.Append((chr[i + 1] == 'A') ? string.Empty : "หนึ่ง");
else
Sb.Append("หนึ่ง");
}
else
Sb.Append((IsOperator(chr[i - 1])) ? "หนึ่ง" : "เอ็ด");
}
break;
case '2':
if (i < chr.Length - 1)
Sb.Append((chr[i + 1] != 'A') ? "สอง" : "ยี่");
else
Sb.Append("สอง");
break;
case '3':
Sb.Append("สาม");
break;
case '4':
Sb.Append("สี่");
break;
case '5':
Sb.Append("ห้า");
break;
case '6':
Sb.Append("หก");
break;
case '7':
Sb.Append("เจ็ด");
break;
case '8':
Sb.Append("แปด");
break;
case '9':
Sb.Append("เก้า");
break;
case 'A':
Sb.Append("สิบ");
break;
case 'B':
Sb.Append("ร้อย");
break;
case 'C':
Sb.Append("พัน");
break;
case 'D':
Sb.Append("หมื่น");
break;
case 'E':
Sb.Append("แสน");
break;
case '+':
Sb.Append(" บวก ");
break;
case '-':
Sb.Append(" ลบ ");
break;
case '*':
Sb.Append(" คูณ ");
break;
case '/':
Sb.Append(" หาร ");
break;
}
}
return Sb.ToString();
}
private string ConvertToEquation(string Expression)
{
StringBuilder Sb = new StringBuilder();
char[] chr = Expression.ToCharArray();
for (int i = 0; i < chr.Length; i++)
{
if (i == 0)
Sb.Append("(");
switch (chr[i])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (i != 0 && i != chr.Length - 1)
{
if (IsOperator(chr[i - 1]))
Sb.AppendFormat("({0}.0f", chr[i].ToString());
else if (!IsNumeric(chr[i - 1]))
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
else if (!IsNumeric(chr[i - 1]))
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
if (IsOperator(chr[i + 1]))
Sb.Append(")");
}
else
{
if (i == 0)
Sb.AppendFormat("({0}.0f", chr[i].ToString());
else if (IsOperator(chr[i - 1]))
Sb.AppendFormat("({0}.0f", chr[i].ToString());
else
Sb.AppendFormat("+({0}.0f", chr[i].ToString());
if (i == chr.Length - 1)
Sb.Append(")");
else if (IsOperator(chr[i + 1]))
Sb.Append(")");
}
break;
case 'A':
if (i != 0 && i != chr.Length - 1)
{
if (IsNumeric(chr[i - 1]))
Sb.Append("*10.0f)");
else if (chr[i - 1] == 'B' || chr[i - 1] == 'C' || chr[i - 1] == 'D' || chr[i - 1] == 'E')
Sb.Append("+(10.0f)");
else if (IsOperator(chr[i - 1]))
Sb.Append("(10.0f)");
else if (IsOperator(chr[i + 1]) && !IsNumeric(chr[i - 1]))
Sb.Append("+(10.0f)");
}
else
{
if (i == 0)
Sb.Append("(10.0f)");
else if (IsNumeric(chr[i - 1]))
Sb.Append("*10.0f)");
else if (i == chr.Length - 1)
Sb.Append("+(10.0f)");
}
break;
case 'B':
Sb.Append("*100.0f)");
break;
case 'C':
Sb.Append("*1000.0f)");
break;
case 'D':
Sb.Append("*10000.0f)");
break;
case 'E':
Sb.Append("*100000.0f)");
break;
case '+':
case '-':
case '*':
case '/':
Sb.AppendFormat("){0}(", chr[i].ToString());
break;
}
if (i == chr.Length - 1)
Sb.Append(")");
}
return Sb.ToString();
}
// IsNumeric Function
private static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
bool isNum;
// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
double retNum;
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
private static bool IsOperator(object Expression)
{
string obj = Expression.ToString();
return (obj == "+" || obj == "-" || obj == "*" || obj == "/") ? true : false;
}
public static double EvaluateExpression(string expression)
{
// สร้างโค้ด class และ method เก็บใส่ตัวแปร string
StringBuilder Sb = new StringBuilder();
Sb.AppendLine("public static class Calculator");
Sb.AppendLine("{");
Sb.AppendLine("\t public static double Execute()");
Sb.AppendLine("\t {");
Sb.AppendFormat("\t\t return {0};", expression);
Sb.AppendLine("\t }");
Sb.AppendLine("}");
try
{
// ส่งโค้ดไป compile
string code = Sb.ToString();
CompilerResults compilerResults = CompileScript(code);
// สั่งรัน class และ method ที่ compile ไว้แล้ว return ค่ากลับไป
Assembly assembly = compilerResults.CompiledAssembly;
MethodInfo method = assembly.GetType("Calculator").GetMethod("Execute");
return (double)method.Invoke(null, null);
}
catch
{
MessageBox.Show(string.Format("error: Expression has a syntax error."), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return 0.0f;
}
// compile โค้ดแล้วเก็บไว้ใน object
private static CompilerResults CompileScript(string source)
{
CompilerParameters parms = new CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.IncludeDebugInformation = false;
CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
return compiler.CompileAssemblyFromSource(parms, source);
}
private string SplitWord2(string numberVar)
{
char[] value1 = numberVar.ToCharArray();
string output = string.Empty;
foreach (char c in value1)
{
switch (c)
{
case '1':
output += "หนึ่ง";
break;
case '2':
output += "สอง";
break;
case '3':
output += "สาม";
break;
case '4':
output += "สี่";
break;
case '5':
output += "ห้า";
break;
case '6':
output += "หก";
break;
case '7':
output += "เจ็ด";
break;
case '8':
output += "แปด";
break;
case '9':
output += "เก้า";
break;
}
}
return output;
}
private string SplitWord(string numberVar)
{
int i = numberVar.Length;
int k = 0;
int n = i;
int m = i;
int b = 6;
//char value2;
char[] value1;
string CurrencyWord = "";
value1 = numberVar.ToCharArray();
for (int a = 0; a <= i; a = a + 7)
{
if (n <= a + 7 && n > 0)
{
b = n - 1;
if (i > 7)
{
k = 1;
}
}
else
{
b = 6;
}
if (n > 0)
{
for (int j = 0; j <= b; j++)
{
n--;
k++;
CurrencyWord = GetWord(value1[n].ToString(), k) + CurrencyWord;
}
}
}
return CurrencyWord;
}
private string GetWord(string str1, int Num1)
{
string value1 = GetCurrency(Num1);
switch (str1)
{
case "1":
if (Num1 == 1)
{
value1 = value1 + "เอ็ด";
}
else if (Num1 > 2)
{
value1 = "หนึ่ง" + value1;
}
break;
case "2":
if (Num1 == 2)
{
value1 = "ยี่" + value1;
}
else
{
value1 = "สอง" + value1;
}
break;
case "3":
value1 = "สาม" + value1;
break;
case "4":
value1 = "สี่" + value1;
break;
case "5":
value1 = "ห้า" + value1;
break;
case "6":
value1 = "หก" + value1;
break;
case "7":
value1 = "เจ็ด" + value1;
break;
case "8":
value1 = "แปด" + value1;
break;
case "9":
value1 = "เก้า" + value1;
break;
default:
value1 = "";
break;
}
return value1;
}
private string GetCurrency(int Num2)
{
string value1;
switch (Num2)
{
case 1:
value1 = "";
break;
case 2:
value1 = "สิบ";
break;
case 3:
value1 = "ร้อย";
break;
case 4:
value1 = "พัน";
break;
case 5:
value1 = "หมื่น";
break;
case 6:
value1 = "แสน";
break;
case 7:
value1 = "ล้าน";
break;
default:
value1 = "";
break;
}
return value1;
}
}
}
Date :
2010-08-21 12:29:35
By :
tungman
พี่สุดยอดมากเลยค่ะ ไม่รู้จะขอบคุณยังงัยดี หนูแค่เพิ่มคำสั่งเสียงเข้าไปใช่ไหมค่ะ
ขอบคุณมากๆๆๆค่ะ
Date :
2010-08-21 13:33:12
By :
พิมพ์
สุดยอดมากเลยครับ พี่ tungman
Date :
2010-08-21 13:35:52
By :
tamkungcru
สุดยอด
Date :
2012-08-29 19:30:09
By :
sible
Load balance : Server 00