Advertisement
Guest User

Untitled

a guest
Nov 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. Write a program that allows the user to convert numbers between any 2 basis. Your program should follow the requirements:
  2. Your program should support all the basis between 2 and 16 inclusively.
  3. It should loop and repeatdly take user input and perform the following operations (shown also in the sample run below): exit the menu, convert a number from any base to any other base, print a file, solve a file.
  4. The repetition over the menu options should be implemented using a loop, not recursive calls to main.
  5. If a number does not have a not valid representation for the basis that it is represented in, the program should indicate that (by printing a message). It should not crash and should also not work with the number. E.g. see the behavior:
  6. Enter a number, current_base, new_base(e.g.: 129,10,2): 127,7,10
  7. Invalid representation n = 127 in base : 7
  8.  
  9. The program should be able to perform multiple conversions specified in a 'test file'. Here the 'test file' has the meaning of a 'homework file'. It specifies a set of exercises to be solved. In particular, each line of the file specifies a number and the basis it is written in and a set of other basis to be converted to. The program should be able to read all the lines in such a file, solve each line and write the result in another file. The line format is:
  10. current_base : number, new_base_1, new_base_2, ..., new_base_M
  11.  
  12. The answer for a line in the format above should include the number representation for each of the new bases:
  13. current_base : number, new_base_1: number_1, new_base_2: number_2, ..., new_base_M : number_M
  14.  
  15. Notice in the example above that collons separate the base from the number and commas separate pairs of base and number.
  16. Your files (both the test and the solution) should follow the format indicated above.
  17. If the number representation is wrong in the test file, the program should also indicate that in the solution file.
  18. Your program should work for other test files with more or less basis transformations per line.
  19. Your program should work name the solution file based on the test file name, by appending '_solution' to the filename (still with extension '.txt'). For example in my sample run I used the file 'test1.txt' and I hase saved the answers to a file named 'test1_solution.txt'.
  20. You can organize your solution however you want. I recommend writting a function for each of the following operations:
  21. test that a number is valid for the base it is represented in: it contains no illegal symbols. E.g. of bad representations: 127 (in base 7), 1G (in base 16), 1A (in base 10 or less).
  22. convert from base 10 to any other base. This function should take all the arguments it needs: number, new base.
  23. convert from any base to base 10
  24. a function (or at least a structure) to map from symbols to values (e.g. 'A' is 10, 'B' is 11, ..., 'F' is 15).
  25. a function (or at least a structure) to map from values to symbols (e.g. 10 is 'A', 11 is 'B', ..., 15 is 'F').
  26. Consider how you want to represent a number (since you have to process it symbol by symbol)
  27. The test file and the solution produced for it in the sample runs are: test1.txt , and test1_solution.txt
  28.  
  29. Sample run:
  30.  
  31. >>>
  32.  
  33. 0. Exit.
  34. 1. Convert a number.
  35. 2. Solve a test (from a file).
  36. 3. Print a file.
  37.  
  38. Enter your choice: 1
  39. Enter a number, current_base, new_base(e.g.: 129,10,2): 129,10,2
  40. 2 : 010000001
  41.  
  42. 0. Exit.
  43. 1. Convert a number.
  44. 2. Solve a test (from a file).
  45. 3. Print a file.
  46.  
  47. Enter your choice: 1
  48. Enter a number, current_base, new_base(e.g.: 129,10,2): Aa,16,10
  49. 10 : 0170
  50.  
  51. 0. Exit.
  52. 1. Convert a number.
  53. 2. Solve a test (from a file).
  54. 3. Print a file.
  55.  
  56. Enter your choice: 1
  57. Enter a number, current_base, new_base(e.g.: 129,10,2): 127,7,10
  58. Invalid representation n = 127 in base : 7
  59.  
  60. 0. Exit.
  61. 1. Convert a number.
  62. 2. Solve a test (from a file).
  63. 3. Print a file.
  64.  
  65. Enter your choice: 1
  66. Enter a number, current_base, new_base(e.g.: 129,10,2): 101,2,10
  67. 10 : 05
  68.  
  69. 0. Exit.
  70. 1. Convert a number.
  71. 2. Solve a test (from a file).
  72. 3. Print a file.
  73.  
  74. Enter your choice: 101,2,3
  75. Invalid choice.
  76.  
  77. 0. Exit.
  78. 1. Convert a number.
  79. 2. Solve a test (from a file).
  80. 3. Print a file.
  81.  
  82. Enter your choice: 1
  83. Enter a number, current_base, new_base(e.g.: 129,10,2): 101,2,3
  84. 3 : 012
  85.  
  86. 0. Exit.
  87. 1. Convert a number.
  88. 2. Solve a test (from a file).
  89. 3. Print a file.
  90.  
  91. Enter your choice: 3
  92. Enter the name of the file to be printed: test1.txt
  93. --------------------
  94. 10:8, 2, 3, 7, 10
  95. 5:123, 10, 2, 3,5
  96. 7:127, 3, 5,7
  97. --------------------
  98.  
  99. 0. Exit.
  100. 1. Convert a number.
  101. 2. Solve a test (from a file).
  102. 3. Print a file.
  103.  
  104. Enter your choice: 2
  105. Enter the name of the test file: test1.txt
  106. Error number format given base: 7 :
  107. Written solution to file: test1_solved.txt
  108. The solution is:
  109. --------------------
  110. 10:8 , 2 : 1000 , 3 : 022 , 7 : 011 , 10 : 08
  111. 5:123 , 10 : 038 , 2 : 0100110 , 3 : 01102 , 5 : 0123
  112. 7:127 base number error
  113.  
  114. --------------------
  115.  
  116. 0. Exit.
  117. 1. Convert a number.
  118. 2. Solve a test (from a file).
  119. 3. Print a file.
  120.  
  121. Enter your choice: 3
  122. Enter the name of the file to be printed: test1_solved.txt
  123. --------------------
  124. 10:8 , 2 : 1000 , 3 : 022 , 7 : 011 , 10 : 08
  125. 5:123 , 10 : 038 , 2 : 0100110 , 3 : 01102 , 5 : 0123
  126. 7:127 base number error
  127.  
  128. --------------------
  129.  
  130. 0. Exit.
  131. 1. Convert a number.
  132. 2. Solve a test (from a file).
  133. 3. Print a file.
  134.  
  135. Enter your choice: 0
  136. >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement