270 likes | 415 Views
C workshop #6. Files, Interface to Excel & Matlab, Introduction to C++, More Financial Applications (Newton-Raphson), Cholesky decomposision By: Yuli Kaplunovsky, yuli@magniel.com, (408) 309 4506. Reminder #1.
E N D
C workshop #6 Files, Interface to Excel & Matlab, Introduction to C++, More Financial Applications (Newton-Raphson), Cholesky decomposision By: Yuli Kaplunovsky, yuli@magniel.com, (408) 309 4506
Reminder #1 • Variablesint I; from -2,147,483,648 to 2,147,483,647double D; a fractional numberchar C; a single character, C=‘K’;char S[100]; an array of 100 characters, strcpy(S,”Hello”); • Control for (I = 0 ; I < 10 ; I++) { printf(“%d “,I); } printf(“\n”);K = 1; while (K < 100) { printf(“%d, “, K); K = K * 2; } do { C = getc(); printf(“%c”, C); } while ( C != ‘q’ );goto ERROR_HANDLE;switch (I) { case 0: K=99; break; case 1: K=88; break; default: K=44; } • ifif ( A < 10 && A < 20 ) printf(“Yes\n”); else printf(“No\n”);if ( strcmp( Job, “finance”) == 0 || Salary > 100000) printf(“$”);
Reminder #2 • Functionsvoid main( void ) { .... }int Max( int A, int B ) { if (A>B) return A; else return B; } • Pointers int Ar[6] = {10,15,20,25,30,35}, *P=NULL, J, K, L;P = &Ar[5];J = Ar[5];K = *P;*P = 88;L = Ar[5]; • Function with pointersvoid Switch( int &P1, int &P2 ) { int C=*P1; *P1=*P2; *P2=C; }calling the function: Switch(&Ar[2], &Ar[4] );or easier to understand: int *Px, *Py; Px = &Ar[2]; Py = &Ar[4]; Switch( Px, Py );
Reminder #3 • Structurestruct { double Fahrenheit; int On; } T[10];T[0].Fahrenheit = 102.5; T[0].On = 1;T[1].Fahrenheit = 90; T[1].On = 0;
void main() { vehicle motorcycle, truck, sedan; motorcycle.initialize(2, 900.0); truck.initialize(18, 45000.0); sedan.initialize(4, 3000.0); cout << "The truck has a loading of " << truck.wheel_loading() << " pounds per wheel.\n"; cout << "The motorcycle weighs " << motorcycle.get_weight() << " pounds.\n"; cout << "The sedan weighs " << sedan.get_weight() << " pounds, and has " << sedan.get_wheels() << " wheels.\n"; } class #include <iostream.h> class vehicle { protected: int wheels; float weight; public: void initialize(int wheels, float weight); int get_wheels(void); float get_weight(void); float wheel_loading(void); }; // initialize to any data desired void vehicle::initialize(int wheels, float weight) { wheels = in_wheels; weight = in_weight; } // get the number of wheels of this vehicle int vehicle::get_wheels() { return wheels; } // return the weight of this vehicle float vehicle::get_weight() { return weight; } // return the weight on each wheel float vehicle::wheel_loading() { return weight/wheels; } Output: The truck has a loading of 2500 pounds per wheel. The motorcycle weighs 900 pounds. The sedan weighs 3000 pounds, and has 4 wheels.
inheritance class car : public vehicle { int passenger_load; public: void initialize(int in_wheels, float in_weight, int people = 4); int passengers(void); }; void car::initialize(int in_wheels, float in_weight, int people) { passenger_load = people; wheels = in_wheels; weight = in_weight; } int car::passengers(void) { return passenger_load; } void main() { car Mercedes; Mercedes.initialize(4, 3500.0, 5); cout << "The Mercedes carries " << Mercedes.passengers() << " passengers.\n"; cout << "The Mercedes weighs " << Mercedes.get_weight() << " pounds.\n"; cout << "The Mercedes's wheel loading is " << Mercedes.wheel_loading() << " pounds per tire.\n\n"; } Output: The Mercedes carries 5 passengers. The Mercedes weighs 3500 pounds. The Mercedes's wheel loading is 875 pounds per tire.
1. Select from the menu: View --> ClassWizard 2. Select from the tabs: ‘Member Vriable’ 3. Select Variable type: int, and type “m_Number1” as the Member variable name
Do the same for all three edit boxes, naming them: m_Number1, m_Number2, m_Result
1. Select the Message Maps tab 4. Press “Add Function” 5. Confirm 3. Select BN_CLICKED 2. Select Object IDs: IDOK