Guest User

Untitled

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. char defV = '0';
  8.  
  9. class Register {
  10. public:
  11. vector <char> reg;
  12.  
  13. void setInitial(string s) {
  14. if (s[0] != '1') {
  15. for (int i = 1; i < s.length(); ++i)
  16. {
  17. reg.push_back(s[i]);
  18. }
  19. }
  20. else {
  21. for (int i = 1; i < s.length(); ++i) {
  22. if (s[i] == '1')
  23. {
  24. reg.push_back('0');
  25. }
  26. else {
  27. reg.push_back('1');
  28. }
  29. }
  30. }
  31. }
  32.  
  33. void shiftLeft(int n) {
  34. if (reg.size() <= n)
  35. {
  36. for (int i = 0; i<reg.size(); i++)
  37. {
  38. reg[i] = defV;
  39. }
  40. }
  41. else {
  42. while(n--) {
  43. for (int i = 0; i < reg.size()-1; i++)
  44. {
  45. reg[i] = reg[i+1];
  46. }
  47. reg[reg.size()-1] = defV;
  48. }
  49. }
  50. }
  51.  
  52. void shiftRight(int n) {
  53. if (reg.size() <= n)
  54. {
  55. for (int i = 0; i<reg.size(); i++)
  56. {
  57. reg[i] = defV;
  58. }
  59. }
  60. else {
  61. while(n--) {
  62. for (int i = reg.size()-1; i > 0; i--)
  63. {
  64. reg[i] = reg[i-1];
  65. }
  66. reg[0] = defV;
  67. }
  68. }
  69. }
  70.  
  71. void setLength (int l) {
  72. if (reg.size() > 0) {
  73. reg.insert(reg.begin(),l-reg.size(),'0');
  74. }
  75. else {
  76. for (int i = 0; i < l; ++i)
  77. {
  78. reg.push_back('0');
  79. }
  80. }
  81. }
  82.  
  83. void printBits () {
  84. for (int i = 0; i < reg.size(); ++i)
  85. {
  86. cout << reg[i];
  87. }
  88. cout << endl;
  89. }
  90.  
  91. void printDecimal() {
  92. for (int i = 0; i < reg.size(); ++i)
  93. {
  94. cout << reg[i];
  95. }
  96. int decVal = 0;
  97. int j = 0;
  98. for (int i = reg.size()-1; i>=0; i--) {
  99. int num = reg[i] - '0';
  100. decVal += num*pow(2.0,j);
  101. j++;
  102. }
  103. cout << '(' << decVal << ')' << endl;
  104. }
  105.  
  106. void printHexa() {
  107. for (int i = 0; i < reg.size(); ++i)
  108. {
  109. cout << reg[i];
  110. }
  111. unsigned int hexval = 0, i = 1, rem;
  112. for (int j = reg.size()-1; j>=0; j--) {
  113. rem = reg[j] - '0';
  114. hexval = hexval + rem * i;
  115. i *= 2;
  116. }
  117. printf("(%X)\n",hexval);
  118. }
  119. };
  120.  
  121. int main(int argc, char const *argv[])
  122. {
  123. Register r1;
  124. Register r2;
  125.  
  126. int count = argc - 1;
  127. string arguments[count];
  128.  
  129. for (int i=1; i<argc; i++) {
  130. arguments[i-1] = argv[i];
  131. }
  132.  
  133. int j = 0;
  134. bool isDefault = true;
  135. bool isDefault2 = true;
  136.  
  137. while (j < count) {
  138. if (arguments[j] == "-i")
  139. {
  140. if (j+1 < count)
  141. {
  142. r1.setInitial(arguments[j+1]);
  143. isDefault = false;
  144. j+=2;
  145. }
  146. else {
  147. return -1;
  148. }
  149. }
  150. else if (arguments[j] == "-s")
  151. {
  152. if (j+1 < count && stoi(arguments[j+1]) >= r1.reg.size())
  153. {
  154. r1.setLength(stoi(arguments[j+1]));
  155. j+=2;
  156. }
  157. else {
  158. return -1;
  159. }
  160. }
  161. else if (arguments[j] == "-r")
  162. {
  163. if (j+1 < count)
  164. {
  165. r1.shiftRight(stoi(arguments[j+1]));
  166. j+=2;
  167. }
  168. else {
  169. return -1;
  170. }
  171. }
  172. else if (arguments[j] == "-l")
  173. {
  174. if (j+1 < count)
  175. {
  176. r1.shiftLeft(stoi(arguments[j+1]));
  177. j+=2;
  178. }
  179. else {
  180. return -1;
  181. }
  182. }
  183. else if (arguments[j] == "-v")
  184. {
  185. if (j+1 < count)
  186. {
  187. if(!arguments[j+1].compare("1"))
  188. {
  189. defV = '1';
  190. }
  191. else if(!arguments[j+1].compare("0"))
  192. {
  193. defV = '0';
  194. }
  195. j+=2;
  196. }
  197. else {
  198. return -1;
  199. }
  200. }
  201. else if (arguments[j] == "-p")
  202. {
  203. if (!isDefault && !isDefault2)
  204. {
  205. r1.printBits();
  206. r2.printBits();
  207. j++;
  208. }
  209. else {
  210. return -1;
  211. }
  212. }
  213. else if (arguments[j] == "-I")
  214. {
  215. if (j+1 < count)
  216. {
  217. r2.setInitial(arguments[j+1]);
  218. isDefault2 = false;
  219. j+=2;
  220. }
  221. else {
  222. return -1;
  223. }
  224. }
  225. else if (arguments[j] == "-S")
  226. {
  227. if (j+1 < count && stoi(arguments[j+1]) >= r2.reg.size())
  228. {
  229. r2.setLength(stoi(arguments[j+1]));
  230. j+=2;
  231. }
  232. else {
  233. return -1;
  234. }
  235. }
  236. else if (arguments[j] == "-R")
  237. {
  238. if (j+1 < count)
  239. {
  240. r2.shiftRight(stoi(arguments[j+1]));
  241. j+=2;
  242. }
  243. else {
  244. return -1;
  245. }
  246. }
  247. else if (arguments[j] == "-L")
  248. {
  249. if (j+1 < count)
  250. {
  251. r2.shiftLeft(stoi(arguments[j+1]));
  252. j+=2;
  253. }
  254. else {
  255. return -1;
  256. }
  257. }
  258. else if (arguments[j] == "-d") {
  259. if (!isDefault)
  260. {
  261. r1.printDecimal();
  262. }
  263. if (!isDefault2)
  264. {
  265. r2.printDecimal();
  266. }
  267. j++;
  268. }
  269. else if (arguments[j] == "-h") {
  270. if (!isDefault)
  271. {
  272. r1.printHexa();
  273. }
  274. if (!isDefault2)
  275. {
  276. r2.printHexa();
  277. }
  278. j++;
  279. }
  280. }
  281.  
  282. return 0;
  283. }
Add Comment
Please, Sign In to add comment