418115 structured programming 3 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 110

418115: Structured Programming การบรรยายครั้งที่ 3 & 4 PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

418115: Structured Programming การบรรยายครั้งที่ 3 & 4. ประมุข ขันเงิน [email protected] โปรแกรม #1. v oid main() { }. โปรแกรม #1. โปรแกรมนี้ทำอะไร ? ไม่ได้ทำอะไร ข้างในไม่มีคำสั่งอะไรเลย เข้ามาแล้วก็ออกไป. โปรแกรม #1. m ain() คืออะไร ? ฟังก์ชัน ฟังก์ชันคืออะไร ? โปรแกรมย่อย

Download Presentation

418115: Structured Programming การบรรยายครั้งที่ 3 & 4

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


418115 structured programming 3 4

418115: Structured Programming 3 & 4

[email protected]


418115 structured programming 3 4

#1

void main()

{

}


418115 structured programming 3 4

#1

  • ?


418115 structured programming 3 4

#1

  • main() ?

  • ?

  • ?

    • (OS)


418115 structured programming 3 4

#2

#include <stdio.h>

void main()

{

printf(Hello, world!\n);

}


418115 structured programming 3 4

#2

  • ?

    • Hello, world!


418115 structured programming 3 4

#2

  • #include <stdio.h> ?

    • stdio.h

    • stdio.h header file

    • main


418115 structured programming 3 4

#2

  • #include <stdio.h> ?

    • stdio.h

    • stdio.h header file

    • main

  • stdio.h?

    • printf


418115 structured programming 3 4

#2

  • printf?

    • standard output

  • standard output ?

    • standard output


418115 structured programming 3 4

#2

  • Hello, world!\n ?

    • C (string)

    • C string ()

    • one, FranscescaLucchini, 3.1415


418115 structured programming 3 4

#2

  • ! \n?

    • \n

    • escape sequence

    • \n


418115 structured programming 3 4

#3

  • ?

    #include <stdio.h>

    void main()

    {

    printf(one\ntwo\nthree\n);

    printf(four\nfive\nsix\n);

    }


418115 structured programming 3 4

#3

one

two

three

four

five

six


Escape sequence

Escape Sequence


418115 structured programming 3 4

#1

  • a\b\\c

    def


418115 structured programming 3 4

#1

#include <stdio.h>

void main()

{

printf(a\\b\\\\\c\\n);

printf(de\f\);

}


418115 structured programming 3 4

#1

#include <stdio.h>

void main()

{

printf(a\\b\\\\\c\\n);

printf(def);

}


418115 structured programming 3 4

(;)

  • (;)


418115 structured programming 3 4

#4

#include <stdio.h>

void main()

{

printf(First value is %d.\n, 5);

printf(Second value is %d.\n, 7);

}


418115 structured programming 3 4

#4

  • printf(First value is %d., 5);

    • (argument)

    • argument (,)

    • printf argument

      • First value is %d.

      • 5


418115 structured programming 3 4

#4

  • printf

  • ?

  • ?

    • First value is %d.

    • %d (decimal)


418115 structured programming 3 4

#4

  • First value is 5.

    Second value is 7.


418115 structured programming 3 4

#5

#incluce <stdio.h>

void main()

{

printf(Sum of %d and %d is %d.\n, 9, 4, 9+4);

printf(Difference of %d and %d is %d.\n,9, 4, 9-4);

printf(Product of %d and %d is %d.\n, 9, 4, 9*4);

printf(Quotient of %d by %d is %d\n, 9, 4, 9/4);

printf(Modulus of %d by %d is %d\n, 9, 4, 9%4);

}


418115 structured programming 3 4

#5

  • Sum of 9 and 4 is 13.

    Difference of 9 and 4 is 5.

    Product of 9 and 4 is 36.

    Quotient of 9 and 4 is 2.

    Modulus of 9 and 4 is 1.


418115 structured programming 3 4

#5

  • printf(Sum of %d and %d is %d.\n, 9, 4, 9+4);

    • printf argument

    • argument


418115 structured programming 3 4

#5

  • 9+4, 9-4, 9*4, 9/4 9%4

    • + -

    • *

    • /

    • %


418115 structured programming 3 4

#5

    • +

    • * / %

    • * / % +


418115 structured programming 3 4

