2014年10月30日 星期四

[C++]加上LOC PS:十六進位 十進位

#include<iostream>
#include<iomanip>
#include<string>
#include <fstream>    
#include<vector>
#include <algorithm>
#include<math.h>
using namespace std;
int main()
{
int i, j, k,l,m,n,o;
int hex = 0,count,len,start,end;
char t;
fstream file;
string filename;//INTFILE.txt
fstream outputname;
fstream SYMTAB;
char int_len[7] ;
cin >> filename;
file.open(filename, ios::in);        //將檔案開啟為輸入狀態
outputname.open("answer.txt", ios::out);
SYMTAB.open("SYMTAB.txt", ios::out);
if (!file)     //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1);     //在不正常情形下,中斷程式的執行
}
else
{
string inputline;
string tmp = "";
string op[] = { "ADD", "AND", "COMP", "DIV", "J", "JEQ", "JGT", "JLT", "JSUB", "LDA", "LDCH", "LDL", "LDX", "MUL", "OR", "RD", "RSUB", "STA", "STCH", "STL", "STSW", "STX", "SUB", "TD", "TIX", "WD" }; //SIC
string op_code_table[] = { "18", "40", "28", "24", "3C", "30", "34", "38", "48", "00", "50", "08", "04", "20", "44", "D8", "4C", "0C", "54", "14", "E8", "10", "1C", "E0", "2C", "DC" };
vector<string>label;
vector<string>opcode;
vector<string>operand;
vector<int>LOC;
LOC.push_back(0);
LOC.push_back(0);
while (getline(file, inputline))
{
if (inputline[0] == '.')
continue;
////////////// L a b e l///////////////
tmp = "";
for (i = 0; i < 8; i++)
if (inputline[i] == ' ')
continue;
else
tmp = tmp + (inputline[i]);
label.push_back(tmp);
////////////// O p c o d e/////////////
tmp = "";
for (j = 9; j<15; j++)
{
if (inputline[j] == '\0')
break;
else if (inputline[j] == ' ')
continue;
else
tmp = tmp + (inputline[j]);

}
opcode.push_back(tmp);
////////////// O p e r a n d/////////////
tmp = "";
for (k = 17; k<35; k++)
{
if ((inputline.length()<17) || inputline[k] == '\0' || inputline[k] == '\n' || inputline[k] == 'NULL')
break;
else if (inputline[k] == ' ')
continue;
else
tmp = tmp + (inputline[k]);
}
operand.push_back(tmp);
}
for (i = 0; i < label.size();i++)
transform(label[i].begin(), label[i].end(), label[i].begin(), toupper);
for (j = 0; j < opcode.size(); j++)
transform(opcode[j].begin(), opcode[j].end(), opcode[j].begin(), toupper);
for (k = 0; k < operand.size(); k++)
transform(operand[k].begin(), operand[k].end(), operand[k].begin(), toupper);
for (j = 0; j < opcode.size(); j++)
////// C 'eof' ////////////
for (k = 0; k < operand.size(); k++)
{
if (!(operand[k].find("C'")))
{
tmp = "C'";
for (i = 2; i < operand[k].length(); i++)
{
if (operand[k][i] == '\'')
{
tmp = tmp + '\'';
break;
}
t = operand[k][i] + 32;
tmp = tmp + t;
}
operand[k] = tmp;
}
}
for (m = 1; m < opcode.size()-1; m++) //special opcode
{
if (opcode[m] == "BYTE")
{
count = 0;
if (!operand[m].find("C"))
{
for (i = 2; operand[m][i] != '\''; i++)
count++;
LOC.push_back(LOC[m] + count);
}
else
LOC.push_back(LOC[m] + 1);
}
else if (opcode[m] == "RESW")
LOC.push_back(LOC[m] + 3 * atoi(operand[m].c_str()));
else if (opcode[m] == "RESB")
LOC.push_back(LOC[m] + atoi(operand[m].c_str()));
else
LOC.push_back(LOC[m] + 3);
}
for (n = 0; n < operand[0].size(); n++) //START to 10's
{
if (operand[0][n] == 'A')
hex = hex + 10*pow(16.0, operand[0].size() - 1-n);
else if (operand[0][n] == 'B')
hex = hex + 11 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'C')
hex = hex + 12 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'D')
hex = hex + 13 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'E')
hex = hex + 14 * pow(16.0, operand[0].size() - 1 - n);
else if (operand[0][n] == 'F')
hex = hex + 15 * pow(16.0, operand[0].size() - 1 - n);
else
hex = hex + (operand[0][n]-'0')*pow(16.0, (operand[0].size() - n - 1));
}

