#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;
}
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;
}
#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;
}
#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;
}
#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;
}
#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;
}
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
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))
(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))
2014年3月30日 星期日
spanning tree prim C++
#include <iostream>
#include<string>
#include<vector>
using namespace std;
struct link{
string node1;
string node2;
int weighted;
};
void split(string, vector<link>*);
int count_node(vector<link>*, vector<string>*);
int main()
{
vector<link>input; //原始input
vector<string>n_list; //node的list
vector<string>new_n_list; //node的list
vector<link>Candidate;
vector<link>answer; //最後要輸出的結果
string command; //每一行輸入
int size, edge_count = 0, node_count;
while (true)
{
getline(cin, command);
if (command == "exit")
break;
split(command, &input);
}
node_count = count_node(&input, &n_list); //算有幾個點 & 做出點的list
/////prim//////
int total_weighted = 0;
link tmp;
new_n_list.push_back(n_list[0]); //以第一個當作起點
n_list.erase(n_list.begin());
for (int i = 0; edge_count != (node_count - 1); i++)
{
string n1, n2;
for (int j = 0; j < new_n_list.size(); j++) //需要想法QAQ
{
n1 = new_n_list[j];
for (int k = 0; k < n_list.size(); k++)
{
n2 = n_list[k];
for (int l = 0; l < input.size(); l++)
{
if (n1==input[l].node1 && n2==input[l].node2)
{
Candidate.push_back(input[l]);
}
else if (n1==input[l].node2 && n2==input[l].node1)
{
Candidate.push_back(input[l]);
}
}
}
}
tmp = Candidate[0];
for (int j = 0; j < Candidate.size(); j++) //挑最小的
{
if (tmp.weighted>Candidate[j].weighted)
tmp = Candidate[j];
}
answer.push_back(tmp);
edge_count++;
bool flag = 0; //scan node1 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node1 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node1);
for (int j = 0; j < n_list.size(); j++) //刪除另一個set裡面的東西
{
if (tmp.node1 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
flag = 0; //scan node2 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node2 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node2);
for (int j = 0; j < n_list.size(); j++)
{
if (tmp.node2 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
for (int j = 0; j < input.size(); j++)
{
if ((tmp.node1 == input[j].node1) && (tmp.node2 == input[j].node2) && (tmp.weighted == input[j].weighted))
{
input.erase(input.begin() + j);
break;
}
}
Candidate.clear();
}
size = answer.size(); //印答案
for (int i = 0; i < size; i++)
{
cout << answer[i].node1 << "-" << answer[i].node2 << " " << answer[i].weighted << endl;
total_weighted = total_weighted + answer[i].weighted;
}
cout << "total weighted = " << total_weighted << endl;
return 0;
}
void split(string input, vector<link>* data)
{
int dash_pos, space_pos;
int str_len = input.length();
link push_back;
string tmp = "";
for (int i = 0; i < str_len; i++) //找 - 位置
{
if (input[i] == '-')
{
dash_pos = i;
break;
}
}
for (int i = dash_pos + 1; i < str_len; i++)//找 空白 位置
{
if (input[i] == ' ')
{
space_pos = i;
break;
}
}
for (int j = 0; j < dash_pos; j++)
tmp = tmp + input[j];
push_back.node1 = tmp;
tmp = "";
for (int j = dash_pos + 1; j < space_pos; j++)
tmp = tmp + input[j];
push_back.node2 = tmp;
tmp = "";
for (int j = space_pos + 1; j < str_len; j++)
tmp = tmp + input[j];
push_back.weighted = atoi(tmp.c_str());
data->push_back(push_back);
}
int count_node(vector<link>* data, vector<string>* node_set) //算點數 同時做出點的list
{
int count = 0;
int size = data->size();
int len;
for (int i = 0; i < size; i++)
{
link tmp = (*data)[i];
string node1 = tmp.node1;
string node2 = tmp.node2;
bool node1_flag = 0, node2_flag = 0;
len = node_set->size();
for (int j = 0; j < len; j++)
{
if (node1 == (*node_set)[j])
{
node1_flag = 1;
break;
}
}
if (node1_flag == 0)
{
(*node_set).push_back(node1);
count++;
}
len = (*node_set).size();
for (int k = 0; k < len; k++)
{
if (node2 == (*node_set)[k])
{
node2_flag = 1;
break;
}
}
if (node2_flag == 0)
{
(*node_set).push_back(node2);
count++;
}
}
return count;
}
#include<string>
#include<vector>
using namespace std;
struct link{
string node1;
string node2;
int weighted;
};
void split(string, vector<link>*);
int count_node(vector<link>*, vector<string>*);
int main()
{
vector<link>input; //原始input
vector<string>n_list; //node的list
vector<string>new_n_list; //node的list
vector<link>Candidate;
vector<link>answer; //最後要輸出的結果
string command; //每一行輸入
int size, edge_count = 0, node_count;
while (true)
{
getline(cin, command);
if (command == "exit")
break;
split(command, &input);
}
node_count = count_node(&input, &n_list); //算有幾個點 & 做出點的list
/////prim//////
int total_weighted = 0;
link tmp;
new_n_list.push_back(n_list[0]); //以第一個當作起點
n_list.erase(n_list.begin());
for (int i = 0; edge_count != (node_count - 1); i++)
{
string n1, n2;
for (int j = 0; j < new_n_list.size(); j++) //需要想法QAQ
{
n1 = new_n_list[j];
for (int k = 0; k < n_list.size(); k++)
{
n2 = n_list[k];
for (int l = 0; l < input.size(); l++)
{
if (n1==input[l].node1 && n2==input[l].node2)
{
Candidate.push_back(input[l]);
}
else if (n1==input[l].node2 && n2==input[l].node1)
{
Candidate.push_back(input[l]);
}
}
}
}
tmp = Candidate[0];
for (int j = 0; j < Candidate.size(); j++) //挑最小的
{
if (tmp.weighted>Candidate[j].weighted)
tmp = Candidate[j];
}
answer.push_back(tmp);
edge_count++;
bool flag = 0; //scan node1 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node1 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node1);
for (int j = 0; j < n_list.size(); j++) //刪除另一個set裡面的東西
{
if (tmp.node1 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
flag = 0; //scan node2 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node2 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node2);
for (int j = 0; j < n_list.size(); j++)
{
if (tmp.node2 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
for (int j = 0; j < input.size(); j++)
{
if ((tmp.node1 == input[j].node1) && (tmp.node2 == input[j].node2) && (tmp.weighted == input[j].weighted))
{
input.erase(input.begin() + j);
break;
}
}
Candidate.clear();
}
size = answer.size(); //印答案
for (int i = 0; i < size; i++)
{
cout << answer[i].node1 << "-" << answer[i].node2 << " " << answer[i].weighted << endl;
total_weighted = total_weighted + answer[i].weighted;
}
cout << "total weighted = " << total_weighted << endl;
return 0;
}
void split(string input, vector<link>* data)
{
int dash_pos, space_pos;
int str_len = input.length();
link push_back;
string tmp = "";
for (int i = 0; i < str_len; i++) //找 - 位置
{
if (input[i] == '-')
{
dash_pos = i;
break;
}
}
for (int i = dash_pos + 1; i < str_len; i++)//找 空白 位置
{
if (input[i] == ' ')
{
space_pos = i;
break;
}
}
for (int j = 0; j < dash_pos; j++)
tmp = tmp + input[j];
push_back.node1 = tmp;
tmp = "";
for (int j = dash_pos + 1; j < space_pos; j++)
tmp = tmp + input[j];
push_back.node2 = tmp;
tmp = "";
for (int j = space_pos + 1; j < str_len; j++)
tmp = tmp + input[j];
push_back.weighted = atoi(tmp.c_str());
data->push_back(push_back);
}
int count_node(vector<link>* data, vector<string>* node_set) //算點數 同時做出點的list
{
int count = 0;
int size = data->size();
int len;
for (int i = 0; i < size; i++)
{
link tmp = (*data)[i];
string node1 = tmp.node1;
string node2 = tmp.node2;
bool node1_flag = 0, node2_flag = 0;
len = node_set->size();
for (int j = 0; j < len; j++)
{
if (node1 == (*node_set)[j])
{
node1_flag = 1;
break;
}
}
if (node1_flag == 0)
{
(*node_set).push_back(node1);
count++;
}
len = (*node_set).size();
for (int k = 0; k < len; k++)
{
if (node2 == (*node_set)[k])
{
node2_flag = 1;
break;
}
}
if (node2_flag == 0)
{
(*node_set).push_back(node2);
count++;
}
}
return count;
}
訂閱:
文章 (Atom)