ajax1.html

1/2

lectures/9/src/ajax/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

78: ajax1 79: 80: 81:
82: Symbol: 83:

84: 85:
86: 87:

2/2

ajax2.html

1/2

lectures/9/src/ajax/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

78: ajax2 79: 80: 81:
82: Symbol: 83:
84: Price: to be determined 85:

86: 87:
88: 89:

2/2

ajax3.html

1/3

lectures/9/src/ajax/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

ajax3
Symbol:

ajax3.html

3/3

lectures/9/src/ajax/ 91:

92: 96:
97:

98: 99:
100: 101:

ajax4.html

1/2

lectures/9/src/ajax/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

69: ajax4 70: 71: 72:
73: Symbol: 74:

75: 76:
77: 78:

ajax5.html

1/3

lectures/9/src/ajax/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

ajax5


ajax5.html lectures/9/src/ajax/ 91: Symbol: 92:

93: Price: 94:
95: High: 96:
97: Low: 98:
99:

100: 101:
102: 103:

3/3

quote1.php

1/1

lectures/9/src/ajax/ 1:

/** * quote1.php * * Outputs price of given symbol as text/html. * * Computer Science 50 * David J. Malan */ // get quote $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?s={$_GET["symbol"]}&f=e1l1", "r"); if ($handle !== FALSE) { $data = fgetcsv($handle); if ($data !== FALSE && $data[0] == "N/A") print($data[1]); fclose($handle); }

quote2.php lectures/9/src/ajax/ 1:

/** * quote2.php * * Outputs price, low, and high of given symbol as text/html, after * inserting an artificial delay. * * Computer Science 50 * David J. Malan */ // pretend server is slow sleep(5); // try to get quote $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?s={$_GET["symbol"]}&f=e1l1hg", "r"); if ($handle !== FALSE) { $data = fgetcsv($handle); if ($data !== FALSE && $data[0] == "N/A") { print("Price: {$data[1]}"); print("
"); print("High: {$data[2]}"); print("
"); print("Low: {$data[3]}"); } fclose($handle); }

1/1

quote3.php

1/1

lectures/9/src/ajax/ 1:

/** * quote3.php * * Outputs price, low, and high of given symbol as JSON. * * Computer Science 50 * David J. Malan */ // try to get quote $quote = array(); $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?s={$_GET["symbol"]}&f=e1l1hg", "r"); if ($handle !== FALSE) { $data = fgetcsv($handle); if ($data !== FALSE && $data[0] == "N/A") { $quote["price"] = $data[1]; $quote["high"] = $data[2]; $quote["low"] = $data[3]; } fclose($handle); } header("Content-type: application/json"); print(json_encode($quote));

dump.php lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:

dump
  

1/1

form1.html

1/1

lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:

form1
Email:
Password:
Password (again):
I agree to the terms and conditions:



form2.html

1/2

lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:



form2.html

2/2

lectures/9/src/forms/ 46: form2 47: 48: 49:
50: Email: 51:
52: Password: 53:
54: Password (again): 55:
56: I agree to the terms and conditions: 57:

58: 59:
60: 61:

form3.html

1/2

lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:



form3.html

2/2

lectures/9/src/forms/ 46: form3 47: 48: 49:
50: Email: 51:
52: Password: 53:
54: Password (again): 55:
56: I agree to the terms and conditions: 57:

58: 59:
60: 61:

form4.html

1/2

lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:

54: form4 55: 56: 57:
58: Email: 59:
60: Password: 61:
62: Password (again): 63:
64: I agree to the terms and conditions: 65:

66: 67:
68: 69:

form5.html

1/2

lectures/9/src/forms/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:



form5.html

2/2

lectures/9/src/forms/ 46: form5 47: 48: 49:
50: Email: 51:
52: Password: 53:
54: Password (again): 55:
56: I agree to the terms and conditions: 57:

58: 59:
60: 61:

dictionary.php

1/2

lectures/9/src/mispellings/ 1:
/*********************************************************************** * dictionary.php * * Computer Science 50 * David J. Malan * * Implements a dictionary. **********************************************************************/ // size of dictionary $size = 0; // dictionary $dictionary = array(); /* * bool * check($word) * * Returns TRUE if word is in dictionary else FALSE. */ function check($word) { global $dictionary; if ($dictionary[strtolower($word)]) return TRUE; else return FALSE; } /* * bool * load($dict) * * Loads dict into memory. */ function load($dict) {

Returns TRUE if successful else FALSE.

dictionary.php

2/2

lectures/9/src/mispellings/ 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: ?>

global $dictionary, $size; if (!file_exists($dict) && is_readable($dict)) return FALSE; foreach (file($dict) as $word) { $dictionary[chop($word)] = TRUE; $size++; } return TRUE; } /* * int * size() * * Returns number of words in dictionary if loaded else 0 if not yet loaded. */ function size() { global $size; return $size; } /* * int * unload() * * Unloads dictionary from memory. */

