#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;
}
//////SYMTAB print//////////////////
for (i = 0; i < opcode.size(); i++)
{
if (opcode[i] == "START")
start = LOC[i];
if (opcode[i] == "END")
end = LOC[i];
}
for (j = 0; j < label.size(); j++)
{
if (j == 0)
{
SYMTAB << std::uppercase << left << setw(7) << label[j];
SYMTAB << right<<setw(6)<<setfill('0') << std::hex << (start + hex)<<" ";
SYMTAB <<setw(6)<< right << (end - start) << endl;
}
else if (label[j] == "")
;
else
{
SYMTAB << std::uppercase << left << setw(6)<<setfill(' ') << label[j]<<" ";
SYMTAB << std::uppercase << right << setw(6) << setfill('0')<< std::hex << LOC[j] + hex << endl;
}
}
}
return 0;
}
2014年11月17日 星期一
2014年11月15日 星期六
c++題目
#include<iostream>
using namespace std;
int strcmp2(char* pStr1, char* pStr2)
{
if (pStr1 == pStr2)
return 0;
else
return 1;
}
int func()
{
static int iNumber1 = 10;
int iNumber2 = 0;
iNumber1++;
iNumber2++;
return (iNumber1 + iNumber2);
}
int foo(int x,int y)
{
while (x)
{
++x;
++y;
}
if (y)
--y;
return x + y;
}
void funct1(int *x)
{
x = (int *)malloc(sizeof(int));
*x = 12;
}
int Test(int n)
{
int i;
int ret = 0;
for (i = 1; i <= --n; i++) {
if (i > 12)
{
ret /= 2;
break;
}
if (i >= 4 && i <= 6)
continue;
++ret;
}
return ret;
}
void printsum(int x, int y)
{
int sum = 0;
#ifdef DO_IT
sum = x + y;
#endif
printf("%d\n", sum);
}
void test1()
{
printsum(1, 2);
}
#define DO_IT
void test2()
{
printsum(3, 4);
}
void m_14()
{
int a = 5;
{
int b = 10;
++a;
++b;
{
int a = 20;
++a;
a = ++b;
}
++a;
++b;
printf("%d %d ", a,b);
}
printf("%d\n", a);
}
#define SQUARE(z) z * z
void foo3(int x, int *py)
{
int n = SQUARE(x + 1);
x = *py;
*py = n;
}
int foo2(int iParam1,int iParam2)
{
int iRet;
if (iParam1 = 2 || iParam2 == 2)
iRet = iParam1 + iParam2;
else
iRet = iParam1 - iParam2;
return iRet;
}
int main()
{
int opt = 0;
cout << "請輸入想知道第幾題答案" << endl;
cin >> opt;
switch (opt)
{
case 1:
{
cout << "第" << opt << "題:";
char str1[] = "abc";
char str2[] = "abc";
int iVal = strcmp2(str1, str2);
printf("%d\n", iVal);
break;
}
case 2:
{
cout << "第" << opt << "題:";
int iVal;
int i;
for (i = 0; i < 2; i++)
iVal = func();
printf("%d\n", iVal);
break;
}
case 3:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b)&c;
printf("%d\n", d);
break;
}
case 4:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b) &c;
printf("%X\n", e);
break;
}
case 5:
{
cout << "第" << opt << "題:";
union u_tag{
char cVal[16];
int nVal;
double dVal;
}u;
struct s_tag{
char cVal[16];
int nVal;
double dVal;
}s;
char chVals[16];
char* pVals = chVals;
printf("u= %d , s= %d , chval= %d , pvals= %d", sizeof(u), sizeof(s), sizeof(chVals), sizeof(pVals));
break;
}
case 6:
{
cout << "第" << opt << "題:沒有" << endl;
break;
}
case 7:
{
cout << "第" << opt << "題:";
printf("%d",foo(0, -1));
break;
}
case 8:
{
cout << "第" << opt << "題:";
int x = 10;
funct1(&x);
printf("%d\n", x);
break;
}
case 9:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(5));
break;
}
case 10:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(12));
break;
}
case 11:
{
cout << "第" << opt << "題:";
int n;
for (int c = 0; c <= 6; c++)
{
n = 0;
switch (c){
case 1: n++;
case 2: n++; break;
case 3: n++;
case 4: n++;
case 5: n++; break;
default: n++; break;
}
}
printf("%d\n", n);
break;
}
case 12:
{
cout << "第" << opt << "題:";
test1();
break;
}
case 13:
{
cout << "第" << opt << "題:";
test2();
break;
}
case 14:
{
cout << "第" << opt << "題:";
m_14();
break;
}
case 15:
{
cout << "第" << opt << "題:";
int sum = 0;
int ary[][4] = { 1, 3, 5, 7, 9, 13, 15, 17, 19, 21 };
int(*ptr)[4] = ary;
sum = (*ptr)[1] + ary[0][3] + ary[1][2];
printf("%d", sum);
break;
}
case 16:
{
cout << "第" << opt << "題:自己開註解 答案是5"<<endl;
/*
int x;
while (x < 100)
printf("%d", x);
*/
break;
}
case 17:
{
cout << "第" << opt << "題:";
int ans = foo2(2, 2);
printf("%d\n", ans);
break;
}
case 18:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", a);
break;
}
case 19:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", b);
break;
}
case 20:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1,, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
//int* ++ = +4 byte
char*++ = +1byte
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p1);
break;
}
case 21:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p2);
break;
}
case 22:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[0]);
break;
}
case 23:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[1]);
break;
}
case 24:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", *pn);
break;
}
case 25:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", **ppn);
break;
}
default:
return 0;
}
return 0;
}
using namespace std;
int strcmp2(char* pStr1, char* pStr2)
{
if (pStr1 == pStr2)
return 0;
else
return 1;
}
int func()
{
static int iNumber1 = 10;
int iNumber2 = 0;
iNumber1++;
iNumber2++;
return (iNumber1 + iNumber2);
}
int foo(int x,int y)
{
while (x)
{
++x;
++y;
}
if (y)
--y;
return x + y;
}
void funct1(int *x)
{
x = (int *)malloc(sizeof(int));
*x = 12;
}
int Test(int n)
{
int i;
int ret = 0;
for (i = 1; i <= --n; i++) {
if (i > 12)
{
ret /= 2;
break;
}
if (i >= 4 && i <= 6)
continue;
++ret;
}
return ret;
}
void printsum(int x, int y)
{
int sum = 0;
#ifdef DO_IT
sum = x + y;
#endif
printf("%d\n", sum);
}
void test1()
{
printsum(1, 2);
}
#define DO_IT
void test2()
{
printsum(3, 4);
}
void m_14()
{
int a = 5;
{
int b = 10;
++a;
++b;
{
int a = 20;
++a;
a = ++b;
}
++a;
++b;
printf("%d %d ", a,b);
}
printf("%d\n", a);
}
#define SQUARE(z) z * z
void foo3(int x, int *py)
{
int n = SQUARE(x + 1);
x = *py;
*py = n;
}
int foo2(int iParam1,int iParam2)
{
int iRet;
if (iParam1 = 2 || iParam2 == 2)
iRet = iParam1 + iParam2;
else
iRet = iParam1 - iParam2;
return iRet;
}
int main()
{
int opt = 0;
cout << "請輸入想知道第幾題答案" << endl;
cin >> opt;
switch (opt)
{
case 1:
{
cout << "第" << opt << "題:";
char str1[] = "abc";
char str2[] = "abc";
int iVal = strcmp2(str1, str2);
printf("%d\n", iVal);
break;
}
case 2:
{
cout << "第" << opt << "題:";
int iVal;
int i;
for (i = 0; i < 2; i++)
iVal = func();
printf("%d\n", iVal);
break;
}
case 3:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b)&c;
printf("%d\n", d);
break;
}
case 4:
{
cout << "第" << opt << "題:";
int a = 0x7700;
int b = 0x8800;
int c = 0xff00;
int d = (a && b) || c;
int e = (a | b) &c;
printf("%X\n", e);
break;
}
case 5:
{
cout << "第" << opt << "題:";
union u_tag{
char cVal[16];
int nVal;
double dVal;
}u;
struct s_tag{
char cVal[16];
int nVal;
double dVal;
}s;
char chVals[16];
char* pVals = chVals;
printf("u= %d , s= %d , chval= %d , pvals= %d", sizeof(u), sizeof(s), sizeof(chVals), sizeof(pVals));
break;
}
case 6:
{
cout << "第" << opt << "題:沒有" << endl;
break;
}
case 7:
{
cout << "第" << opt << "題:";
printf("%d",foo(0, -1));
break;
}
case 8:
{
cout << "第" << opt << "題:";
int x = 10;
funct1(&x);
printf("%d\n", x);
break;
}
case 9:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(5));
break;
}
case 10:
{
cout << "第" << opt << "題:";
printf("%d\n", Test(12));
break;
}
case 11:
{
cout << "第" << opt << "題:";
int n;
for (int c = 0; c <= 6; c++)
{
n = 0;
switch (c){
case 1: n++;
case 2: n++; break;
case 3: n++;
case 4: n++;
case 5: n++; break;
default: n++; break;
}
}
printf("%d\n", n);
break;
}
case 12:
{
cout << "第" << opt << "題:";
test1();
break;
}
case 13:
{
cout << "第" << opt << "題:";
test2();
break;
}
case 14:
{
cout << "第" << opt << "題:";
m_14();
break;
}
case 15:
{
cout << "第" << opt << "題:";
int sum = 0;
int ary[][4] = { 1, 3, 5, 7, 9, 13, 15, 17, 19, 21 };
int(*ptr)[4] = ary;
sum = (*ptr)[1] + ary[0][3] + ary[1][2];
printf("%d", sum);
break;
}
case 16:
{
cout << "第" << opt << "題:自己開註解 答案是5"<<endl;
/*
int x;
while (x < 100)
printf("%d", x);
*/
break;
}
case 17:
{
cout << "第" << opt << "題:";
int ans = foo2(2, 2);
printf("%d\n", ans);
break;
}
case 18:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", a);
break;
}
case 19:
{
cout << "第" << opt << "題:";
int a = 2;
int b = 10;
foo3(a, &b);
++b;
printf("%d\n", b);
break;
}
case 20:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1,, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
//int* ++ = +4 byte
char*
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p1);
break;
}
case 21:
{
cout << "第" << opt << "題:";
char ary[] = { 0, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
char *p1 = ary;
char *p2;
int *pn;
p1 += 2;
pn = (int*)&p1[2];
pn += 1;
p1 = (char*)pn;
--pn;
p2 = (char*)pn;
--p2;
printf("%d\n", *p2);
break;
}
case 22:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[0]);
break;
}
case 23:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", na[1]);
break;
}
case 24:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", *pn);
break;
}
case 25:
{
cout << "第" << opt << "題:";
int na[3] = { 30, 20, 10 };
int *pn;
int **ppn;
pn = na;
ppn = &pn;
*pn -= 1;
pn = &na[1];
**ppn += 1;
pn++;
printf("%d\n", **ppn);
break;
}
default:
return 0;
}
return 0;
}
訂閱:
文章 (Atom)