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

📄 baseconv.cpp

📁 基数转换算法。 在基数间进行相互的转换工作!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//*************************************
//***************B*I*N*O***************
//*************************************
//*******Base Conversion Algorithm*******
//Programmer/Engineer: Ryan Lloyd
//Project Name: "bino"
//Date Started: 10-25-00
//Date of Completion: 10-30-00
//Course: Advanced Placement Computer Science
//Teacher: Mr. Swanson
//Period: Six

//Visit Wonder Workers' C++ / C / Quick BASIC Site:
//		http://www.geocities.com/wonworkers/main.html

//Feel free to e-mail Wonder Workers, Inc. at:
//		wonderworkers@excite.com



#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>

int main()
	{
   int cotinue=1;
   long int org_num=0, org_base=0, valid_org_val, pos_org_num, new_num, base10_num, remainder;
   int new_base=0;
   int pow_limit, pow_test;
   int cur_term=0;
   cout<<setiosflags(ios::left);
   do
   	{
      //The outermost "do...while" loop allows for repetitive revolutions of
      //the primary block of the application.  The program will continue to
      //oscillate between the input and output pages as long as the variable
      //referred to as "cotinue" is equal to "1."  This variable can be changed
      //through user input at the end of the output page.
      do
      	{
         //This repetition statement ensures that the base of the initial number,
         //which is discovered by the program through a user input statement,
         //is a value between 1 and 17 exclusively.  The prompt message will
         //continue to be displayed and the user will continue to be required
         //to enter in the value of the base for the original number if he/she
         //incessently enters invalid bases into the system.
	      cout<<endl<<"Enter the base that the original number must be converted to\n"<<setw(67)<<"(the value should be between 2 and 16, inclusively).";
         cin>>new_base;
         }	while((new_base < 2) || (new_base > 16));
      do
      	{
         //This "do...while" loop will continue to run as long as the "org_num"
         //variable, which sybolizes the amount of the initial number, does not
         //meet specific requirements concerning the size of the first value.  The
         //user is expected to abide by the rules connecting the value of the
         //new base and the amount of the number that must be converted,
         //which are explained to the operator during the execution of the project
         //with the aid of the "cout" statement below.  If the first amount is too
         //large or too small (in terms of a negative value) to be converted
         //into the specified base, the user will be required to enter in a new
         //number which fits the parameters for the new base.
	      cout<<"\n\nInput the integer that must be converted.\n\nIf the new base is 2, then the original number must be\ngreater than or equal to -1,000 and less than or equal to 1,000.\n\nIf the new base is > 2 and < 11, then the initial number\nshould be between -10,000 and 10,000, inclusively.\n\nWhen the new base is larger than 10, the number\n"<<setw(67)<<"must be between -1,000,000 and 1,000,000, inclusively.";
   	   cin>>org_num;
         }	while((((org_num < -1000) || (org_num > 1000)) && (new_base == 2)) || (((org_num < -10000) || (org_num > 10000)) && ((new_base > 2) && (new_base < 11))) || (((org_num < -1000000) || (org_num > 1000000)) && (new_base > 10)));
      cout<<endl;
      do
       	{
         //The nested "do...while" repetition statement will repeat as long as
         //the original base of the original integer is less than 2 or greater
         //than 10.  Since the variable of "org_num," which stores the initial
         //whole number, is an integer, the base of the first number cannot exceed
         //10.  If the base was allowed to exceed 10, then letters and other
         //nonnumerical characters would be allowed as input into the "org_num"
         //integer identifier, effectively breaking C++ syntax rules.  Similar
         //to the two previous "do...while" loops, this repetition unit will
         //continue to execute with a prompt for user input continually being
         //displayed on the monitor while the base of the starting whole number
         //is less than 2 or larger than 10.   
	      cout<<"\nWhat is the base of the initial number\n"<<setw(67)<<"(the base should be between 2 and 10, inclusively)?";
         cin>>org_base;
         }	while((org_base < 2) || (org_base > 10));
      clrscr();					//Second Page Begins to Be Processed 
	   //***Full Conversion Process Begins Below
      if(org_num < 0)
      	//If it is discovered that the original number is negative, the initial amount
         //is transformed into a positive value by storing the product of -1 and
         //"org_num" in the identifier named "pos_org_num."
      	pos_org_num = org_num * -1;
      else
      	//If "org_num" is positive or equal to 0, the value of the first whole number
         //is placed in "pos_org_num."
      	pos_org_num = org_num;
      //**Start of Conversion to Base Ten
      for(pow_test=0; pow(10, pow_test) <= pos_org_num; ++pow_test);
      	//The above "for" loop, which does not contain a single executable statement
         //but rather serves as a counter, determines the final value of "pow_test."
         //"pow_test" is an integer variable that holds the first value "n" in
         //the expression "1 * 10^n" such that the result of the expression
         //is greater than "pos_org_num."  The final value of "pow_test" also
         //stores the number of single digits that are found in the positive
         //form of the intial number.  "pow_test" is incremented by 1 after each
         //revolution through the loop, and the repetition statement will end
         //when the result of "10^pow_test" is greater than the original amount
         //in its positive form.
      for(pow_limit=(pow_test - 1), remainder=pos_org_num, base10_num=0, valid_org_val=1; pow_limit >= 0; --pow_limit)
      	{
         //The purpose of this "for" loop involves converting the initial amount,
         //which is now present in its positive form in the variable called
         //"pos_org_num," to its equivalent value in base 10.  The identifier
         //named "pow_limit" is initialized at an upper bound--the number of
         //digits that are in "pos_org_num" subtracted by 1.  "remainder,"
         //an identifier that stores the amount within "pos_org_num" which have
         //not been subtracted by the succeeding processes, is set to the positive
         //form of "org_num."  "base10_num" is an accumulator that holds the current
         //result of the conversion process of "pos_org_num" into a base 10 value.
         //On the other hand, "valid_org_val" is initialized to 1, signifying
         //that the program assumes at first that all digits in "pos_org_num"
         //are less than the original base that the value is in.  This loop will
         //continue to execute as long as "pow_limit" is greater that -1, and
         //the value stored in "pow_limit" will decrement by 1 at the end of each
         //revolution.
         cur_term = (remainder / pow(10, pow_limit));
         //The above assignment statement allows the computer to identify any
         //digit that lies within "pos_org_num."
         if(cur_term >= org_base)
         	{
            //If the current term that is being analyzed in the "for" loop is
            //discovered to be greater than or equal to the base that the amount
            //was originally in, "pow_limit" is set to -1 (the outer "for" loop
            //will terminate when "pow_limit" is less than 0) and "valid_org_base"
            //is assigned the value of 0, signifying that the orginal value does
            //not coincide with its suggested original base.

⌨️ 快捷键说明

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