0

我有这个使用文本的声明,但是我需要让它只适用于带有(,)小数的数字,现在我被卡住了......

public ref class Form1 : public System::Windows::Forms::Form
    {
    private: Stack<String^>^ talen;
    public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
        talen = gcnew Stack<String^>();
        }

'

private: System::Void btnPush_Click(System::Object^  sender, System::EventArgs^  e) {
    String^ tal = tbxTal->Text;
    talen->Push(tal);
    tbxIn->AppendText(tal + "\n");
    tbxTal->Text = "";
}

private: System::Void Pop_Click(System::Object^  sender, System::EventArgs^  e) {
    while (talen->Count!=0)
    {
        String^ tal = talen->Pop();
        tbxUt->AppendText(tal + "\n");
    }
}
4

1 回答 1

0

要检查字符串是否包含有效数字,我会使用其中一种TryParse可用的方法。

也许是这样的Int32::TryParse。当然,您可以使用Single::TryParse, UInt16::TryParse, 来表示您喜欢的任何类型的数字。

int value;
String^ tal = tbxTal->Text;
if(Int32::TryParse(tal, value))
{
    tbxIn->AppendText(tal + "\n");
    tbxTal->Text = "";
    talen->Push(tal);
}
else
{
    // Show an error.
}

如果您对堆栈所做的唯一事情就是您在 中显示的内容Pop_Click,那Stack<String^>^很好,但如果您正在使用它做其他事情,请考虑使用 a Stack<int>^(或您选择的任何数据类型)。

于 2013-11-07T18:16:15.913 回答