250 likes | 349 Views
CS31: Introduction to Computer Science I. Discussion 1A 5/14/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week? C-strings string type can be used only in c++ C-string is an array of characters
E N D
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang
Quick Review • What did we learn last week? • C-strings • string type can be used only in c++ • C-string is an array of characters • it must end with zero byte/null character : ‘\0’ • indicates the end of the string • C-string input/output • cout prints out characters until it meets null character • cin.getline reads one line • need to specify the maximum length of the input string • null character will be automatically inserted • converting C-strings to C++ strings • simple ‘=‘ operator works • not vice versa • C-string functions : #include <cstring> • strlen(s) • returns the length of s, not counting ‘\0’ • strcpy(t, s) • copies the string s to t • strcat(t, s) • concatenate (append) s to the end of t • strcmp(t, s) • compare t and s • return 0 if they are equal • return something greater than 0 if t > s • return something less than 0 if t < s
Pointers • where are variables stored? • how can computers know where they are?? 001FFD08 10 int main () { int a = 10; double d = 5.5; char c = ‘a’; bool b = true; } ⁞ ⁞ ⁞ ⁞ 002FDC68 5.5 ⁞ ⁞ 002FF128 a ⁞ ⁞ 004DEF00 true
Pointers • Variables are stored in memory locations • The memory location of a variable is called the address of the variable • The address specifies a place, not a value • Pointer variables store the address!
pointers int main () { int a = 10; int b = 100; int* ptr1; int *ptr2; ptr1 = &a; ptr2 = &b; int sum = *ptr1 + *ptr2; cout << ptr1 << " : " << ptr2 << endl; cout << &ptr1 << " : " << &ptr2 << endl; cout << a << " : " << b << endl; cout << *ptr1 << " : " << *ptr2 << endl; cout << sum << endl; } 001AF73C : 001AF738 001AF744 : 001AF740 10 : 100 10 : 100 110
pointers • multiple pointers can point to the same variable int main () { int* ptr1; int *ptr2; int a = 10; ptr1 = &a; ptr2 = ptr1; cout << *ptr1 << endl; cout << *ptr2 << endl; } 10 10
quick questions • what will be displayed? int main () { int a = 10; int *ptr = &a; int *p = ptr; cout << a << endl; cout << *&a << endl; cout << *ptr << endl; cout << *p << endl; } int main () { int* ptr1; int *ptr2; int a = 10; int b = 100; ptr1 = &a; ptr2 = &b; a = *ptr2+2*a; b = *&b*b; cout << b+*ptr1 << endl; } 10 10 10 10 10120
pointers and arrays • The name of an array is a pointer to its first element(item) int main () { int arr[10] = {0,1,2,3,4,5,6,7,8,9}; cout << arr << endl; cout << &arr[0] << endl; } 22FE38DF 22FE38DF
array element access • when an array is created, items are allocated in consecutive memory spaces • if we know the beginning address, we know the locations of all the items. int main () { int i = 5; int arr[10] = {10,1,2,3,4,5,6,7,8,9}; cout << *arr << endl; cout << *(arr+ 1) << endl; cout << *(arr + 2) << endl; cout << *arr + 3 << endl; cout << *arr + 4 << endl; cout << *(arr + i) << endl; cout << *arr+ i+1 << endl; } 10 1 2 13 14 5 16
Pointers and functions • Pointer is one type of variable • stores addresses • Pointers can be function parameters • Value pointer parameters • Reference pointer parameters • Pointers can be function return values • Value returned is a pointer
value parameter • passing pointer by value • cannot change the address outside • but, can change the values pointers point to void func(int* arg1, int* arg2) { int* temp; temp = arg1; arg1 = arg2; arg2 = temp; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } void func(int* arg1, int* arg2) { *arg1 = 100; *arg2 = 10; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } 100 : 10 10 : 100 100 : 10 100 : 10
reference parameter • passing pointer by reference • can change the address outside void func(int* &arg1, int* &arg2) { int* temp; temp = arg1; arg1 = arg2; arg2 = temp; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } 100 : 10 100 : 10
Project #5 • Plain Text File Reformatter • reformats a plain text file into neatly arranged paragraphs with a particular maximum allowed line length • int reformat(istream& inf, ostream& outf, intlineLength); • an already-opened input source you will read from (probably a file the caller opened). • an already-opened output destination you will write to (probably either cout or an output file the caller created) • an int with the desired maximum line length • return value • 0 if is successful (i.e., neither of the following problems occurs) • 1 if any input word portion is longer than the maximum line length • 2 if the desired maximum line length is less than 1
definition of an word • A word is a sequence of non-whitespace characters. • The <cctype> function isspace tells you if a character is a whitespace character • A word can be viewed as a sequence of one or more word portions
reformatting rules • Fit as many word portions in an output line as you can without exceeding the line length. Output lines may well end up being different lengths, giving a "ragged-right" effect It•always•does•seem•to•me•that•I•am doing•more•work•than•I•should•do.••It•is not•that•I•object•to•the•work,•mind•you; I•like•work:•it•fascinates•me.••I•can sit•and•look•at•it•for•hours.••I•love•to keep•it•by•me:•the•idea•of•getting•rid of•it•nearly•breaks•my•heart.
reformatting rules • Words in an output line must normally be separated by one blank. • if the last character of an output word is a period, question mark, or exclamation point, it must be separated from any following word on that output line by two blanks. • The last word on an output line must not be followed by any blanks. • The first word portion on an output line must not be preceded by any blanks. • Blanks must not appear on an output line within any word. • The last line of output must end with a newline, and there must be no output lines (not even empty ones) after the last word of the last paragraph. It•always•does•seem•to•me•that•I•am doing•more•work•than•I•should•do.••It•is not•that•I•object•to•the•work,•mind•you; I•like•work:•it•fascinates•me.••I•can sit•and•look•at•it•for•hours.••I•love•to keep•it•by•me:•the•idea•of•getting•rid of•it•nearly•breaks•my•heart.
reformatting rules • If a word portion is longer than the line length, as much as will fit must be on an output line by itself. • The rest of that word portion must begin the next output line (and, of course, is subject to similar splitting if it's too long). • If this situation ever occurs, your function continues reformatting, but it must eventually return 1 instead of 0 to its caller. • Notice that this is the only situation where a word is allowed to be split across lines other than at a hyphen. abcdefghij klmnopqrst uvwxyz function returns 1
reformatting rules • A paragraph break is indicated in the input by one or more consecutive lines that contain no words (i.e., are empty or contain only whitespace characters). • The first word in the input following a paragraph break will be the first word of a new paragraph in the output. • If a paragraph has already been output, the new paragraph must be separated from the one that precedes it by an empty line (i.e., a line with no characters other than the terminating newline). The very first output paragraph must not be preceded by an empty line I•love•to•keep•it•by•me:•the•idea•of getting•rid•of•it•nearly•breaks•my heart. You•cannot•give•me•too•much•work;•to accumulate•work•has•almost•become•a passion•with•me:
more rules • Your reformat function and any functions you write that it calls directly or indirectly must not use any std::string objects. If you need to use a string, use a C string. • Your function may assume that no input line will be 200 or more characters long. • This project is doable without assuming any upper limit for the output line length (the third parameter of reformat) — rather than storing the parts of an output line in a C string to be written later, you instead write them as soon as you can. However, some people may not figure out how to do that, so we'll give you a choice. If the third parameter of reformat is greater than 500, your implementation of reformat must either: • return 2 without writing any output • reformat the text using the indicated line length, returning 0 or 1 as appropriate (i.e., your algorithm doesn't impose any upper limit on the output line length). Implementing this choice correctly is worth 5 bonus points.
File I/O • #include <iostream> #include <fstream> • ifstream • input file stream type • this object will be associated with a particular file • ofstream • output file stream type • this object will be associated with a particular file
Advice from Professor • Break down the function into two major tasks • one fetches next word from the input text file • identifies the next interest is a word, paragraph break, or the end of file • if it is a word, get the word • the other one writes the fetched word according to the defined format • will it be the first word in a line? • will it be the last word in a line? • does it end with period/question mark/exclamation?
getting next word • basically deliver a single word to the reformat function • read single character in text file • .get() function • need code that skips redundant spaces • isspace() function • maybe helpful to provide additional information along with the word • is this word preceded by a newline? • is this word followed by a newline?
sample code bool getWord(istream& inf, char outWord[], int& wordLen) { char c; int index = 0; do { if ( ! inf.get(c)) return false; } while (isspace(c)); do { outWord[index] = c; index++; if ( ! inf.get(c)) break; } while( ! isspace(c)); outWord[index] = '\0'; wordLen = index; return true; } int main () { ifstream inf("input.txt"); char word[200]; int wordLen; while (getWord(inf, word, wordLen)) { cout << word << " : "; cout << wordLen << endl; } } Hello everyone, my name is Sungwon Yang. Nice to meet you! Hello : 5 everyone, : 9 my : 2 name : 4 is : 2 Sungwon : 7 Yang. : 5 Nice : 4 to : 2 meet : 4 you! : 4
sample code • it eliminates all spaces/newlines • need to add some code that identifies line breaks Hello everyone, my name is Sungwon Yang. Nice to meet you! Hello : 5 everyone, : 9 my : 2 name : 4 is : 2 Sungwon : 7 Yang. : 5 Nice : 4 to : 2 meet : 4 you! : 4
writing text in file • it is very similar to cout object int main () { ofstream outText("result.txt"); char space = ' '; char newline = '\n'; char word1[10] = "Hello,"; char word2[10] = "how"; char word3[10] = "are"; char word4[10] = "you?"; outText << word1 << newline; outText << word2 << space << word3 << space << word4; } Hello, how are you?