当我将更改除以25时如何删除额外的小数我得到3.48并且我需要一个完整数字的任何想法?

时间:2013-05-08 17:44:05

标签: visual-c++

#pragma once

namespace VendingMachine {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox1;

    protected: 

    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Label^  label6;
    private: System::Windows::Forms::Label^  label7;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox5;
    private: System::Windows::Forms::TextBox^  textBox6;











    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma 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>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->label5 = (gcnew System::Windows::Forms::Label());
            this->label6 = (gcnew System::Windows::Forms::Label());
            this->label7 = (gcnew System::Windows::Forms::Label());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox5 = (gcnew System::Windows::Forms::TextBox());
            this->textBox6 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(14, 14);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(153, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Amount of change (0-99 cents)";
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(173, 11);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(95, 20);
            this->textBox1->TabIndex = 1;
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(20, 59);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(60, 28);
            this->button1->TabIndex = 2;
            this->button1->Text = L"Calculate";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(190, 59);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(63, 28);
            this->button2->TabIndex = 3;
            this->button2->Text = L"Clear";
            this->button2->UseVisualStyleBackColor = true;
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(14, 132);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(99, 13);
            this->label2->TabIndex = 4;
            this->label2->Text = L"Number of Quarters";
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(14, 178);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(88, 13);
            this->label3->TabIndex = 5;
            this->label3->Text = L"Number of Dimes";
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(14, 218);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(0, 13);
            this->label4->TabIndex = 6;
            // 
            // label5
            // 
            this->label5->AutoSize = true;
            this->label5->Location = System::Drawing::Point(14, 215);
            this->label5->Name = L"label5";
            this->label5->Size = System::Drawing::Size(94, 13);
            this->label5->TabIndex = 7;
            this->label5->Text = L"Number of Nickels";
            // 
            // label6
            // 
            this->label6->AutoSize = true;
            this->label6->Location = System::Drawing::Point(14, 256);
            this->label6->Name = L"label6";
            this->label6->Size = System::Drawing::Size(96, 13);
            this->label6->TabIndex = 8;
            this->label6->Text = L"Number of pennies";
            // 
            // label7
            // 
            this->label7->AutoSize = true;
            this->label7->Location = System::Drawing::Point(14, 301);
            this->label7->Name = L"label7";
            this->label7->Size = System::Drawing::Size(59, 13);
            this->label7->TabIndex = 9;
            this->label7->Text = L"Total coins";
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(173, 129);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(95, 20);
            this->textBox2->TabIndex = 10;
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(173, 178);
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(95, 20);
            this->textBox3->TabIndex = 11;
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(173, 215);
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(95, 20);
            this->textBox4->TabIndex = 12;
            // 
            // textBox5
            // 
            this->textBox5->Location = System::Drawing::Point(173, 253);
            this->textBox5->Name = L"textBox5";
            this->textBox5->Size = System::Drawing::Size(95, 20);
            this->textBox5->TabIndex = 13;
            // 
            // textBox6
            // 
            this->textBox6->Location = System::Drawing::Point(173, 294);
            this->textBox6->Name = L"textBox6";
            this->textBox6->Size = System::Drawing::Size(95, 20);
            this->textBox6->TabIndex = 14;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 338);
            this->Controls->Add(this->textBox6);
            this->Controls->Add(this->textBox5);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->label7);
            this->Controls->Add(this->label6);
            this->Controls->Add(this->label5);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 // Declare variables
                 double change, quarters, dimes, nickels, pennies, total_coins;

                 // Read in data 
                 Double::TryParse(textBox1->Text, change);
                 Double::TryParse(textBox2->Text, quarters);
                 Double::TryParse(textBox3->Text, dimes);
                 Double::TryParse(textBox4->Text, nickels);
                 Double::TryParse(textBox5->Text, pennies);

问题出在我的代码的过程数据部分

                 // Process data
                 change = change; 0 - 99;
                 textBox1->Text = change.ToString();
                 MessageBox::Show(change.ToString());
                 /*error-I-need-a-whole-number ->*/quarters += change / 25;
                 MessageBox::Show(quarters.ToString());
                 // current code above gives me an answer of 3.48 when change equals 87    
                 // Display results
                 textBox6->Text = total_coins.ToString();
                 textBox2->Text = quarters.ToString();
             }
};
}

1 个答案:

答案 0 :(得分:2)

通过change = change; 0 - 99;之类的尴尬行,您想要做的就是使用int类型作为数量。

为什么要对这些数量使用double呢?你听说过四分之一或一角钱的分数吗?使它们都为int类型。 (change表示美分,而非美元,因此将其设为int是有意义的。)

如果您使用int87 / 25将被截断为3个季度,这是您期望的结果。