120 likes | 196 Views
Solution to HW 3. Style and Syntax. Indent, http://en.wikipedia.org/wiki/Indent_style , choose one style and be consistent Try/catch exception handling should be used for each main, as the example given in P. 150. PR 1. Compile error Line 1 : #include "std_lib_facilities _4 .h“
E N D
Style and Syntax • Indent, http://en.wikipedia.org/wiki/Indent_style, choose one style and be consistent • Try/catch exception handling should be used for each main, as the example given in P. 150
PR 1 • Compile error • Line 1 : #include "std_lib_facilities_4.h“ • Line 17 : Add another construct functionToken(char ch, string n) : kind(ch), name(n) {}Or there will be compile error for Line 78 • Line 74 : while(cin.get(ch) && (isalpha(ch) || isdigit(ch))) s+=ch;
Logic error • Line 77 : • From : if (s == "quit") return Token(name); • To : if (s == "quit") return Token(quit); • Line 140 : • Add return d; after line 140 • case '(':{... return d;} • Line 153 : • In term() function, add handling for % operation • See P.230 on textbook • Line 114 : • From : i<=names.size() • To : i<names.size() • Actually, this set_value function is not called in this program
PR 2 For input 0,0 a and b are not logically equal For input 0,1 a and b are logically equal For input 1,0 a and b are not logically equal For input 1,1 a and b are logically equal
PR 2 • Avoid re-assignment of variable a bool a=true; bool b=false; for(inti=0;i < 2;i++) { // a = bool(i); // this is not right for(int j=0;j < 2;j++) { a=bool(i); b=bool(j); cout << "For input " << a << "," << b << endl; if (a = b) cout<<"a and b are logically equal \n"; else cout<<"a and b are not logically equal \n"; } }
PR 3 • Check the user input format string url; cin >> url; inturl_size = url.size(); if(url_size < 7) { cout << "URL too short"; return 1; } bool prefix = url[0]=='h'&&url[1]=='t'&&url[2]=='t'&&url[3]=='p' &&url[4]==':'&&url[5]=='/'&&url[6]=='/'; bool suffix1 = url[url_size-3]=='p'&&url[url_size-2]=='d'&&url[url_size-1]=='f'; bool suffix2 = url[url_size-3]=='P'&&url[url_size-2]=='D'&&url[url_size-1]=='F'; boolurl_ok = prefix && (suffix1 || suffix2);
PR 3 • Another way to check the user input format string url; cin >> url; inturl_size = url.size(); if(url_size < 7) { cout << "URL too short"; return 1; } bool prefix = url.substr(0,7) == “http://”; bool suffix1 = url(url_size-3,3)==“pdf”; bool suffix2 = url(url_size-3,3)==“PDF”; boolurl_ok = prefix && (suffix1 || suffix2);
PR 3 • Download the file and open it string command = "wget "+url+" -O webfile.pdf"; system(command.c_str()); system("display webfile.pdf");
PR 4 • Duplicate counting • Key : 3675 • Input : 3333, Output: 1 bull, 0 cow • Input : 0333, Output: 0 bull, 1 cow • Numbers that has been used should not be used again.
PR 4 • Random number generate unsigned int s; cin >> s; srand(s); vector<int> key; for(inti = 0;i < 4;i++) { key.push_back(rand()%10); }