所以我的实验室基本上是从 cin 中取出一个字符串,将它分成组件(分成各种字符),然后使用按位运算符对所有字符进行组件总和。最后,打印出结果。这就是我所拥有的。
输入第一个字符串后立即出现分段错误。
[编辑]现在运行没有 segFaults,但我得到结果 = 0、aInt = 0 和 bInt = 0。不知道为什么?我输入了 a = hello 和 b = world
using namespace std;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
int main()
{
string a, b;
char *aStopstring, *bStopstring;
unsigned long aInt, bInt;
cout<<"Please enter a string: "<<endl;
cin>> a;
const char* aString = a.c_str();
cout<<"Please enter another string: "<<endl;
cin>> b;
const char* bString = b.c_str();
aInt = strtoul(aString, &aStopstring, 2);
bInt = strtoul(bString, &bStopstring, 2);
cout<<aInt<< " " << bInt<<endl;
unsigned int c = aInt&bInt;
unsigned int d = aInt^bInt;
c = c>>1;
unsigned int result = c^d;
cout<<"The sum is: "<< (int)result <<endl;
return 1;
}