问题描述
- C语言编程 加减乘除。。。
-
有这样一道题,现在由于精度问题,数字加减会有误差,现要编辑一个程序,使得输入数字无误差,并给定长度在规定字符串数组(30),求加法(包含小数
解决方案
解决方案二:
我是来围观的我是来围观的我是来围观的
解决方案三:
#include
#include
#include
using namespace std;
void main()
{
char ch[ 30 ];
memset( ch, '?', 30 );
cin>>ch;
char symbol;
callback:
cin>>symbol;
if( symbol != '+' )
{
cout << "算术仅限加法,请重新输入运算符:"<
goto callback;
}
char ch_1[ 30 ];
memset( ch_1, '?', 30 );
cin>>ch_1;
char c = 0;
int uIndex = 0;
int uFloatIndex = 0;
bool b1Float = false, b2Float = false;
while( c != '.' && uIndex < 30 && c != '?' )
{
c = ch[ uIndex ];
uIndex++;
}
if( c == '.' )
{
b1Float = true;
uIndex--;
uFloatIndex = uIndex;
uIndex --;
char cc = 0;
int indexc = 0;
while( indexc < 30 && cc != '?' )
{
cc = ch[ indexc ];
indexc++;
}
ch[ indexc - 2] = '?';
}
else
{
uIndex -=2;
ch[ uIndex ] = '?';
uIndex--;
c = ch[ uIndex ];
}
char c1 = 0;
int uIndex1 = 0;
int uFloatIndex1 = 0;
while( c1 != '.' && uIndex1 < 30 && c1 != '?' )
{
c1 = ch_1[ uIndex1 ];
uIndex1++;
}
if( c1 == '.' )
{
b2Float = true;
uIndex1--;
uFloatIndex1 = uIndex1;
uIndex1--;
char c11 = 0;
int index11 = 0;
while( index11 < 30 && c11 != '?' )
{
c11 = ch_1[ index11 ];
index11++;
}
ch_1[ index11 - 2 ] = '?';
}
else
{
uIndex1-= 2;
ch_1[ uIndex1 ] = '?';
uIndex1--;
c1 = ch_1[ uIndex1 ];
}
stringstream sstream;
int carry = 0;
int remainder = 0;
string addRet = "";
vector< int >array1, array2;
int tmpIndex = uFloatIndex + 1, tmpIndex1 = uFloatIndex1 + 1;
int normalIndex = 0;
if( b1Float && b2Float )
{
int indexCount = 0;
while( true )
{
if( ch[ tmpIndex ] != '?' )
{
tmpIndex++;
}
else
{
normalIndex = indexCount;
break;
}
if( ch_1[ tmpIndex1 ] != '?' )
{
tmpIndex1++;
}
else
{
tmpIndex--;
normalIndex = indexCount;
break;
}
indexCount++;
}
if( ch[ tmpIndex ] != '?' )
{
while( ch[ tmpIndex ] != '?' )
{
int value = 0;
sstream << ch[ tmpIndex ];
sstream >> value;
sstream.clear();
array2.push_back( value );
tmpIndex++;
}
}
else
{
while( ch_1[ tmpIndex1 ] != '?' )
{
int value = 0;
sstream << ch_1[ tmpIndex1 ];
sstream >> value;
sstream.clear();
array2.push_back( value );
tmpIndex1++;
}
}
int tpIndex = uFloatIndex + normalIndex, tpIndex1 = uFloatIndex1 + normalIndex;
while( true )
{
/*if( normalIndex == 0 )
{
break;
}*/
int value1 = 0, value2 = 0;
if( tpIndex != uFloatIndex )
{
sstream << ch[ tpIndex ];
sstream >>value1;
sstream.clear();
tpIndex--;
}
else
{
break;
}
if( tpIndex1 != uFloatIndex1 )
{
sstream << ch_1[ tpIndex1 ];
sstream >> value2;
sstream.clear();
tpIndex1--;
}
else
{
break;
}
int ret = value1 + value2;
ret += carry;
if( ret >= 10 )
{
carry = ret / 10;
remainder = ret % 10;
}
else
{
carry = 0;
remainder = ret;
}
array2.push_back( remainder );
//normalIndex--;
}
}
else if( ( b1Float && !b2Float ) || ( !b1Float && b2Float ) )
{
if( b1Float && !b2Float )
{
while( ch[ tmpIndex ] != '?' )
{
int value = 0;
sstream << ch[ tmpIndex ];
sstream >> value;
sstream.clear();
array2.push_back( value );
tmpIndex++;
}
}
else
{
while( ch_1[ tmpIndex1 ] != '?' )
{
int value = 0;
sstream << ch_1[ tmpIndex1 ];
sstream >> value;
sstream.clear();
array2.push_back( value );
tmpIndex1++;
}
}
}
while( true )
{
if( uIndex == -1 && uIndex1 == -1 )
{
break;
}
int value1 = 0, value2 = 0;
if( uIndex >= 0 )
{
sstream << ch[ uIndex ];
sstream >>value1;
sstream.clear();
}
if( uIndex >= 0 )
{
uIndex--;
}
if( uIndex1 >= 0 )
{
sstream << ch_1[ uIndex1 ];
sstream >> value2;
sstream.clear();
}
if( uIndex1 >= 0 )
{
uIndex1--;
}
int ret = value1 + value2;
ret += carry;
if( ret >= 10 )
{
carry = ret / 10;
remainder = ret % 10;
}
else
{
carry = 0;
remainder = ret;
}
array1.push_back( remainder );
}
string calculateRet;
int arrayIndex = 0;
while( true )
{
if( array1.empty() )
{
break;
}
string tmp = "";
sstream << array1.back();
sstream >> tmp;
sstream.clear();
calculateRet += tmp;
array1.pop_back();
}
arrayIndex = 0;
if( !array2.empty() )
{
calculateRet += ".";
while( true )
{
if( array2.empty() )
{
break;
}
string tmp = "";
sstream << array2.back();
sstream >> tmp;
sstream.clear();
calculateRet += tmp;
array2.pop_back();
}
}
cout << "="<<endl<< calculateRet.c_str() <<endl;
system( "pause" );
}
/////////////////////////////////////////////////////////////////
//输入第一个数后按回车,然后再输入+号按回车,最后输入第二个数按回车。
//代码没有整理,勿怪,希望能帮到你。
解决方案四:
头文件需要加sstream、vector、iostream