ΗΥ-150. Προγραμματισμός

Σχετικά έγγραφα
ΗΥ-150 Προγραμματισμός Αλυαριθμητικά (Strings)

ΗΥ-150. Προγραμματισμός

Συµβολοσειρές - Strings

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

HY150a Φροντιστήριο 3 24/11/2017

2 Composition. Invertible Mappings

(C) 2010 Pearson Education, Inc. All rights reserved.

C.S. 430 Assignment 6, Sample Solutions

Homework 3 Solutions

The Simply Typed Lambda Calculus

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Other Test Constructions: Likelihood Ratio & Bayes Tests

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Elements of Information Theory

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Μορφοποίηση υπό όρους : Μορφή > Μορφοποίηση υπό όρους/γραμμές δεδομένων/μορφοποίηση μόο των κελιών που περιέχουν/

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Areas and Lengths in Polar Coordinates

A ΜΕΡΟΣ. 1 program Puppy_Dog; 2 3 begin 4 end. 5 6 { Result of execution 7 8 (There is no output from this program ) 9 10 }

Διάλεξη 8η: Αλφαριθμητικά (strings)

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Areas and Lengths in Polar Coordinates

Section 8.3 Trigonometric Equations

Partial Trace and Partial Transpose

ST5224: Advanced Statistical Theory II

Γ7.2 Συμβολοσειρές (Strings) Γ Λυκείου Κατεύθυνσης

EE512: Error Control Coding

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Modbus basic setup notes for IO-Link AL1xxx Master Block

Srednicki Chapter 55

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΣΧΟΛΗ ΘΕΤΙΚΩΝ ΕΠΙΣΤΗΜΩΝ ΤΜΗΜΑ ΜΑΘΗΜΑΤΙΚΩΝ ΙΠΛΩΜΑΤΙΚΗ ΕΡΓΑΣΙΑ

Every set of first-order formulas is equivalent to an independent set

CRASH COURSE IN PRECALCULUS

Γλώσσα Προγραμματισμού C++ Εισαγωγή - Μια πρώτη ματιά

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

derivation of the Laplacian from rectangular to spherical coordinates

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

4.6 Autoregressive Moving Average Model ARMA(1,1)

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

ΕΠΛ 012. JavaScripts

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΕΙΡΑΙΩΣ ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ. Βάσεις Δεδομένων (4 ο εξάμηνο) Εργαστήριο MySQL #2

Solutions to Exercise Sheet 5

Homework 8 Model Solution Section

17TimeThis.h function returns reference pointer to same object { return *this; }

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

Example Sheet 3 Solutions

If we restrict the domain of y = sin x to [ π, π ], the restrict function. y = sin x, π 2 x π 2

Models for Probabilistic Programs with an Adversary

Διάλεξη 3: Προγραμματισμός σε JAVA I. Διδάσκων: Παναγιώτης Ανδρέου

Example of the Baum-Welch Algorithm

Fractional Colorings and Zykov Products of graphs

alpha Language age (3/5) alpha Language Φροντιστήριο Syntax Directed Translation and

Τεχνικές Συµπίεσης Βίντεο. Δρ. Μαρία Κοζύρη Τµήµα Πληροφορικής Πανεπιστήµιο Θεσσαλίας

