1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream> // gives cin, cout
using namespace std; // eliminates needing the "std::" prefix
/***************************************************************
New function prototypes. Include a description of the function
input(s) and the function output. Often some data is helpful:
int largest( int, int, int ); // largest(4,5,-1) returns 5
***************************************************************/
int flipDigit(int series, int replace, int replacer); //flipDigit(12345, 3, 9) returns 12945
/***************************************************************
main() function code.
The main() function always takes zero parameters/arguments
and returns 0 to indicate that the input was successfully
processed into output.
Later: A return of [some other value] means [something else].
****************************************************************/
int main()
{
int userNum = 0;
int d1 = 0;
int d2 = 0;
cout << "Welcome to the number swapper\n";
cout << "Input the number series: ";
cin >> userNum;
cout << "What number do you want switched: ";
cin >> d1 ;
cout << "what number do you want to swap it with: ";
cin >> d2;
cout << "The number series " << userNum << " with " << d1 << " replaced by " << d2 << " is "<< flipDigit(userNum, d1, d2) << "\n";
system("pause"); // pauses the screen waiting for the user to hit anything, security issue. replace this with something else
return 0; // return 0 to indicate successful execution of main()
}
/***************************************************************
Code for other functions
Use the main() comment above as the model.
***************************************************************/
int flipDigit(int series, int replace, int replacer)
{
int base = series; // our base for the series
if (series < 0) // check to see if input number series is negative
{
base = (base * (-1)); // make calculation base positive
}
// vars used for various things I like to initialize mine
int quotient = 0;
int mod = 0;
int size = 1; // initialize to one for later
int output = 0;
while ((base/size) > 10) //while size is down at least one place from the end
{
size = size * 10; //increase size up a space, remember it was initialized at 1
}
mod = base;
while (size > 0) // while we are still working with a place in the num
{
quotient = mod/size; // what is the num at the place of size
mod = mod % size; // mod becomes everything lower than the current place
if (quotient == replace) // if the num at place is the replacement number
{
output = (output + (size*replacer)); // set place to replacement num
}
else
{
output = (output + (size*quotient)); // set place to original value
}
size = size/10; // go down one place
}
if (series < 0) // if it was negative originally we go back to being negative
{
output = (output * (-1));
}
return output;
}
| |