在标准答案的基础上改动的数据结构与算法分析习题1.3答案

来源:互联网 发布:本地ip和网络ip 编辑:程序博客网 时间:2024/04/29 06:40
#include<iostream>
using namespace std;
int IntPart(double N);
double DecPart(double N);
void PrintDigit(int ADigit);
void PrintOut(int n);
void PrintFractionPart(double FractionPart,int DecPlaces)//输入一个浮点型数据FractionPart,和整型DecPlaces
{
int i,ADigit;//设立整形i和ADigit,作用不明
    int j=0;
for(i=0;i<DecPlaces;i++)//循环DecPlaces次
{
FractionPart*=10;//FractionPart为10*FractionPart
ADigit=IntPart(FractionPart);//Adigit等于FractionPart的整数部分
if(ADigit!=0)
j++;
PrintDigit(ADigit);//输出ADigit,即输出FractionPart的整数
FractionPart=DecPart(FractionPart);//FractionPart变为它自己的小数部分
}
printf("\n");
printf("%d",j);
}


void PrintReal(double N,int DecPlaces)//输入一个浮点数N,和整形数DecPlaces
{
int IntegerPart;//设立IntegerPart整形
double FractionPart;//设立浮点型FractionPart


if(N<0)//如果N<0
{
putchar('-');//输出到屏幕一个-
N=-N;//将N变为-N,即为正数
}
//N=RoundUp(N,DecPlaces);//N为N加上AmountToAdd
IntegerPart=IntPart(N);//IntegerPart为N的整数部分
FractionPart=DecPart(N);//FractionPart为N的小数部分
PrintOut(IntegerPart);//输出N的整数部分
if(DecPlaces>0)//如果DecPlaces大于0
putchar('.');//输出,到屏幕
PrintFractionPart(FractionPart,DecPlaces);//输出FractionPart不停乘10的数
}


int IntPart(double N)
{
int n=N;
return n;
}
double DecPart(double N)
{
int n=N;
double n1=N-n;
return n1;
}
 
void PrintDigit(int ADigit)
{
printf("%d",ADigit);
}




void PrintOut(int n)   
{  
if(n<0)
n=-n;
    /*该整数的中间数都以正数打出*/  
    if(n >= 10)   
    {  
        PrintOut(n/10);  
        printf("%d", n%10);  
    } else      //第一个数字按原样打出  
        printf("%d", n%10);  
      
}  




int main()
{
double i=-12.14;
int dec=1000;
PrintReal(i,dec);
system("pause");
}
0 0
原创粉丝点击