A CopyRight: 2003, Mahler Works C << >> 1.1 C (5 min) 1.1.1C 1.1.21998 ANSIAmerican National Standards InsituteISO International Standards Organzation 1.1.3C/C++C C 1.1.4 C JAVA PLATFORM C 1.2 (5 min) 1.2.1 1.2.2 1.2.3 1.3 (5 min) 1.3.1Input Device 1.3.2Output Device 1.3.3Memory Unit 1.3.4Arithmetic Unit 1.3.5Central processing Unit (CPU) 1.3.6Secondary storage Unit 1.4 (5 min) 1.4.1Machine Language +1300042774 +1400593419 +1200274027 1.4.2Assembly Language ADD AX,2 MOV dx,ax 1.4.3High-level Language 1 44
Ex1.4 DOS DEBUG a 100 add ax,2 ENTER u100 Q 1.5 C (5 min) 1.5.1C.cpp ;.cxx ;.c 1.5.2C C Editor DISK Preprocessor DISK Compiler DISK Linker DISK Loader Primary Memory CPU Primary Memory 1.6 Microsoft Visual C++ (5 min) MS VC++ 1.7 - (15~30 min) //Prg. 1.7.1: prg1.7.1.cpp // int main() cout << I love u all baby!! ; return 0; // 0 1.7.1// 1.7.2 int main() int MAIN 1.7.3cout 1.7.4 \n \t 2 44
\r \a \\ \ 1.8 - (15~30 min) //Prg. 1.8.1: prg1.8.1.cpp // int main() cout << 2 + 3 ; return 0; // 0 1.8.1 1.8.2 1.8.3cin 1.8.4 1.9 (Memory Concept)(5~30 min) 1.9.1 1.10 (Arithmetic)(5~30min) 1.10.1 C C + F+7 F + 7 - p-c P c * Bm B * m / X/y X / y % R mod s R % s ( ) *, /, % 3 44
+, - 1.10.2 y = ax + b y = a * x + b 1.11 (Equality and Relational Operator)(5~30min) 1.11.1 C == X==Y X Y!= X!=Y X Y > > X > Y X Y < < X < Y X Y >= X >= Y X Y <= X <= Y X Y 1.11.2if //Prg. 1.11.2: prg1.11.2.cpp // int main() int x,y; cout << Input X: ; cin >> x; cout << Input Y ; cin >> y; if (x>y) cout << First is BIG ; if(x<y) cout << Second is BIG ; return 0; // 0 4 44
5 44
2.1 if/else (20 min) 2.1.1 if/else if else //Prg 2.1.1 : prg2_1_1 //if / else int grad; cout << ; cin >> grad ; if (grad >=60) cout << ; else cout << ; 2.2 (20 min) if 90 A else if 80 B else if 70 C else if 60 D else 2.3 while (20 min) 2.3.1while while int product = 2; 6 44
while ( product <= 1000 ) product = product + 1 ; 2.3.2 WHILE 1. -1 2. 99 2 2 * 1 = 2 2 * 2 = 4.. 2 * 9 = 18 3. 7 44
3.1 (20~40 min) 3.1.1 += C += 7 C = C + 7 -= D -= 4 D = D 4 *= E *= 5 E = E * 5 /= F /= 3 F = F / 3 %= G %=9 G = G % 9 3.1.2 / ++ A++ A = A + 1 ++ ++A A = A + 1 -- B-- B = B 1 -- --B B = B 1 //Prg. 3.1.2: prg3_1_2.cpp //++ int main() int c ; c = 5 ; cout << c << endl ; cout << c++ << endl ; cout << c << endl << endl ; c = 5 ; cout << c<< endl; cout << ++c << endl ; cout << c << endl ; return 0; // 0 3.2 (10~20 min) 3.2.1 1. (int counter;) 2. (counter = 1;) 3. (++counter;) 4. while (counter <= 10) 8 44
//Prg. 3.2.1: prg3_2_1.cpp // int main() int counter ; counter = 1; while (counter <= 10) cout << counter << endl ; ++counter; return 0; // 0 9 44
4.1for (20~60 min) 4.1.1for for (expression1; expression2; expression3) statement expression1 while (expression2) statement expression3 4.1.2for //Prg. 4.1.1: prg4_1_1.cpp // FOR int main() for (int counter = 1 ; counter <=10 ; counter++ ) cout << counter << endl ; return 0; // 0 4.2 for (20~60 min) 4.2.0FOR for( int number = 2; // number <= 100 ; // sum += numbner, number += 2) // 4.2.1 for(int i = 1; i <= 100 ; i++) 4.2.2 for(int i = 100; i >= 1 ; i--) 4.2.37 77 FOR 4.2.420 2 2 4.2.5 2,5,8,11,14,17,20 4.2.6 99,88,77,66,55,44,33,22,11,0 4.3switch (20~60 min) 4.3.1switch switch ( ) case 1 break; case 2 10 44
break; 4.3.2switch if/else 90 A80 B.. 4.4do/while (20~60 min) 4.4.1 while while( condition) 4.4.2do / while do statement while(condition); do statement while (condition); //Prg. 4.4.2: prg4_2_2.cpp //do/while int main() int counter = 1; do cout << counter << ; counter ++ ; while (counter <= 10) ; cout << endl ; return 0 ; 4.5 4.5.1 AND if 20 if age >= 20 && sex = 1 ; 4.5.2 OR 11 44
if age >= 20 or age <= 12 12 44
5.1 5min 5.2 (5~10 min) 5.2.1 math.h 5.2.2 ceil( x ) ceil( 9.2 ) is 10.0 cos( x ) Cosine cos( 0.0 ) is 1.0 exp( x ) e exp( 1.0 ) = 2.71282 pow( x, y ) X pow( 2, 3 ) is 8 5.3 (10~30min) 5.3.1 //Prg. 5.3.1: prg5_3_1.cpp // int square( int ) ; // int main() for ( i = 1 ; i <= 10 ; i ++) cout << square ( i ) << \t ; cout << endl ; return 0; // 0 int square( int y) return y * y ; 5.3.2 int square( int ) ; int square () int 5.3.3 return-value-type function-name( parameter-list) declarations and statements 13 44
5.3.4return return expression; 5.4 a,b,c C++ 14 44
6.1 6.1.1 C++ long double double float unsigned long int long int unsigned int int unsigned short int short int unsigned char char 6.2 (Header File) 6.2.1 6.2.2 6.2.3 6.3 (Random Number Generation) 6.3.1 i = rand() ; rand() stdlib.h 6.3.2 rand() Rand 0 32767 6.3.3 //Prg. 6.3.3: prg6_3_3.cpp // #include <stdlib.h> int main() for(int i = 1; i <= 20 ; i ++) cout << (1 + rand() % 6) << \t ; if ( i % 5 == 0 ) cout << endl ; // 15 44
return 0; // 0 6.3.4 6000 6.3.5 srand() srand( x ), x unsigned int //Prg. 6.3.5: prg6_3_5.cpp // #include <stdlib.h> int main() unsigned seed ; cout << ; cin >> seed ; srand ( seed ) ; for(int i = 1; i <= 20 ; i ++) cout << (1 + rand() % 6) << \t ; if ( i % 5 == 0 ) cout << endl ; // return 0; // 0 6.4 C++ 6000 16 44
7.1 overloading 7.1.1 C C C C 7.1.2 int add_values(int a, int b) return (a + b) ; int add_values(int a, int b,int c) return ( a + b + c) ; cout << 200 + 100 = << add_values(200,100) << endl; cout << 200 + 100 + 2 = << add_values(200,100, 2) << endl; 7.2 Recursion 7.2.1 (Recursion) 7.2.2 n * ( n-1 ) * (n-2). * 1 n! 7.2.3 FOR n n! 7.2.4(Recusion) N unsigned long n( unsigned long) 17 44
for (int i=1 ; i <=10 ; i++) cout << \t << i <<! = << n(i) << endl ; unsigned long n( unsigned long i) if (i<=1) return 1 ; else return i * n(i 1) 7.3 (call by value, or, call by refrerence) 7.3.1 (call by value) int add_value(int a, int b) return (a + b) ; int x, y ; x = 2, y = 3; cout << add_value(x,y) << endl ; cout << x << endl; cout << y << endl; Add_valuec MAIN x,y VALUE add_value AB CALL BY VALUE MAIN XY 7.3.2 (call by reference) void add_value(int &a, int &b) a = (a + b) ; int x, y ; x = 2, y = 3; add_value(x,y) ; cout << x << endl; cout << y << endl; X 7.4 7.4.1 C DEFAULT 18 44
void show(int a=1, int b=2, int c=3) cout << a << a << b << b << c << c << endl; show(); show(1001); show(1001,2002); show(1001;2002;3003); 19 44
8.1 8.1.1 C[0] 2 C[1] 3 C[2] 0 C[3] -123 C[4] 3 C[5] 1310 C[6] 41 C[7] 23 C[8] 54 C[9] 467 C[10] -2342 8.1.2 cout << c[0] << c[1] << c[10] ; x = x[3] / 10; 8.2 8.2.1 int b[10] ; int x[9], y[9] ; 8.2.2 int a, n[10] ; for (a = 0; a< 10 ; a++) n[a] = a ; cout << Elements Value ; for (a=0 ; a<10 ; a++) cout << a << \t << n[a] << endl; FOR int n[10] = 32, 26, 54, 12, 23, 44, 56, 78, 77, 88; 20 44
for (int j=0 ; j<10 ; j++) cout << no. << j << is << n[j] << endl ; 8.3constant varible 8.3.1 const int x = 7 ; cout << const variables x is : << x ; 8.3.2 const int x ; x=7; cout << const variables x is : << x ; 8.4 8.4.1 const int arraysize = 12 ; int a [ arraysize ] = 1, 3, 5, 7, 4, 12, 24, 44, 54, 53, 98, 12; int total = 0 ; for (int k=0; k< arraysize; k++) total += a [ k] ; cout << The sum is: << total ; 8.4.2 const int responsesize = 40, frequencysize = 11; int responses[ responsesize ] 21 44
= 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10; int frequency[ frequencysize ] = 0 ; for (int answer = 0; answer < responsesize ; answer++) ++frequency[ responses[ answer ] ]; cout << "Rating" << "\t" << "Frequency" << endl; for (int rate=1 ; rate <frequencysize ; rate++) cout << rate << "\t" << frequency[rate] << endl; 22 44
10.1 const int arraysize = 10 ; int a [ arraysize ] = 2,6, 4, 8, 10, 12, 89, 68, 45, 37 ; int i, hold ; cout << " \n"; for (i=0 ; i<arraysize; i++) cout << " " << a [ i ] ; for (int pass = 0 ; pass < arraysize - 1; pass++) for (i=0 ; i<arraysize ; i++) if ( a[i] > a[i+1] ) hold = a[i] ; a[i] = a[i+1]; a[i+1] = hold ; cout << "\n \n"; for (i=0 ; i<arraysize; i++) cout << " " << a [ i ] ; 23 44
10.2 C MEAN MEDIAN MODE C 24 44
25 44
10.3 (linear Search) CUSTOM Linear Search C 2 2 100 2 int linearsearch( const int array[], int key, int sizeofarray ); const int arraysize = 100; int a[ arraysize ], searchkey, element; for(int x=0; x<arraysize; x++) a[x] = 2 * x; cout << " (Search Key" ; cin >> searchkey ; element = linearsearch( a, searchkey, arraysize ) ; if( element!= -1 ) cout << " :" << element << endl ; else cout << " " << endl ; int linearsearch( const int array[], int key, int sizeofarray ) for(int n=0; n<sizeofarray; n++) if( array[n] == key ) 26 44
return n ; return -1 ; 10.4 Colum0 Colum1 Colum2 Column3 Row0 A[0][0] A[0][1] A[0][2] A[0][3] Row1 A[1][0] A[1][1] A[1][2] A[1][3] Row2 A[2][0] A[2][1] A[2][2] A[2][3] void printmarray(int a[][3]); int array1[2][3] = 1,2,3,4,5,6, array2[2][3] = 1,2,3,4,5,6, array3[2][3] = 1,2,4; cout << " array1 " << endl ; printmarray( array1 ); cout << " array2 " << endl ; printmarray( array2 ); cout << " array3 " << endl ; printmarray( array3 ); void printmarray(int a[][3]) for(int i=0;i<2;i++) for(int j=0;j<3;j++) cout << a[i][j] << ' ' ; cout << endl; 27 44
11.0 #include <string.h> char x[] = "Happy Birthday to You" ; char y[25], z[15]; cout << "The string in array x is:" << x << "\nthe string in array y is:" << strcpy(y,x) << '\n' ; strncpy(z,x,14); // NULL z[14] = NULL; cout << "The String in array z is: " << z << endl ; 11.1 C int *countptr, count; int Float *xptr, *ypter; 28 44
11.2 Int y =5; Int *yptr; yptr = &y ; y yptr int a; //a int *aptr; //aptr a=7; aptr = &a; //a aptr cout << "a " << &a ; cout << "\naptr " << aptr; cout << "\n\na " << a ; cout << "\n*aptr " << aptr; cout<< "\n\n " << "\n&*aptr = " << &*aptr << "\n*&aptr = " << *&aptr << endl ; 29 44
11.3 (Call-By-Reference) void bubblesortr(int *array, const int size); const int arraysize = 10; int a[ arraysize ] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 ; int i; cout << "Data Item in origrinal order\n" ; for(i=0;i<arraysize;i++) cout << " " << a[i] ; bubblesortr(a, arraysize); cout << "\ndata items in ascending order\n" ; for(i=0;i<arraysize;i++) cout << " " << a[i] ; cout << endl ; void bubblesortr(int *array, const int size) void swap(int *,int *); for(int pass=0;pass<size-1;pass++) for(int j=0;j<size-1;j++) if(array[j] > array[j+1]) swap(&array[j],&array[j+1]); void swap(int *element1ptr, int *element2ptr) int hold = *element1ptr; *element1ptr = *element2ptr; *element2ptr = hold; 30 44
11.4 suit[0] = H e a r t s \0 suit[1] = D i a m o n d s \0 suit[2] = C l u b s \0 suit[3] = S p a d e s \0 Heart 0 Diamonds 1 Clubs 2 Spades 3 Ace Two Three Four Five Six Seven Eight Night ten Jack Queen King 0 1 2 3 4 5 6 7 8 9 10 11 12 Deck[2][12] CLUB KING Club King deck DECK suit face suit[row]face[column] 2 9 suit face deck deck ^_^ 31 44
32 44
12.1 struct ; ; ; struct Time int hour; int minute; int second; ; void printstandard( const Time &t ); void printmilitary( const Time &t ); Time dinnertime; // TIME dinnertime dinnertime.hour = 18; // dinnertime.minute = 30; dinnertime.second = 0; cout << "The dinner will br held at "; printmilitary( dinnertime ); cout << "mlitary time, \nwhich is "; printstandard( dinnertime ); cout << "standard time.\n"; // dinnertime dinnertime.hour = 29; dinnertime.minute = 73; cout << "\ntime with invalid values: " ; printmilitary( dinnertime ); cout << endl ; void printmilitary( const Time &t ) cout << ( t.hour < 10? "0" : "") << t.hour << ":" << ( t.minute < 10? "0" : "") << t.minute ; 33 44
void printstandard( const Time &t ) cout << ((t.hour == 0 t.hour == 12 )? 12 : t.hour % 12 ) << ":" << (t.minute < 10? "0" : "" ) << t.minute << ":" << (t.second < 10? "0" : "" ) << t.second << (t.hour < 12? "AM" : "PM" ) ; 12.2 CLASS class struct CLASS STRUCT CLASS STRUCT class TimeClass public: TimeClass(); void settime(int,int,int); void printmilitary(); void printstandard(); private: int hour; int minute; int second; ; CLASS PUBLIC C PRIVATE CLASS CONTRUCTOR 12.2.1CLASS class STUDENT // STUDENT public: int no; int age; ; STUDENT curtis; cout << " "; cin >> curtis.no ; 34 44
cout << " " ; cin >> curtis.age ; cout << curtis.no << endl << curtis.age ; 12.2.2globale function & member function C CLASS STRUCT MEMBER FUNCTION class STUDENT // STUDENT public: void print(); int no; int age; ; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; STUDENT curtis; cout << " "; cin >> curtis.no ; cout << " " ; cin >> curtis.age ; curtis.print(); // 35 44
12.2.3public private PUBLIC PUBLIC PRIVATE PUBLIC MEMBER FUNCTION class STUDENT // STUDENT public: void print(); void input(int,int); private: int no; int age; ; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; // // no age PRIVATE // MEMBER void STUDENT::input(int i_no, int i_age) no = i_no; age = i_age; STUDENT curtis; int age,no ; cout << " "; cin >> no ; cout << " " ; cin >> age ; curtis.input(no, age); curtis.print(); // input // print 36 44
12.2.4constructor constructor constructor 0 contructor class STUDENT // STUDENT public: STUDENT(); // constructor void print(); void input(int,int); private: int no; int age; ; // // //private STUDENT::STUDENT() no = 0; age = 0; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; // // no age PRIVATE // MEMBER void STUDENT::input(int i_no, int i_age) no = i_no; age = i_age; STUDENT curtis; int age,no ; 37 44
cout << " "; curtis.print(); cout << " "; cin >> no ; cout << " " ; cin >> age ; curtis.input(no, age); curtis.print(); // input // print 12.3 CLASS CLASS class TimeClass public: TimeClass(); void settime(int, int, int); void printmilitary(); void printstandard(); private: int hour; int minute; int second; ; TimeClass::TimeClass() hour=minute=second=0; void TimeClass::setTime(int h,int m,int s) hour = (( h>=0 && h<24)? h : 0); minute = ( m>=0 && m< 60 )? m : 0; second = ( s>=0 && s< 60 )? s : 0; void TimeClass::printMilitary() cout << ( hour < 10? "0" : "") << hour << ":" << ( minute < 10? "0" : "") << minute ; void TimeClass::printStandard() cout << ((hour == 0 hour == 12 )? 12 : hour % 12 ) << ":" << (minute < 10? "0" : "" ) << minute 38 44
<< ":" << (second < 10? "0" : "" ) << second << (hour < 12? "AM" : "PM" ) ; TimeClass t; // t cout << "The initial militay time is: "; t.printmilitary(); cout << "\nthe initial standard time is: "; t.printstandard(); t.settime(13,27,6); cout << "\n\nthe militay time after set is: "; t.printmilitary(); cout << "\nthe standard time after set is: "; t.printstandard(); t.settime(99,99,99); cout << "\n\nthe militay time after invalid set is: "; t.printmilitary(); cout << "\nthe standard time after invalid set is: "; t.printstandard(); cout << endl ; 12.4CLASS CLASS GOOD private no price quan 1. constructor 2. input no,quan,price 3. print 4. cal 1 0 39 44
13.1CONSTCONSTANT MEMBER FUNCTION OBJECT C const Time noon(12,0,0); TIME NOON C CONST #include "time5.h" // Time::Time(int hr,int min,int sec)settime(hr,min,sec); void Time::setTime(int h,int m,int s) sethour(h); setminute(m); setsecond(s); void Time::setHour(int h) hour = ( (h>=0 && h<=24)? h:0 ); void Time::setMinute(int m) minute = ( (m>=0 && m<60)? m:0); void Time::setSecond(int s) second = ( (s>=0 && s<60)? s:0); int Time::getHour() const return hour; int Time::getMinute() const return minute; int Time::getSecond() const return second; void Time::printMilitary() const cout << ( hour < 10? "0" : "") << hour << ":" << ( minute < 10? "0" : "") << minute ; void Time::printStandatd() 40 44
cout << ( (hour == 12)? 12: hour % 12) << ":" << ( minute < 10? "0" : "") << minute << ":" << ( second < 10? "0" : "") << second << ":" << ( hour < 12? "AM" : "PM" ); // Time wakeup(6,45,0); const Time noon(12, 0, 0); //non-constant object //constant object //Member Function Object wakeup.sethour(18); //non-const non-const noon.sethour(12); //non-const const wakeup.gethour(); //const non-const noon.getminute(); //const const noon.printmilitary(); //const const noon.printstandatd(); //non-const const Compiling... paper13_1.cpp D:\C++\taiwan\test_for_paper13\paper13_1.cpp(52) : error C2662: 'sethour' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers D:\C++\taiwan\test_for_paper13\paper13_1.cpp(58) : error C2662: 'printstandatd' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers Error executing cl.exe. test_for_paper13.exe - 2 error(s), 0 warning(s) noon.sethour CONST sethour NON-CONST noon.printstandard() CONST CONST MEMBER FUNCTION 41 44
13.2 (operator overloading) #include <string.h> class string public: string(char *); //Constructor void str_append(char *); void chr_minus(char); void show_string(void); private: char data[256]; ; string::string(char *str) strcpy(data,str); void string::str_append(char *str) strcat(data,str); void string::chr_minus(char letter) char temp[256]; int i,j; for(i=0,j=0;data[i];i++) if(data[i]!= letter) temp[j++] = data[i]; temp[j] = NULL; strcpy(data,temp); void string::show_string() cout << data << endl ; 42 44
string title("rescued By C++"); string lesson("understanding Operator Overloading"); title.show_string(); title.str_append(" rescued me!"); title.show_string(); lesson.show_string(); lesson.chr_minus('n'); lesson.show_string(); #include <string.h> class string public: string(char *); //Constructor void operator +(char *); void operator -(char); void show_string(void); private: char data[256]; ; string::string(char *str) strcpy(data,str); void string::operator +(char *str) strcat(data,str); void string::operator -(char letter) char temp[256]; int i,j; for(i=0,j=0;data[i];i++) if(data[i]!= letter) temp[j++] = data[i]; temp[j] = NULL; strcpy(data,temp); void string::show_string() cout << data << endl ; 43 44
string title("rescued By C++"); string lesson("understanding Operator Overloading"); title.show_string(); title + " rescued me!"; title.show_string(); lesson.show_string(); lesson - 'n'; lesson.show_string(); 13.3 struct XY void setvalue(int,int); ; int x; int y; void XY::setValue(int a,int b) x=a; y=b; ostream &operator <<(ostream &ooo,const XY &t) ooo << "[" << t.x << "," << t.y << "]" << endl ; return ooo ; XY test1; test1.setvalue(1,1); cout << test1; 44 44