C++で「半加算器と全加算器」のプログラミングを教えてもらえないでしょうか?
初心者なので全然わかりません。
よろしくお願いします。
#include <utility>
#include <iostream>
#include <iomanip>
typedef std::pair<bool,bool> argument;
inline bool xor(argument x) {
  return x.first != x.second;
}
inline bool and(argument x) {
  return x.first && x.second;
}
inline argument half_add(argument x) {
  return argument(xor(x), and(x));
}
void evaluate(bool a, bool b) {
  std::cout << std::boolalpha;
  argument result = half_add(argument(a,b));
  std::cout << std::setw(5) << a <<  +  
            << std::setw(5) << b <<  =  
            << std::setw(5) << result.first <<  (carry =  
            << std::setw(5) << result.second << ) 
            << std::endl;
}
int main() {
  evaluate(false, false);
  evaluate( true, false);
  evaluate(false,  true);
  evaluate( true,  true);
  return 0;
}
/* 実行結果    
false + false = false (carry = false)
 true + false =  true (carry = false)
false +  true =  true (carry = false)
 true +  true = false (carry =  true)
*/
full_adder は half_adder 2個と xor 1個でできるはず。
> full_adder は half_adder 2個と xor 1個でできるはず。
おっとマチガイ。half_adder 2個と or 1個でできるはず。
xorやandは予約語だったように思いますが...
え? だったらなぜコンパイルできたんやろ?
gcc-3.2だと、
ファイル名:7: parse error before ~^' token
ファイル名:11: parse error before ~&&' token
ファイル名: In function ~argument half_add(std::pair<bool, bool>)':
ファイル名:16: parse error before ~^' token
となります。
C言語のiso646.hで定義されていたマクロはすべて予約語になったはずです。
もしかして、Visual C++を使われたとか?
> もしかして、Visual C++を使われたとか?
よく考えると、Visual C++の掲示板なので、Visual C++でコンパイルできればOKという
見方もありますね。
失礼しました。
なるほど、そういうことですか。了解しました。
logical_xor とかにしとけばよかったかな。
改版:
#include <utility>
#include <iostream>
#include <iomanip>
typedef std::pair<bool,bool> result_type;
inline bool logical_xor(bool x, bool y) { return x != y; }
inline bool logical_and(bool x, bool y) { return x && y; }
inline bool logical_or(bool x, bool y) { return x || y; }
inline result_type half_add(bool x, bool y) {
  return result_type(logical_xor(x,y), logical_and(x,y));
}
// ----------------------------------------
void half_evaluate(bool a, bool b) {
  std::cout << std::boolalpha;
  result_type result = half_add(a, b);
  std::cout << std::setw(5) << a <<  +  
            << std::setw(5) << b <<  =  
            << std::setw(5) << result.first <<  (carry =  
            << std::setw(5) << result.second << ) 
            << std::endl;
}
int main() {
  std::cout << half adder: << std::endl;
  half_evaluate(false, false);
  half_evaluate( true, false);
  half_evaluate(false,  true);
  half_evaluate( true,  true);
  return 0;
}
επιστημηさん、ぽちさん、どうもありがとうございました。
大変参考になりました。僕のような素人に知己会って頂いて感謝です。
本当に申し訳ないのですが、マルチポストという違反行為をしてしまいました。
パソコン初心者が注意書きも読まずに、勝手な事をして皆さんに迷惑をかけてしまい、
深く反省しています。
解決したらしいので'オマケ' : full_add を追加。
#include <utility>
#include <iostream>
#include <iomanip>
typedef std::pair<bool,bool> result_type;
inline bool logical_xor(bool x, bool y) { return x != y; }
inline bool logical_and(bool x, bool y) { return x && y; }
inline bool logical_or(bool x, bool y) { return x || y; }
inline result_type half_add(bool x, bool y) {
  return result_type(logical_xor(x,y), logical_and(x,y));
}
result_type full_add(bool x, bool y, bool carry) {
  result_type result1 = half_add(x, y);
  result_type result2 = half_add(result1.first, carry);
  return result_type(result2.first, logical_or(result1.second,result2.second));
}
// ----------------------------------------
void half_evaluate(bool a, bool b) {
  std::cout << std::boolalpha;
  result_type result = half_add(a, b);
  std::cout << std::setw(5) << a <<  +  
            << std::setw(5) << b <<  =  
            << std::setw(5) << result.first <<  (carry =  
            << std::setw(5) << result.second << ) 
            << std::endl;
}
void full_evaluate(bool a, bool b, bool c) {
  std::cout << std::boolalpha;
  result_type result = full_add(a, b, c);
  std::cout << std::setw(5) << a <<  +  
            << std::setw(5) << b <<  +  
            << std::setw(5) << c <<  =  
            << std::setw(5) << result.first <<  (carry =  
            << std::setw(5) << result.second << ) 
            << std::endl;
}
int main() {
  std::cout << half adder: << std::endl;
  half_evaluate(false, false);
  half_evaluate( true, false);
  half_evaluate(false,  true);
  half_evaluate( true,  true);
  std::cout << std::endl << full adder: << std::endl;
  full_evaluate(false, false, false);
  full_evaluate( true, false, false);
  full_evaluate(false,  true, false);
  full_evaluate( true,  true, false);
  full_evaluate(false, false,  true);
  full_evaluate( true, false,  true);
  full_evaluate(false,  true,  true);
  full_evaluate( true,  true,  true);
  return 0;
}
