为什么按回车输入还不停止

为什么按回车输入还不停止


```c++
#include
#include
using namespace std;

class Set
{
public:
    Set(){
        this->length=0;
        memset(this->Setnum,0,sizeof(Setnum));
        cout<<"默认构造函数"<Set(int *n){
        for(int i=0;i<_msize(n)/sizeof(*n);i++)
            this->addnum(n[i]);
        cout<<"普通构造函数"<Set(Set &s){
        length=s.length;
        for(int i=0;i"拷贝构造函数"<Set(){
        cout<<"析构函数"<Set jiaoji(Set &s2);
    Set bingji(Set &s2);
    Set chaji(Set &s2);
    bool equals1(Set &s2);
    bool ifinclude(Set &s2);
    bool ifempty();
    int numsize();
    void beempty();

private:
    int Setnum[100];
    int length=0;
};

void Set::addnum(int num){
    int flag=0;
    for(int i=0;iif(Setnum[i]==num){
            flag=1;
            break;
        }
    }
    if(flag==0){
        Setnum[length]=num;
        length++;
    }
}

void Set::delnum(int num){
    int n=length;
    while(n--){
        if(Setnum[n]==num)
            break;
    }
    for(int i=n;ifor(int i=0;i" ";
    }
    cout<Set Set::jiaoji(Set &s2){
    Set s;
    for(int i=0;ilength;i++){
        s.Setnum[i]=this->Setnum[i];
        s.length=this->length;
    }
    for(int i=0;iSet Set::bingji(Set &s2){
    Set s;
    for(int i=0;ilength;i++){
        for(int j=0;jif(this->Setnum[i]==s2.Setnum[j]){
                s.Setnum[i]==s2.Setnum[j];
                s.length++;
            }
        }
    }
}

Set Set::chaji(Set &s2){
    int flag=0;
    Set s,s3;
    s3=this->bingji(s2);
    for(int i=0;ilength;i++){
        flag=0;
        for(int j=0;jif(this->Setnum[i]==s3.Setnum[j]){
                flag=1;
                break;
        }
    }
        if(flag==0){
            s.Setnum[s.length]=this->Setnum[i];
            s.length++;
        }
    }
    return s;
}

bool Set::equals1(Set &s2){
    if(this->length!=s2.length)
        return false;
    for(int i=0;ilength;i++){
        if(this->Setnum[i]!=s2.Setnum[i])
            return false;
    }
    return true;
}

bool Set::ifinclude(Set &s2){
    Set *s3=new Set();
    *s3=this->bingji(s2);
    if(s3->length==this->length||s3->length==s2.length)
        return true;
    else
        return false;
}

bool Set::ifempty(){
    if(this->length==0)
        return true;
    else
        return false;
}

int Set::numsize(){
    return this->length;
}

void Set::beempty(){
    memset(Setnum,0,sizeof(Setnum));
    this->length=0;
}

int main()
{
    Set s1;
    char *ch='ch';
    int num;
    cout<<"Enter multiple numbers"<while(ch!='endl'){
        cin>>num;
        s1.addnum(num);
        cin>>ch;
    }
    s1.shownum();
}





```

参考GPT和自己的思路:根据你贴出的代码,我看到你在输入数字时使用了一个 while 循环,判断的条件是 ch 不等于 "endl",但是 ch 先被赋值为了一个 char 指针 "ch",并没有进行正确的赋值。正确的做法应该是将 ch 声明为字符类型,然后在循环中使用 getchar() 函数进行输入,直到输入的字符为回车键时跳出循环即可。修改后的代码如下:

int main()
{
    Set s1;
    char ch;
    int num;
    cout<<"Enter multiple numbers"<<endl;
    while((ch=getchar())!='\n'){
        cin>>num;
        s1.addnum(num);
    }
    s1.shownum();
}

这样输入数字后按回车即可停止输入。

你这都写的啥啊。


int main()
{
    Set s1;
    char ch;
    int num;
    cout<<"Enter multiple numbers"<<endl;
    while(ch!='\n'){
        cin>>num;
        s1.addnum(num);
        cin>>ch;
    }
    s1.shownum();
}

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/261227
  • 这篇博客你也可以参考下:线程方法中有异常时线程的停止
  • 除此之外, 这篇博客: 计算机组成原理(7)定点运算中的 不恢复余数法 (恢复余数法的改良,又称加减交替法) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    加减交替法

    1. 符号位不参加运算,并要求|x|<|y|
    2. 先用被除数减去除数
      • 当余数为正时,商上1,余数左移一位,再减去除数。
      • 当余数为负时,商上0,余数左移一位,再加上除数。
    3. 当第n+1步余数为负时需加上 |y| 得到第n+1步正确的余数,最后余数为rn×2(-n)(余数与被除数同号)。

    特点

    • 上商n+1次
    • 第一次上商判断是否溢出
    • 移动n次,加n+1次
    • 用移位的次数判断出发是否结束

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^