请教C++集合运算的做法
发布网友
发布时间:2023-07-08 09:14
我来回答
共5个回答
热心网友
时间:2024-08-26 04:34
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
int chang1=0,chang2=0,yuansuchang=0,yuansu=0,yuansushuliang1=0,yuansushuliang2=0,i=0,kongge=0,j=0,k=0,yiwei=1,t=0,buchongfushu=0;
char yuansu1[9998];/*定义元素A字符数数组*/
gets(yuansu1);
chang1=strlen(yuansu1);
int*shuzu1=new int[chang1];/*定义元素A字数组*/
for(i=0;i<chang1;i++)
{
shuzu1[i]=0;
}
i=0;
for(i=0;i<chang1;i++)
{
if(yuansu1[i]<=48||yuansu1[i]>=57)
{
continue;
}
if(yuansu1[i]>=48&&yuansu1[i]<=57)
{
yuansuchang++;
}
if((yuansu1[i]>=48&&yuansu1[i]<=57)&&(yuansu1[i+1]<48||yuansu1[i+1]>57))
{
for(j=i;j>i-yuansuchang;j--)
{
shuzu1[yuansu]=((yuansu1[j]-48)*yiwei)+shuzu1[yuansu];
yiwei=yiwei*10;
}
yiwei=1;
yuansu++;
yuansuchang=0;
}
}
yuansushuliang1=yuansu;
cout<<"数组A总共输入了"<<yuansushuliang1<<"个元素:"<<endl;
for(i=0;i<yuansu;i++)
{
cout<<shuzu1[i]<<endl;
}
cout<<"数组A输入完毕。"<<endl;/*数组A结束*/
/*数组B开始*/
yuansu=0;
char yuansu2[9998];/*定义元素B字符数数组*/
gets(yuansu2);
chang2=strlen(yuansu2);
int*shuzu2=new int[chang2];/*定义元素B字数组*/
for(i=0;i<chang2;i++)
{
shuzu2[i]=0;
}
i=0;
for(i=0;i<chang2;i++)
{
if(yuansu2[i]<=48||yuansu2[i]>=57)
{
continue;
}
if(yuansu2[i]>=48&&yuansu2[i]<=57)
{
yuansuchang++;
}
if((yuansu2[i]>=48&&yuansu2[i]<=57)&&(yuansu2[i+1]<48||yuansu2[i+1]>57))
{
for(j=i;j>i-yuansuchang;j--)
{
shuzu2[yuansu]=((yuansu2[j]-48)*yiwei)+shuzu2[yuansu];
yiwei=yiwei*10;
}
yiwei=1;
yuansu++;
yuansuchang=0;
}
}
yuansushuliang2=yuansu;
cout<<"数组B总共输入了"<<yuansu<<"个元素:"<<endl;
for(i=0;i<yuansushuliang2;i++)
{
cout<<shuzu2[i]<<endl;
}
cout<<"数组B输入完毕。"<<endl;/*数组B结束*/
int jiaojishu=0;/*1、A n B开始*/
for(i=0;i<yuansushuliang1;i++)
{
for(j=0;j<yuansushuliang2;j++)
{
if(shuzu1[i]==shuzu2[j])
{
{
jiaojishu++;
}
}
}
}cout<<jiaojishu<<endl;
int*jihe1=new int[jiaojishu];
for(i=0;i<jiaojishu;i++)
{
jihe1[i]=0;
}
for(i=0;i<yuansushuliang1;i++)
{
for(j=0;j<yuansushuliang2;j++)
if(shuzu1[i]==shuzu2[j])
{
if(shuzu1[i]==shuzu2[j])
{
for(k=0;k<jiaojishu;k++)
jihe1[k]=shuzu2[j];
}
}
}
cout<<"A n B:"<<endl;
for(i=0;i<jiaojishu;i++)
{
cout<<jihe1[i]<<endl;
}/*1、A n B结束*/
for(i=0;i<yuansushuliang1;i++)
{
for(j=0;j<yuansushuliang2;j++)
{
if(shuzu1[i]!=shuzu2[j])
{
buchongfushu++;
}
}
}cout<<"[]"<<buchongfushu<<endl;
int*jihe2=new int[yuansushuliang1+buchongfushu];/*2、A U B开始*/
for(i=0;i<(yuansushuliang1+buchongfushu);i++)
{
jihe2[i]=0;
}
for(i=0;i<yuansushuliang1;i++)
{
jihe2[i]=shuzu1[i];
}
for(i=0;i<yuansushuliang2;i++)
{
{
for(j=0;j<buchongfushu;j++)
{
if(jihe2[i]!=shuzu2[j])
{
jihe2[yuansushuliang1+j]=shuzu2[j];
}
}
}
}
cout<<"A U B:"<<endl;
for(i=0;i<(yuansushuliang1+buchongfushu);i++)
{
cout<<jihe2[i]<<endl;
}/*1、A U B结束*/
int*jihe3=new int[yuansushuliang1];/*3、A-B开始*/
for(i=0;i<(yuansushuliang1+buchongfushu);i++)
{
jihe3[i]=0;
}
for(i=0;i<yuansushuliang1;i++)
{
jihe3[i]=shuzu1[i];
}
for(i=0;i<yuansushuliang1;i++)
{
for(j=0;j<yuansushuliang2;j++)
if(jihe3[i]==shuzu2[j])
{
jihe3[i]=0;
}
}
cout<<"A-B:"<<endl;
for(i=0;i<yuansushuliang1;i++)
{
cout<<jihe3[i]<<endl;
}/*3、A-B结束*/
return 0;
}
热心网友
时间:2024-08-26 04:34
是数组当中求这些吗?
是纯数值的吗?
请补充下
热心网友
时间:2024-08-26 04:35
程序太麻烦了
给你点一下, 可以把字母用强制转换函数转换后,再进行运算。就可以了。。
热心网友
时间:2024-08-26 04:35
写一个太麻烦了,引用一个~~
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main()
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
set<int> S( a, a + 9 );
int b[] = { 3, 6, 8, 9 };
set<int> S2( b, b + 4 );
set<int>::iterator site;
set<int> Su;
set<int> Si;
set<int> Sd;
set<int> Ssd;
//交集
set_intersection( S.begin(), S.end(),
S2.begin(), S2.end(),
inserter( Si, Si.begin() ) );
//并集
set_union( S.begin(), S.end(),
S2.begin(), S2.end(),
inserter( Su, Su.begin() ) );
//差集
set_difference( S.begin(), S.end(),
S2.begin(), S2.end(),
inserter( Sd, Sd.begin() ) );
//对称差集
set_symmetric_difference( S.begin(), S.end(),
S2.begin(), S2.end(),
inserter( Ssd, Ssd.begin() ) );
site = Si.begin();
cout<<"the intersection of S and S2 is : ";
while( site != Si.end() )
{
cout<< *site <<" ";
++ site;
}
cout<<endl;
site = Su.begin();
cout<<"the union of S and S2 is : ";
while( site != Su.end() )
{
cout<< *site <<" ";
++ site;
}
cout<<endl;
site = Sd.begin();
cout<<"the difference of S and S2 is : ";
while( site != Sd.end() )
{
cout<< *site <<" ";
++ site;
}
cout<<endl;
site = Ssd.begin();
cout<<"the symmetric difference of S and S2 is : ";
while( site != Ssd.end() )
{
cout<< *site <<" ";
++ site;
}
cout<<endl;
return 0;
}
参考资料:http://blog.sina.com.cn/s/blog_4c98b9600100az2v.html
热心网友
时间:2024-08-26 04:36
#include<iostream>
#include<fstream>
using namespace std;
#define maxsize 64
typedef struct
{
char data[maxsize];
int top;
}seqstack;
seqstack head1,head2,head3;
//char a,b,c;
void setnull(seqstack *s)
{
s->top=-1;
}
int empty(seqstack *s)
{
if(s->top>=0) return 1;
else return 0;
}
void push(seqstack *s,char x)
{
if(s->top==maxsize-1)
{
cout<<"栈已满"<<endl;
}
else
{
s->top++;
s->data[s->top]=x;
}
}
char pop(seqstack *s)
{
if(s->top==-1)
{
cout<<"空站"<<endl;
return NULL;
}
else
{
s->top--;
return (s->data[s->top+1]);
}
}
char top(seqstack *s)
{
//if(empty(s)) return NULL;
return(s->data[s->top]);
}
char jisuan1(char fuhao,char m2,char m1)
{
char jieguo;
if(fuhao=='+')
{
jieguo=m2*m1;
}
else if(fuhao=='-')
{
if(m1=='1'||m2=='1')
jieguo='1';
else jieguo='0';
}
else if(fuhao=='$')
{
if(m1=='1'&&m2=='0')
jieguo='0';
else jieguo='1';
}
return jieguo;
}
char jisuan2(char fuhao,char m)
{
if(m='1')
return '0';
else return '1';
}
void main()
{
char biaodashi[21];
char a,b,c;
cout<<"输入a"<<endl;
cin>>a;
cout<<"输入b"<<endl;
cin>>b;
cout<<"输入c"<<endl;
cin>>c;
ifstream in("e:\\input.txt",ios_base::binary);
in.read((char *)&biaodashi[0],sizeof(biaodashi));
in.close();
int i=0;
setnull(&head1);
setnull(&head2);
setnull(&head3);
while(biaodashi[i]!='@')
{
if(biaodashi[i]=='('||biaodashi[i]=='!'||biaodashi[i]=='+'||biaodashi[i]=='-'||biaodashi[i]=='#'||biaodashi[i]=='$')
{
push(&head1,biaodashi[i]);
}
else if(biaodashi[i]=='a'||biaodashi[i]=='b'||biaodashi[i]=='c')
{
if(biaodashi[i]=='a')
biaodashi[i]=a;
else if(biaodashi[i]=='b')
biaodashi[i]=b;
else if(biaodashi[i]=='c')
biaodashi[i]=c;
push(&head2,biaodashi[i]);
}
else if(biaodashi[i]==')')
{
char s='\0';
//if(empty(&head2)==0)
//{
push(&head3,pop(&head2));
//}
//if(empty(&head1)==0)
s=pop(&head1);
while(1)
{
if(s=='(')
{
if(top(&head1)=='!')
{
push(&head3,pop(&head1));
char jieguo2=jisuan2(pop(&head3),pop(&head3));
push(&head3,jieguo2);
}
break;
}
else
{
if(empty(&head2)==0)
push(&head3,pop(&head2));
push(&head3,s);
char jieguo1=jisuan1(pop(&head3),pop(&head3),pop(&head3));
//push(&head3,jieguo1);
push(&head2,jieguo1);
}
s=pop(&head1);
}
}
cout<<biaodashi[i];
i++;
}
char jieguo=pop(&head3);
cout<<jieguo<<endl;
}
文件内容
((!(a+b)#(a-b)))$c)类似的。