UVA_446_Kibbles "n" Bits "n" Bits "n" Bits

来源:互联网 发布:右友是一款什么软件 编辑:程序博客网 时间:2024/06/04 18:08
#include<iostream>    #include<sstream>    #include<string>    #include<vector>    #include<list>    #include<set>    #include<map>    #include<stack>    #include<queue>    #include<algorithm>  #include<bitset>#pragma warning(disable:4996)    using std::cin;using std::cout;using std::endl;using std::stringstream;using std::string;using std::vector;using std::list;using std::pair;using std::set;using std::multiset;using std::map;using std::multimap;using std::stack;using std::queue;using std::priority_queue;using std::bitset;int HexToDecimal(const string &hex){int ret = 0;for (size_t i = 0; i < hex.size(); i++){ret *= 16;switch (hex[i]){case '0':ret += 0; break;case '1':ret += 1; break;case '2':ret += 2; break;case '3':ret += 3; break;case '4':ret += 4; break;case '5':ret += 5; break;case '6':ret += 6; break;case '7':ret += 7; break;case '8':ret += 8; break;case '9':ret += 9; break;case 'A':ret += 10; break;case 'B':ret += 11; break;case 'C':ret += 12; break;case 'D':ret += 13; break;case 'E':ret += 14; break;case 'F':ret += 15; break;default:break;}}return ret;}int main(){//freopen("input.txt", "r", stdin);    //freopen("output.txt", "w", stdout);int T;while (cin >> T){cin.get();while (T--){string hex1, Operator, hex2;cin >> hex1 >> Operator >> hex2;auto decimal1 = HexToDecimal(hex1);auto decimal2 = HexToDecimal(hex2);cout << bitset<13>(decimal1) << ' '<< Operator<<' ' << bitset<13>(decimal2) << " = ";if (Operator == "+"){cout << decimal1 + decimal2 << endl;}else{cout << decimal1 - decimal2 << endl;}}}return 0;}

0 0
原创粉丝点击