Guest User

Untitled

a guest
Apr 6th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include<cmath>
  4.  
  5. using namespace std;
  6.  
  7. void notFunction(int array1[]);
  8. void andFunction(int array1[], int array2[]);
  9. void orFunction(int array1[], int array2[]);
  10. void convertFunction(int array1[]);
  11. void LshiftFunction(int array1[], int shift);
  12.  
  13.  
  14.  
  15.  
  16. int main() {
  17.  
  18. enum CommandList {NOT, AND, OR, CONVERT, LSHIFT};
  19.  
  20. const int numberOfCommands = 5;
  21.  
  22. ifstream dataFile;
  23. dataFile.open("binaryData.txt");
  24.  
  25.  
  26.  
  27. char array1[8];
  28. int array2[8];
  29. // int shift;
  30. string commands[numberOfCommands];
  31. if(dataFile.is_open())
  32. {
  33. int count = 0;
  34.  
  35. for(int i = 0; i < 10; i++)
  36. {
  37. dataFile >> commands[count];
  38. dataFile >> array1[count];
  39.  
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. } else
  48. {
  49. cout<<"Error openning binaryData file.";
  50. return 0;
  51. }
  52.  
  53.  
  54. // notFunction();
  55. // andFunction(array1, array2);
  56. // orFunction(array1, array2);
  57. // convertFunction(array1);
  58. //cin >> shift;
  59. // LshiftFunction(array1, shift);
  60.  
  61.  
  62. return 0;
  63. }
  64.  
  65.  
  66. void notFunction(int array1[]){
  67.  
  68. for(int i = 0; i < 8; i++){
  69. if(array1[i] == 0){
  70. array1[i] = 1;
  71. }else{
  72. array1[i] = 0;
  73. }
  74. }
  75. for(int i = 0; i < 8; i++){ //displays edited array
  76. //cout << array1[i] << " ";
  77. }
  78.  
  79.  
  80. }
  81.  
  82. void andFunction(int array1[], int array2[]){
  83. int result[8];
  84.  
  85.  
  86. for(int i = 0; i < 8; i++){ //if array 1 and 2 have both 1 in same index, final result is 1
  87.  
  88. if(array1[i] == 1 and array2[i] == 1){
  89. result[i] = 1;
  90. }else{ //otherwise final result for index is 0
  91. result[i] = 0;
  92. }
  93.  
  94. }
  95. for(int i = 0; i < 8; i++){ //displays edited array
  96. //cout << array1[i] << " ";
  97. }
  98.  
  99.  
  100.  
  101. }
  102.  
  103. void orFunction(int array1[], int array2[]){
  104. int result[8];
  105.  
  106. for(int i = 0; i < 8; i++){ //if array 1 or 2 have 1 in same index, final result is 1
  107.  
  108. if(array1[i] == 1 or array2[i] == 1){
  109. result[i] = 1;
  110. }else{ //otherwise final result for index is 0
  111. result[i] = 0;
  112. }
  113. }
  114.  
  115. }
  116.  
  117.  
  118. void convertFunction(int array1[]){
  119. int result=0, temp=0;
  120.  
  121. int power = 8;
  122.  
  123. for(int i = 0; i < 8; i++){
  124. power--;
  125.  
  126. temp = (array1[i] * (pow(2,power)));
  127.  
  128.  
  129. result = result + temp;
  130. }
  131.  
  132. //cout << result << endl;
  133.  
  134.  
  135. //cout << result;
  136.  
  137. }
  138.  
  139. void LshiftFunction(int array1[], int shift){
  140.  
  141. //int array1[] = {1,1,0,0,1,0,1,1};
  142.  
  143. for(int j = 0; j < shift; j++){ //shifts everything to the left by the amount of times indicated
  144. for(int i = 0; i<8; i++){
  145.  
  146. array1[i]=array1[i + 1];
  147. }
  148. }
  149. for (int i = 0; i< shift; i++){ //replaces last numbers with 0
  150. array1[7 - i]=0;
  151. }
  152.  
  153.  
  154. for(int i = 0; i < 8; i++){ //displays edited array
  155. //cout << array1[i] << " ";
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment