fwkkkk 2021-05-15 23:08 采纳率: 66.7%
浏览 28
已结题

c++题目,可以指点一下这道题吗?想了特别久,样例已经实现了但是交的时候是runtime error

Description

 

科学记数法是一种记录大数字的简单方法,但是无聊的栗酱修改了规则,这种记数方式可以把大数字用形如 [+/-][B].[C]E[+/-][D] 的字符串表示,其中B,C,DB,C,D的长度分别满足:|B|\le300,|C|\le300,|D|\le5∣B∣≤300,∣C∣≤300,∣D∣≤5,BB保证无前导零,且输入都满足以上格式。

但是这种表示形式过于复杂,栗酱已经不知道如何把它还原成原来的数了,所以聪明的你帮帮栗酱吧。

Input

 

第一行输入数据组数TT。

接下来有TT行,每行输入一行非空字符串ss。

T \le 20T≤20

|s|\le300∣s∣≤300

Output

 

每组数据一行,输出还原后的数。

Sample Input 1 

3
+123.4E+03
-123.40E-4
+123.4000E+2

Sample Output 1

123400
-0.012340
12340.00
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
	int i, j, k,l;
	int T;
	cin >> T;
	for (i = 0; i < T; i++) {
		string s;
		string n1 = "";
		int n = 0;
		char a, b;
		cin >> s;
		for (j = 0; j < s.length(); j++) {
			if (s[j] == '+' || s[j] == '-') {
				a = s[j]; continue;
			}
			if (s[j] == '.') {
				l = j; continue;
			}
			if (s[j] == 'E')break;
			n1 += s[j];
		}
		for (k = j + 1; k < s.length(); k++) {
			if (s[k] == '+' || s[k] == '-')b = s[k];
			else n = n * 10 + s[k] - '0';
		}
		int ci;
		ci = 0-(j - l - 1);
		int sum;
		if (b =='-')sum = ci - n;
		else sum = ci + n;
		double number = stoi(n1);
		int d = log10(number) + 1;
		int t = abs(sum);
		cout << a;
		if (sum >= 0) {
			number = number * pow(10, sum);
			cout<< number << endl;
		}
		else {
			if (t >= d) {
				cout << "0.";
				for (int i = 0; i < t - d; i++)cout << '0';
				cout << number << endl;
			}
			else {
				cout << n1.substr(0,d-t) <<'.'<< n1.substr(d - t) <<endl;
			}
		}
	


		
	}
		

}
  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-05-16 00:24
    关注

    代码如下,如有帮助,请采纳一下,谢谢。

    #include <stdio.h>
    #include <string>
    
    using namespace std;
    
    string trans(char* buf)
    {
    	string s = buf;
    	int len = strlen(buf);
    	int indexe = s.find('E');
    	char c = buf[indexe + 1]; //正还是负
    
    	//E后面的数字
    	string strnmb = s.substr(indexe+2,s.length() - indexe -1);
    	int nb = atoi(strnmb.c_str()); 
    
    	string strout = "";
    	//数据部分
    	string dd = "";
    	if (buf[0] == '+' )
    	{
    		dd = s.substr(1,indexe -1);
    	}
    	else if (buf[0] == '-')
    	{
    		dd = s.substr(1,indexe -1);
    		strout = "-";
    	}
    	else
    		dd = s.substr(0,indexe);
    	
    	//查找数据部分小数点的位置
    	int indexdot = dd.find('.');
    	if (indexdot > 0 && indexdot < dd.length())
    	{
    		string zs = dd.substr(0,indexdot);
    		string xs = dd.substr(indexdot+1,dd.length() - indexdot -1);
    		//有小数点
    		if(c == '+') //小数点右移动
    		{
    			string sm = "";
    			if (xs.length() > nb)
    			{
    				sm = xs.substr(0,nb);
    				strout = strout + zs + sm + "." + xs.substr(nb-1,xs.length() -nb);
    			}else if (xs.length() == nb)
    			{
    				sm = xs;
    				strout = strout + zs + sm;
    			}else
    			{
    				sm = xs;
    				while(sm.length()< nb)
    					sm += "0";
    				strout = strout + zs + sm;
    			}
    		}else if (c == '-') //小数点左移
    		{
    			string sm = "";
    			if (zs.length() > nb)
    			{
    				sm = zs.substr(0,zs.length() - nb) + "." + zs.substr(zs.length() - nb-1,nb);
    				strout = strout + sm + xs;
    			}else if (zs.length() == nb)
    			{
    				sm = "0."+ zs;
    				strout = strout + sm + xs;
    			}else
    			{
    				sm = zs;
    				while(sm.length() < nb)
    					sm = "0" + sm;
    				sm = "0." + sm;
    				strout = strout + sm + xs;
    			}
    		}
    	}else
    	{
    		//没有小数点
    		if (c == '+') //右移
    		{
    			string sm = "";
    			while (sm.length() < nb)
    				sm += "0";
    			strout = strout + sm;
    		}else if (c == '-')
    		{
    			string sm = "";
    			if (dd.length() > nb)
    			{
    				sm = dd.substr(0,dd.length() - nb) + "." + dd.substr(dd.length() - nb -1,nb);
    				strout = strout + sm;
    			}else if (dd.length() == nb)
    			{
    				strout = strout + "0." + dd;
    			}else
    			{
    				while(dd.length() < nb)
    					dd = "0"+dd;
    				strout = strout + "0." + dd;
    			}
    		}
    
    	}
    
    
    
    	return strout;
    }
    
    
    int main()
    {
    	int nmb = 0;
    	scanf("%d",&nmb);
    	char buf[20][20] = {0};
    	for (int i = 0; i < nmb;i++)
    	{
    		scanf("%s",buf[i]);
    	}
    
    	for (int i = 0; i < nmb; i++)
    	{
    		printf("%s\n",trans(buf[i]).c_str());
    	}
    	
    
    
    
    	getchar();
    	getchar();
    	return 0;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月18日
  • 已采纳回答 9月10日

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?