Returns TRUE if successful else FALSE.

function unload() { return TRUE; }

speller lectures/9/src/mispellings/ 1: #!/usr/bin/php 2:
1/4

speller

2/4

lectures/9/src/mispellings/ 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:

{ print("Could not load $dict.\n"); return 2; } // calculate time to load dictionary $ti_load = $after - $before; // try to open file $file = ($argc == 3) ? $argv[2] : $argv[1]; $fp = fopen($file, "r"); if ($fp === FALSE) { print("Could not open $file.\n"); return 3; } // prepare to report misspellings printf("\nMISSPELLED WORDS\n\n"); // prepare to spell-check $word = ""; $index = 0; $misspellings = 0; $words = 0; // spell-check each word in file for ($c = fgetc($fp); $c !== FALSE; $c = fgetc($fp)) { // allow alphabetical characters and apostrophes (for possessives) if (preg_match("/[a-zA-Z]/", $c) || ($c == "’" && $index > 0)) { // append character to word $word .= $c; $index++; // ignore alphabetical strings too long to be words if ($index >= LENGTH) { // consume remainder of alphabetical string while (($c = fgetc($fp)) !== FALSE && preg_match("/[a-zA-Z]/", $c)); // prepare for new word $index = 0; $word = ""; } }

speller

3/4

lectures/9/src/mispellings/ 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135:

// ignore words with numbers (like MS Word) else if (ctype_digit($c)) { // consume remainder of alphabetical string while (($c = fgetc($fp)) !== FALSE && preg_match("/[a-zA-z0-9]/", $c)); // prepare for new word $index = 0; $word = ""; } // we must have found a whole word else if ($index > 0) { // update counter $words++; // check word’s spelling $before = microtime(TRUE); $misspelled = !check($word); $after = microtime(TRUE); // update benchmark $ti_check += $after - $before; // print word if misspelled if ($misspelled) { print("$word\n"); $misspellings++; } // prepare for next word $index = 0; $word = ""; } } // close file fclose($fp); // determine dictionary’s size $before = microtime(TRUE); $n = size(); $after = microtime(TRUE); // calculate time to determine dictionary’s size

speller

4/4

lectures/9/src/mispellings/ 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: ?>

$ti_size = $after - $before; // unload dictionary $before = microtime(TRUE); $unloaded = unload(); $after = microtime(TRUE); // abort if dictionary not unloaded if (!$unloaded) { print("Could not load $dict.\n"); return 5; } // calculate time to determine dictionary’s size $ti_unload = $after - $before; // report benchmarks printf("\nWORDS MISSPELLED: printf("WORDS IN DICTIONARY: printf("WORDS IN FILE: printf("TIME IN load: printf("TIME IN check: printf("TIME IN size: printf("TIME IN unload: printf("TOTAL TIME:

%d\n", $misspellings); %d\n", $n); %d\n", $words); %.2f\n", $ti_load); %.2f\n", $ti_check); %.2f\n", $ti_size); %.2f\n", $ti_unload); %.2f\n\n", $ti_load + $ti_check + $ti_size + $ti_unload);

ajax1.html 1/2 ajax1.html 2/2 - CS50 CDN

3: ajax1.html. 4: 5: Gets stock quote from quote1.php via Ajax, displaying result with alert(). 6: 7: Computer Science 50. 8: David J. Malan. 9: 10: -->. 11: 12:

66KB Sizes 1 Downloads 129 Views

Recommend Documents

Untitled - CS50 CDN
http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Characteristics/Characteristics.html ... content="yes"> http://developer.apple.com/library/safari/documentation/appleapplications/reference/SafariHTMLRef/Article

Untitled - CS50 CDN
void swap(int a, int b). { int tmp = a; a = b; b = tmp;. } Page 11. void swap(int *a, int *b). { int tmp = *a;. *a = *b;. *b = tmp;. } Page 12. main's parameters.

Untitled - CS50 CDN
Mac OS 10.6. Windows 7. Mac OS 10.5. Windows Vista. Windows XP. Mac OS 10.4. Linux. 0. 50. 100. 150. 200. Page 19. Elective. Concentration. Unsure.

Untitled - CS50 CDN
50. 100. 150. 200. Page 19. Elective. Concentration. Unsure. Gen Ed. Core. 0. 50. 100. 150. 200. 250. 300. Page 20. arrays. Page 21. to be continued... Page 22.

cs50.c 1/5 cs50.c 2/5 - CS50 CDN
11: * Based on Eric Roberts' genlib.h and simpio.h. 12: *. 13: * The latest version of this file can be found at. 14: * http://www.cs50.net/pub/releases/cs50/cs50.h.