寫出一個程序,接受一個十六進制的數,輸出該數值的十進制表示。(多組同時輸入 )


#include <iostream> #include <string> #include <cmath> int main() { std::string hex; while(std::cin>>hex) { int sum=0, flag=0; for(std::string::const_reverse_iterator cit=hex.rbegin(); cit!=hex.rend()-2; ++cit) { int digit; if(*cit>='A' && *cit<='F') { digit = *cit - 'A' + 10; } else if(*cit>='a' && *cit<='f') { digit = *cit - 'a' + 10; } else { digit = *cit - '0'; } sum += pow(16, flag) * digit; flag++; } std::cout<<sum<<std::endl; } return 0; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM