实现伪代码,无需在C#中返回 [英] implement Pseudocode code without return in c#

查看:122
本文介绍了实现伪代码,无需在C#中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

如何在不返回c#的情况下实现该伪代码(因为我想在按钮事件处理程序中使用它)?

谢谢

Hi friends,

how can I implement this Pseudocode without return in c#(because I want to use it in button event handler)?

Thanks

large_int example(large_int u,large_int v)
{
  .
  .
  .//some codes
  .
      x = u % 10 ^ 2;
      y = u / 10 ^ 2;
       w = v % 10 ^ 2;
       z = v / 10 ^ 2;
  return example(x,w)+example(y,z)///it should gives sum of them as output
  //it means when example(x,w)+example() returns, x replace with u and w replace with v

}

推荐答案

WinForms,是吗?我猜你是说.NET v.4和System.Numerics.BigInteger,对吗?
(没有"large_int"这样的类型,所以我不知道要考虑什么.无论如何,请使用所需的类型:其余的代码将完全相同;请参见下文.)

然后,您将得到类似以下内容的伪代码:
WinForms, yeh? And I guess you mean .NET v.4 and System.Numerics.BigInteger, right?
(There is no such type as "large_int", so I did not know what to think about. No matter, use the type you want: the rest of the code will be exactly the same; see below.)

Then, instead of you pseudo-code you''ll have something like this:
System.Numerics.BigInteger Example(System.Numerics.BigInteger u, System.Numerics.BigInteger v) {
    //...
    return u + v; //in fact, whatever you want to calculate
    //I put this line just to compile
}



好的,那么您假设有3个控件来保存大整数的字符串表示形式以及按钮:



Ok, then you suppose to have some 3 controls to hold string representation of your big integers, plus your button:

TextBox
    TextBoxLeftOperand,
    TextBoxRightOperand;
    TextBoxBigIntegerTestResult;
Button MyButtonTextBoxBigIntegerTest;



而且您将需要处理代码:



And you will need a code for a hander:

void BigIntegerTestClickHandler() {
    System.Numerics.BigInteger u =
        System.Numerics.BigInteger.Parse(TextBoxLeftOperand.Text);
    System.Numerics.BigInteger v =
        System.Numerics.BigInteger.Parse(TextBoxRightOperand.Text);
    TextBoxBigIntegerTestResult.Text = Example(u, v).ToString();
}



期望从上面的代码中抛出异常.

最后,您需要设置按钮事件.您可以在构造表单之后(或稍后,但是一定要在用户实际单击此按钮之前)立即进行操作:



Expect getting exceptions thrown from the code above.

Finally, you''ll need to setup your button event. You can do it right after the form is constructed (or later, but certainly before the user can actually click this button):

MyButtonTextBoxBigIntegerTest.Click +=
    (sender, args) => { BigIntegerTestClickHandler(); };



我的建议:始终使用上面的语法,而不要使用Microsoft自动生成的代码中使用的严重过时的语法.好处是:您不必为事件处理程序参数指定确切的类型,如果您不使用该参数,则特别有用(非常典型的情况);同样,您也不必在处理程序中拥有相同的参数(否则,即使您不使用参数,也必须指定参数senderargs).最后,您完全可以避免编写像BigIntegerTestClickHandler这样的hander作为单独的方法.相反,您可以简单地将此方法的主体直接放在上述小代码的大括号之间.

注意:也许数字部分是过大的;并且您的意思是一些简单的整数类型(BigInteger是支持无限精度的极其复杂的类型.(并准备通过具有很大中间数的某些操作来挂起您的计算机.)).不要担心.只需使用相同的代码,然后将System.Numerics.BigInteger替换为所需的任何类型即可.

就这样.
这是您想要的吗?

请不要投票否决这个答案,因为它不能回答提出的问题!

我认为没有人可以肯定地说出问题的作者的意思.这只是一个介绍性建议,希望对您有所帮助.问题的作者回答了我的问题并解决了争议后,我将可以给出最终答案.



My advice: always use the syntax above, not the gravely obsolete syntax used in Microsoft''s auto-generated code. The benefits: you don''t have to specify exact type for event handler arguments, which is especially good if you''re not using the argument (very typical case); also, you''re not bound to having same arguments in the handler (otherwise you would have to specify the arguments sender and args even if you''re not using them). Finally, you''re allowed to avoid writing a hander like BigIntegerTestClickHandler as a separate method at all; instead, you could simply put a body of this method directly between curly brackets of the above codelet.

Note: Perhaps, the numeric part is overkill; and you meant some simple integer type (BigInteger is extremely sophisticated type supporting unlimited presision. (And be ready to hang your computer by certain operations with really big intermediate numbers.)). Not to worry. Just use the same code and replace System.Numerics.BigInteger with whatever type you want.

That''s it.
Is this something you wanted?

Please do not down-vote this answer because it does not answer the question posed!

I think nobody can say for sure what the author of the question means. This is just an introductory suggestion in hope it can be helpful. As soon as author of the question answers my questions and resolve the controversies, I''ll be able to give a final answer.


这篇关于实现伪代码,无需在C#中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