len = 6-operand[0].length();
//// P r i n t //////////////////
for (l = 0; l < label.size(); l++)
{
for (i = 0; i < len; i++)
outputname << "0";
outputname << std::uppercase << std::hex << LOC[l] + hex << " ";
outputname << left << setw(9) << label[l];
if (operand[l].length() == 0)
outputname << opcode[l] << endl;
else
outputname << setw(6) << opcode[l] << "  " << operand[l] << endl;
}
}
return 0;
}

2014年10月25日 星期六

[C++]assembly read split

#include<iostream>
#include<iomanip>
#include<string>
#include <fstream>    
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
int i, j, k,l;
char t;
fstream file;
string filename;//EXER3-input2.txt
fstream outputname;

cin >> filename;
file.open(filename, ios::in);        //將檔案開啟為輸入狀態
outputname.open("answer.txt", ios::out);
if (!file)     //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1);     //在不正常情形下,中斷程式的執行
}
else
{
string inputline;
string tmp = "";
vector<string>label;
vector<string>opcode;
vector<string>operand;

while (getline(file, inputline))
{
if (inputline[0] == '.')
continue;
////////////// L a b e l///////////////
tmp = "";
for (i = 0; i < 8; i++)
if (inputline[i] == ' ')
continue;
else
tmp = tmp + (inputline[i]);
label.push_back(tmp);
////////////// O p c o d e/////////////
tmp = "";
for (j = 9; j<15; j++)
{
if (inputline[j] == '\0')
break;
else if (inputline[j] == ' ')
continue;
else
tmp = tmp + (inputline[j]);

}
opcode.push_back(tmp);
////////////// O p e r a n d/////////////
tmp = "";
for (k = 17; k<35; k++)
{
if ((inputline.length()<17) || inputline[k] == '\0' || inputline[k] == '\n' || inputline[k] == 'NULL')
break;
else if (inputline[k] == ' ')
continue;
else
tmp = tmp + (inputline[k]);
}
operand.push_back(tmp);
}
for (i = 0; i < label.size();i++)
transform(label[i].begin(), label[i].end(), label[i].begin(), toupper);
for (j = 0; j < opcode.size(); j++)
transform(opcode[j].begin(), opcode[j].end(), opcode[j].begin(), toupper);
for (k = 0; k < operand.size(); k++)
transform(operand[k].begin(), operand[k].end(), operand[k].begin(), toupper);
////// C 'eof' ////////////
for (k = 0; k < operand.size(); k++)
{
if (!(operand[k].find("C'")))
{
tmp = "C'";
for (i = 2; i < operand[k].length(); i++)
{
if (operand[k][i] == '\'')
{
tmp = tmp + '\'';
break;
}
t = operand[k][i] + 32;
tmp = tmp + t;
}
operand[k] = tmp;
}
}
//// P r i n t //////////////////
for (l = 0; l < label.size(); l++)
{
outputname << left << setw(9) << label[l];
if (operand[l].length() == 0)
outputname << opcode[l] << endl;
else
outputname << setw(6) << opcode[l] << "  " << operand[l] << endl;
}
}
return 0;
}

2014年10月12日 星期日

GCD 輾轉相除法

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int number1, number2, tmp, Quotient=9999, Remainder=9999;
cout << "請先輸入兩個數字" << endl;
cin >> number1;
cin >> number2;
if (number2 > number1)
{
tmp = number1;
number1 = number2;
number2 = tmp;
}
while (true)
{
Quotient = number1 / number2;
if ((number1 % number2) == 0)
{
cout << number2 << endl;
break;
}
Remainder = number1 % number2;
number1 = number2;
number2 = Remainder;
}
return 0;
}

2014年9月28日 星期日

單精準度轉換成十進位

http://zh.wikipedia.org/wiki/%E5%96%AE%E7%B2%BE%E5%BA%A6%E6%B5%AE%E9%BB%9E%E6%95%B8

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int main()
{
//01000000011000000000000000000000
//10111111100000000000000000000000
//01000001010010000000000000000000
string input;
string a="",b="",c="",b_invert="";
double d_b=0.0,d_c=0.0,ans=0;
cout<<"please input number";
cin>>input;
cout<<"szie="<<input.length()<<endl;
a=input[0];

for(int i=1;i<=8;i++)
b=b+input[i];
for(int j=7;j>=0;j--)
b_invert=b_invert+b[j];

for(int i=0;i<=7;i++)
d_b=d_b+(b_invert[i]-48)*pow(2.0,i);
d_b=d_b-127;

cout<<"exp = "<<d_b<<endl;


for(int k=9;k<=31;k++)
c=c+input[k];

for(int l=0;l<=22;l++)
d_c=d_c+(c[l]-48)*pow(2.0,-1*(l+1));
cout<<"dc = "<<d_c<<endl;
d_c=1+d_c;

cout<<"fraction = "<<d_c<<endl;

ans=d_c*pow(2.0,d_b);
if(a=="0")
cout<<"ans = +" ;
else
cout<<"ans = -" ;
cout<<ans<<endl;

return 0;
}

