2017-11-04 4 views
-1

私は、私はそれらを使用しようとするたびに、私はエラーの下に取得するには、電卓を作るしようとしている、と私はちょっとMODとexpの操作にこだわっ:電卓にエラーが表示されます: '入力文字列が正しい形式ではありませんでした。'

System.FormatException: 'Input string was not in a correct format.'

これは中のイベントですエラーが起こる:おそらく

private void Equal_Click(object sender, EventArgs e) 
     { 
      switch (operationPerf) 
      { 
       case "+": 
        TB.Text = (result + Double.Parse(TB.Text)).ToString(); 
        CO.Text = ""; 
        break; 
       case "-": 
        TB.Text = (result - Double.Parse(TB.Text)).ToString(); 
        CO.Text = ""; 
        break; 
       case "*": 
        TB.Text = (result * Double.Parse(TB.Text)).ToString(); 
        CO.Text = ""; 
        break; 
       case "/": 
        TB.Text = (result/Double.Parse(TB.Text)).ToString(); 
        CO.Text = ""; 
        break; 
       case "Mod": 
        TB.Text = (result % Double.Parse(TB.Text)).ToString(); 
        CO.Text = ""; 
        break; 
       case "Exp": 
        TB.Text = Math.Exp(Double.Parse(TB.Text) * Math.Log((result) * 4)).ToString(); 
        CO.Text = ""; 
        break; 
       default: 
        break; 
      } 
      result = Double.Parse(TB.Text); // Here is where the error happens 
      CO.Text = ""; 
     } 
+0

何をデバッガショー '例外がスローされる前にTB.Text'が含まれていますか? – Richard

+0

それはこの "23Exp3"を含んでいます – DarkAngel5125

+3

これを番号に変換するように頼んだら、できますか?* 23Exp3 *?あなたができなければ、コンピュータはそれを行うことができないので、エラーが発生します。 – CodingYoshi

答えて

0

operationPerfが期待される事業者のいずれでもない、デフォルトのケースを実行し、テキストボックスはまだもちろん、doubleに変換することはできません、元の式(例えば"23Exp3")が、含まれているので、 。

実際に計算機ロジックを入力と出力から分離する必要があります。それが唯一の純粋な数学を含まず、任意の解析や書式設定を必要としないので理解しやすいです

public class Calculator 
{ 
    public double Result { get; set; } 

    public void Calculate(string operation, double operand) 
    { 
     switch (operation) 
     { 
      case "+": 
       Result = Result + operand; 
       break; 
      case "-": 
       Result = Result - operand; 
       break; 
      case "*": 
       Result = Result * operand; 
       break; 
      case "/": 
       Result = Result/operand; 
       break; 
      case "Mod": 
       Result = Result % operand; 
       break; 
      case "Exp": 
       Result = Math.Exp(operand * Math.Log(Result * 4)); 

       // Did you want this instead? 
       // Result = Math.Pow(Result, operand); 
       break; 
      default: 
       break; 
     } 
    } 
} 

(Calculator.cs)別々のコードファイルにCalculatorクラスを作成します。フォームフィールドで

(フォームクラスの最上部に)あなたは、あなたは、テキストボックスはまだ "23Exp3"が含まれている場合は、イベントハンドラが簡単すぎ

private void Equal_Click(object sender, EventArgs e) 
{ 
    if (Double.TryParse(TB.Text, out double operand)) { 
     _calculator.Calculate(operationPerf, operand); 
     TB.Text = _calculator.Result.ToString(); 
     CO.Text = ""; 
    } else { 
     MsgBox.Show("The TextBox does not contain a number"); 
    } 
} 

ある

public partial class Form1 : Form 
{ 
    private Calculator _calculator = new Calculator(); 

    ... 
} 

宣言することができますそれをdoubleに変換することはできません。異なるテキストボックスを使用して操作、演算子(数値)および結果を入力するか、文字列を3つの部分(2つの数値と操作)に分けます。

私は本当にMath.Exp(operand * Math.Log(Result * 4))の背後にある論理を理解していません。予想される行動がわからないので、アドバイスはできません。代わりにMath.Powを使用しましたか?

public static double Pow(double x, double y) 

Returns a specified number raised to the specified power.

参照:Math.Pow Method (Double, Double)

+0

私はCalculatorクラスを作成する場所が混乱しています。私はフォーム1の上にありますが、実際には機能しませんし、どちらもフォームクラス内では動作しませんが、 "Form1。 Calculator.result 'は " – DarkAngel5125

+0

"に割り当てられません。Calculatorクラスの新しいコードファイルを作成します。 Visual Studioでプロジェクト>追加>クラス... I.Eを右クリックします。クラスコードは別のファイルにありますが、 '_calculator'オブジェクトの宣言はフォームにあり、私はサンプルコードを変更しました。 –

+0

"フィールド 'Calculator.result'はまだ"警告 "に割り当てられていないと試してみました。私が最初にダブルで結果を初期化した後、それを得ることに言及する必要があります。 – DarkAngel5125

0

あなたはこのような何かを入力するようユーザーに求めている:10Exp3。そして、あなたはあなたのプログラムが3の威力で10を上げ、結果を表示すると期待しています。あなたはこの場合1000を期待しています。しかし、あなたは単純にこれをやっている:あなたのコードで

case "Exp": 
    TB.Text = Math.Exp(Double.Parse(TB.Text) * Math.Log((result) * 4)).ToString(); 

まず問題がある、あなたはExpを使用している、これはExpのドキュメントであるため、:

Returns e raised to the specified power.

指定に上げ電子をしたいですかパワー?いいえ、あなたはしません。だからあなたはその方法を使うことはできません。しかし、たとえあなたがしたいと思っていたとしても(おそらくe)、コンピュータは10Exp3に変換することができません - 何をするか教えてください。ここで

は(より明確にするためのインライン私のコメントを読んで)方法です:

var input = "10Exp3"; 
// Let's split the numbers using <Exp> 
var splits = input.Split(new[] { "Exp" }, StringSplitOptions.None); 

// Let's try and convert the first part into a double and see if it works 
double numberBase = 0; 
if (!double.TryParse(splits[0], out numberBase)) 
{ 
    // The part before the word Exp is 
    // not a number, perhaps show an error to the user in a message box 
} 

// Now let's try and convert the second part 
double exponent = 0; 
if (!double.TryParse(splits[1], out exponent)) 
{ 
    // The part after the word Exp is 
    // not a number, perhaps show an error to the user in a message box 
} 

// Now we will call the Pow method or use Math.Exp if you want e 
double result = Math.Pow(numberBase, exponent); 
関連する問題