mysol1 直接解析:
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxTableSize = 26 + 1;
typedef struct{
char identify;
int row;
int col;
}Matrix;
#define ERROR -1
class MatrixTab
{
public:
MatrixTab():m_size(0){};
bool add(char * buf);
bool find(char identify, Matrix * matrix);
private:
int m_size;
Matrix m_table[maxTableSize];
};
bool MatrixTab::add(char * buf)
{
int result;
char * addr;
sscanf(buf, "%c", &m_table[m_size].identify);
buf += 2;
m_table[m_size].row = atoi( buf );
while( (*buf) != ' ' )
buf++;
m_table[m_size].col = atoi( buf );
m_size++;
return true;
}
bool MatrixTab::find(char identify, Matrix * matrix)
{
assert( m_size > 0 );
for(int i=0;i<m_size;i++)
{
if( identify == m_table[i].identify )
{
*matrix = m_table[i];
return true;
}
}
return false;
}
class Parser
{
public:
Parser(char * buf, MatrixTab & matrixTab):m_matrixTab(matrixTab),
m_iLook(0),
m_buf(buf){
m_len = strlen(buf);
};
int parse();
int expression(Matrix * matrix);
private:
int m_iLook;
int m_len;
char * m_buf;
MatrixTab & m_matrixTab;
};
int Parser::expression(Matrix * matrix)
{
if( m_buf[m_iLook] == '(' )
{
Matrix factor1, factor2;
int mults1, mults2;
int sum;
m_iLook++;
mults1 = expression( &factor1 );
mults2 = expression( &factor2 );
m_iLook++;
if( mults1 == ERROR | mults2 == ERROR )
return ERROR;
else
{
if( factor1.col == factor2.row )
{
sum = mults1 + mults2 + factor1.row * factor1.col * factor2.col;
matrix->row = factor1.row;
matrix->col = factor2.col;
return sum;
}
else
return ERROR;
}
}
else
{
assert( 'A' <= m_buf[m_iLook] && m_buf[m_iLook] <= 'Z' );
m_matrixTab.find( m_buf[m_iLook], matrix );
m_iLook++;
return 0;
}
}
int Parser::parse()
{
Matrix matirx;
return expression(&matirx);
}
int main()
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int N;
int i, j;
const int bufSize = 100;
char buf[100];
MatrixTab matrixTab;
cin >> N;
getchar();
for(i=0;i<N;i++)
{
cin.getline(buf, bufSize);
matrixTab.add(buf);
}
while( !cin.eof() )
{
cin.getline(buf, bufSize);
Parser parser( buf, matrixTab );
int result = parser.parse();
if( result == ERROR )
{
cout << "error" << endl;
}
else
{
cout << result << endl;
}
}
return 0;
}
mysol2 压栈:
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
typedef struct{
int row;
int col;
}Mat;
Mat mats[100];
int main()
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int N;
char c;
char buf[100];//保存expression
int iLook;
int error;
int sum;
cin >> N;
while( N-- )
{
cin >> c;
cin >> mats[c].row >> mats[c].col;
getchar();
}
while( scanf("%s", buf) != EOF )
{
getchar();//读一个空格
iLook = 0;
error = 0;
sum = 0;
stack <Mat> s;
while( iLook < strlen(buf) )
{
if( buf[iLook] == '(' )
{
iLook++;
}
else if( buf[iLook] == ')' )
{
Mat fac1, fac2;
fac2 = s.top();
s.pop();
fac1 = s.top();
s.pop();
iLook++;
if( fac1.col != fac2.row )
{
error = 1;
break;
}
else
{
Mat new_mat;
new_mat.row = fac1.row;
new_mat.col = fac2.col;
s.push( new_mat );
sum += fac1.row * fac1.col * fac2.col;
}
}
else
{
s.push( mats[ buf[iLook] ] );
iLook++;
}
}
if( error )
cout << "error" << endl;
else
cout << sum << endl;
}
return 0;
}