我正在尝试对http://www.cstutoringcenter.com/problems/problems.php?id=2进行挑战,但在将科学计数法转换为十进制计数法时遇到问题,我需要将指数加在一起:到目前为止,这是我的代码:#define FIRST 2#define SECOND 20#include <iostream>#include <cmath>#include <sstream>#include <iomanip>using namespace std;int main(){ // Give variables initial setting int total = 0; float powanswer = 0; // Get rid of scientific notation for large numbers stringstream ss; ss.setf(ios::fixed); ss << pow(FIRST,SECOND); ss >> powanswer; // Output // Expected: 2^20 = 1048576 => 1+0+4+8+5+7+6 => 31 // Outcome: 2^20 = 1.04858e+06 => 1+.+0+4+8+5+8+e+++0+6 => 32 cout << FIRST << "^" << SECOND << " = " << powanswer << " => "; // Convert power int to string string ResultText = ""; stringstream convert; convert << powanswer; ResultText = convert.str(); // Loop over total for (int x=0; x<ResultText.size(); x++) { // Convert character to integer int ResultNum = 0; stringstream convert; convert << ResultText[x]; convert >> ResultNum; total+=ResultNum; // Output cout << ResultText[x]; ResultText.size()-1 == x ? cout << " => " : cout << "+"; } cout << total << endl; return 0;}我已经尝试过到处搜索如何转换它,并且我读到可以在流中使用如代码中所示,这是当前输出:2^20 = 1.04858e+06 => 1+.+0+4+8+5+8+e+++0+6 => 32我想要并且期望看到的是:2^20 = 1048576 => 1+0+4+8+5+7+6 => 31编辑:拼写错误 最佳答案 变量powanswer是float,但是您想将其打印为integer吗?怎么样:int powanswer_int = (int) powanswer;cout << FIRST << "^" << SECOND << " = " << powanswer_int << " => ";并在以后使用powanswer_int。关于c++ - 将科学计数法转换为十进制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8445258/ 10-12 16:04