 |
|
สอนเขียนโปรแกรมเครื่องคิดเลขแบบวิทยาศาสตร์หน่อยครับ ใน VB.Net |
|
 |
|
|
 |
 |
|
แล้วถ้าผมจะเขียนให้มันรับค่าตัวเลขจากแป้นพิมพ์ด้วยผมต้องเขียนยังไงครับผม
|
 |
 |
 |
 |
Date :
2016-02-09 22:17:43 |
By :
teerapat_kill |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Credit: หน้าฮี
Code (VB.NET)
Imports System.Collections.Generic
Imports System.Math
Public Class หน้าฮี_Calculator
Private Delegate Function DelegateFormula(ByVal m As Double, ByVal n As Double) As Double
Private Shared Formula As New Dictionary(Of String, DelegateFormula)() From {{"+", Function(m, n) m + n},
{"-", Function(m, n) m - n},
{"*", Function(m, n) m * n},
{"/", Function(m, n) m / n}
}
''' <summary>
''' Example : Dim ret = Calculate(12.50, 10.00, "-")
''' result = 2.50
''' </summary>
Public Shared Function Calculate(ByVal a As Double, b As Double, Optional ByVal Oper As String = "+") As Double
Dim retValue As Double = 0.0
If (("+-*/").Contains(Oper)) AndAlso (Not (Oper = "/" AndAlso b = 0)) Then
retValue = Formula(Oper).Invoke(a, b)
End If
Return retValue
End Function
End Class
ปล. ตื่นเช้าขึ้นมาก็ได้ยินเสียงข้างฯบ้าน บ่นพรึมพรัมฯ "ไก่ได้พลอย" แต่ผมไม่รู้ความหมายของมัน
|
 |
 |
 |
 |
Date :
2016-02-10 07:32:36 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยกกำลัง
Code (VB.NET)
{"^", Function(m, n) Math.Pow(m, n)}
If (("+-*/^").Contains(Oper)) AndAlso (Not (Oper = "/" AndAlso b = 0)) Then
...
...
|
 |
 |
 |
 |
Date :
2016-02-10 07:39:33 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2016-02-10 08:34:46 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไหนฯก็ไหนฯ
จาก #NO3, #NO4
--- ผมอยากได้เครื่องคิดเลขตั้งชื่อว่า "ลูกไม่มีพ่อ" (ฐานสิบ [+-*/] ฐานสอง [+-*/] ฐานสิบหก) ต้องทำอย่างไร?
แนวทาง...
Code (VB.NET)
Public Class DHBConversionTable 'Dec, Hex, Bin
Public Property Dec As String
Public Property Hex As String
Public Property Bin As String
Public Property Description As String
Public Shared Function GetConversionTable(Optional ByVal n As Integer = 255) As List(Of DHBConversionTable)
Dim lstDHB As New List(Of DHBConversionTable)
n.ToString("N0").AsParallel.ForAll(Sub(x)
Dim y As Integer = Char.GetNumericValue(x)
Dim z = Char.IsLetterOrDigit(x)
End Sub)
For i As Integer = 0 To 255
lstDHB.Add(New DHBConversionTable() With {.Dec = i.ToString("D2"),
.Hex = i.ToString("X2"),
.Bin = Convert.ToString(i, 2).PadLeft(8, "0"c)
})
Next
Return lstDHB
End Function
End Class
ปล. คิดได้หมดแต่ไม่มีเวลาทำ
|
 |
 |
 |
 |
