这一题自己花的时间有点多。
要注意几个问题:
1,注意题目的输入格式,小数点就在固定的位数上,
2,输入用整个字符串读取
3,正号不输出
4 ,考虑正指数和负指数两种情况就行了
#include <iostream>
#include <cstring>
using namespace std;
char a[20000];
int E; //大E的位置
int exp(int n) //找大E的位置
{
int temp = 0;
int i;
for(i = 0;i<n;i++)
if(a[i] == 'E'){
E = i;
break;
}
bool flag = false;
for(i++;i<n;i++)
{
if(a[i] == '-')
flag = true;
if(a[i] >= '0' && a[i] <= '9')
temp = temp*10 + a[i] -'0';
}
if(flag)
temp *= -1;
return temp;
}
int main()
{
scanf("%s",a);
int n = strlen(a);
int i = 2;
int ex = exp(n);
bool flag = true;
if(ex < 0)
{
flag = false;
ex *= -1;
}
if(a[0] == '-')
cout << a[0];
if(ex == 0)
{
for(int j = 1;j < E;j++)
cout << a[j];
return 0;
}
if(flag)
{
int j = 1;
for(;j<i;j++)
cout << a[j];
i++;
while(ex--)
{
if(i<E)
cout << a[i++];
else
cout << 0;
}
if( i < E){
cout << ".";
while(i < E)
cout << a[i++];
}
}
else
{
cout << "0.";
ex = ex + 1 - i;
while(ex--)
cout << 0;
cout << a[1];
i++;
while(i < E)
cout << a[i++];
}
cout << endl;
return 0;
}
本文详细解析了一段使用C++处理包含指数形式的复杂数值输入的程序,重点在于理解并优化输入解析过程,包括处理小数点位置、正负号以及正负指数的识别与转换。通过实例分析,展示了如何有效读取和处理此类数据,以提升程序的效率和准确性。
2767

被折叠的 条评论
为什么被折叠?



