Syntax struct complex double real double image struct
Syntax מבנים : • הגדרת מבנה struct complex { double real; double image; }; struct complex c; struct complex* pc; של המבנה members ה • הגדרת משתנים מסוג זה
Syntax מבנים struct complex { double real; double image; }; int main() { struct complex c; struct complex *pc; c. real = 5; c. image = 7; } c real 5 image 7
Syntax מבנים struct complex { double real; double image; }; int main() { struct complex c; struct complex *pc; c. real = 5; c. image = 7; pc = &c; pc->real = 3; pc->image = 4; } c 6024 real 3 5 image 4 7 pc 6024
Syntax מבנים struct complex { double real; double image; }; : סיכום נשתמש בנקודה c של members לגישה ל -> נשתמש ב pc של members לגישה ל
#include <stdio. h> struct complex { double real; double im; }; void Print. Complex (struct complex num); void Scan. Complex(struct complex *p_num); int main() { struct complex x; Scan. Complex(&x); Print. Complex(x); return 0; }
void Print. Complex (struct complex num) { printf(“%lf + %lf in”, num. real, num. im); } void Scan. Complex(struct complex *p_num) { scanf(“%lf%lf”, &(p_num->real), &(p_num->im)); }
double Abs. Complex(struct complex a) { return sqrt(a. real * a. real + a. im * a. im); } struct complex Add(struct complex a, struct complex b) { struct complex s; s. real = a. real + b. real; s. im = a. im + b. im; return s; } שימוש בפונקציות x = Add(x, y); ! העברה לפי ערך ab = Abs. Complex(x);
typedef- מבנים ושימוש ב struct complex { double real; double image; }; int main() { struct complex c={5, 7}; struct complex* pc; } struct complex { double real; double image; }; typedef struct complex_t; int main() { complex_t c={5, 7}; complex_t *pc; } typedef struct complex_t; טיפוס קיים שם חדש לטיפוס קיים
double Abs. Complex(complex_t a) { return sqrt(a. real * a. real + a. im * a. im); } complex_t Add(complex_t a, complex_t b) { complex_t s; s. real = a. real + b. real; s. im = a. im + b. im; return s; }
double Abs. Complex(complex_t* pa) { return sqrt(pa->real*pa->real + pa->im*pa->im); } complex_t Add(complex_t* pa, complex_t* pb) { complex_t s; s. real = pa->real + pb->real; s. im = pa->im + pb->im; return s; }
מערכים ומבנים • פונקציה להדפסת מערך void Print. Complex. Arr(complex_t arr[ ], int n) { int i; for (i=0; i<n; i++) { Print. Complex(arr[i]); } }
פתרון int Max. Abs(complex_t arr[], int n( } int i, max. In; double max. Abs=-1; for (i=0; i<n; i(++ if ( Abs. Complex(arr[i]) > max. Abs( } max. Abs = Abs. Complex(arr[i; ([ max. In = i; { return max. In; {
#include <stdio. h> #define ROW 3 #define COLUMN 4 int main() { int a[ROW][COLUMN]= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int i, j; מעבר על השורות for ( i = 0; i < ROW; i++ ) { הדפסת כל שורה for ( j = 0; j < COLUMN; j++ ) printf( "%4 d", a[ i ][ j ] ); printf( "n" ); } return 0; }
void print. Array(int a[][ COLUMN ] , int size ) { int i, j; מעבר על השורות for ( i = 0; i < size; i++ ) { הדפסת כל שורה for ( j = 0; j < COLUMN; j++ ) printf( "%4 d", a[ i ][ j ] ); printf( "n" ); } }
#include <stdio. h> #define ROW 3 #define COLUMN 4 void print. Array(int [][ COLUMN ] , int ); int main() { int array 1[ ROW ][ COLUMN ] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; printf( "Values in array 1 by row are: n" ); print. Array( array 1 , ROW ); return 0; } void print. Array(int a[][ COLUMN ] , int size ) { int i, j; for ( i = 0; i < size; i++ ) { for ( j = 0; j < COLUMN; j++ ) printf( "%4 d", a[ i ][ j ] ); printf( "n" ); } }
פתרון void fill. Array( int a[][ COLUMN ] , int size ) { int i, j; for ( i = 0; i < size; i++ ) { for ( j = 0; j < COLUMN; j++ ) a[ i ][ j ] = (i+1)*(j+1); } }
מערכים דו מימדים ומחרוזות void mark. Name(char Names[][STR_SIZE], int size, char *name) { int i; for ( i = 0; i < size; i++ ) if (strcmp(Names[i], name) == 0) { strcat(Names[i], "*"); return; } }
. נגדיר מערך של מצביעים char *names[N]={NULL, NULL}; names[0] = (char*)malloc(5); strcpy(names[0], ”Gadi”); names[1] = (char*)malloc(5); strcpy(names[1], ”Rami”); names[2] = (char*)malloc(5); strcpy(names[2], ”Dani”); 0 1 2 6024 NULL 5124 NULL 1084 NULL 6024 G a d i 5124 R a m i 1084 D a n i
- Slides: 34