Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <sstream>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. template <class N> N ToNumber(string string_number) {
  15.     N number;
  16.     istringstream iss(string_number);
  17.     iss >> number;
  18.     return number;
  19. }
  20.  
  21.  
  22.  
  23. string ReverseString(string str) {
  24.     string new_string = "";
  25.     for(int i=(str.length()-1); i>=0; i--)
  26.         new_string += str.at(i);
  27.     return new_string;
  28. }
  29.  
  30.  
  31.  
  32.  
  33. string binary(int num) {
  34.     int coefficient = -1, numerator  = num;
  35.     string binary;
  36.     string result;
  37.  
  38.  
  39.  
  40.     if(num <=7) {
  41.         result= "";
  42.         if(num <=3) {
  43.             result="0";
  44.             if(num <=1) {
  45.                 result="00";
  46.             }
  47.         }
  48.     }
  49.  
  50. //  if(num<=7){
  51. //      if(num<=1)binary="00";
  52. //      else if(num<=3)binary="0";
  53. //      else binary="";
  54. //
  55. //  }
  56.     while(coefficient != 0) {
  57.         binary += to_string(numerator % 2);
  58.         coefficient = numerator / 2;
  59.         numerator = coefficient;
  60.     }
  61.     binary =  ReverseString(binary);
  62.     result+=binary;
  63.     return result;
  64. }
  65.  
  66.  
  67.  
  68. string printWords(string str) {
  69.     string word;
  70.     stringstream iss(str);
  71.  
  72.     while (iss >> word)
  73.         return word;
  74. }
  75.  
  76.  
  77. unsigned long long convertBinaryToDecimal(string  n) {
  78.     string reverse = ReverseString(n);
  79.     char cstr[n.size()];
  80.     strcpy(cstr, reverse.c_str());
  81.     unsigned int decimalNumber = 0, i = 0, remainder;
  82.     while (i!=sizeof(cstr)) {
  83.         remainder = (int)cstr[i] -48;
  84.         decimalNumber += (remainder*(pow(2,i)));
  85.         ++i;
  86.     }
  87.     return decimalNumber;
  88. }
  89.  
  90. int main() {
  91.  
  92.     std::vector<string> ans (31);
  93.  
  94.     int number1;
  95.  
  96.     int number2;
  97.  
  98.     int number3;
  99.  
  100.     string inst;
  101.  
  102. /// test input
  103.     cout << "input your instruction :";
  104.     cin >> inst;
  105.  
  106.     cout << "input your rd :";
  107.     cin  >>number1;
  108.  
  109.     cout << "input your rs :";
  110.     cin  >>number2;
  111.  
  112.     cout << "input your rt :";
  113.     cin  >>number3;
  114.  
  115.  
  116.  
  117.     if(inst =="add" || inst =="nand") { // i-type
  118.  
  119.  
  120.         /*  ans.push_back(binary(number1));
  121.  
  122.  
  123.             for(int i =0; i<14 ; i++) {
  124.                 ans.push_back("0");
  125.             }
  126.  
  127.  
  128.             ans.push_back(binary(number2));
  129.             ans.push_back(binary(number3));
  130.  
  131.  
  132.             for(int j =0; j<3 ; j++) {   // opcode=000
  133.                 ans.push_back("0");
  134.             }
  135.             for(int j =0; j<8 ; j++) {      //fill 0 25-31
  136.                 ans.push_back("0");
  137.             }*/
  138.  
  139.         for(int j =0; j<7 ; j++) {      //fill 0 25-31
  140.             ans.push_back("0");
  141.         }
  142.  
  143.         for(int j =0; j<3 ; j++) {   // opcode=000
  144.             ans.push_back("0");
  145.         }
  146.         ans.push_back(binary(number3));
  147.         ans.push_back(binary(number2));
  148.  
  149.         for(int i =0; i<13 ; i++) {
  150.             ans.push_back("0");
  151.         }
  152.         ans.push_back(binary(number1));
  153.  
  154.  
  155.  
  156.     } else
  157.  
  158.         if(inst =="jalr" ) { // j-type
  159.  
  160.             for(int j =0; j<7 ; j++) {      //fill 0 25-31
  161.                 ans.push_back("0");
  162.             }
  163.             ans.push_back("101");// opcode=101
  164.             ans.push_back(binary(number2));
  165.             ans.push_back(binary(number1));
  166.             for(int i =0; i<16 ; i++) {
  167.                 ans.push_back("0");
  168.             }
  169.  
  170.         }
  171.  
  172.     if(inst == "halt" || inst =="noop") {
  173.         for(int j =0; j<7 ; j++) {      //fill 0 25-31
  174.             ans.push_back("0");
  175.         }
  176.         if(inst == "halt") {
  177.             ans.push_back("110");// opcode=110
  178.         }
  179.         if(inst == "noop") {
  180.             ans.push_back("111");// opcode=111
  181.         }
  182.         for(int i =0; i<22 ; i++) {
  183.             ans.push_back("0");
  184.         }
  185.     }
  186.  
  187.  
  188.     string ansz;
  189.     unsigned long long int_ans=0;
  190.     for(int i =0; i<ans.size() ; i++) { // print out the result!!
  191.         ansz+= ans.at(i);
  192.     }
  193.  
  194.     int_ans= convertBinaryToDecimal(ansz);
  195.     cout << ansz.length();
  196.     cout <<endl;
  197.     cout <<ansz;
  198.     cout <<endl;
  199.     cout << int_ans;
  200.     cout <<endl;
  201.     cout << binary(number1);
  202.     cout <<endl;
  203.     cout << binary(number2);
  204.     cout <<endl;
  205.     cout << binary(number3);
  206.     cout <<endl;
  207.  
  208.     ifstream file("C:/Users/LuckyCat/Desktop/comarch/test1.txt");
  209.     string str;
  210.     while (std::getline(file, str)) {
  211.         printWords(str);
  212.     }
  213.  
  214.  
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement