没错,但不能实现

lrd408 2008-10-23 04:36:18
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> a,b;
bool decide=1;
int c=0;

cout<<"please Enter the first vector<int> (73767 end to)"<<endl;
cin>>c;
while(c<=32767)
{
a.push_back(c);
cin>>c;
}


cout<<"please Enter the second vector<int> (end to 32767)"<<endl;
cin>>c;
while(c<=32767) {
b.push_back(c);
cin>>c;
}

vector<int>::size_type size1=a.size(),size2=b.size(),ix;
for(ix=0;ix!=(size1>size2 ? size2 : size1);ix++)
if(a[ix]!=b[ix])
{decide=0;
break;
}
if(decide==1) {
if(size1==size2)
cout<<"a is equal to b!"<<endl;
if(size1>size2)
cout<<"b is a prefix of a!"<<endl;
else cout<<"a is a prefix of b!"<<endl;
}
else {
if(size1>=size2)
cout<<"b is not a prefix of a!"<<endl;
else cout<<"a is not a prefix of b!"<<endl;
}

return 0;
}






这是c++ rimer上的一个联系,我变得还行,可没有这个水平高
自己试着把这个写了一遍,可运行时:输入玩第一组数后,连续多次按ctrl+Z,后又按Enter
再按ctrl+z,按说应该出现:please Enter the second vector<int> (end to 32767)
可是到这里就只有光标闪什么也没有
题目:::给出两个 int 型的 vector 对象,编写程序判断一个对象是否是另一个对象的前缀。如果两个 vector 对象的长度不相同,假设较短的 vector 对象长度为 n,则只对这两个对象的前面 n 个元素做比较。例如,对于 (0, 1, 1, 2) 和 (0, 1, 1, 2, 3, 5, 8) 这两个 vector,你的程序应该返回 true
...全文
108 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
figo1885 2008-10-24
  • 打赏
  • 举报
回复
楼主脑袋秀逗了
moolf 2008-10-23
  • 打赏
  • 举报
回复
首先你要清楚在windows下先弄ctrl+z的实现原理,这里有骗文章你看看吧:http://blog.csdn.net/b271737818/archive/2008/10/23/3132763.aspx,
第二个是要看清你的循环结束条件,只有满足c<=32767第一个循环才会结束,才能进入第二个循环。在遇到第二个空的ctrl+z的时候输入流会终止,在我机子上的运行情况如下:
please Enter the first vector<int> (73767 end to)
123456^Z
^Z
please Enter the second vector<int> (end to 32767)
a is equal to b!
a is a prefix of b!
Press any key to continue
lrd408 2008-10-23
  • 打赏
  • 举报
回复
我以前看的是c++ 经典入门 那里的程序不太常用while(cin>>b),我也从没用过,
感觉这个形式既简单用实用
可''''''''',我感觉我哪也出毛病了
很惭愧
tienchiu 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hai040 的回复:]
while(c <=32767)
退出条件是c>32767而不是cin.fail()
输入ctrl-z会置cin的failbit和eofbit
令cin>>c;不执行输入
c的值不会再变,造成死循环
[/Quote]


我都忘了这个了,呵呵,看来得加强基础了
K行天下 2008-10-23
  • 打赏
  • 举报
回复
lrd408
大家很热情 我就不再罗嗦了
elegant87 2008-10-23
  • 打赏
  • 举报
回复

//稍微修改一下!不错,继续努力吧!
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> a,b;
bool decide=1;
int c=0;

cout<<"please Enter the first vector<int> ( end to 32767)"<<endl;
cin>>c;
while(c<32767)
{
a.push_back(c);
cin>>c;
}


cout<<"please Enter the second vector<int> (end to 32767)"<<endl;
cin>>c;
while(c<32767)
{
b.push_back(c);
cin>>c;
}

vector<int>::size_type size1=a.size(),size2=b.size(),ix;
for(ix=0;ix!=(size1>size2 ? size2 : size1);ix++)
if(a[ix]!=b[ix])
{
decide=false;
break;
}
if(decide)
{
if(size1==size2)
cout<<"a is equal to b!"<<endl;
else
if(size1>size2)
cout<<"b is a prefix of a!"<<endl;
else
cout<<"a is a prefix of b!"<<endl;
}
else
{
if(size1>=size2)
cout<<"b is not b prefix of a!"<<endl;
else
cout<<"a is not a prefix of b!"<<endl;
}
system("pause");
return 0;
}



hai040 2008-10-23
  • 打赏
  • 举报
回复
while(c<=32767)
退出条件是c>32767而不是cin.fail()
输入ctrl-z会置cin的failbit和eofbit
令cin>>c;不执行输入
c的值不会再变,造成死循环
hz_80 2008-10-23
  • 打赏
  • 举报
回复
ctrl+z 是多少??比32767大吗?如果没有32767大的话,那还在第一个输入循环中,还没有进入到第二组输入的循环中.
taodm 2008-10-23
  • 打赏
  • 举报
回复
cout<<"please Enter the first vector<int> (73767 end to)"<<endl;
那么这一句你认真看了吗?
lrd408 2008-10-23
  • 打赏
  • 举报
回复
please Enter the second vector <int> (end to 32767)
这句话都没出现
taodm 2008-10-23
  • 打赏
  • 举报
回复
cout<<"please Enter the second vector<int> (end to 32767)"<<endl;
你没认真看这个提示么?
lrd408 2008-10-23
  • 打赏
  • 举报
回复
的还行,可这就是答案啊
boys2002 2008-10-23
  • 打赏
  • 举报
回复
vector
taodm 2008-10-23
  • 打赏
  • 举报
回复
何不先看看《C++ Primer》配套的习题解答是怎么做的。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