Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
10
Добавлен:
26.05.2014
Размер:
5.26 Кб
Скачать
#include <stdio.h>
#include <math.h>

//The place where are situated the function's prototipes
int get_digit(int ch,int *digit);
int get_mantiss(char s[],float *mantiss);
int get_power(char s[],int *power);
void bin_mantiss(float *mantiss,int a[]);
void bin_power(int *power,int a[]);
int dec_invert_m(int a[],float *mantiss);
void dec_invert_p(int n,int a[],int *power);

//This function gets a value (digit) from the character "ch".
int get_digit(ch,digit)
        int ch,*digit;
 {  int status;

   status=0;
   switch(ch)
     {
      case 'A': *digit=10; break;
      case 'B': *digit=11; break;
      case 'C': *digit=12; break;
      case 'D': *digit=13; break;
      case 'E': *digit=14; break;
      case 'F': *digit=15; break;
      default: if(ch>='0' && ch<='9')
                 *digit=ch-'0';
               else
           		  status=1;
     }

   return(status);
 }


//This function gets a value of mantiss from the string "s".
int get_mantiss(s,mantiss)
       char    s[];
       float   *mantiss;
 {     int     i=0,status=0,Status=0,sign=1,digit=0;
       float   base;

    for(i=0;s[i]!='\0',s[i]==' ';i++);

    if(s[i]=='\0')
      Status=3;
    else
      {
        if(s[i]=='-')
          {
            sign=-1;
            i++;
          }

        for(digit=0,base=1.0,*mantiss=0;i<=4,get_digit(s[i],&digit)==0;i++)
           *mantiss+=(base*=0.0625)*digit;

        if(status==1 && s[i]!='e')
          Status=1;
        else if(i==4 && s[i+1]!='e')
               Status=2;
             else if(i==4 && s[i+1]=='e')
                    Status=0;
                  else if(s[i]=='e')
                         Status=0;
                       else
                         Status=4;

        *mantiss*=sign;
      }

    return(Status);
 }

//This function gets a value of power from the string "s".
int get_power(s,power)
       char  s[];
       int   *power;
 {     int i,digit,base,status=0,Status=0,j;

   for(i=0;s[i]!='\0';i++);
   i--;

   for(;s[i]==' ';i--);

   for(digit=0,base=1,j=0;j<=1,get_digit(s[i],&digit)==0;j++,i--)
      {
       *power+=base*digit;
       base*=16;
      }

   if(s[i]=='e')
     Status=0;
   else if(s[i]=='-')
          {
           *power*=-1;
           Status=0;
          }
        else if(j==1 && s[i-1]=='-')
               {
                *power*=-1;
                Status=0;
               }
             else if(j==1 && s[i-1]=='e')
                    Status=0;
                  else Status=1;

   return(Status);
 }

//This functions turns mantiss into the binary system.
void bin_mantiss(mantiss,a)
       float  *mantiss;
       int    a[32];
 {     int    i;
       float  x=*mantiss;

   for(i=1;i<=31;i++)
      {
       a[i]=((x*2.0)>=1)? 1:0;
       if(x>=1.0) x-=1;
      // printf("%d",a[i]);
       x=x*2.0;
      }

   a[0]=(*mantiss<0)? 1:0;
 }

//This function turns power into the binary system.
void bin_power(power,a)
       int  *power;
       int  a[8];
 {     int  i,x=*power;

   x=(x*10)/3;

   for(i=1;i<=7;i++)
      {
       a[i]=x%2;
       x/=2;
      }

   a[0]=(*power<0)? 1:0;
 }

//This function inverts,fixes and turns mantiss into decimal system
//and then returns the number of bit's displacements.
int dec_invert_m(a,mantiss)
       int    a[32];
       float  *mantiss;
 {     int    i,j,t,changes=0;
       float  base;

   for(i=0;i<=31;i++)
      a[i]=(a[i]==1)? 0:1;

   for(j=0;a[1]==0,j!=30;j++)
      for(i=1;i<=30;i++)
         {
           t=a[i];
           a[i]=a[i+1];
           a[i+1]=t;
         }

   changes=j;

   for(i=1,base=1;i<=31;i++)
       *mantiss+=a[i]*(base*=0.5);

   *mantiss*=(a[0]==1)? -1:1;

   return(changes);
 }

//This function inverts and turns power into decimal system with use
//of the number of bit's displacements.
void dec_invert_p(n,a,power)
         int    n;
         int    a[8];
         int    *power;
 {       int    i,base;

    for(i=0;i<=7;i++)
      a[i]=(a[i]==1)? 0:1;

    for(i=7,base=1;i>=1;i--)
       {
        *power+=a[i]*base;
        base*=2;
       }

    *power*=(a[0]==1)? -1:1;

    *power+=n;
 }

//Main function
void main(void)
{  char   s[20],ch;
   float  mantiss1=0,mantiss2=0;
   int    power1=0,power2=0;
   int    m[32],p[8];

 putchar('\n');
 printf("****************** Changing the system of the value ***************************\n");
 printf("Enter the value to be converted, please(......e....): ");
 gets(s);
 putchar('\n');

 if(get_mantiss(s,&mantiss1)==0)
   if(get_power(s,&power1)==0)
     {
      bin_mantiss(&mantiss1,m);

   //   printf("%d",power1);

      bin_power(&power1,p);
      dec_invert_p(dec_invert_m(m,&mantiss2),p,&power2);

      printf("Value before convertion: %fe%d\n",mantiss1,power1);
      printf("Converted value is: %fe%d\n",mantiss2,power2);
     }
   else
     printf("Error with power statement!\n");
 else
   printf("Error with mantiss statement!\n");

 printf("****************************** Thank you **************************************\n");

 do
 ch=getchar();
 while(ch!='\n');
}


Соседние файлы в папке tarasov