#2

  • ?

    #include <stdio.h>

    void main()

    {

    printf(%d\n, 7+10%3);

    printf(%d\n, (7+10)%3);

    printf(%d\n, 7+10%3*4-(5*20));

    }


418115 structured programming 3 4

#2

8

2

-89


418115 structured programming 3 4

#6

#incluce <stdio.h>

void main()

{

printf(%f + %f = %f\n, 9.0, 4.0, 9.0+4.0);

printf(%f - %f = %f\n, 9.0, 4.0, 9.0-4.0);

printf(%f * %f = %f\n, 9.0, 4.0, 9.0*4.0);

printf(%f / %f = %f\n, 9.0, 4.0, 9.0/4.0);

}


418115 structured programming 3 4

#6

  • 9.000000 + 4.000000 = 13.000000

    9.000000 - 4.000000 = 13.000000

    9.000000 * 4.000000 = 36.000000

    9.000000 / 4.000000 = 2.250000


418115 structured programming 3 4

#6

  • C

  • C

  • +, -, *, /

  • %f


418115 structured programming 3 4

#7

#include <stdio.h>

void main()

{

int age;

age = 15;

printf(The child age is %d\n, age);

}


418115 structured programming 3 4

#7

  • The child age is 15.


Variable

(Variable)

  • #7 age

  • age printf

    printf(The child age is %d\n, age);


418115 structured programming 3 4

  • 1;

  • 1, 2, 3, , n;


418115 structured programming 3 4

intage;

intx, y, z;

float area, length;

  • int

  • float


Assignment

(Assignment)

  • (=)

    = ;

    x = 5; y = (10*3)%7;

  • x = 10*y+y/5;

    • x = x+1;

    • z = z*z + 2*z + 1

    • x 5 x = x+1 x 6

    • z 3 z = z*z + 2*z + 1 z 16


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

2


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

2

5


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

2

5

6


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

5


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

5

32


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

5

32

6


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

-33

5

32

6


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

-33

5

32

6

32


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

-33

5

32

6

32

5


418115 structured programming 3 4

#3

#include <stdio.h>

void main()

{

int x, y, z;

x = 2; y = 5; z = 6;

x = x+y*z;

z = yx-z;

y = 4*x/y

printf(%d %d %d\n, x, y, z);

}

32

2

5

6

-33

5

32

6

32

5

24


418115 structured programming 3 4

#3

  • 32 24 -33


Identifier

(Identifier)

  • C

    • underscore(_)

      • name, student, a10, light_saber, _times, _101

      • one man

      • [email protected]

      • 10a

      • salary, Salary, SALARY

    • (reserved words)


Reserved words

(Reserved Words)

  • C

  • 36

  • IDE

  • reserved words

    • void

    • int


418115 structured programming 3 4

  • 5

    • int =

    • float =

    • double = ( float 2 )

    • char =

    • void =


418115 structured programming 3 4

  • int

    • unsigned =

    • short =

    • long =

  • int 6

    int, unsigned int,

    short int, unsigned short int,

    long int, unsigned long int

  • short int short

    long int long


418115 structured programming 3 4


418115 structured programming 3 4

  • ( 10 ) int long

  • 64 long long

    • -9,223,372,036,854,775,808

    • 9,223,372,036,854,775,807


418115 structured programming 3 4

#8

#include <stdio.h>

void main()

{

int a = 1000000000;

short b = 1000000000;

long c = 1000000000;

long longd = 1000000000;

printf(%d %d %d %d\n, a, b, c, d);

}


418115 structured programming 3 4

#8

  • 1000000000 -13824 1000000000 100000000


418115 structured programming 3 4