Date :
2016-02-10 08:36:32 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
@ห้ามตอบเกินวันละ 2 กระทู้ จาก #NO5
อยากเอาแต่เช้าเลย (ยัดเข้าไปในสมองตัวเอง)
--- ขอดู Source code หน่อยครับ แต่ขอเป็น C#
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2016-02-10 08:47:10 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Form1.Designer.cs
namespace InfixNotationConverter
{
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.components = new System.ComponentModel.Container();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelResult = new System.Windows.Forms.ToolStripStatusLabel();
this.textBoxInput = new System.Windows.Forms.TextBox();
this.buttonCalculate = new System.Windows.Forms.Button();
this.dataGridViewConvert1 = new System.Windows.Forms.DataGridView();
this.dataGridViewOutput1 = new System.Windows.Forms.DataGridView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPagePostfix = new System.Windows.Forms.TabPage();
this.textBoxOutput1 = new System.Windows.Forms.TextBox();
this.labelPostfix = new System.Windows.Forms.Label();
this.textBoxInfix1 = new System.Windows.Forms.TextBox();
this.labelInfix1 = new System.Windows.Forms.Label();
this.tabPagePrefix = new System.Windows.Forms.TabPage();
this.textBoxOutput2 = new System.Windows.Forms.TextBox();
this.labelPrefix = new System.Windows.Forms.Label();
this.textBoxInfix2 = new System.Windows.Forms.TextBox();
this.labelInfix2 = new System.Windows.Forms.Label();
this.dataGridViewConvert2 = new System.Windows.Forms.DataGridView();
this.dataGridViewOutput2 = new System.Windows.Forms.DataGridView();
this.textBoxAnswer = new System.Windows.Forms.TextBox();
this.labelEqual = new System.Windows.Forms.Label();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewConvert1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewOutput1)).BeginInit();
this.tabControl1.SuspendLayout();
this.tabPagePostfix.SuspendLayout();
this.tabPagePrefix.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewConvert2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewOutput2)).BeginInit();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelResult});
this.statusStrip1.Location = new System.Drawing.Point(0, 523);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(897, 22);
this.statusStrip1.SizingGrip = false;
this.statusStrip1.TabIndex = 3;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelResult
//
this.toolStripStatusLabelResult.Name = "toolStripStatusLabelResult";
this.toolStripStatusLabelResult.Size = new System.Drawing.Size(118, 17);
this.toolStripStatusLabelResult.Text = "toolStripStatusLabel1";
//
// textBoxInput
//
this.textBoxInput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxInput.Location = new System.Drawing.Point(12, 12);
this.textBoxInput.Name = "textBoxInput";
this.textBoxInput.Size = new System.Drawing.Size(604, 20);
this.textBoxInput.TabIndex = 0;
this.textBoxInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxInput_KeyPress);
//
// buttonCalculate
//
this.buttonCalculate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonCalculate.Location = new System.Drawing.Point(805, 9);
this.buttonCalculate.Name = "buttonCalculate";
this.buttonCalculate.Size = new System.Drawing.Size(75, 23);
this.buttonCalculate.TabIndex = 1;
this.buttonCalculate.Text = "Calculate";
this.buttonCalculate.UseVisualStyleBackColor = true;
this.buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);
//
// dataGridViewConvert1
//
this.dataGridViewConvert1.AllowUserToAddRows = false;
this.dataGridViewConvert1.AllowUserToDeleteRows = false;
this.dataGridViewConvert1.AllowUserToResizeColumns = false;
this.dataGridViewConvert1.AllowUserToResizeRows = false;
this.dataGridViewConvert1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridViewConvert1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewConvert1.Location = new System.Drawing.Point(6, 32);
this.dataGridViewConvert1.MultiSelect = false;
this.dataGridViewConvert1.Name = "dataGridViewConvert1";
this.dataGridViewConvert1.RowHeadersVisible = false;
this.dataGridViewConvert1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewConvert1.Size = new System.Drawing.Size(423, 416);
this.dataGridViewConvert1.TabIndex = 4;
this.dataGridViewConvert1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView_DataBindingComplete);
//
// dataGridViewOutput1
//
this.dataGridViewOutput1.AllowUserToAddRows = false;
this.dataGridViewOutput1.AllowUserToDeleteRows = false;
this.dataGridViewOutput1.AllowUserToResizeColumns = false;
this.dataGridViewOutput1.AllowUserToResizeRows = false;
this.dataGridViewOutput1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridViewOutput1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewOutput1.Location = new System.Drawing.Point(435, 32);
this.dataGridViewOutput1.MultiSelect = false;
this.dataGridViewOutput1.Name = "dataGridViewOutput1";
this.dataGridViewOutput1.RowHeadersVisible = false;
this.dataGridViewOutput1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewOutput1.Size = new System.Drawing.Size(423, 416);
this.dataGridViewOutput1.TabIndex = 5;
this.dataGridViewOutput1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView_DataBindingComplete);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imageList1.ImageSize = new System.Drawing.Size(20, 20);
this.imageList1.TransparentColor = System.Drawing.Color.White;
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPagePostfix);
this.tabControl1.Controls.Add(this.tabPagePrefix);
this.tabControl1.Location = new System.Drawing.Point(12, 38);
this.tabControl1.Margin = new System.Windows.Forms.Padding(3, 3, 3, 5);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(872, 480);
this.tabControl1.TabIndex = 2;
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
//
// tabPagePostfix
//
this.tabPagePostfix.Controls.Add(this.textBoxOutput1);
this.tabPagePostfix.Controls.Add(this.labelPostfix);
this.tabPagePostfix.Controls.Add(this.textBoxInfix1);
this.tabPagePostfix.Controls.Add(this.labelInfix1);
this.tabPagePostfix.Controls.Add(this.dataGridViewConvert1);
this.tabPagePostfix.Controls.Add(this.dataGridViewOutput1);
this.tabPagePostfix.Location = new System.Drawing.Point(4, 22);
this.tabPagePostfix.Name = "tabPagePostfix";
this.tabPagePostfix.Padding = new System.Windows.Forms.Padding(3);
this.tabPagePostfix.Size = new System.Drawing.Size(864, 454);
this.tabPagePostfix.TabIndex = 0;
this.tabPagePostfix.Text = "Postfix notation";
this.tabPagePostfix.UseVisualStyleBackColor = true;
//
// textBoxOutput1
//
this.textBoxOutput1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxOutput1.Location = new System.Drawing.Point(482, 6);
this.textBoxOutput1.Name = "textBoxOutput1";
this.textBoxOutput1.ReadOnly = true;
this.textBoxOutput1.Size = new System.Drawing.Size(376, 20);
this.textBoxOutput1.TabIndex = 9;
//
// labelPostfix
//
this.labelPostfix.AutoSize = true;
this.labelPostfix.Location = new System.Drawing.Point(435, 9);
this.labelPostfix.Name = "labelPostfix";
this.labelPostfix.Size = new System.Drawing.Size(41, 13);
this.labelPostfix.TabIndex = 8;
this.labelPostfix.Text = "Postfix:";
//
// textBoxInfix1
//
this.textBoxInfix1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxInfix1.Location = new System.Drawing.Point(41, 6);
this.textBoxInfix1.Name = "textBoxInfix1";
this.textBoxInfix1.ReadOnly = true;
this.textBoxInfix1.Size = new System.Drawing.Size(388, 20);
this.textBoxInfix1.TabIndex = 7;
//
// labelInfix1
//
this.labelInfix1.AutoSize = true;
this.labelInfix1.Location = new System.Drawing.Point(6, 9);
this.labelInfix1.Name = "labelInfix1";
this.labelInfix1.Size = new System.Drawing.Size(29, 13);
this.labelInfix1.TabIndex = 6;
this.labelInfix1.Text = "Infix:";
//
// tabPagePrefix
//
this.tabPagePrefix.Controls.Add(this.textBoxOutput2);
this.tabPagePrefix.Controls.Add(this.labelPrefix);
this.tabPagePrefix.Controls.Add(this.textBoxInfix2);
this.tabPagePrefix.Controls.Add(this.labelInfix2);
this.tabPagePrefix.Controls.Add(this.dataGridViewConvert2);
this.tabPagePrefix.Controls.Add(this.dataGridViewOutput2);
this.tabPagePrefix.Location = new System.Drawing.Point(4, 22);
this.tabPagePrefix.Name = "tabPagePrefix";
this.tabPagePrefix.Padding = new System.Windows.Forms.Padding(3);
this.tabPagePrefix.Size = new System.Drawing.Size(864, 454);
this.tabPagePrefix.TabIndex = 1;
this.tabPagePrefix.Text = "Prefix notation";
this.tabPagePrefix.UseVisualStyleBackColor = true;
//
// textBoxOutput2
//
this.textBoxOutput2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxOutput2.Location = new System.Drawing.Point(477, 6);
this.textBoxOutput2.Name = "textBoxOutput2";
this.textBoxOutput2.ReadOnly = true;
this.textBoxOutput2.Size = new System.Drawing.Size(381, 20);
this.textBoxOutput2.TabIndex = 15;
//
// labelPrefix
//
this.labelPrefix.AutoSize = true;
this.labelPrefix.Location = new System.Drawing.Point(435, 9);
this.labelPrefix.Name = "labelPrefix";
this.labelPrefix.Size = new System.Drawing.Size(36, 13);
this.labelPrefix.TabIndex = 14;
this.labelPrefix.Text = "Prefix:";
//
// textBoxInfix2
//
this.textBoxInfix2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxInfix2.Location = new System.Drawing.Point(41, 6);
this.textBoxInfix2.Name = "textBoxInfix2";
this.textBoxInfix2.ReadOnly = true;
this.textBoxInfix2.Size = new System.Drawing.Size(388, 20);
this.textBoxInfix2.TabIndex = 13;
//
// labelInfix2
//
this.labelInfix2.AutoSize = true;
this.labelInfix2.Location = new System.Drawing.Point(6, 9);
this.labelInfix2.Name = "labelInfix2";
this.labelInfix2.Size = new System.Drawing.Size(29, 13);
this.labelInfix2.TabIndex = 12;
this.labelInfix2.Text = "Infix:";
//
// dataGridViewConvert2
//
this.dataGridViewConvert2.AllowUserToAddRows = false;
this.dataGridViewConvert2.AllowUserToDeleteRows = false;
this.dataGridViewConvert2.AllowUserToResizeColumns = false;
this.dataGridViewConvert2.AllowUserToResizeRows = false;
this.dataGridViewConvert2.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridViewConvert2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewConvert2.Location = new System.Drawing.Point(6, 32);
this.dataGridViewConvert2.MultiSelect = false;
this.dataGridViewConvert2.Name = "dataGridViewConvert2";
this.dataGridViewConvert2.RowHeadersVisible = false;
this.dataGridViewConvert2.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewConvert2.Size = new System.Drawing.Size(423, 416);
this.dataGridViewConvert2.TabIndex = 10;
this.dataGridViewConvert2.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView_DataBindingComplete);
//
// dataGridViewOutput2
//
this.dataGridViewOutput2.AllowUserToAddRows = false;
this.dataGridViewOutput2.AllowUserToDeleteRows = false;
this.dataGridViewOutput2.AllowUserToResizeColumns = false;
this.dataGridViewOutput2.AllowUserToResizeRows = false;
this.dataGridViewOutput2.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridViewOutput2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewOutput2.Location = new System.Drawing.Point(435, 32);
this.dataGridViewOutput2.MultiSelect = false;
this.dataGridViewOutput2.Name = "dataGridViewOutput2";
this.dataGridViewOutput2.RowHeadersVisible = false;
this.dataGridViewOutput2.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewOutput2.Size = new System.Drawing.Size(423, 416);
this.dataGridViewOutput2.TabIndex = 11;
this.dataGridViewOutput2.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView_DataBindingComplete);
//
// textBoxAnswer
//
this.textBoxAnswer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxAnswer.Location = new System.Drawing.Point(641, 12);
this.textBoxAnswer.Name = "textBoxAnswer";
this.textBoxAnswer.ReadOnly = true;
this.textBoxAnswer.Size = new System.Drawing.Size(158, 20);
this.textBoxAnswer.TabIndex = 4;
this.textBoxAnswer.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// labelEqual
//
this.labelEqual.AutoSize = true;
this.labelEqual.Location = new System.Drawing.Point(622, 14);
this.labelEqual.Name = "labelEqual";
this.labelEqual.Size = new System.Drawing.Size(13, 13);
this.labelEqual.TabIndex = 5;
this.labelEqual.Text = "=";
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// Form1
//
this.AcceptButton = this.buttonCalculate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(897, 545);
this.Controls.Add(this.labelEqual);
this.Controls.Add(this.textBoxAnswer);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.buttonCalculate);
this.Controls.Add(this.textBoxInput);
this.Controls.Add(this.statusStrip1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Infix notation";
this.Load += new System.EventHandler(this.Form1_Load);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewConvert1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewOutput1)).EndInit();
this.tabControl1.ResumeLayout(false);
this.tabPagePostfix.ResumeLayout(false);
this.tabPagePostfix.PerformLayout();
this.tabPagePrefix.ResumeLayout(false);
this.tabPagePrefix.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewConvert2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewOutput2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelResult;
private System.Windows.Forms.TextBox textBoxInput;
private System.Windows.Forms.Button buttonCalculate;
private System.Windows.Forms.DataGridView dataGridViewConvert1;
private System.Windows.Forms.DataGridView dataGridViewOutput1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPagePostfix;
private System.Windows.Forms.TabPage tabPagePrefix;
private System.Windows.Forms.TextBox textBoxOutput1;
private System.Windows.Forms.Label labelPostfix;
private System.Windows.Forms.TextBox textBoxInfix1;
private System.Windows.Forms.Label labelInfix1;
private System.Windows.Forms.TextBox textBoxOutput2;
private System.Windows.Forms.Label labelPrefix;
private System.Windows.Forms.TextBox textBoxInfix2;
private System.Windows.Forms.Label labelInfix2;
private System.Windows.Forms.DataGridView dataGridViewConvert2;
private System.Windows.Forms.DataGridView dataGridViewOutput2;
private System.Windows.Forms.TextBox textBoxAnswer;
private System.Windows.Forms.Label labelEqual;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
}
}
|
 |
 |
 |
 |
