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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
using namespace std;
double userInput;
double number;
double fraction;
string one [] = {"Zero", "One" , "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten",
"Eleven", "Twelve", "Thirteen","Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
string tens [] = {"error", "erorr", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string huns [] = {"","Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion"};
int num[100];
int frac[2];
double length = 0, getHun, h, p;
int x = 0, n = 0, f = 0, d = 0;
int ones = 0, ten = 0, hun = 0, tths = 0, hths = 0;
string result, hunsGroup;
void getLength ()
{
//Gets the length of the number by log10 and 1 since it is one number short
length = log10(number);
length = length + 1;
length = (int)length;
}
void getNumber ()
{
//getNumber gets the number and splits into three parts
//and stores it in an array by modulo and dividing to get
//three parts untill number is 0
n = (int)number;
for (x = 0; x < length; x++)
{
num[x] = n % 1000;
n = n / 1000;
if (n == 0)
{
break;
}
}
}
void getDecimal ()
{
//getDecimal and sees if fraction is from .99 or from .00 and
//makes it a int by multiplying by 100 and stores the decimal in frac[] untill fraction is 0
if(fraction < 1 && fraction >= .00)
{
fraction = fraction * 100;
f = (int)fraction;
for (x = 0; x < 2; x++)
{
frac[x] = f % 10;
f = f / 10;
}
}
}
void getHuns ()
{
//This gets the to check how many times the number gets divided into three parts
//to see what hundred place it is in.
for (x = 0; x < length; x++)
{
number = ((int)number % 1000) / 1000;
getHun++;
}
getHun = getHun / 3;
p = modf(getHun, &h);
if(p < 1 && p > 0)
{
h = h + 1;
}
cout << huns[(int)h] << "\n";
h--;
}
void getToWords ()
{
//getToWords takes the number and decimal was stored in the array
//and prints off the number it holds
//gets the number for each postion and stores it and repeats this untill
//the number it gets the result and prints it off
x = (int)length;
while(x > -1)
{
hun = num[x] / 100;
ten = (num[x] /10)% 10;
ones = (num[x] % 10);
tths = frac[1] % 10;
hths = frac[0] % 10;
if (hun > 0)
{
result.append(one[hun] + " " + huns[1] + " ");
}
if (ten == 0 && ones == 0)
{
}
if (ten == 1)
{
result.append(one[ten * 10 + ones] + " ");
}
else if (ten > 1 && ones >= 1)
{
result.append(tens[ten] + " " + one[ones] + " ");
}
else if (ten > 1)
{
result.append(tens[ten] + " ");
}
else if (ones >= 1 && ten == 0)
{
result.append(one[ones] + " ");
}
//result.append(huns[(int)h] + " ");
h--;
x--;
}
d = sizeof frac / sizeof(long);
while (d > 1)
{
if (tths == 0 && hths == 0)
{
result.append("No cents");
}
else if (hths >= 1 && tths == 0)
{
result.append(one[hths] + " Cents");
}
else if (tths == 1)
{
result.append(one[tths * 10 + hths] + " Cents");
}
else if (tths > 1 && hths >= 1)
{
result.append(tens[tths] + " " + one[hths] + " Cents");
}
else if (tths > 1 && hths == 0)
{
result.append(tens[tths] + " Cents");
}
d--;
}
cout << result << "\n";
}
int main()
{
cout << "Ryan Cloud\nThis is The Written number project\n";
//get a number
//turn number to word form
//display word form
// ask the user to input a dollar amount
cout << "\nEnter a Dollar amount\n";
cin >> userInput;
if(userInput > 0)
{
fraction = modf(userInput, &number);
// This seperates the number and the decimal into 2 variables.
//Number gets just the number and fraction just gets the decimal point from it.
getLength ();
getNumber ();
getDecimal ();
getHuns ();
getToWords ();
}
else if (userInput == 0)
{
cout << one[0] << "\n";
}
else
{
cout << "Error\n";
}
return 0;
}
|