/* Subject: Contest submission for problem #1, file 1.cc */ /* rob@imail.EECS.Berkeley.EDU */ /* Thu Sep 18 01:53:03 PDT 2003 */ /*___CONTEST_SUBMISSION___ rob 1 */ /* Assignment #3 Prob.1 - Clunk nickname: skyrob estimated time: 2 hr beginning time: 10:40am ending time: actual time: can't get the input to work with arbitrary whitespace... problem with scanf very unfamiliar with STL */ #include #include #include #include #include #include using namespace std; struct compare { bool operator()(const char c1, const char c2) const { if(c1 < c2) return true; else return false; } }; vector v; //vector::iterator goTo; set charSeen; int lineIndex = 0; bool done = false; int numSet = -1; bool checkIf(string); void checkAssign(string, bool); int main() { while(true) { done = false; lineIndex = 0; v.clear(); charSeen.clear(); char input[256]; char stripedInput[256]; bool firstTime = true; while(true) { if(cin.peek() == EOF) { exit(0); } /* cin.getline(input, 256); strcpy(stripedInput, input+3); if(strcmp(stripedInput, "stop;") == 0) { numSet++; break; } string in = stripedInput; v.push_back(in); */ scanf("%s", input); if(input[0] >= '0' && input[0] <= '9' && input[strlen(input)-1] == ':') { if(firstTime) { firstTime = false; continue; } string in = stripedInput; v.push_back(in); stripedInput[0] = '\0'; } else if(strcmp(input, "stop;") == 0) { numSet++; stripedInput[0] = '\0'; input[0] = '\0'; break; } else strcat(stripedInput, input); } //copy(v.begin(), v.end(), ostream_iterator(cout, "\n")); vector::iterator iter = v.begin(); while(iter != v.end()) { string s1 = *iter++; if(!checkIf(s1)) checkAssign(s1, true); if(done) { break; } lineIndex++; } if(!done) cout << "Set " << numSet << ": No use before definition\n"; } } bool checkIf(string s1) { //cout << s1 << endl; if(s1[0] == 'i' && s1[1] == 'f') { int i = 4; while(s1[i] != ')') { if(s1[i] != ' ' && s1[i] != ',') { if(find(charSeen.begin(), charSeen.end(), s1[i]) == charSeen.end()) { cout << "Set " << numSet << ": Possible use before definition\n"; done = true; break; } } i++; } if(done) { return true; } i += 5; int length = s1.size() - i; int j; char temp[length]; for(j = 0; j < length-1; j++) { temp[j] = s1[i+j]; } temp[j] = '\0'; int lineNum = atoi(temp); //cout << "temp: " << temp << endl; //cout << "lineNum: " << lineNum << endl; if(lineNum < lineIndex) { return true; } vector::iterator iter = v.begin(); if(checkIf(*(iter+lineNum))) return true; else { checkAssign(*(iter+lineNum), false); return true; } } else return false; } void checkAssign(string s1, bool addToSet) { if(addToSet) { charSeen.insert(s1[0]); //cout << s1 << " "; //copy(charSeen.begin(), charSeen.end(), ostream_iterator(cout, " ")); //cout << endl; } int i = 4; while(s1[i] != ')') { if(s1[i] != ' ' && s1[i] != ',') { if(find(charSeen.begin(), charSeen.end(), s1[i]) == charSeen.end()) { //cout << s1[i] << " is not found\n"; cout << "Set " << numSet << ": Possible use before definition\n"; done = true; break; } } i++; } }