gpt4 book ai didi

如果一次性直接输入,C++ 程序将失败

转载 作者:太空宇宙 更新时间:2023-11-04 08:27:55 25 4
gpt4 key购买 nike

<分区>

抱歉,如果标题不完善,我不确定如何表达。

我正在为类编写一个程序,该程序通过了我的所有测试,但未通过一些测试用例。一些研究表明,事物的输入方式很重要。

如果我输入这样的东西:00第一的0第二0第三0第四0第五1个第六1个第七1个第八(进入)1个第九4个(输入)

或者在每行之间按回车键,一切都很好。但是,如果我复制/粘贴整组输入(或像我们的评分那样用 cin>> 将其输入),我会发现在随机输出(4 个触发器输出)或段错误 11 之间变化的怪异现象。

这是主要方法(不能改变这个)

    #include"tDeque.h"
using namespace std;

template <typename T>

void test (T s) {
Deque<T> *DQ = new Deque<T>();
T input;
int op=0;

while (op<7)
{

std::cin>> op;
switch(op) {
case 0:
std::cin>>input;
try {
DQ->push_front(input);
} catch (exception e) {
cout<<"Out of Memory Exception!"<<endl;
}
break;
case 1:
std::cin>>input;
try{
DQ->push_back(input);
} catch (exception e) {
cout<<"Out of Memory Exception!"<<endl;
}
break;
case 2:
try{
std::cout<<DQ->pop_front()<<std::endl;
} catch (exception e) {
cout<<"Caught Exception for empty stack!"<<endl;
}
break;
case 3:
try{
std::cout<<DQ->pop_back()<<std::endl;
} catch (exception e) {
cout<<"Caught Exception for empty stack!"<<endl;
}
break;
case 4:
std::cout<<DQ->toStr();
break;
case 5:
std::cout<<DQ->size()<<std::endl;
break;
case 6:
std::cout<<DQ->empty()<<std::endl;
break;
}
}
}


int main(int argc, char **argv) {
int op=0;
std::string input;
int type;
cin>>type;
string s = "tDeque";
switch(type) {
case 0:
test(s);
break;
case 1:
test(3.2);
break;
case 2:
test(1);
break;
default:
return 1;
}

return 0;

}

这是我的一些代码(我一直在尝试在代码中打断。它有帮助,但不能解决我的问题)

void Deque<T>::needIGrow()
{
try {


if(front==arrSize-1 &&back==0)
{ //we need a new array double in size
T *arr2=new T[arrSize*2];
//we watn to be 1/3 of the way up the new array to start, and the new array is double in size.
int newBack=arrSize*2/3;
int oldFront=front;
for(int i=back;i<=oldFront;i++)
{ //Transfer old contents to new array
arr2[i+newBack]=arr[i];
front=newBack+i;
}
// cout << arr2[newBack];
back=newBack;
//saftey check. Rounded numbers can be a pain

// delete [] arr;
//update arraysize
arrSize=arrSize*2;
//transfer over the array
arr=arr2;
// delete [] arr2;
}
}
catch (bad_alloc ex) {
delete[] _emergencyMemory;

cerr << "Out of memmory while growing array";
exit(1);
}


}

template <typename T>
void Deque<T>::needIShiftLeft()
{
try
{
//if the left has room but the right doesnt, shift left. Do so far enough to move all the way to the middle to minimize this opperation.
if(front>=arrSize-2)
{
int shiftLeftBy=back/2;
for(int i=0;i+shiftLeftBy<arrSize;i++)
{
arr[i]=arr[i+shiftLeftBy];
}


back=back-shiftLeftBy;
front=front-shiftLeftBy;
}
}
catch (bad_alloc ex) {
delete[] _emergencyMemory;

cerr << "Out of memmory while shifting array left";
exit(1);
}
}

template <typename T>
// Inserts the element at the front of the queue.
void Deque<T>::push_back(T item) {
sleepToFix();
try{
//ignore emtpy Ts. they will cause havok
//check if we need to grow. the false lets the method we are coming from a back push, so it knows what to check.
needIGrow();
//do we need to shift?
needIShiftLeft();
if(mySize==0)
{
arr[front]=item;
}
else{
front=front+1;
arr[front]=item;
}
mySize++;
}
catch (bad_alloc ex) {
delete[] _emergencyMemory;

cerr << "Out of memmory while pushing back";
exit(1);
}
}




// Removes and returns the element at the back of the queue.
template <typename T>
T Deque<T>::pop_front() {
sleepToFix();
if (mySize==0) {
throw range_error("Tried to pop front on empty stack");
}
try{
//same as pop back
T s=arr[back];
//arr[back]=NULL;
//handle the initial case of front=back, dont seperate them yet
if(front!=back)
{
back=back+1;
}
needIShrink();
if(mySize!=0)
mySize=mySize-1;
return s;}
catch (bad_alloc ex) {
delete[] _emergencyMemory;

cerr << "Out of memmory while popping front";
exit(1);
}
}

感谢您的帮助。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com