The second blog work: function + binary converter v1.0beta

A: run shot

 

 

 

 

 

 

 

 

 

 

 

 

 2: Introduction of functions

1,

. 1  int panduan1 ( int n-, char A [], int COUNT, int Sign) // determines whether the user input numbers and characters except the range af 
2  {
 . 3  
. 4      int I;
 . 5      for (I = 0 ; I <COUNT ; I ++ ) {
 . 6          IF ! (n-= 2 ! && n-= . 8 ! && n-= 10 ! && n-= 16 ) return  . 1 ;
 . 7          IF (A [I]> = ' A ' && A [I] <= ' F ' ) A [I] - = 87;
 8         if(a[i]>='0'&&a[i]<='9')a[i]-=48;
 9         if(a[i]>=n){
10             if(sign!=0){
11                 return 0;
12         }
13         return 1;
14     }
15     }
16     return 0;
17 }

2,

. 1  int panduan2 ( char A [], int COUNT, int Sign) // determines a user input to be converted if the number of standard 
2  {
 . 3          int I, X;
 . 4          X = COUNT;
 . 5          IF (! Sign = 0 ) return  0 ;
 . 6      for (I = 0 ; I <COUNT; I ++ ) {
 . 7          IF (A [I]> = ' A ' && A [I] <= ' F ' ) x-- ;
 . 8          IF (A [I]> = ' A '&&a[i]<='F')x--;
 9         if(a[i]>='0'&&a[i]<='9')x--;    
10     }
11     if(x==0)return 0;
12     return 1;
13 }

3,

. 1  void Change ( int n-, char A [], int m, int COUNT) // the number of the first user input is converted into a decimal number, is converted into the desired binary number is converted into a 
2  {    
 . 3      int I, J, K = 0 , NUM, SUM = 0 , C;
 . 4      char B [ 1001 ];
 . 5      for (I = 0 ; A [I] =! ' \ 0 ' ; I ++ )
 . 6      {
 . 7          IF (A [I]> = ' 0 ' && A [I] <= ' . 9 ' ) NUM = A [I] -'0';
 8         else
 9             num=a[i]-'a'+10;
10         sum=sum*n+num;
11     }
12     while(sum)
13     {
14         c=sum%m;
15         b[k++]="0123456789abcdef"[c];
16         sum=sum/m;
17     }
18     i=0;
19     for(i=k-1;i>=0;i--)
20     {
21         if(b[i]>='0'&&b[i]<='9')printf("%d",b[i]-'0');
22         else
23             printf("%c",b[i]);
24     }
25     printf("\n"); 
26 }

Three: the main function display

. 1  int main ()
 2  {
 . 3      the printf ( " binary, octal, decimal converter \ n-. " ); 
 . 4      int m, I, COUNT = 0 , Among, n-, In Flag = 0 , X;
 . 5      int Sign = 0 ;
 . 6      static  char a [ 1000 ], CH, B [ 1001 ];
 . 7      L: X = 0 ;
 . 8      do 
. 9      {
 10          the printf ( " integer input to be transformed: \ n- " );
 . 11          for (I = 0; i < 1000; i++)
12         {
13             scanf("%c", &a[i]);
14             if (a[i] == '\n')
15             {
16                 break;
17             }
18             count++;
19         }
20         if(count!=0)break; 
21     }
22     while (count == 0);
23     for(i=0 ; I <COUNT; I ++ ) {
 24          IF (A [I]> = ' A ' && A [I] <= ' F. ' ) {
 25              A [I] = A [I] + 32 ;
 26 is          }
 27      }
 28      for (I = 0 ; I <COUNT; I ++ ) {
 29          IF (panduan2 (a, COUNT, Sign)) {
 30              Sign ++ ;
 31 is              the printf ( " error, and re-enter it again \ n- " );
 32              GOTO L;
 33 is          }
 34 is          for(I = 0 ; I <COUNT; I ++ )
 35          B [I] = A [I];
 36          B [I] = ' \ 0 ' ;
 37 [  }
 38 is      the printf ( " Enter the integer hex: \ n- " ) ;
 39      Scanf ( " % D " , & n-);
 40      CH = getchar ();
 41 is      the while (panduan1 (n-, a, COUNT, Sign))
 42 is      {
 43 is          the printf ( " ! error, and re-enter it again \ n " );
 44 is          Scanf ( " % D ", & N);
 45      }
 46 is      Sign = 0 ;
 47      the printf ( " Enter the desired transformed into an n-ary \ " );
 48      Scanf ( " % D " , & m);
 49      the while ! (M = 2 && m! = . 8 ! && m = 10 && m =! 16 )
 50      {
 51 is          the printf ( " error, and re-enter it again \ n-! " );
 52 is          Scanf ( " % D " , & m);
53     } 
54     the printf ( " successful conversion: " );
 55      Change (n-, B, m, COUNT);
 56 is      }

Four: Mind Mapping

 

 

 Five: Problems and Solutions

1, before programming, binary, octal, decimal, and transform each other between the calculated hex is not very clear, so first go to the query textbooks and online calculation method, first thoroughly understand how each number between hexadecimal conversion before they can start writing programs on the internet learned that the number you want to convert first converted to a decimal number as a decimal number is calculated when the binary conversion is relatively simple, according to this line of thinking it is easier to write programs .

2, the use of the function is not required to distinguish between the return value and do not need to use the return value, many times hovering between int, double, and later learned that by asking the students do not need the return value of the function should use the void, or because pta question brush too little experience are required to return the value of the subject, because of his own problems and in order to do do problems, there is no depth to understand, resulting vacancy knowledge.

3, the array just learning applications, usage of the array is not very familiar with and skilled, do not know how you can enter the operating array of strings and so on, and then after inquiries by the network information and ask the students, and eventually perfected this knowledge deficiencies.

VI: Code peer review

 Network 1914 Spohr slope

 1         if(c>=2)
 2             l++;
 3         y/=10;
 4         s10=s10+c*pow(2,i);
 5         i++;
 6     }
 7     y=s10;
 8     i=0;
 9     while(y!=0){
10         c=y%8;
11         y/=8;
12         s8=s8+c*pow(10,i);
13         i++;
14      }
 15      IF (L == 0 ) {
 16          the printf ( " binary \ n Switch " ); 
 . 17          the printf ( " octal:% d \ n Decimal:% d \ n " , S8, S10);
 18 is      } 
 . 19      the else  
20 is          the printf ( " this input is wrong duck " );
 21 }

Several of his judgment entered is correct code more concise and clear, not like me, wrote two functions, high efficiency, and his judgment on the main function, to write more convenient, efficient and effective programming It is well worth learning.

Seven: summary

1, the program can be used to write a really hard hard hard, we need the knowledge base to do if you encounter a variety of problems during the preparation process, we must first solve their own, whether it is open book or query the network information, they have to try it yourself, you can not ask someone else, really do not understand, ask someone else to go, this assignment also let me learn on their own prior encounter, such as in knowledge translation between binary, I understand enough thorough, so the internet to find information on their own, their own pen to paper calculations, calculation worked out step by step the law, in order to complete the job.

2, for a function, its use is really varied, only you think, no less than it function in the nested recursion usage, very powerful, can do many calculations, you can make flexible use of the function procedures play a greater capacity.

Guess you like

Origin www.cnblogs.com/hhs1/p/11824175.html