您的位置:首页 > 其它

PAT-A1028. List Sorting (25)(排序.)

2016-03-03 21:13 323 查看
水。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <stdlib.h>
using namespace std;

#define MAX 10000+100

int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
char str[MAX], zhishu[MAX], xiaoshu[MAX];
memset(xiaoshu, '\0', sizeof(xiaoshu));
memset(zhishu, '\0', sizeof(zhishu));
int i = 0, j = 0, f = 0, k = 0; //k是小数中小数点后个位数
cin >> str;
for (i = 1, j = 0; str[i] != 'E'; i++ )
{
xiaoshu[j++] = str[i];
k++;
if (str[i] == '.')
k = 0;
}

i++;
if (str[i] == '-')
f = 1;
for (i++, j = 0; str[i] != '\0'; i++)
zhishu[j++] = str[i];

//指数为负数
if (f == 1)
{
if ('-' == str[0])
cout << "-";
if (atoi(zhishu) == 0)
{
for (i = 0; xiaoshu[i] != '\0'; i++)
cout << xiaoshu[i];
}
else
{
cout << "0.";
for (i = 0; i < atoi(zhishu)-1; i++)
cout << "0";
for (i = 0; xiaoshu[i] != '\0'; i++)
if (xiaoshu[i] != '.')
cout << xiaoshu[i];
}
cout << endl;
}
//指数为正
else
{
if ('-' == str[0])
cout << "-";
if (atoi(zhishu) == 0)
{
for (i = 0; xiaoshu[i] != '\0'; i++)
cout << xiaoshu[i];
}
else
{
if ( k <= atoi(zhishu))
{
for (i = 0; xiaoshu[i] != '\0'; i++)
if (xiaoshu[i] != '.')
cout << xiaoshu[i];
for(i = 0; i < atoi(zhishu)-k; i++)
cout << "0";
}
else
{
//yiwei
for (i = 0; xiaoshu[i] != '\0'; i++)
if (xiaoshu[i] == '.')
break;
int j = i;
for (; i < j+atoi(zhishu); i++)
xiaoshu[i] = xiaoshu[i+1];
xiaoshu[j+atoi(zhishu)] = '.';
cout << xiaoshu;
}
}
cout << endl;

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: