如何获取一个大整数作为输入并将其存储在内存中

发布于 2024-09-06 18:33:22 字数 376 浏览 1 评论 0原文

我知道在 Brainfuck 中对大整数执行算术虽然有时可能相当乏味,但完全是可能的。

然而,我想知道的是,普遍接受的最佳实践是什么,用于接受大整数(甚至是字符串,我想)作为输入。

大多数编译器/解释器允许您一次提供完整的字符串作为输入(然后使用 , 单独读取每个字符)。但我想知道的是——如果你不知道输入流何时停止,你怎么能读进去呢?我想一种方法是告诉用户将某个字符/字符串附加到他们的号码上以表明它已经结束,但这似乎有点不用户友好。

我更喜欢一个牢记可移植性的答案(特定于实现的解决方案很有趣,但不是这个问题的主要焦点)。如果没有完全与实现无关的方法来做到这一点,那么一种可以在大多数实现上工作并正常失败的方法将是下一个最好的方法。

提前致谢。

I know that performing arithmetic on large integers in brainfuck, while perhaps quite tedious at times, is entirely possible.

However what I'm wondering about is what the generally acceptd best-practices are for taking in large integers (or even strings, I suppose) as input.

Most compilers/interpreters allow you to provide full strings at once as input (and then each character is read in individually with a ,). But what I'm wondering is this - how can you read one in if you don't know when the input stream is going to stop? I suppose one way is to tell the user to append a certain character/string of characters to their number to indicate that it's over, but that seems a bit non-user-friendly.

I'd prefer an answer that keeps portability in mind (implementation-specific solutions are of interest, but are not the primary focus of this question). If there is no completely implementation-agnostic way to do this, one that will work on most implementations and fail gracefully otherwise would be the next best thing.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-09-13 18:33:22

大多数语言允许您从输入中读取一行(例如,C 中的 gets()、C# 中的 ReadLine() 等)。为什么不要求用户将每个值作为一行输入(即用 Enter 分隔)?

Most languages let you read a line from input (e.g. gets() in C, ReadLine() in C# etc). Why not ask the user to enter each value as a line (i.e. separated by enter)?

只是我以为 2024-09-13 18:33:22

实际上,我出于不同的目的针对不同的问题发布了相同的代码。
下面的代码将继续接受您输入的任何内容的 ASCII,除非遇到换行符。然后打印您输入的内容。

不用担心便携性;我已经用这种读取数字的策略实现了两个 n 位数字的加法,您可以找到 这里

> +
[ - >,>+< 
  ----- -----    ; minus 10
  [              ; if enters means it is not a \n
    +++++ +++++  ; restore prev value
    < 
  ] >>           ; moving forward
]
                 ; numbers are 0 0 49 0 50 0 51
                 ; for input 123
<<<<[<<]         ; moving to the beginning
>>               ; reaching first char
[.>>]            ; just printing till end

Actually I had posted the same code for a different question for a different purpose.
Here following code will keep on accepting the ASCII of whatever you type unless a newline character is met.Then prints what you typed.

Don't worry about the portability; I've already implemented addition of two n-digit numbers with this strategy of reading numbers, you can find here.

> +
[ - >,>+< 
  ----- -----    ; minus 10
  [              ; if enters means it is not a \n
    +++++ +++++  ; restore prev value
    < 
  ] >>           ; moving forward
]
                 ; numbers are 0 0 49 0 50 0 51
                 ; for input 123
<<<<[<<]         ; moving to the beginning
>>               ; reaching first char
[.>>]            ; just printing till end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文