1 / 12

Solution to HW 3

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“

maida
Download Presentation

Solution to HW 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Solution to HW 3

  2. 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

  3. 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;

  4. 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

  5. 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

  6. 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"; } }

  7. 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);

  8. 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);

  9. PR 3 • Download the file and open it string command = "wget "+url+" -O webfile.pdf"; system(command.c_str()); system("display webfile.pdf");

  10. PR 4

  11. 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.

  12. 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); }

More Related