1073 Scientific Notation (20分)/字符串处理

题目描述

在这里插入图片描述在这里插入图片描述

解析

由于正则表达式的特殊样式,找到其中字符E的位置,即可从正则表达式中将底数和指数部分分割出来,加上对STL string的熟练使用,就会很简单了。

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main() {
	string s, ans; cin >> s;
	if (s[0] == '-') cout << "-";
	int pos = s.find('E');
	int ex = stoi(s.substr(pos + 1)); //指数
	if (ex >= 0) {
		if (pos - 3 <= ex) {  //小数部分长度小于等于指数(需要补0)
			ans = s[1] + s.substr(3, pos - 3) + string(ex - pos + 3, '0');
		}
		else {
			ans = s[1] + s.substr(3, pos - 3);
			ans.insert(ex + 1, ".");
		}
	}
	else {
		ans = "0." + string(abs(ex) - 1, '0') + s[1] + s.substr(3, pos - 3);
	}
	cout << ans;
	return 0;
}
发布了62 篇原创文章 · 获赞 7 · 访问量 3091

猜你喜欢

转载自blog.csdn.net/weixin_43590232/article/details/104222815