1,请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句
答:
若a>b,则a-b和abs(a-b)均大于0,若a<b,则a-b和abs(a-b)异号,则互相抵消,值为0,若a==b,则无所谓,直接返回b
2,如何输出源文件的标题和目前执行行的行数
using namespace std;
int main()
{
cout<<"源文件名称: "<<__FILE__<<endl;
cout<<"当前行数: "<<__LINE__<<endl;
return 0;
}
3,两个数相加,小数点后位数没有限制,请写一个高精度算法
*
* @author phinecos
* @since 2009-05-19
*/
public class test
{
private static String addFloatNum(String num1, String num2)
{//两个浮点大数相加,小数点位数任意
String result = "";
int pos1,pos2,len1,len2;
len1 = num1.length();
len2 = num2.length();
pos1 = num1.indexOf('.');
pos2 = num2.indexOf('.');
//分别剥离两个数的整数和小数部分
String num1a = num1.substring(0, pos1);
String num1b = num1.substring(pos1+1, len1);
String num2a = num2.substring(0, pos2);
String num2b = num2.substring(pos2+1, len2);
//整数部分相加
String rsOne = add(num1a, num2a);
//小数位对齐,不足的补0
int i,nZeroes,maxLen;
maxLen = (num1b.length()>num2b.length()) ? num1b.length() : num2b.length();
if (num1b.length()>num2b.length())
{//第一个数的小数部分长,则第二个补不足的0
nZeroes = num1b.length() - num2b.length();//待补的0的个数
for (i = 0; i < nZeroes; ++i)
{
num2b += '0';
}
}
else if(num2b.length() > num1b.length())
{//第二个数的小数部分长,则第一个补不足的0
nZeroes = num2b.length() - num1b.length();//待补的0的个数
for (i = 0; i < nZeroes; ++i)
{
num1b += '0';
}
}
//小数位对齐准备完毕,进行小数部分相加
String rsTwo = add(num1b, num2b);
if (rsTwo.length() > maxLen)
{//说明有进位, 剥离第一位进位,加到整数部分去
String nAddOn = rsTwo.substring(0,1);
rsOne = add(rsOne, nAddOn);
rsTwo = rsTwo.substring(1,rsTwo.length());
}
//两部分结果拼凑起来
StringBuilder sb = new StringBuilder(rsOne);
sb.append(".");
sb.append(rsTwo);
result = sb.toString();
return result;
}
private static String add(String num1, String num2)
{ //大数相加
String result = "";
int len1 = num1.length();
int len2 = num2.length();
int nAddOn = 0;
int i,j,n1,n2,sum;
StringBuilder sb = new StringBuilder();
for (i = len1 - 1,j = len2 - 1 ; i >= 0 && j >= 0; --i,--j)
{
n1 = num1.charAt(i) - '0';
n2 = num2.charAt(j) - '0';
sum = n1 + n2 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
if (len1 > len2)
{
for (; i >= 0; --i)
{
n1 = num1.charAt(i) - '0';
sum = n1 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
}
else if (len2 > len1)
{
for (; j >= 0; --j)
{
n2 = num2.charAt(j) - '0';
sum = n2 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
}
if (nAddOn > 0)
{
sb.append(nAddOn);
}
sb.reverse();
result = sb.toString();
return result;
}
public static void main(String[] args) throws Exception
{
String num1 = "13454354352454545454354354354354543.9999999999993545624524435245425435435435";
String num2 = "3415545435435435435435435434525435245245454252.999999999999999994535435435435252245426";
String result = addFloatNum(num1, num2);//大浮点数相加
System.out.println(result);
}
}
4,对第3题做下修改,变成:两个数相乘,小数点后位数没有限制,请写一个高精度算法。
*
* @author phinecos
* @since 2009-05-19
*/
public class test
{
private static String multipy(String num1, String num2)
{//大数乘法
String result = "0";
int i,j,n1,n2;
int len1 = num1.length();
int len2 = num2.length();
if (len1 < len2)
{
for (i = len1 -1; i >=0; --i)
{
n1 = num1.charAt(i) - '0';
String sum = "0";
for (j = 0; j < n1; ++j)
{
sum = add(sum,num2);
}
StringBuilder tmpSB = new StringBuilder(sum);
for (j = i; j < len1 -1; ++j)
{
tmpSB.append("0");
}
result = add(result,tmpSB.toString());
}
}
else
{
for (i = len2 -1; i >=0; --i)
{
n2 = num2.charAt(i) - '0';
String sum = "0";
for (j = 0; j < n2; ++j)
{
sum = add(sum,num1);
}
StringBuilder tmpSB = new StringBuilder(sum);
for (j = i; j < len2 -1; ++j)
{
tmpSB.append("0");
}
result = add(result,tmpSB.toString());
}
}
return result;
}
private static String multipyFloatNum(String num1, String num2)
{//两个浮点大数相乘
String result = "";
int pos1,pos2,len1,len2,nDot,posDot;
len1 = num1.length();
len2 = num2.length();
//两个数的小数点位置
pos1 = num1.indexOf('.');
pos2 = num2.indexOf('.');
nDot = (len1 - pos1-1) + (len2 - pos2-1);//乘积结果的小数位数
//去掉两个数的小数点
String num1a = num1.substring(0, pos1);
String num1b = num1.substring(pos1+1, len1);
String num2a = num2.substring(0, pos2);
String num2b = num2.substring(pos2+1, len2);
num1 = num1a + num1b;
num2 = num2a + num2b;
//不带小数点进行大数相乘
String rsTemp = multipy(num1, num2);
//调整结果,加入小数点
StringBuilder sb = new StringBuilder(rsTemp);
posDot = sb.length() - nDot;//小数点插入位置
sb.insert(posDot, '.');//插入小数点
result = sb.toString();
return result;
}
private static String add(String num1, String num2)
{//两个大数相加
String result = "";
int len1 = num1.length();
int len2 = num2.length();
int nAddOn = 0;
int i,j,n1,n2,sum;
StringBuilder sb = new StringBuilder();
for (i = len1 - 1,j = len2 - 1 ; i >= 0 && j >= 0; --i,--j)
{
n1 = num1.charAt(i) - '0';
n2 = num2.charAt(j) - '0';
sum = n1 + n2 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
if (len1 > len2)
{
for (; i >= 0; --i)
{
n1 = num1.charAt(i) - '0';
sum = n1 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
}
else if (len2 > len1)
{
for (; j >= 0; --j)
{
n2 = num2.charAt(j) - '0';
sum = n2 + nAddOn;
if (sum >= 10)
{
nAddOn = 1;
}
else
{
nAddOn = 0;
}
sb.append(sum % 10);
}
}
if (nAddOn > 0)
{
sb.append(nAddOn);
}
sb.reverse();
result = sb.toString();
return result;
}
public static void main(String[] args) throws Exception
{
String num1 = "12656436456456543.45874078765765765764542576756645745673467075";
String num2 = "26546456654564564563.964006563565464654645565636543665635634565";
String result = multipyFloatNum(num1, num2);//大浮点数相加
System.out.println(result);
}
}