c语言数组输入格式化,求优化方案(动态输入数组)

#include

using namespace std;

const double _inpArrMakenum(const bool mtag, const double integer, const int demical)

{

double _d = 0;

for(int _cpd = demical; _cpd; _cpd /= 10)

_d = (_d + double(_cpd % 10)) / 10.;

return mtag? -integer-_d : integer+_d;

}

double* input_array(int *arrLen=NULL)

{

vector v;

pair n = make_pair(0, 0);

bool mtag = false,ftag = false,ntag = false;

while(true)

{

int c = getchar();

if(c == '-')

{

if(ntag)

{

v.push_back(_inpArrMakenum(mtag, n.first, n.second));

n = make_pair(0, 0);mtag = ftag = ntag = false;

}

else

mtag = ntag = true;

}

else if(c == '.')

{

if(ftag)

{

v.push_back(_inpArrMakenum(mtag, n.first, n.second));

n = make_pair(0, 0);mtag = ftag = ntag = false;

}

else

ftag = ntag = true;

}

else if(c >= '0'&&c <= '9')

{

if(!ftag)

n.first *= 10,n.first += c-'0';

else if(n.second < 100000)

n.second *= 10,n.second += c-'0';

ntag = true;

}

else if(ntag)

{

v.push_back(_inpArrMakenum(mtag, n.first, n.second));

n = make_pair(0,0);mtag = ftag = ntag = false;

if(c == '\n')break;

}

}

int len = v.size();

double* array = new double[len];

for(int i = 0; i < len; i++)

array[i] = (double)v[i];

if(arrLen!=NULL)*arrLen=len;

return array;

}

int main()

{

int len;

double *a=input_array(&len);

for(int i=0;i

printf("%lf ",a[i]);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值