#8

  • %d int, short, long, long long

    • long long ( #9)

    • C compile error

    • 1,000,000,000 b -13,824


418115 structured programming 3 4

#9

#include <stdio.h>

void main()

{

int a = 1000000000000;

short b = 1000000000000;

long c = 1000000000000;

long long d = 1000000000000;

printf(%d %d %d %d\n, a, b, c, d);

}


418115 structured programming 3 4

#9

  • 1,000,000,000,000

  • int long ( short)

  • 1,000,000,000,000

  • :

    -727379968 4096 -727379968 -727379968


418115 structured programming 3 4

#9

  • ?

  • %d int 32

  • 64 long long %lld


418115 structured programming 3 4

#10

#include <stdio.h>

void main()

{

int a = 1000000000000;

short b = 1000000000000;

long c = 1000000000000;

long long d = 1000000000000;

printf(%lld %lld %lld %lld\n, a, b, c, d);

}


418115 structured programming 3 4

#10

  • -727379968 4096 -727379968 1000000000000

  • ?

    • a, b, c

    • d long long %lld %d int


418115 structured programming 3 4

  • long long ll

    • %lld, %llo, %llx, %llX, %llu

  • short h

    • %hd, %ho, %hx, %hX, %hu

  • long l

    • %ld, %lo, %lx, %lX, %lu


418115 structured programming 3 4

#10

#include <stdio.h>

void main()

{

int x = 253;

int y = -1*x;

printf(%d %u %o %x %X\n, x, x, x, x, x);

printf(%d %u %o %x %X\n, y, y, y, y, y);

}


418115 structured programming 3 4

#10

  • 253 253 375 fdFD

    -253 4294967043 37777777403 ffffff03 FFFFFF03


418115 structured programming 3 4

#10

  • -253 %u 4294967043?

    • -253 int 4294667043 unsigned int

    • ?

      • 16 8

    • Twos Complement

    • 8 16


Sizeof

sizeof

  • sizeof

    sizeof()

    • sizeof(int)

    • sizeof(unsigned long long)

    • sizeof(short)

    • sizeof(unsigned long)


418115 structured programming 3 4

#11

#include <stdio.h>

void main()

{

printf("The number of bytes in an int is %d.\n", sizeof(int));

printf("The number of bytes in a short int is %d.\n", sizeof(short));

printf("The number of bytes in a long int is %d.\n", sizeof(long));

printf("The number of bytes in a long long is %d.\n", sizeof(long long));

}


418115 structured programming 3 4

#11

  • The number of bytes in an int is 4.

    The number of bytes in a short int is 2.

    The number of bytes in a long int is 4.

    The number of bytes in a long long is 8.


418115 structured programming 3 4

char

    • a, B, c, +, -, ., !

    • 8 -128 127

  • char

    • %d -128 127

    • %u 0 255

    • %c


418115 structured programming 3 4

#12

#include <stdio.h>

void main()

{

char x = 65;

printf("x as a number is %d.\n", x);

printf("x as a character is %c.\n", x);

}


418115 structured programming 3 4

#12

  • x as a number is 65.

    x as a character is A.


Ascii

ASCII

  • 65 A?

    • 65 ASCII A

    • ASCII


418115 structured programming 3 4

char

  • C

  • ()

    • a

    • B

    • +

    • 9


418115 structured programming 3 4

#13

#include <stdio.h>

void main()

{

char x = '9';

printf("x as a number is %d.\n", x);

printf("x as a character is %c.\n", x);

}


418115 structured programming 3 4

#13

  • x as a number is 57.

    x as a character is 9.


418115 structured programming 3 4

char

  • char

    • A + 5

    • B * x

    • 100 / a

    • A + 5 70 ASCII F A 5

    • x a 23 x a 23


String

string

  • string

    char *;

    • char *name;

  • (=)

    • name = Dennis Richie

  • string printf %s

    • printf(%s\n, name);


418115 structured programming 3 4

#14

#include <stdio.h>

void main()

{

char *name;

name = "Dennis Ritchie";

printf("%s is the creator of the C language.\n",

name);

}


418115 structured programming 3 4

#15

  • Dennis Ritchie is one of the creator of the C language.


418115 structured programming 3 4

#15

  • #include <stdio.h>

    void main()

    {

    char *name= "Dennis Ritchie";

    printf("%s is the creators of the C language.\n",

    name);

    }


418115 structured programming 3 4

  • 3

    • float 32

    • double 64

    • long double 64

  • long double double long double 128

    • float %f

    • double long double %lf


418115 structured programming 3 4

  • %f

  • %e scientific notation

    • 123.45 1.234500e+02 1.2345 102

  • %E scientific notation E


418115 structured programming 3 4

#16

#include <stdio.h>

void main()

{

float score;

score = 300.545;

printf("Score are %f, %e, and %E\n",

score, score, score);

}


418115 structured programming 3 4

#16

  • Score are 300.545013, 3.005450e+002, and 3.005450E+002


418115 structured programming 3 4

#16

  • 300.545013 300.545?

    • 32 ( float)

    • 300.545

    • 300.545013

    • 0.000013 round-off error


418115 structured programming 3 4

#16

  • round-off error ?

    • !

  • double float

    • !


418115 structured programming 3 4

#17

#include <stdio.h>

void main()

{

double score;

score = 300.545;

printf("Score are %f, %e, and %E\n",

score, score, score);

}


418115 structured programming 3 4

#17

  • Score are 300.545000, 3.005450e+002, and 3.005450E+002


418115 structured programming 3 4

  • %w.df

    w d

    • w

    • d

  • w w

  • 12.35 %7.2f


418115 structured programming 3 4

#18

#include <stdio.h>

void main()

{

double temp;

temp = 12.34;

printf("%7.2f, %10.3E, %10.3e\n", temp, temp, temp);

temp = 1.2365e-5;

printf("%7.2f, %10.3E, %10.3e\n", temp, temp, temp);

temp = 584.365E+17;

printf("%7.2f, %10.3E, %10.3e\n", temp, temp, temp);

}


418115 structured programming 3 4

#18

  • 12.34, 1.234E+001, 1.234e+001

    0.00, 1.237E-005, 1.237e-005

    58436500000000000000.00, 5.844E+019, 5.844e+019


418115 structured programming 3 4

#18

  • __12.34, 1.234E+001, 1.234e+001

    ___0.00, 1.237E-005, 1.237e-005

    58436500000000000000.00, 5.844E+019, 5.844e+019


418115 structured programming 3 4

+=, -=, *=, /=, %=

  • = ;

    = ();

  • x += 1;x = x+1;

    y -= x*5;y = y (x*5);


418115 structured programming 3 4

#19

#include <stdio.h>

void main()

{

int a = 0, b = 5;

a += 4;

printf("a is %d\n", a);

a *= 3;

printf("a is %d\n", a);

a /= 4;

printf("a is %d\n", a);

a %= 5;

printf("a is %d\n", a);

a = 7;

a /= a - b;

printf("a is %d\n", a);

b *= b + a;

printf("b is %d\n", b);

}


418115 structured programming 3 4

#19

  • a is 4

    a is 12

    a is 3

    a is 3

    a is 3

    b is 40


418115 structured programming 3 4

++, --

  • x+=1; x++; ++x;

  • x-=1; x--; --x;


418115 structured programming 3 4

#20

#include <stdio.h>

void main()

{

int x = 8;

printf("x is %d\n", x);

x++;

printf("x is %d\n", x);

++x;

printf("x is %d\n", x);

x--;

printf("x is %d\n", x);

--x;

printf("x is %d\n", x);

}


418115 structured programming 3 4

#20

  • x is 8

    x is 9

    x is 10

    x is 9

    x is 8


Scanf

scanf

  • standard input

    • printf

  • stdio.h printf

  • scanf(, & 1, & 2, , & n);

  • scanf

  • scanf


Scanf1

scanf

  • scanf(%d %d, &x, &y);

    10 20

    x 10 y 20


418115 structured programming 3 4

#21

#include <stdio.h>

void main()

{

intval;

printf("Enter a decimal integer: ");

scanf("%d", &val);

printf("The value is %o in octal, and %x in \

hexadecimal.\n", val, val);

printf("Enter an octal integer: ");

scanf("%o", &val);

printf("The value is %d in decimal.\n", val);

printf("Enter a hexadecimal integer: ");

scanf("%x", &val);

printf("The value is %d in decimal.\n", val);

}


418115 structured programming 3 4

#21

  • Enter a decimal integer: 78

    The value is 116 in octal, and 4e in hexadecimal.

    Enter an octal integer: 116

    The value is 78 in decimal.

    Enter a hexadecimal integer: 4e

    The value is 58446 in decimal.

    ( )


418115 structured programming 3 4

#4

  • x x, x2, x3


418115 structured programming 3 4

#4

#include <stdio.h>

void main()

{

int x;

scanf("%d\n", &x);

printf("%d %d %d\n", x, x*x, x*x*x);

}


418115 structured programming 3 4

#5


418115 structured programming 3 4

#5

#include <stdio.h>

void main()

{

float r;

float pi = 3.14159265;

scanf("%f\n", &r);

float area = pi * r * r;

float circumference = 2 * pi * r;

printf("%f %f\n", area, circumference);

}


  • Login