If we restrict the domain of y = sin x to [ π 2, π 2

Solution Series 9. i=1 x i and i=1 x i.

Homework for 1/27 Due 2/5

Εντολές εισόδου - εξόδου. Εισαγωγή στη C++

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Finite Field Problems: Solutions

Φροντιςτήριο. Linked-List

2.1. Εντολές Σχόλια Τύποι Δεδομένων

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

Inverse trigonometric functions & General Solution of Trigonometric Equations

ΕΠΙΧΕΙΡΗΣΙΑΚΗ ΑΛΛΗΛΟΓΡΑΦΙΑ ΚΑΙ ΕΠΙΚΟΙΝΩΝΙΑ ΣΤΗΝ ΑΓΓΛΙΚΗ ΓΛΩΣΣΑ

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Business English. Ενότητα # 9: Financial Planning. Ευαγγελία Κουτσογιάννη Τμήμα Διοίκησης Επιχειρήσεων

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Galatia SIL Keyboard Information

Introduction to Java. Module 4: Advanced Language Features. Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

5.4 The Poisson Distribution.

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

STARTING STEPS IN GRAMMAR, FINAL TEST C TERM 2012 UNITS 1-18

Lecture 2. Soundness and completeness of propositional logic

Στοιχειώδης προγραμματισμός σε C++

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

PARTIAL NOTES for 6.1 Trigonometric Identities

Μηχανική Μάθηση Hypothesis Testing

Εργαστήριο 7: Ο αλγόριθμος ταξινόμησης Radix Sort

Statistical Inference I Locally most powerful tests

Block Ciphers Modes. Ramki Thurimella

Trigonometric Formula Sheet

ΤΕΧΝΟΛΟΓΙΕΣ ΠΟΛΥΜΕΣΩΝ

Goals of this Lecture. Generics(Γενικότητα) Generic Data Structures Example. Generic Data Structures Example

Approximation of distance between locations on earth given by latitude and longitude

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

Δυναμική μνήμη με πίνακες και λίστες

TMA4115 Matematikk 3

Μεθόδων Επίλυσης Προβλημάτων

Section 7.6 Double and Half Angle Formulas

Προαπαιτούμενες Ασκήσεις 5 ου Εργαστηρίου. Dose stoixeio (integer) : 25 Found stoixeio in position 7 Dose stoixeio (integer) :94 Value not found

Transcript:

ΗΥ-150 Αλφαριθμητικά (Strings)

Χαρακτήρες Αναπαριστώνται από έναν δυαδικό αριθμό 8 δυαδικών ψηφίων (8 bits) (256 διαφορετικές τιμές) Η κωδικοποίησή τους έχει τυποποιηθεί με τον κώδικα ASCII Εκτός από γράμματα και ψηφία υπάρχουν και πολλοί ειδικοί χαρακτήρες Σταθερές χαρακτήρων Η έκφραση 'z' είναι μια σταθερά και αντιστοιχεί σε έναν int με τα 8 τελευταία του bits ίσα με τον ASCII κωδικό του χαρακτήρα 'z, \t, \n, κτλ

Βασικά για τα Αλφαριθμητικά (strings) Μια σειρά από χαρακτήρες που αντιμετωπίζονται σαν ένα αντικείμενο Γράμματα, αριθμοί, ειδικοί χαρακτήρες (*, /, $) και όλοι οι εκτυπώσιμοι χαρακτήρες Τιμές εισάγονται μέσα σε διπλά εισαγωγικά "Hello" Τα Strings είναι πάντα πίνακες από χαρακτήρες Ένα String είναι δείκτης στον 1 ο χαρακτήρα του πίνακα Τιμή του string είναι η διεύθυνση του 1 ου χαρακτήρα του πίνακα

char vs. C string A data type char and is stored in 1 byte 5000 A A is a C string of 1 character and is stored in 2 bytes 6000 A 6001 \0

Διαφορά μεταξύ'a' και "A". Το πρώτο είναι ο, ένας, χαρακτήρας A και το δεύτερο είναι το string A. Αφού τα strings είναι null-terminated, το "A" χρησιμοποιεί 2 χαρακτήρες το 'A' και το'\0'. Το "Hello" χρησιμοποιεί 6 χαρακτήρες, 'H', 'e', 'l', 'l', 'o', and '\0'. Έστω η δήλωση: char name[16]; Αφού τα C/C++ strings είναι null-terminated και ο name έχει 16 χαρακτήρες,, το μεγαλύτερο string που μπορεί να αποθηκευθεί στον name είναι ένα 15 χαρακτήρων. Εάν αποθηκεύσουμε ένα string 10 χαρακτήρων στον name, τότε μόνο τα πρώτα 11 στοιχεία του είναι σε χρήση, ενώ τα υπόλοιπα 5 όχι.

char message[8]; // declaration allocates memory Για το μεταφραστή, η τιμή του message είναι η διεύθυνση της αρχής του πίνακα. 6000 H e l l o \0 message [0] [1] [2] [3] [4] [5] [6] [7]

string[0] = J string[1] = I string[2] = b string[3] = r string[4] = a string[5] = n string[6] = string[7] = B string[8] = h string[9] = a string[10] = t string[11] = \0

Ανάγνωση Διάβασμα strings Χρήση cin cin >>word; Αντιγράφει στο word[] Προσθέτει το '\0' Αφήνουμε χώρο στον πίνακα και για το '\0'

String variables string first; first = "Hello, "; string second = "world."; cout << first << second << endl;

Extracting characters from a string string fruit = "banana"; char letter = fruit[1]; cout << letter << endl; char letter = fruit[0];

Length int length; length = fruit.length(); int length = fruit.length(); char last = fruit[length]; // WRONG!! int length = fruit.length(); char last = fruit[length-1];

Traversal 1 int index = 0; while (index < fruit.length()) { char letter = fruit[index]; cout << letter << endl; index = index + 1; }

Traversal 2 for (int index=0; index< fruit.length();index++) { char letter = fruit[index]; cout << letter << endl; }

The find function Searches the string for the content and returns the position of the first occurrence in the string. string fruit = "banana"; int index = fruit.find( a ); string fruit = "banana"; int index = fruit.find("nan"); This example returns the value 2.

Ορισμός Strings Παραδείγματα: hello, high school, H2O. Συνηθισμένες «πράξεις» με strings: Παράθεση: high + school = highschool Σύγκριση: high < school // alphabetical

The string Class in C++ C++ has a <string> library Include it in your programs when you wish to use strings: #include <string> In this library, a class string is defined and implemented

Declaration of strings The following instructions are all equivalent. They declare x to be an object of type string, and assign the string high school to it: string x( high school ); string x= high school ; string x; x= high school ;

Operations on strings (Concatenation) Let x and y be two strings To concatenate x and y, write: x+y string x= high ; string y= school ; string z; z=x+y; cout<< z= <<z<<endl; z =z+ was fun ; cout<< z= <<z<<endl; Output: z=highschool z= highschool was fun

Concatenation of Mixed-Style Strings In s=u+v+w; u can be A string object, or where s is of type string, a C-style string (a char array or a char pointer), a C-style char or a double-quoted string, or a single-quoted character. Same with v and w. At least u or v or w must be a string object

Παράδειγμα string x= high ; char y[]= school ; char z[]= { w, a, s, \0 }; char *p = good ; string s= x+y+ +z+ very + +p+! ; cout<< s= <<s<<endl; cout<< s= +s<<endl; Output: s=highschool was very good! s=highschool was very good!

The concat-assign Operator += Assume x is a string object. The statement x += y; is equivalent to x=x+y; where y can be a string object, a C-style string variable, a char variable, a double-quoted string, or a single-quoted char.

Comparison Operators for string Objects We can compare two strings x and y using the following operators: ==,!=, <, <=, >, >= The comparison is alphabetical (ASCII) The outcome of each comparison is: true or false The comparison works as long as at least x or y is a string object. The other string can be a string object, a C-style string variable, or a double-quoted string.

Example of String Comparisons string x= high ; char y[]= school ; char *p = good ; if (x<y) cout<< x<y <<endl; if (x< tree ) cout<< x<tree <,endl; if ( low!= x) cout<< low!= x <<endl; if( (p>x) cout<< p>x <<endl; else cout<< p<=x <<endl; Output: x<y x<tree low!= x p>x

The Index Operator [] If x is a string object, and you wish to obtain the value of the k-th character in the string, you write: x[k]; This feature makes string objects string x= appear high ; like arrays of chars. char c=x[0]; // c is h c=x[1]; // c is i c=x[2]; // c is g

Getting a string Object Length & Checking for Emptiness To obtain the length of a string object x, call the method length() or size(): int len=x.length( ); --or-- int len=x.size( ); To check of x is empty (that is, has no characters in it): bool x.empty();

Obtaining Substrings of Strings Logically, a substring of a string x is a subsequence of consecutive characters in x For example, rod is a substring of product If x is a string object, and we want the substring that begins at position pos and has len characters (where pos and len are of type int), write: string y = x.substr(pos,len); The default value of len is x.length( ) string y = x.substr(pos);//x[pos..end-1] The default value for pos is 0

Inserting a String Inside Another Suppose x is a string object, and let y be another string to be inserted at position pos of the string of x To insert y, do: The argument y can be: a string object, a C-style string variable, or a double-quoted string x.insert(pos,y);

Replacing a Substring by Another Suppose x is a string object, and suppose you want to replace the characters in the range [pos,pos+len) in x by a string y. To do so, write: The argument y can be: a string object, a C-style string variable, or a double-quoted string x.replace(pos,len,y);

Deleting (Erasing) a Substring of a string Object Suppose x is a string object, and suppose you want to delete/erase the characters in the range [pos,pos+len) in x. To do so, write: The default value of len is the x.length( ) x.erase(pos,len); The default value for pos is 0 To erase the whole string of x, do: x.erase(pos); // erases x[pos..end-1] x.clear( );

Searching for (and Finding) Patterns in Strings Suppose x is a string object, and suppose you want to search for a string y in x. To do so, write: int startloc = x.find(y); This method returns the starting index of the leftmost occurrence of y in x, if any occurrence exits; otherwise, the method returns the length of x. To search starting from a position pos, do int startloc = x.find(y, pos);

Searching for Patterns (Contd.) In all the versions of find and rfind, the argument y can be a string object, a C-style string variable, double-quoted string, a char variable, or a singlequoted char. startloc = x.rfind(y); // or startloc = x.rfind(y, pos);

An Example string x= FROM:ayoussef@gwu.edu ; int colonpos=x.find( : ); string prefix=x.substr(0,colonpos); //=FROM string suffix = x. substr(colonpos+1); cout<< -This message is from <<suffix<<endl; Output: -This message is from ayoussef@gwu.edu

Trimming Leading & Trailing Spaces // this function removes leading and trailing spaces from x void trim (string& x){ int k = 0; // k will procced to the first non-blank char while(k<x.size() &&(x[k]==' ' x[k]=='\t' x[k]=='\n')) k++; x.erase(0,k); } int s=x.size(); // s will move backward to the rightmost non-blank char while(s>0 &&(x[s-1]==' ' x[s-1]=='\t' x[s-1]=='\n')) s--; x.erase(s);

Correspondence between the C library and the C++ string Class C Library Functions C++ string operators/methods strcpy strcat = (the assignment operator) += (assign+concat operator) strcmp = =,!=, <, >, <=, >= strchr, strstr strrchr.find( ) method.rfind( ) method strlen.size( ) or.length( ) methods

Char Functions in C (and C++) The <ctype.h> library in C provides useful functions for single char variables The next slide gives the most common char functions. Although the input argument appears to be of type int, it is actually a char.