⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseconv.cpp

📁 基数转换算法。 在基数间进行相互的转换工作!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            pow_limit=-1;
            valid_org_val=0;
            }
         base10_num = (base10_num + (cur_term * pow(org_base, pow_limit)));
         //The previous value of "base10_num" is added to the current digit that
         //was pulled from the positive form of the initial number times the result
         //of the number's original base raised to the current value of "pow_limit."
         //After the surrounding repetition statement is finished executing,
         //"base10_num" will contain the value of the original amount converted
         //into base 10 number representation.  This process is performed to
         //improve the logical pathway involved in transforming a number in any
         //base that is less than 10 into its equivalent amount in a second base
         //that is under 10.
         remainder = (remainder - (pow(10, pow_limit) * cur_term));
         //The updated version of "remainder" is found by raising 10 to the current
         //value for "pow_limit," multiplying the result by the digit that is being
         //analyzed from the intial amount, and subtracting the product from the
         //previous value of "remainder."
         }
      //**End of Base Ten Conversion
      if(valid_org_val == 0)
      	//If the original number contains digits that exceed or equal the first
         //number's base, an error message will be outputted and the program will
         //ask the operator if he/she wants to attempt the conversion stage again.
      	cout<<"\n\n\n\nError: The Base and the Value of the Original Number Do not Coincide";
      else
      	{
         //This block of code will be accessed by the computer if all of the digits
         //in the initial number are less than the base that the number is supposedly in.
         cout<<"\n\n\n\nThe conversion of "<<org_num<<" from base "<<org_base<<" to base "<<new_base<<" reads as follows:\n\n";
	      for(pow_test=0; pow(new_base, pow_test) <= base10_num; ++pow_test);
         	//Although the above "for" loop lacks a body, its heading serves as
            //a counter for the number of characters that lie within the newly
            //created "base10_num" variable.  The loop's control variable, which
            //is referred to as "pow_test," will increment by one after each run
            //through the loop, and the loop will continue to execute while the
            //result of "new_base ^ pow_test" is less than or equal to the base 10
            //conversion of the positive form of the original number.
   	   if(new_base < 11)
      		{
            //If the base that the intial value is being converted over to is
            //less than 11, then the following segment of code is performed.
	      	for(pow_limit=(pow_test-1), remainder=base10_num, new_num=0; pow_limit >= 0; --pow_limit)
    	  			{
               //The purpose of this repetition statement is to convert the newly
               //developed "base10_num" variable into the new base that was
               //specified by the user.
               //This "for" loop initializes "pow_limit," which symbolizes the digit
               //that is being reviewed within "base10_num" in terms of scientific
               //notation, to the difference of "pow_test" minus 1.  In addition,
               //"remainder," a variable that holds the amount in "base10_num"
               //that has not been subtracted from the variable's original value
               //by the following operations, is first set to equal "base10_num."
               //The sum of the conversion process of "base10_num" to the number's
               //new base is set to a starting value of 0.  The loop's control variable
               //of "pow_limit" decreases by 1 following each successful cycle of the
               //repetition statement's body, and the loop's body will continue
               //to execute as long as "pow_limit" is not less than 0.
		         cur_term = pow(new_base, pow_limit);
               //The current term or digit that will be divided by "base10_num"
               //is calculated and stored in "cur_term" by raising the original
               //integer's new base to the value of "pow_limit."
   		      new_num = new_num + (pow(10, pow_limit) * (remainder / cur_term));
               //The value of "new_num," which represents the current progress of
               //converting the original whole number to the new base, is found
               //by raising 10 to "pow_limit," multiplying the result by the outcome
               //of "remainder" divided by "cur_term," and adding the product to
               //the previous value of "new_num."
      		   remainder = (remainder - ((remainder / cur_term) * cur_term));
               //The new value of "remainder" stores the current amount residing
               //in "base10_num" which have not been "touched" by the division
               //and conversion processes.
         		}
	         if(org_num < 0)
            	//If the original amount is discovered to be negative, the positive
               //form of "new_num" will become negative by multiplying "new_num"
               //by -1.
   	   		new_num = new_num * -1;
      	   cout<<new_num;			//The user's final, highly desired conversion solution is outputted onto the screen.
         	}
	      else
   	   	{
            //If the new base is not between 2 and 10, inclusively, then an entirely
            //different process must be performed to reveal the proper answer.
            //Instead of storing the converted solution to the original number in
            //a variable, the individual characters, including letters and numbers,
            //are printed on the monitor as they are discovered.
				for(pow_limit=(pow_test-1), remainder=base10_num; pow_limit >= 0; --pow_limit)
    	  			{
               //This "for" loop is designed to calculate and display each value
               //of the conversion's solution character-by-character.  The loop's
               //control variable (or "pow_limit") is initialized at "pow_test - 1,"
               //which reveals the number of digits that exist in the number stored
               //in "base10_num."  On the other hand, "remainder" is first set to
               //equal "base10_num."  The loop's control variable decrements by 1
               //at the conclusion of each revolution through the loop, and the
               //loop will run as long as "pow_limit" is not less than 0.
	         	cur_term = pow(new_base, pow_limit);
               //The current "term" of the conversion process is calculated by
               //raising the new base to the current value for "pow_limit."  For
               //instance, if "new_base" is 6 and "pow_limit" is equivalent to
               //3, then "cur_term" equals 216.
	            if((remainder / cur_term) < 10)
               	//If the newly discovered character in the solution is a number
                  //that is less than 10, then the numerical form of the single
                  //digit is displayed on the screen with the aid of the
                  //output statement that is written below.
   	         	cout<<(remainder / cur_term);
      	      else
               	//If the new character in the problem's solution is a value
                  //which is greater than 9, then the letter form of the new number
                  //is displayed to the user.  For example, if the new element is
                  //12, the letter "C" will be displayed instead of "12."
         	   	cout<<char(65 + ((remainder / cur_term) - 10));
      	   	remainder = (remainder - ((remainder / cur_term) * cur_term));
               //The above line of code determines the new value of "remainder,"
               //which contains the amount of "base10_num" that has not been affected
               //by the program.
	         	}
   	      }
      	}
      //***End of Full Base Conversion
      cout<<"\n\n\n\n\n\n\n\tWould you like to run the conversion process again?\n\t\t\t\t(1 = Yes, 2 = No)\n\t\t\t\t\t";
      //The above output statement and the input line below require the user to
      //choose whether he/she would like to cycle through the base conversion
      //process again.  If the operator enters a "1," the first screen of the
      //application will reappear.  If the user presses any key other than "1,"
      //the project will terminate.
      cin>>cotinue;
      clrscr();
      }	while(cotinue == 1);
   return 0;
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -