/* parses a line of the file * tries to set the corresponding row in the matrix * returns false on error */ bool parse_row(char* s, int row, LinearProgram* lp) { assert(lp_is_valid(lp)); assert(row >= 0); assert(row < get_rows(lp)); char* end_ptr; int cols = get_cols(lp); int i; for (i = 0; i < cols; i++) { num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } set_coef(lp, row, i, num); s = end_ptr; } s = parse_type(s, row, lp); if (NULL == s) { return false; } num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } s = end_ptr; s = skip_spaces(s); if ('\0' != *s) { return false; } set_rhs(lp, row, num); assert(lp_is_valid(lp)); return true; }
/* parses a line of the file * tries to set the corresponding row in the matrix * returns false on error */ bool parse_row(char* s, int row, LinearProgram* lp) { assert(lp_is_valid(lp)); assert(row >= 0); assert(row < lp->rows); int i; char* end_ptr; for (i = 0; i < lp->cols; i++) { num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } lp->matrix[row][i] = num; s = end_ptr; } s = parse_type(s, row, lp); if (NULL == s) { return false; } num_t num = parse_num(s, &end_ptr); if (!is_num_valid(num, s, end_ptr)) { return false; } s = end_ptr; s = skip_spaces(s); if ('\0' != *s) { return false; } lp->vector[row] = num; assert(lp_is_valid(lp)); return true; }
/*------------------------------------------------ * Procedure to solve this problem * return how many valid answers * * 1. set up numbers: abc, de * here are 5 loops to compute * * 2. Check partProduct valid or not * all digits must be the member of num[] * * 3. Check sum vaild or not * head digit cant be 0 * * 4. The partial products must be three digits long!! * */ int comp_crypt() { int abc, de; int i, j, k, l, m; int partProduct1; // e*abc int partProduct2; // d*abc int sum; // p1 + 10*p2 int ans = 0; //n=1; // Set up numbers for(i=0; i<n; i++) for(j=0; j<n; j++) for(k=0; k<n; k++) for(l=0; l<n; l++) for(m=0; m<n; m++) { // Set numbers abc = num[i]*100 + num[j]*10 + num[k]; de = num[l]*10 + num[m]; partProduct1 = num[m]*abc; partProduct2 = num[l]*abc; sum = partProduct1 + partProduct2*10; // The partial products must be three digits long if( partProduct1 > 999 || partProduct2 > 999 ) continue; if( is_num_valid(partProduct1) && is_num_valid(partProduct2) && is_num_valid(sum) ) ans++; } return ans; }
/*词法分析*/ void MYMICRO::parsetoken(std::vector<TOKEN> & tokens) { tokens.clear(); std::string tokenv ; char ch = 0; int flag = 0; int paras =0; while(input->good()) { *input>>ch; if(ch!='\n'&& isspace(ch)) continue; /*跳过空格*/ input->putback(ch); flag = 0; while(input->good()) { *input>>ch; switch (ch) { case TOKEN::ASSI: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::VAID; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::ASSI; temp.tokenval= '='; tokens.push_back(temp); flag = 1; break; } case TOKEN::MMULT: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::MMULT; temp.tokenval= TOKEN::MMULT; tokens.push_back(temp); flag = 1; break; } case TOKEN::DIV: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::DIV; temp.tokenval= TOKEN::DIV; tokens.push_back(temp); flag = 1; break; } case TOKEN::LINE: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::SEMI; temp.tokenval= TOKEN::SEMI; tokens.push_back(temp); flag = 1; break; } case TOKEN::COMM: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::COMM; temp.tokenval= TOKEN::COMM; tokens.push_back(temp); flag = 1; break; } case TOKEN::LPAR: { //std::cout<<paras<<"\n"; if(paras<0) throw(PAERROR); ++paras; if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::FUNC; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::LPAR; temp.tokenval= TOKEN::LPAR; tokens.push_back(temp); flag = 1; break; } case TOKEN::RPAR: { //std::cout<<paras<<"\n"; if(paras<1) throw(PAERROR); --paras; if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::RPAR; temp.tokenval= TOKEN::RPAR; tokens.push_back(temp); flag = 1; break; } case TOKEN::MINU: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::MINU; temp.tokenval= TOKEN::MINU; tokens.push_back(temp); flag = 1; break; } case TOKEN::PLUS: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::PLUS; temp.tokenval= TOKEN::PLUS; tokens.push_back(temp); flag = 1; break; } case TOKEN::MULT: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::MULT; temp.tokenval= TOKEN::MULT; tokens.push_back(temp); flag = 1; break; } case TOKEN::SEMI: { if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear();/*清空tokenval*/ } TOKEN temp; temp.tokentype= TOKEN::SEMI; temp.tokenval= TOKEN::SEMI; tokens.push_back(temp); flag = 1; break; } default: tokenv.push_back(ch); break; } if(flag = 1) { break; } } } if(paras>0) throw(PAERROR); if(tokenv.size()>0) { TOKEN temp; temp.tokentype= TOKEN::IDNM; temp.tokenval= tokenv; tokens.push_back(temp); tokenv.clear(); } /*判断token正确性*/ for(auto item = tokens.begin();item != tokens.end();++item) { if(item->tokentype == TOKEN::FUNC) { if( ! is_id_valid(item->tokenval)) { throw(IDERROR); } } if(item->tokentype == TOKEN::VAID) { if( ! is_id_valid(item->tokenval)) { throw(IDERROR); } } if(item->tokentype == TOKEN::IDNM) { if(is_id_valid(item->tokenval)) { item->tokentype = TOKEN::VAID; } else if(is_num_valid(item->tokenval)) { item->tokentype = TOKEN::CONSTNUM; } else throw(NUERROR); } if(item != tokens.begin()) { auto titem = item-1; if(item->tokentype == TOKEN::IDNM || item->tokentype == TOKEN::VAID) { if(titem->tokentype == TOKEN::IDNM || titem->tokentype == TOKEN::VAID) { throw(NUERROR); } } } } }