fork(1) download
  1. //Austin Liu
  2. //ACSL Contest 2
  3. //Senior Division
  4. //Print Formatting
  5.  
  6. #include<iostream>
  7. #include<string.h>
  8. #include<iomanip>
  9.  
  10. using namespace std;
  11.  
  12. int digits(int);
  13. string commify(unsigned long long);
  14. int digitsPast(string);
  15.  
  16. int main()
  17. {
  18. //Declarations
  19. string input[5];
  20. string format[5];
  21. string valueNum[5];
  22. double value[5];
  23.  
  24. int numOfAmp[5];
  25. int numOfAmpAfter[5];
  26.  
  27. bool comma[5];
  28. bool period[5];
  29. bool dollar[5];
  30. bool starDollar[5];
  31. bool exponential[5];
  32.  
  33. string fin[5];
  34.  
  35. int numOfTrials = 5;
  36.  
  37. for (int i = 0; i < numOfTrials; i++)
  38. {
  39. comma[i] = false;
  40. period[i] = false;
  41. dollar[i] = false;
  42. starDollar[i] = false;
  43. exponential[i] = false;
  44. }
  45.  
  46. cout << "AUSTIN LIU" << endl
  47. << "HTHS" << endl
  48. << "ACSL CONTEST 2: PRINT FORMATTING" << endl
  49. << "2013-2014" << endl << endl;
  50.  
  51. cout << "INPUT: " << endl;
  52.  
  53. for (int i = 0; i < numOfTrials; i++)
  54. {
  55. //Input
  56. cout << i+1 << ". ";
  57. getline(cin, input[i]);
  58.  
  59. format[i] = input[i].substr(0,input[i].find_last_of(','));
  60. valueNum[i] = input[i].substr(input[i].find_last_of(',')+2, input[i].length());
  61. value[i] = atof(valueNum[i].c_str());
  62.  
  63. //Testing for Cases
  64. for (int j = 0; j < format[i].length(); j++)
  65. {
  66. if (format[i][j] == ',')
  67. {
  68. comma[i] = true;
  69. }
  70.  
  71. if (format[i][j] == '.')
  72. {
  73. period[i] = true;
  74. }
  75. }
  76.  
  77. if(format[i][0] == '$')
  78. {
  79. dollar[i] = true;
  80. }
  81. if(format[i][0] == '*' && format[i][1] == '$')
  82. {
  83. starDollar[i] = true;
  84. }
  85. if(format[i][format[i].length()-1] == 'E')
  86. {
  87. exponential[i] = true;
  88. }
  89.  
  90. //Counting
  91. if (period[i] == false)
  92. {
  93. int tempCount = 0;
  94. for(int j = 0; j < format[i].length(); j++)
  95. {
  96. if(format[i][j] == '&')
  97. {
  98. tempCount++;
  99. }
  100. }
  101. numOfAmp[i] = tempCount;
  102. numOfAmpAfter[i] = -1;
  103. }
  104. else
  105. {
  106. int tempCount1 = 0;
  107. int tempCount2 = 0;
  108. for(int j = 0; j < format[i].find('.'); j++)
  109. {
  110. if(format[i][j] == '&')
  111. {
  112. tempCount1++;
  113. }
  114. }
  115. for(int j = format[i].find('.'); j < format[i].length(); j++)
  116. {
  117. if(format[i][j] == '&')
  118. {
  119. tempCount2++;
  120. }
  121. }
  122. numOfAmp[i] = tempCount1;
  123. numOfAmpAfter[i] = tempCount2;
  124. }
  125. }
  126.  
  127. cout << endl;
  128.  
  129. cout << "OUTPUT: " << endl;
  130.  
  131. //Answers
  132. for(int i =0; i < 5; i++)
  133. {
  134. cout << i+1 << ". ";
  135. //Tested
  136. if(dollar[i] && !starDollar[i])
  137. {
  138. cout << "$";
  139. }
  140. //Tested
  141. if (starDollar[i])
  142. {
  143. int temp = (int) value[i];
  144. for (int j = 0; j < numOfAmp[i] - digits(temp); j++)
  145. {
  146. cout << "*";
  147. }
  148. cout << "$";
  149. }
  150. //Tested
  151. if ((dollar[i]||starDollar[i]) && period[i])
  152. {
  153. int temp = (int) value[i];
  154. if (digitsPast(valueNum[i])<=numOfAmpAfter[i])
  155. {
  156. cout << setw(numOfAmpAfter[i]+digits((int) value[i])+1) << setfill('0')<< left<< value[i];
  157. }
  158. if (digitsPast(valueNum[i]) > numOfAmpAfter[i])
  159. {
  160. double temporary = value[i];
  161. for(int j = 0; j < numOfAmpAfter[i]; j++)
  162. {
  163. temporary*=10;
  164. }
  165. temporary = floor(temporary+0.5);
  166. for(int j = 0; j < numOfAmpAfter[i]; j++)
  167. {
  168. temporary/=10;
  169. }
  170. cout << setw(digits(value[i])+numOfAmpAfter[i]) << setfill('0') << left << temporary;
  171. }
  172. }
  173. else if(period[i] && !dollar[i] && !starDollar[i])
  174. {
  175. int temp = (int) value[i];
  176. for (int j = 0; j < numOfAmp[i] - digits(temp); j++)
  177. {
  178. cout << "*";
  179. }
  180. if (digitsPast(valueNum[i])<=numOfAmpAfter[i])
  181. {
  182. cout << setw(numOfAmpAfter[i]+digits((int) value[i])+1) << setfill('0')<< left<< value[i];
  183. }
  184. if (digitsPast(valueNum[i]) > numOfAmpAfter[i])
  185. {
  186. double temporary = value[i];
  187. for(int j = 0; j < numOfAmpAfter[i]; j++)
  188. {
  189. temporary*=10;
  190. }
  191. temporary = floor(temporary+0.5);
  192. for(int j = 0; j < numOfAmpAfter[i]; j++)
  193. {
  194. temporary/=10;
  195. }
  196. cout << setw(digits(value[i])+numOfAmpAfter[i]) << setfill('0') << left << temporary;
  197. }
  198. }
  199.  
  200. //Tested
  201. if(exponential[i])
  202. {
  203. int count1 = 0;
  204. double temp = value[i];
  205. if (value[i] < 1)
  206. {
  207. while(temp<1)
  208. {
  209. temp*=10;
  210. count1++;
  211. }
  212. }
  213. else
  214. {
  215. while(temp>=10)
  216. {
  217. temp/=10;
  218. count1++;
  219. }
  220. }
  221. if(numOfAmp[i] == 1)
  222. {
  223. cout << valueNum[i][0] << 'E' << count1;
  224. }
  225.  
  226. else if((int) value[i]>=1 && (int) value[i]<10 && value[i]-(int)value[i] < 0.0001)
  227. {
  228. cout << (int) value[i] << ".";
  229. for(int j = 0; j < numOfAmp[i]-1; j++)
  230. {
  231. cout << '0';
  232. }
  233. cout << "E0";
  234. }
  235.  
  236. else
  237. {
  238. if (digitsPast(to_string(temp))<=(numOfAmp[i]-1))
  239. {
  240. cout << setw(numOfAmp[i]+1) << setfill('0') << left << temp;
  241. }
  242. if (digitsPast(to_string(temp))>(numOfAmp[i]-1))
  243. {
  244. double temporary = temp;
  245. for(int j = 0; j < numOfAmp[i]-1; j++)
  246. {
  247. temporary*=10;
  248. }
  249. temporary = floor(temporary+0.5);
  250. for(int j = 0; j < numOfAmp[i]-1; j++)
  251. {
  252. temporary/=10;
  253. }
  254. cout << setw(numOfAmp[i]+1) << setfill('0') << left << temporary;
  255. }
  256. cout << "E" << count1;
  257. }
  258. }
  259.  
  260. //Tested
  261. if(comma[i])
  262. {
  263. cout << commify(value[i]);
  264. }
  265.  
  266. //Tested
  267. if(comma[i] == false && period[i] == false && dollar[i] == false && starDollar[i] == false && exponential[i] == false)
  268. {
  269. cout << setfill('*') << setw(numOfAmp[i]) << value[i];
  270. }
  271.  
  272. cout << endl;
  273. }
  274.  
  275. cout << endl;
  276.  
  277. system("PAUSE");
  278. return 0;
  279. }
  280.  
  281. int digits(int x)
  282. {
  283. int digits = 0;
  284. int step = 1;
  285. while (step <= x)
  286. {
  287. digits++;
  288. step *= 10;
  289. }
  290. return digits ? digits : 1;
  291. }
  292.  
  293. string commify(unsigned long long n)
  294. {
  295. string s;
  296. int cnt = 0;
  297. do
  298. {
  299. s.insert(0, 1, char('0' + n % 10));
  300. n /= 10;
  301. if (++cnt == 3 && n)
  302. {
  303. s.insert(0, 1, ',');
  304. cnt = 0;
  305. }
  306. } while (n);
  307. return s;
  308. }
  309.  
  310. int digitsPast(string x)
  311. {
  312. return(x.length()-1-x.find('.'));
  313. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
&&&&, 25
&&.&&&, 3.78
*$&&&.&&, 45.598
&&&E, 1000
&,&&, 384
compilation info
prog.cs(7,0): error CS1024: Wrong preprocessor directive
prog.cs(8,0): error CS1024: Wrong preprocessor directive
prog.cs(9,0): error CS1024: Wrong preprocessor directive
prog.cs(10,6): error CS1041: Identifier expected, `namespace' is a keyword
prog.cs(10,19): error CS1001: Unexpected symbol `;', expecting identifier
prog.cs(19,8): error CS1041: Identifier expected, `string' is a keyword
prog.cs(39,16): error CS1001: Unexpected symbol `comma', expecting identifier
prog.cs(56,16): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(59,43): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(59,67): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(60,45): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(60,67): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(60,91): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(61,49): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(64,52): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(66,24): error CS1041: Identifier expected, `if' is a keyword
prog.cs(68,32): error CS1001: Unexpected symbol `comma', expecting identifier
prog.cs(73,32): error CS1001: Unexpected symbol `period', expecting identifier
prog.cs(79,24): error CS1001: Unexpected symbol `dollar', expecting identifier
prog.cs(83,24): error CS1001: Unexpected symbol `starDollar', expecting identifier
prog.cs(85,45): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(87,24): error CS1001: Unexpected symbol `exponential', expecting identifier
prog.cs(93,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(94,59): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(96,32): error CS1041: Identifier expected, `if' is a keyword
prog.cs(98,40): error CS1001: Unexpected symbol `tempCount', expecting identifier
prog.cs(106,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(108,57): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(110,32): error CS1041: Identifier expected, `if' is a keyword
prog.cs(112,40): error CS1001: Unexpected symbol `tempCount1', expecting identifier
prog.cs(115,50): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(115,77): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(117,32): error CS1041: Identifier expected, `if' is a keyword
prog.cs(119,40): error CS1001: Unexpected symbol `tempCount2', expecting identifier
prog.cs(134,16): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(138,24): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(143,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(146,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(153,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(156,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(160,32): error CS1041: Identifier expected, `double' is a keyword
prog.cs(163,40): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(168,40): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(175,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(178,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(182,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(186,32): error CS1041: Identifier expected, `double' is a keyword
prog.cs(189,40): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(194,40): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(203,24): error CS1041: Identifier expected, `int' is a keyword
prog.cs(207,32): error CS1041: Identifier expected, `while' is a keyword
prog.cs(209,40): error CS1001: Unexpected symbol `temp', expecting identifier
prog.cs(215,32): error CS1041: Identifier expected, `while' is a keyword
prog.cs(217,40): error CS1001: Unexpected symbol `temp', expecting identifier
prog.cs(223,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(228,32): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(231,40): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(238,32): error CS1041: Identifier expected, `if' is a keyword
prog.cs(240,48): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(244,40): error CS1041: Identifier expected, `double' is a keyword
prog.cs(247,48): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(252,48): error CS1001: Unexpected symbol `temporary', expecting identifier
prog.cs(263,24): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(269,24): error CS1001: Unexpected symbol `cout', expecting identifier
prog.cs(283,8): error CS1041: Identifier expected, `int' is a keyword
prog.cs(287,16): error CS1001: Unexpected symbol `digits', expecting identifier
prog.cs(295,2): error CS1041: Identifier expected, `string' is a keyword
prog.cs(299,4): error CS1001: Unexpected symbol `s', expecting identifier
prog.cs(299,12): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(303,6): error CS1001: Unexpected symbol `s', expecting identifier
prog.cs(303,14): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(312,8): error CS1041: Identifier expected, `return' is a keyword
prog.cs(312,23): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(312,34): error CS1001: Unexpected symbol `(', expecting identifier
prog.cs(313,1): error CS8025: Parsing error
Compilation failed: 75 error(s), 0 warnings
stdout
Standard output is empty