2014年8月25日 星期一

複習stack

#include <iostream>
using namespace std;
class my_stack
{
    private:
        int *number;
        int pos;
        int size;
    public:
        my_stack()                                          //default
        {
            number=new int[100];
            pos=-1;
            size=100;
        }
        my_stack(int len)                                   //assign size
        {
            number=new int[len];
            pos=-1;
            size=len;
        }
        void pop()                                          //pop
        {
            cout<<"pop "<<number[pos]<<endl;
            pos--;
            if(pos==-2)
            {
                cout<<"====================="<<endl;
                cout<<"index can't be slower than 0,try again"<<endl;
                cout<<"====================="<<endl;
                pos++;
            }

        }
        void push(int num)                                  //push
        {
            pos++;
            if(pos+1>size)
            {
                cout<<"====================="<<endl;
                cout<<"error buffer is filled,try again"<<endl;
                cout<<"====================="<<endl;
                pos--;
            }
            else
                number[pos]=num;
        }
        void show()                                         //show
        {
            if(pos==-1)
            {
                cout<<"====================="<<endl;
                cout<<"there is no content"<<endl;
                cout<<"====================="<<endl;
            }
            else
            {
                cout<<"====================="<<endl;
                cout<<"index low to high"<<endl;
                cout<<"stack contents"<<endl;
                cout<<"====================="<<endl;
                for(int i=pos;i>=0;i--)
                    cout<<"|"<<"__"<<number[i]<<"__"<<"|"<<endl;
            }
        }
        ~my_stack(){}                                       //delete
};
int main()
{
    int number;
    int opt;
    int size;
    cout<<"input stack size : ";
    cin>>size;
    my_stack source(size);

    cout<<"1 --- push "<<endl
        <<"2 --- pop  "<<endl
        <<"3 --- show "<<endl;

    while(true)
    {
      cout<<"command number : " ;
      cin>>opt;
      switch(opt)
      {
          cin>>opt;
          case 1:
              cout<<"enter a number : ";
              cin>>number;
              source.push(number);
              break;
          case 2:
              source.pop();
              break;
          case 3:
              source.show();
            break;

          default:
            cout<<"input error , try again"<<endl;
            break;
      }
    }
    return 0;
}

2014年4月6日 星期日

MIPS 輸入10組密碼 如果是這十組密碼內 則成功

.data
value: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
msg1: .asciiz "correct\n“
msg2: .asciiz "incorrect please try again\n“
msg3: .asciiz "===========================\n“
.text
main:
 la $t0, value         #  array
 li $s1, 0        #  i
 li $t1, 0        #  index
 li $t2, 0        #  arr[index]
read:
 li $v0, 5
 syscall
 add $t2,$t1,$t0        #t2=arr[t1]
 sw $v0, 0($t2)         #store to array
 add $t1,$t1,4
 add $s1, $s1, 1        #  i++
 beq $s1, 10 , line  #  i<10?
 j read
line:
 li $v0, 4        # draw a line
 la $a0, msg3
 syscall
guess:
 li $s3, 0        #  k
 li $t3, 0        #  index2
 li $t4, 0        #  arr[index2]
 li $v0, 5
 syscall
inner_for:
 add $t4,$t3,$t0
 lw  $t5,0($t4)        #arr[index2] load to $t5
 bne $v0,$t5,ER
 li $v0, 4
 la $a0, msg1  #print correct
 syscall
 j guess   #restart guess
ER:
 add $s3, $s3, 1        #  k++
 add $t3,$t3,4
 beq $s3, 10 , while  #  k<10?
 j inner_for
while:
 li $v0, 4
 la $a0, msg2  #print incorrect
 syscall
 j guess

2014年4月2日 星期三

LISP

 (fast 2 * n 1)  ==> 2^n
 (fast 2 + n 0)  ==> 2*n

(define (fast a op n unit)
 (define (square x) (op x x))
 (define (iter a n result)
         (cond ((= n 0) result)
               ((even? n) (iter (square a) (/ n 2) result))
               (else (iter a (- n 1) (op a result)))))
(iter a n unit))