#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;
}
沒有留言:
張貼留言