调了一个下午还没有调好,估计今天脑袋要晕掉,过几天接着调。想想思路上有啥好的改进没
// Console08172.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <assert.h>
#include <math.h>
using namespace std;
const int N = 100;
void Calc2BigNum(int num1[], int len1, int num2[],int len2, int result[]);
int _tmain(int argc, _TCHAR* argv[])
{
int signed1 = 1;
int signed2 = 1;
//首先以字符形式存储
char s1[N];
char s2[N];
//转换为整数型先存储
int num1[N];
int num2[N];
//初始化数组
for (int initnum = 0; initnum < N; ++initnum)
{
s1[initnum] = '0';
s2[initnum] = '0';
num1[initnum] = -1;
num2[initnum] = -1;
}
//结果存放数组
int result[2*N] = {0};
//记录小数点位置
int dotloc1 = -1;
int dotloc2 = -1;
//记录小数点后的最终有效位置
int dot1Afterloc = 0;
int dot2Afterloc = 0;
//记录小数点后的位数
int dot1AfterNum = 0;
int dot2AfterNum = 0;
char ctemp;
int i;
cout << "请分别输入2个数(长度 <100),以#结束,若需要输入小数点,请以xxx.xxx的形式:\n" ;
cout << "\n请输入第1个数:" ;
cin >> ctemp;
if (ctemp == '-') { signed1 = -1;}
if (ctemp == '+') { signed1 = 1; }
else
{
assert(ctemp >='0' && ctemp <= '9');
num1[0] = ctemp - '0';
for (i = 1; i< N; ++i)
{
cin >> s1[i];
if (s1[i] == '#')
{
break;
}
if (s1[i] == '.')
{
dotloc1 = i;
num1[i] = -2;
}
else
{
assert(s1[i] >= '0' && s1[i] <= '9');
num1[i] = s1[i] - '0';
}
}
}
//确定小数点后面的第1个非0数的位置,
if (dotloc1 != -1)
{
for (int cnt1 = N-1; num1[cnt1] != -2; --cnt1) {dot1Afterloc = cnt1;}
dot1AfterNum = dot1Afterloc - dotloc1 + 1;
}
cout << "\n请输入第2个数:" ;
int j;
cin >> ctemp;
if (ctemp == '-') { signed2 = -1;}
if (ctemp == '+') { signed2 = 1; }
else
{
assert(ctemp >='0' && ctemp <= '9');
num2[0] = ctemp - '0';
for (j = 1; j< N; ++j)
{
cin >> s2[j];
if (s2[j] == '#')
{
break;
}
if (s2[j] == '.')
{
dotloc2 = j;
num2[j] = -2;
}
else
{
assert(s2[j] >= '0' && s2[j] <= '9');
num2[j] = s2[j] - '0';
}
}
}
//小数点后面的第1个非0数的位置,如果有小数点的话
if (dotloc2 != -1)
{
for (int cnt2 = N-1; num2[cnt2] != -2 && dotloc2 != -1; --cnt2) {dot2Afterloc = cnt2;}
dot2AfterNum = dot2Afterloc - dotloc2 + 1;
}
//小数点总数
int dotsNum = dot1AfterNum + dot2AfterNum;
//去小数点
int k1 = 0;
int k2 = 0;
if (dotsNum != 0)
{
if (dotloc1 != -1)
{
for (k1 = dotloc1; k1 < N; ++k1) { num1[k1] = num1[k1+1];}
num1[k1] = -1;
}
if (dotloc2 != -1)
{
for (k2 = dotloc2; k2 < N; ++k2) { num1[k2] = num2[k2+1];}
num2[k2] = -1;
}
}
//数实际长度
int len1 = 0;
int len2 = 0;
for (k1 = N-1; num1[k1] != -1; k1--) { ;} len1 = N-k1;
for(k2 = N-1; num2[k2] != -1; k2--){;} len2 = N-k2;
Calc2BigNum(num1, len1, num2, len2, result);
//输出最终计算结果
cout << "\n计算结果为:";
if ((signed1*signed2) == -1)
{
cout << "-";
}
int locNot0 = 0;
for (int rcnt = 0; rcnt< 2*N;rcnt++)
{
if (result[rcnt] != 0)
{
locNot0 = rcnt;
break;
}
}
for (int rcnt = locNot0; rcnt< 2*N;rcnt++)
{
if (rcnt == 2*N-1-dotsNum && dotsNum != 0)
{
cout << "." << result[rcnt];
}
else
cout << result[rcnt];
}
cout << endl;
return 0;
}
void Calc2BigNum( int num1[], int len1, int num2[],int len2, int result[] )
{
int i;
int j;
int resultCnt = 2*N-1;
int resultCtl = 2*N-1;
int tmpNum = 0;
for (i = len1-1; i >=0; i--)
{
resultCnt = resultCtl;
for (j = len2-1; j >=0; j--)
{
tmpNum = num1[i]*num2[j];
result[resultCnt] += tmpNum;
if (tmpNum > 9)
{
result[resultCnt] = tmpNum%10;
result[resultCnt-1] += tmpNum/10;
}
resultCnt--;
}
resultCtl--;
}
}