4:下面是某嵌入式系统的一些基本函数说明,代替标准c中的相关函数。
FREE()
Description:
This function corresponds to free() in the standard C library.
Prototype:
void FREE(void * po)
Parameters: po Points to the memory to be freed.
Return Value: None
 
MALLOC()
Description:
This function corresponds to malloc() in the standard C library. This function zeros the
memory that it returns.
Prototype:
void * MALLOC(DWORD dwSize)
Parameters: dwSize Size of buffer in bytes.
Return Value: Pointer to the allocated memory.
 
该系统不支持浮点数,只支持整数运算。但由于目前的程序当中对计算的要求精度相对较高,
希望你能设计一个类,来实现模拟小数的运算。(只需要实现加减乘除,并假设程序中所有参与
运算的数值和运算结果都在char的范围里。)
 
例如:
char a = 3;
char b = 2;
char c = a/b;
char d = 6/c;
结果d = 6;
我们希望通过你的类来实现运算,并得到的结果是 d = 4;

解决方案 »

  1.   

    除数不变,被除数根据需要扩大10的n次方倍(不能超过DWORD的范围,具体大小还可以根据你所需要的精度来判断)
    比如
    3*10/2=15
    6*10/15=4
      

  2.   

    int mydivide(int x,int y)
    {
    int temp=x;
    int countx=0;
    while(temp!=0)
    {
       temp=temp/10;
       countx++;//统计位数
    }
    int temp=y;
    int county=0;
    while(temp!=0)
    {
       temp=temp/10;
       county++;//统计位数
    }
    if(county!=0)
    {
    switch(countx-county)
    {
    ........................................//根据除数和被除数的位数关系确定该怎么乘以10的N次方,保证不超过DWORD,使结果不丢失有效数字
    }}