猫史档案馆


【请问此C++代码哪里错了】

用户:树林归来树林归来查看:20 回复:6 评论:20 创建时间:2022-08-25T22:31:15


#include <iostream>
#include <string>
using namespace std;

struct tSet{
   bool set[26];

   void input(){
     string s;
     cin>>s;
     for(int i = 0;i < s.size();i++){
        set[s[i] - 'a'] = true;
     }
   }

   void output(){
     for(int i = 0;i < 26;i++){
        if(set[i] == true){
          cout<<char(set[i] + 'a');
        }
        cout<<endl;
     }
   }

   tSet operator+(const tSet x)const{
      tSet tmp;
      for(int i = 0;i < 26;i++){
         tmp.set[i] = set[i] || x.set[i];
      }
      return tmp;
   }

   tSet operator-(const tSet x)const{
      tSet tmp;
      for(int i = 0;i < 26;i++){
         tmp.set[i] = set[i] || (!x.set[i]);
      }
      return tmp;
   }

   tSet operator*(const tSet x)const{
      tSet tmp;
      for(int i = 0;i < 26;i++){
         tmp.set[i] = set[i] && x.set[i];
      }
      return tmp;
   }
};

tSet A,B,C;
char op;
int n;

int main(){
   cin>>n;
   for(int i = 0;i < n;i++){
      A.input();
      cin>>op;
      B.input();
      if(op == '+') C = A + B;
      if(op == '-') C = A - B;
      if(op == '*') C = A * B;
      C.output();
   }
}

这是集合运算的代码,集合用字母代表,如:{a,c,d,f}就是acdf。(集合运算有并、差、交三种运算,详见百度去)

【输入】第一行输入要输入几行的集合运算,然后呢,下一行就按指定的行数写集合的运算。

如:

1

acdf - bcef

 

【输出】集合运算的结果

如:

输入上面的输入例子,则输出:

ad

 

但是为什么没有输出ad?啊啊啊啊啊啊啊啊

哪个大佬帮帮我!


回复

上一页1 页 / 共 1下一页
树林归来树林归来

都一天了,怎么还没人回复,呜呜

点赞0


评论


疯狂戴夫的杂货铺疯狂戴夫的杂货铺

应该是第三行

点赞0


评论


疯狂戴夫的杂货铺疯狂戴夫的杂货铺

结果是什么?

点赞0


评论


呵呵哒的小小李呵呵哒的小小李

这边建议好好再去学学C++(划掉)

 

代码有如下错误或歧义

 

1.就算是在struct里面,也不要使用set这样的名称,容易引起歧义

 

2.在定义的output函数中,你想要输出的是a~z中的字母,而你的set数组是bool类型,因此只能输出b,应改为

 

if (set[i] == true)
    cout << char(i + 'a');

 

3.在output函数的for循环中,你想实现的效果是输出在同一行,但是你把cout << endl写进了for循环里,应该挪到外面。综上,正确的output函数应该是:

 

void output(){
    for (int i=0; i<26; i++){
        if (set[i] == true){
            cout << char(i + 'a');
        }
    }
    cout << endl;
}

 

4.你对于集合间取差集的方式可能有些误解?A - B是由“在集合A中但不在集合B中的元素”组成的集合,因此对减号的重定义应改为:

 

tSet operator - (const tSet x) const{
    tSet 喵p;
    for (int i=0; i<26; i++){
        喵p.set[i] = set[i] && (!x.set[i]);
    }
    return 喵p;
}

 

5.请注意标准缩进应为4个空格(当然也有可能是bcm帖子编辑器的问题)

 

希望能帮到你。

点赞3


评论


不知道该叫啥的一只萌新不知道该叫啥的一只萌新

大屑MX太懒了,不想调代码 写了份新代码不知道对你有没有用(同一道题 应该没问题

#include <bits/stdc++.h>
using namespace std;
int ts;
struct node
{
    bool st[30];
    node operator+(const node &B)
    {
        node tmp;
        for(int i = 0; i < 26; i++)
        {
            tmp.st[i] = st[i] || B.st[i];
        }
        return tmp;
    }
    node operator-(const node &B)
    {
        node tmp;
        for(int i = 0; i < 26; i++)
        {
            tmp.st[i] = st[i] && (!B.st[i]);
        }
        return tmp;
    }
    node operator*(const node &B)
    {
        node tmp;
        for(int i = 0; i < 26; i++)
        {
            tmp.st[i] = st[i] && B.st[i];
        }
        return tmp;
    }
}de;
void solve()
{
    string as,bs,op;
    node a, b, c;
    for(int i = 0; i < 26; i++) a.st[i] = 0, b.st[i] = 0, c.st[i] = 0;
    cin>>as>>op>>bs;
    for(int i=0;i<as.size();i++){
        a.st[as[i]-'a']=1;
    }
    for(int i=0;i<bs.size();i++){
        b.st[bs[i]-'a']=1;
    }
    if(op == "+") c = a + b;
    else if(op == "-") c = a - b;
    else if(op == "*") c = a * b;
    for(int i = 0; i < 26; i++)
        if(c.st[i]) printf("%c", i + 'a');
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--) solve();
    return 0;
}

点赞0


评论


呵呵哒的小小李呵呵哒的小小李

给你整体重构了一下代码(直接从帖子复制出来的太丑了),昨天没注意到的是多组数据的时候应该记得把ABC都初始化一下。但是其他地方应该都没啥问题才对(雾

 

新的代码如下:

 

#include <iostream>
#include <string>
using namespace std;

struct tSet{
   bool set[26];
   void input(){
    	string s;
    	cin >> s;
    	for (int i=0; i<s.size(); i++){
			set[s[i] - 'a'] = true;
    	}
	}
	void output(){
		for (int i=0; i<26; i++){
        	if (set[i] == true){
				cout << char(i + 'a');
        	}
		}
		cout << endl;
	}
	tSet operator + (const tSet &x) const {
    	tSet tmp;
    	for (int i=0; i<26; i++){
        	tmp.set[i] = set[i] | x.set[i];
    	}
    	return tmp;
	}
	tSet operator - (const tSet &x) const {
		tSet tmp;
		for (int i=0; i<26; i++){
        	tmp.set[i] = set[i] & (!x.set[i]);
		}
		return tmp;
	}
	tSet operator * (const tSet &x) const {
    	tSet tmp;
    	for (int i=0; i<26; i++){
        	tmp.set[i] = set[i] & x.set[i];
		}
    	return tmp;
	}
};

tSet A,B,C;
char op;
int n;

int main(){
	cin >> n;
	for (int i=0; i<n; i++){
		for (int j=0; j<26; j++) A.set[j] = B.set[j] = C.set[j] = 0;
    	A.input();
    	cin >> op;
    	B.input();
    	if (op == '+') C = A + B;
    	if (op == '-') C = A - B;
    	if (op == '*') C = A * B;
    	C.output();
	}
	return 0;
}

点赞0


评论