Date :
2016-02-10 09:03:47 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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.Threading.Tasks;
using System.Windows.Forms;
namespace InfixNotationConverter
{
public partial class Form1 : Form
{
private ToolStripLabel toolStripLabelResult;
private string postfixAnswer;
private string prefixAnswer;
public Form1()
{
InitializeComponent();
this.toolStripLabelResult = (ToolStripLabel)this.statusStrip1.Items["toolStripStatusLabelResult"];
this.postfixAnswer = string.Empty;
this.prefixAnswer = string.Empty;
this.imageList1.Images.Add("Error", global::InfixNotationConverter.Properties.Resources.FalseSign);
this.imageList1.Images.Add("Warning", global::InfixNotationConverter.Properties.Resources.CautionSign);
this.imageList1.Images.Add("Success", global::InfixNotationConverter.Properties.Resources.TrueSign);
}
private void Form1_Load(object sender, EventArgs e)
{
this.toolStripLabelResult.Text = string.Empty;
this.toolStripLabelResult.Image = null;
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
if (this.textBoxInput.Text != string.Empty)
{
this.Clear();
this.backgroundWorker1.RunWorkerAsync(this.textBoxInput.Text);
}
else
{
this.Clear();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
InfixNotation infix = new InfixNotation((string)e.Argument);
if (infix.IsValid)
{
this.backgroundWorker1.ReportProgress(0, infix.Expression);
this.backgroundWorker1.ReportProgress(1, infix.ToString());
DataTable postfixSolution;
PostfixNotation postfix = infix.ToPostfix(out postfixSolution);
this.backgroundWorker1.ReportProgress(2, postfix.ToString());
this.backgroundWorker1.ReportProgress(3, postfixSolution);
string message1;
bool isSuccess1 = postfix.IsSuccess(out message1);
DataTable resultSolution1;
double result1 = postfix.Calculate(out resultSolution1);
this.postfixAnswer = result1.ToString();
this.backgroundWorker1.ReportProgress(4, resultSolution1);
DataTable prefixSolution;
PrefixNotation prefix = infix.ToPrefix(out prefixSolution);
this.backgroundWorker1.ReportProgress(5, prefix.ToString());
this.backgroundWorker1.ReportProgress(6, prefixSolution);
string message2;
bool isSuccess2 = prefix.IsSuccess(out message2);
DataTable resultSolution2;
double result2 = prefix.Calculate(out resultSolution2);
this.prefixAnswer = result2.ToString();
this.backgroundWorker1.ReportProgress(7, resultSolution2);
if (isSuccess1 && isSuccess2)
{
this.backgroundWorker1.ReportProgress(8, result1.ToString());
e.Result = (result1 == result2) ? "Success." : "Warning: Unclear proposition.";
}
else
{
this.backgroundWorker1.ReportProgress(8, string.Empty);
e.Result = message1;
}
}
else
{
e.Result = "Error: Parse error.";
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
switch (e.ProgressPercentage)
{
case 0:
this.textBoxInput.Text = (string)e.UserState;
break;
case 1:
this.textBoxInfix1.Text = (string)e.UserState;
this.textBoxInfix2.Text = (string)e.UserState;
break;
case 2:
this.textBoxOutput1.Text = (string)e.UserState;
break;
case 3:
this.dataGridViewConvert1.DataSource = (DataTable)e.UserState;
break;
case 4:
this.dataGridViewOutput1.DataSource = (DataTable)e.UserState;
break;
case 5:
this.textBoxOutput2.Text = (string)e.UserState;
break;
case 6:
this.tabControl1.SelectedTab = this.tabPagePrefix;
this.dataGridViewConvert2.DataSource = (DataTable)e.UserState;
break;
case 7:
this.dataGridViewOutput2.DataSource = (DataTable)e.UserState;
break;
case 8:
this.tabControl1.SelectedTab = this.tabPagePostfix;
this.textBoxAnswer.Text = (string)e.UserState;
break;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string message = (string)e.Result;
if (message == "Error: Parse error.")
this.Clear();
this.toolStripLabelResult.Text = message;
this.toolStripLabelResult.Image = (!message.StartsWith("Success")) ? (!message.StartsWith("Warning")) ? this.imageList1.Images["Error"] : this.imageList1.Images["Warning"] : this.imageList1.Images["Success"];
}
private void Clear()
{
this.toolStripLabelResult.Text = string.Empty;
this.toolStripLabelResult.Image = null;
this.textBoxAnswer.Text = string.Empty;
this.textBoxInfix1.Text = string.Empty;
this.textBoxOutput1.Text = string.Empty;
this.dataGridViewConvert1.DataSource = null;
this.dataGridViewOutput1.DataSource = null;
this.textBoxInfix2.Text = string.Empty;
this.textBoxOutput2.Text = string.Empty;
this.dataGridViewConvert2.DataSource = null;
this.dataGridViewOutput2.DataSource = null;
this.postfixAnswer = string.Empty;
this.prefixAnswer = string.Empty;
}
private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !((".+-*/^()").Contains(e.KeyChar) ||
char.IsDigit(e.KeyChar) ||
char.IsControl(e.KeyChar) ||
e.KeyChar == (char)Keys.Back);
}
private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
foreach (DataGridViewColumn dc in dgv.Columns)
{
dc.ReadOnly = true;
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
dgv.Columns[0].HeaderText = "#";
dgv.Columns[0].Width = 30;
dgv.Columns[1].Width = 80;
dgv.Columns[2].Width = 80;
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
TabControl tab = (TabControl)sender;
this.textBoxAnswer.Text = tab.SelectedIndex == 0 ? this.postfixAnswer : this.prefixAnswer;
}
}
}

|
 |
 |
 |
 |
Date :
2016-02-10 09:05:21 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfixNotationConverter
{
public static class ExtensionMethods
{
public static Queue<T> ToQueue<T>(this List<T> items)
{
Queue<T> queue = new Queue<T>();
items.ForEach(item => queue.Enqueue(item));
return queue;
}
public static Stack<T> ToStack<T>(this List<T> items)
{
Stack<T> stack = new Stack<T>();
items.ForEach(item => stack.Push(item));
return stack;
}
}
}
Token.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfixNotationConverter
{
public class Token
{
public string Element { get; set; }
public double Value { get; set; }
public ElementType Type { get; set; }
}
public enum ElementType
{
Operand,
Operator
}
}
|
 |
 |
 |
 |
Date :
2016-02-10 09:06:33 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
InfixNotation.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace InfixNotationConverter
{
public class InfixNotation : List<Token>
{
private string _input;
private string _expression;
private bool _isValid;
public InfixNotation()
{
this._input = string.Empty;
this._expression = string.Empty;
this._isValid = false;
}
public InfixNotation(string input)
{
this.Input = input;
}
public string Input
{
set
{
this._input = value;
List<Token> tokens = this.ToTokens(this._input);
this._isValid = this.Validate(tokens);
if (this._isValid)
{
this.AddRange(this.ToInfix(tokens));
this._expression = this.Select(t => (t.Type == ElementType.Operand) ? t.Value.ToString() : t.Element).Aggregate((current, next) => current + next);
}
else
{
this._expression = string.Empty;
}
}
get
{
return this._input;
}
}
public string Expression
{
set
{
this._expression = value;
}
get
{
return this._expression;
}
}
public bool IsValid
{
get
{
return this._isValid;
}
}
public override string ToString()
{
return (this.Count > 0) ? this.Select(t => t.Element).Aggregate((current, next) => current + next) : string.Empty;
}
private List<Token> ToInfix(List<Token> input)
{
List<Token> infix = new List<Token>();
Dictionary<string, double> variables = new Dictionary<string, double>();
input.Where(e => e.Type == ElementType.Operand).GroupBy(e => Convert.ToDouble(e.Element)).Select(g => g.Key).ToList().ForEach(g =>
{
string key = (variables.Count > 25) ? "X" + (variables.Count - 25).ToString() : ((char)('A' + variables.Count)).ToString();
variables.Add(key, g);
});
foreach (Token token in input)
{
if (token.Type == ElementType.Operand)
{
KeyValuePair<string, double> variable = variables.Where(v => v.Value == Convert.ToDouble(token.Element)).First();
infix.Add(new Token() { Element = variable.Key, Value = variable.Value, Type = ElementType.Operand });
}
else
{
infix.Add(token);
}
}
return infix;
}
public PostfixNotation ToPostfix()
{
DataTable solution;
return this.ToPostfix(out solution);
}
public PostfixNotation ToPostfix(out DataTable solution)
{
solution = new DataTable();
solution.Columns.Add(new DataColumn("Index", typeof(string)));
solution.Columns.Add(new DataColumn("Infix", typeof(string)));
solution.Columns.Add(new DataColumn("Operator", typeof(string)));
solution.Columns.Add(new DataColumn("Postfix", typeof(string)));
Queue<Token> infix = this.ToQueue();
PostfixNotation postfix = new PostfixNotation();
Stack<Token> operators = new Stack<Token>();
int row = 0;
while (infix.Count > 0)
{
Token token = infix.Dequeue();
row++;
if (token.Type == ElementType.Operator)
{
if (token.Element == "(")
{
operators.Push(token);
}
else if (token.Element == ")")
{
if (operators.Count > 0)
{
while ((operators.Count > 0) ? operators.Peek().Element != "(" : false)
postfix.Add(operators.Pop());
if (operators.Count > 0)
operators.Pop();
}
}
else
{
if (operators.Count > 0)
{
while ((operators.Count > 0) ? (token.Element != "^") ? operators.Peek().Value >= token.Value : false : false)
postfix.Add(operators.Pop());
}
operators.Push(token);
}
}
else
{
postfix.Add(token);
}
this.AddDataRow("Postfix", solution, string.Format("{0}.", row), token.Element, this.PrintStack(operators), postfix.ToString());
}
if (operators.Count > 0)
{
string opr = string.Empty;
while (operators.Count > 0)
postfix.Add(operators.Pop());
this.AddDataRow("Postfix", solution, string.Format("{0}.", row + 1), string.Empty, this.PrintStack(operators), postfix.ToString());
}
return postfix;
}
public PrefixNotation ToPrefix()
{
DataTable solution;
return this.ToPrefix(out solution);
}
public PrefixNotation ToPrefix(out DataTable solution)
{
solution = new DataTable();
solution.Columns.Add(new DataColumn("Index", typeof(string)));
solution.Columns.Add(new DataColumn("Infix", typeof(string)));
solution.Columns.Add(new DataColumn("Operator", typeof(string)));
solution.Columns.Add(new DataColumn("Prefix", typeof(string)));
Stack<Token> infix = this.ToStack();
PrefixNotation prefix = new PrefixNotation();
Stack<Token> operators = new Stack<Token>();
int row = 0;
while (infix.Count > 0)
{
Token token = infix.Pop();
row++;
if (token.Type == ElementType.Operator)
{
if (token.Element == ")")
{
operators.Push(token);
}
else if (token.Element == "(")
{
if (operators.Count > 0)
{
while ((operators.Count > 0) ? operators.Peek().Element != ")" : false)
prefix.Insert(0, operators.Pop());
if (operators.Count > 0)
operators.Pop();
}
}
else
{
if (operators.Count > 0)
{
while ((operators.Count > 0) ? operators.Peek().Value >= token.Value : false)
prefix.Insert(0, operators.Pop());
}
operators.Push(token);
}
}
else
{
prefix.Insert(0, token);
}
this.AddDataRow("Prefix", solution, string.Format("{0}.", row), token.Element, this.PrintStack(operators), prefix.ToString());
}
if (operators.Count > 0)
{
string opr = string.Empty;
while (operators.Count > 0)
prefix.Insert(0, operators.Pop());
this.AddDataRow("Prefix", solution, string.Format("{0}.", row + 1), string.Empty, this.PrintStack(operators), prefix.ToString());
}
return prefix;
}
private List<Token> ToTokens(string input)
{
List<Token> output = new List<Token>();
string expression = input;
string number = string.Empty;
while (expression.Contains("()"))
expression = expression.Replace("()", string.Empty);
expression = Regex.Replace(expression, @"[0-9]\(", delegate(Match match)
{
return match.ToString().Substring(0, 1) + "*(";
});
expression = Regex.Replace(expression, @"\)[0-9]", delegate(Match match)
{
return ")*" + match.ToString().Substring(1, 1);
});
foreach (char c in expression.ToCharArray())
{
switch (c)
{
case '+':
case '-':
if (number != string.Empty)
{
output.Add(new Token() { Element = number, Value = 0, Type = ElementType.Operand });
output.Add(new Token() { Element = c.ToString(), Value = this.OperatorOrder(c.ToString()), Type = ElementType.Operator });
number = string.Empty;
}
else if (output.Count == 0)
{
number += c.ToString();
}
else if (output.Last().Element != ")")
{
number += c.ToString();
}
else
{
output.Add(new Token() { Element = c.ToString(), Value = this.OperatorOrder(c.ToString()), Type = ElementType.Operator });
}
break;
case '*':
case '/':
case '^':
case '(':
case ')':
if (number != string.Empty)
{
output.Add(new Token() { Element = number, Value = 0, Type = ElementType.Operand });
number = string.Empty;
}
output.Add(new Token() { Element = c.ToString(), Value = this.OperatorOrder(c.ToString()), Type = ElementType.Operator });
break;
default:
number += c.ToString();
break;
}
}
if (number != string.Empty)
output.Add(new Token() { Element = number, Value = 0, Type = ElementType.Operand });
int index = 1;
while (index > 0 && index < output.Count - 1)
{
string previous = output[index - 1].Element;
ElementType current = output[index].Type;
string next = output[index + 1].Element;
if (previous == "(" && current == ElementType.Operand && next == ")")
{
output.RemoveAt(index + 1);
output.RemoveAt(index - 1);
index--;
}
else
{
index++;
}
}
return output;
}
private bool Validate(List<Token> input)
{
bool isValide = false;
if (input.Count > 0)
{
int OpenBracket = input.Where(e => e.Element == "(").Count();
int CloseBracket = input.Where(e => e.Element == ")").Count();
int Operand = input.Where(e => e.Type == ElementType.Operand).Count();
bool result = true;
double tryDouble = 0.0;
input.Where(e => e.Type == ElementType.Operand).GroupBy(e => e.Element).Select(g => g.Key).ToList().ForEach(g => result &= double.TryParse(g, out tryDouble));
isValide = (OpenBracket == CloseBracket) && (Operand > 0) && result;
}
return isValide;
}
private double OperatorOrder(string op)
{
double value;
switch (op)
{
default: value = -1.0; break;
case "+": value = 1.0; break;
case "-": value = 1.0; break;
case "*": value = 2.0; break;
case "/": value = 2.0; break;
case "^": value = 3.0; break;
}
return value;
}
private string PrintStack(Stack<Token> stack)
{
return (stack.Count > 0) ? stack.ToList().Select(s => s.Element).Reverse().Aggregate((current, next) => current + next) : string.Empty;
}
private void AddDataRow(string name, DataTable dt, string index, string infix, string opr, string output)
{
DataRow dr = dt.NewRow();
dr.SetField<string>("Index", index);
dr.SetField<string>("Infix", infix);
dr.SetField<string>("Operator", opr);
dr.SetField<string>(name, output);
dt.Rows.Add(dr);
}
}
}
|
 |
 |
 |
 |
Date :
2016-02-10 09:07:43 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
PostfixNotation.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfixNotationConverter
{
public class PostfixNotation : List<Token>
{
public override string ToString()
{
return (this.Count > 0) ? this.Select(t => t.Element).Aggregate((current, next) => current + next) : string.Empty;
}
public virtual bool IsSuccess(out string message)
{
Queue<Token> postfix = this.ToQueue();
Stack<double> variables = new Stack<double>();
bool isSuccess = true;
message = "Success";
while (postfix.Count > 0)
{
Token token = postfix.Dequeue();
if (token.Type == ElementType.Operator)
{
double x = 0.0;
double y = 0.0;
double result = 0.0;
if (variables.Count > 0)
{
x = variables.Pop();
if (variables.Count > 0)
{
y = variables.Pop();
}
else
{
isSuccess = false;
message = "Error: Syntax error.";
}
}
else
{
isSuccess = false;
message = "Error: Syntax error.";
}
switch (token.Element)
{
case "^": result = Math.Pow(y, x); break;
case "*": result = y * x; break;
case "/":
if (x != 0)
{
result = y / x;
}
else
{
isSuccess = false;
message = "Error: Devide by zero.";
}
break;
case "+": result = y + x; break;
case "-": result = y - x; break;
}
variables.Push(result);
}
else
{
variables.Push(token.Value);
}
}
return isSuccess;
}
public virtual double Calculate()
{
DataTable solution;
return Calculate(out solution);
}
public virtual double Calculate(out DataTable solution)
{
Queue<Token> postfix = this.ToQueue();
Stack<double> variables = new Stack<double>();
bool hasError = false;
string message = string.Empty;
int row = 0;
solution = new DataTable();
solution.Columns.Add(new DataColumn("Index", typeof(string)));
solution.Columns.Add(new DataColumn("Postfix", typeof(string)));
solution.Columns.Add(new DataColumn("Value", typeof(string)));
solution.Columns.Add(new DataColumn("Answer", typeof(string)));
while (postfix.Count > 0)
{
Token token = postfix.Dequeue();
row++;
if (token.Type == ElementType.Operator)
{
string calc = string.Empty;
double x = 0.0;
double y = 0.0;
double result = 0.0;
if (variables.Count > 0)
{
x = variables.Pop();
if (variables.Count > 0)
{
y = variables.Pop();
}
else
{
hasError = true;
message = "Error: Syntax error.";
}
}
else
{
hasError = true;
message = "Error: Syntax error.";
}
switch (token.Element)
{
case "^":
result = Math.Pow(y, x);
calc = (hasError) ? string.Empty : string.Format("{0}^{1}", y, x);
break;
case "*":
result = y * x;
calc = (hasError) ? string.Empty : string.Format("{0}*{1}", y, x);
break;
case "/":
if (x != 0)
{
result = y / x;
calc = (hasError) ? string.Empty : string.Format("{0}/{1}", y, x);
}
else
{
hasError = true;
message = "Error: Devide by zero.";
}
break;
case "+":
result = y + x;
calc = (hasError) ? string.Empty : string.Format("{0}+{1}", y, x);
break;
case "-":
result = y - x;
calc = (hasError) ? string.Empty : string.Format("{0}-{1}", y, x);
break;
}
this.AddDataRow(solution, string.Format("{0}.", row), token.Element, string.Empty, (!hasError) ? (variables.Count > 0) ? string.Format("{0}, {1}", this.PrintStack(variables), calc) : calc : message);
variables.Push(result);
}
else
{
variables.Push(token.Value);
this.AddDataRow(solution, string.Format("{0}.", row), token.Element, token.Value.ToString(), (hasError) ? message : this.PrintStack(variables));
}
}
double answer = 0.0;
if (variables.Count > 0)
{
answer = variables.Pop();
this.AddDataRow(solution, string.Format("{0}.", row + 1), string.Empty, string.Empty, (!hasError) ? answer.ToString() : message);
}
return answer;
}
protected virtual string PrintStack(Stack<double> stack)
{
return (stack.Count > 0) ? stack.ToList().Select(s => s.ToString()).Reverse().Aggregate((current, next) => current + ", " + next) : string.Empty;
}
private void AddDataRow(DataTable dt, string index, string postfix, string value, string answer)
{
DataRow dr = dt.NewRow();
dr.SetField<string>("Index", index);
dr.SetField<string>("Postfix", postfix);
dr.SetField<string>("Value", value);
dr.SetField<string>("Answer", answer);
dt.Rows.Add(dr);
}
}
}
|
 |
 |
 |
 |
Date :
2016-02-10 09:08:20 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
PrefixNotation.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfixNotationConverter
{
public class PrefixNotation : PostfixNotation
{
public override bool IsSuccess(out string message)
{
Stack<Token> prefix = this.ToStack();
Stack<double> variables = new Stack<double>();
bool isSuccess = true;
message = "Success";
while (prefix.Count > 0)
{
Token token = prefix.Pop();
string calc = string.Empty;
if (token.Type == ElementType.Operator)
{
double x = 0.0;
double y = 0.0;
double result = 0.0;
if (variables.Count > 0)
{
x = variables.Pop();
if (variables.Count > 0)
{
y = variables.Pop();
}
else
{
isSuccess = false;
message = "Error: Syntax error.";
}
}
else
{
isSuccess = false;
message = "Error: Syntax error.";
}
switch (token.Element)
{
case "^": result = Math.Pow(x, y); break;
case "*": result = x * y; break;
case "/":
if (y != 0)
{
result = x / y;
}
else
{
isSuccess = false;
message = "Error: Devide by zero.";
}
break;
case "+": result = x + y; break;
case "-": result = x - y; break;
}
variables.Push(result);
}
else
{
variables.Push(token.Value);
}
}
return isSuccess;
}
public override double Calculate(out DataTable solution)
{
Stack<Token> prefix = this.ToStack();
Stack<double> variables = new Stack<double>();
bool hasError = false;
string message = string.Empty;
int row = 0;
solution = new DataTable();
solution.Columns.Add(new DataColumn("Index", typeof(string)));
solution.Columns.Add(new DataColumn("Prefix", typeof(string)));
solution.Columns.Add(new DataColumn("Value", typeof(string)));
solution.Columns.Add(new DataColumn("Answer", typeof(string)));
while (prefix.Count > 0)
{
Token token = prefix.Pop();
row++;
if (token.Type == ElementType.Operator)
{
string calc = string.Empty;
double x = 0.0;
double y = 0.0;
double result = 0.0;
if (variables.Count > 0)
{
x = variables.Pop();
if (variables.Count > 0)
{
y = variables.Pop();
}
else
{
hasError = true;
message = "Error: Syntax error.";
}
}
else
{
hasError = true;
message = "Error: Syntax error.";
}
switch (token.Element)
{
case "^":
result = Math.Pow(x, y);
calc = (hasError) ? string.Empty : string.Format("{0}^{1}", x, y);
break;
case "*":
result = x * y;
calc = (hasError) ? string.Empty : string.Format("{0}*{1}", x, y);
break;
case "/":
if (y != 0)
{
result = x / y;
calc = (hasError) ? string.Empty : string.Format("{0}/{1}", x, y);
}
else
{
hasError = true;
message = "Error: Devide by zero.";
}
break;
case "+":
result = x + y;
calc = (hasError) ? string.Empty : string.Format("{0}+{1}", x, y);
break;
case "-":
result = x - y;
calc = (hasError) ? string.Empty : string.Format("{0}-{1}", x, y);
break;
}
this.AddDataRow(solution, string.Format("{0}.", row), token.Element, string.Empty, (!hasError) ? (variables.Count > 0) ? string.Format("{0}, {1}", calc, this.PrintStack(variables)) : calc : message);
variables.Push(result);
}
else
{
variables.Push(token.Value);
this.AddDataRow(solution, string.Format("{0}.", row), token.Element, token.Value.ToString(), (hasError) ? message : this.PrintStack(variables));
}
}
double answer = 0.0;
if (variables.Count > 0)
{
answer = variables.Pop();
this.AddDataRow(solution, string.Format("{0}.", row + 1), string.Empty, string.Empty, (!hasError) ? answer.ToString() : message);
}
return answer;
}
protected override string PrintStack(Stack<double> stack)
{
return (stack.Count > 0) ? stack.ToList().Select(s => s.ToString()).Reverse().Aggregate((current, next) => next + ", " + current) : string.Empty;
}
private void AddDataRow(DataTable dt, string index, string prefix, string value, string answer)
{
DataRow dr = dt.NewRow();
dr.SetField<string>("Index", index);
dr.SetField<string>("Prefix", prefix);
dr.SetField<string>("Value", value);
dr.SetField<string>("Answer", answer);
dt.Rows.Add(dr);
}
}
}
|
 |
 |
 |
 |
Date :
2016-02-10 09:08:48 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
@ห้ามตอบเกินวันละ 2 กระทู้ จาก #NO9 - #NO14
ชัดเจนดี และ ตรงไปตรงมา (ผมทำความเข้าใจในเบื้องต้นและผมยังไม่ได้ทดสอบ)
--- ผมสามารถเอาไปคล่อม #NO3 ได้
--- ขอบคุณครับ
แต่ผมสงสัยนิดหนึ่ง จาก #NO9 - #NO14
สมมุติว่าผมป้อนสูตรการหาพื้นที่วงกลม/เส้นรอบวง
¶r2
2¶r
หรือ
X2 + Y2 = 9
เป็นต้น
จะเกิดอะไรขึ้น?
|
 |
 |
 |
 |
Date :
2016-02-10 12:15:06 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับที่ให้คำปรึกษาครับ
|
 |
 |
 |
 |
Date :
2016-02-10 14:26:38 |
By :
teerapat_kill |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|