C++ 我不知道';I don’我不知道如何存储大量表示为字符串的大数字
我正在做一个关于算法和数据结构的大学项目,在这个项目中,我必须处理非常大的整数并对它们执行运算。 我已经将数字实现为字符串,然后实现了操作的不同函数。 问题是当输入数据的数量太大时,应用程序会继续运行而不会出现任何错误 我试图理解为什么会发生这种情况,并制作了一个小main,其中我只将大的数字作为字符串存储在动态数组中,然后打印它们,但即使这样,也会一直运行,不会停止C++ 我不知道';I don’我不知道如何存储大量表示为字符串的大数字,c++,arrays,string,data-structures,biginteger,C++,Arrays,String,Data Structures,Biginteger,我正在做一个关于算法和数据结构的大学项目,在这个项目中,我必须处理非常大的整数并对它们执行运算。 我已经将数字实现为字符串,然后实现了操作的不同函数。 问题是当输入数据的数量太大时,应用程序会继续运行而不会出现任何错误 我试图理解为什么会发生这种情况,并制作了一个小main,其中我只将大的数字作为字符串存储在动态数组中,然后打印它们,但即使这样,也会一直运行,不会停止 int main(int argc, const char * argv[]) { int numOfNumbers;
int main(int argc, const char * argv[]) {
int numOfNumbers;
int numbersAdded=0;
string input;
string * arrayStrings;
cin>>numOfNumbers;
arrayStrings = new string[numOfNumbers];
while(numbersAdded<numOfNumbers){
cin>>input;
arrayStrings[numbersAdded]=input;
numbersAdded++;
}
for( int i = 0;i<numOfNumbers;i++){
cout<<arrayStrings[i]<<endl;
}
delete arrayStrings;
return 0;
}
您的具体问题: 您遇到的问题是,当您输入一个大于有符号整数所能容纳的数字时,
cin
将进入错误状态。这意味着对cin
的后续调用只有在调用cin.clear()
后才能工作
请在此处阅读:
要修复代码,请尝试在调用cin
后添加cin.clear()
,如下所示:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
int numOfNumbers;
int numbersAdded=0;
string input;
/* todo: use a vector<string> instead - which handles resizing and allocation for you */
string * arrayStrings;
if (!(cin >> numOfNumbers))
{
cout << "Warning: cin failed, did you enter a number larger than an numOfNumbers can hold? Value stored: " << numOfNumbers << endl;
/* todo: maybe stop execution here and report back? */
/* this will reset cin and leave numOfNumbers holding the largest value it can */
cin.clear();
}
arrayStrings = new string[numOfNumbers];
/* todo: check the allocation succeeded, otherwise stop and report the error */
while(numbersAdded<numOfNumbers){
if (!(cin >> input))
{
cout << "Warning: input failed, you entered a valid string? Value stored: " << input << endl;
/* this will reset cin and leave input holding the largest value it can */
cin.clear();
}
arrayStrings[numbersAdded]=input;
numbersAdded++;
}
for( int i = 0;i<numOfNumbers;i++){
cout<<arrayStrings[i]<<endl;
}
/* todo: this only deletes the array of pointers, not the strings themselves. Use a vector instead to avoid having to do memory management. */
delete arrayStrings;
return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]{
整数;
int numbersAdded=0;
字符串输入;
/*todo:改用向量-它为您处理大小调整和分配*/
字符串*数组字符串;
如果(!(cin>>数字))
{
cout我建议不要使用字符串来存储数字。在您的上下文中,字符串将占用更多空间,并且操作速度较慢
比如说,您想存储数字123456789001234567890。现在,如果您想使用字符串存储此数字,则需要20个字节(使用8位字符)。
字符串s=“123456789001234567890”
相反,你可以使用无符号整数数组。在大多数现代编译器和计算机体系结构中,整数是4字节的。因此它们可以表示数字直到4294967295。
现在让我们考虑最大的数字是十进制(所有数字是九),可以用无符号整数表示999999999。如果使用整数数组,现在只需要3个整数,这是12个字节。
无符号整数[]={234567890345678901,12}
现在考虑简单的算术运算加法。
无符号整数1[]={123456789,123456789};
无符号整数2[]={987654321,987654321};
现在,若你们想把以上两个数字相加,你们只需要2次迭代就可以得到你们的答案
如果达到位级别,您还可以进一步压缩已完成的内存足迹。这将是一项有点乏味的工作,但您将获得最佳性能。与上述内容无关:“如果您需要将“任何内容”存储为字符串您可以始终使用JSON、YAML或XML。这些格式是可移植的、自描述的,并且随时可用。delete arrayStrings;
调用未定义的行为-您分配了new[]
,而不是新的
为什么你不使用std::vector
的std::string
,即std::vector
?你把这当作一个学术练习来对待自己(一个有效的理由)?如果您的工作重点不是实现多精度整数运算,请使用已经存在的库,并进入工作的重要部分。boost::multiprecision
将处理此问题。当您修复UnholySheep提到的错误时,工作正常。删除阵列字符串;
->delete[]arrayStrings;
。你说“应用程序保持运行而不出现任何错误”是什么意思?对我来说这听起来是件好事。@JohnFilleau我不能使用现有的结构,任务说“你应该实现你自己的数据结构(你不应该使用STL库、向量等)。”我认为这项任务的重点是专门处理不适合整数的数字。非常感谢您的回复,我将尝试用此解决方案实现它。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
int numOfNumbers;
int numbersAdded=0;
string input;
/* todo: use a vector<string> instead - which handles resizing and allocation for you */
string * arrayStrings;
if (!(cin >> numOfNumbers))
{
cout << "Warning: cin failed, did you enter a number larger than an numOfNumbers can hold? Value stored: " << numOfNumbers << endl;
/* todo: maybe stop execution here and report back? */
/* this will reset cin and leave numOfNumbers holding the largest value it can */
cin.clear();
}
arrayStrings = new string[numOfNumbers];
/* todo: check the allocation succeeded, otherwise stop and report the error */
while(numbersAdded<numOfNumbers){
if (!(cin >> input))
{
cout << "Warning: input failed, you entered a valid string? Value stored: " << input << endl;
/* this will reset cin and leave input holding the largest value it can */
cin.clear();
}
arrayStrings[numbersAdded]=input;
numbersAdded++;
}
for( int i = 0;i<numOfNumbers;i++){
cout<<arrayStrings[i]<<endl;
}
/* todo: this only deletes the array of pointers, not the strings themselves. Use a vector instead to avoid having to do memory management. */
delete arrayStrings;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, const char * argv[])
{
unsigned int numOfNumbers;
vector<string> numberStrings;
cout << "How many numbers would you like to enter?" << endl;
if (cin >> numOfNumbers)
{
unsigned int i;
/* input numbers from console */
cout << endl << "Please enter " << numOfNumbers << " numbers:" << endl;
for (i = 0; i < numOfNumbers; i++)
{
string input;
cin >> input;
numberStrings.push_back(input);
}
/* print numbers out */
cout << endl << "You entered:" << endl;
for (const auto& num: numberStrings)
{
cout << num << endl;
}
}
else
{
cout << "Number of numbers must be a positive integer less than " << UINT_MAX << endl;
}
return 0;
} /* vector magically deallocates when it leaves scope */