55
Computer Programming กกกกกกกกกกกกกกกกกกกกกกกกกก กกกกกกกกกก 4 กกกกกกกกกกกกกกกกกกกกกกก กกกกกก

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Embed Size (px)

DESCRIPTION

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์. สัปดาห์ที่ 4 การรับค่าและการแสดงผลในภาษาซี. Outline. 1. Objectives. 2. Scanf Function. p. 3. Printf Function. The Other Function. 4. 5. Assignment #3. objectives. รู้จักและเข้าใจคำสั่งเพื่อใช้ในการรับค่าและแสดงผลในภาษาซี - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Computer Programmingการเขี�ยนโปรแกรมคอมพิ�วเตอร�

สั�ปดาห์�ที่�� 4การร�บค�าและการแสัดงผลในภาษา

ซี�

Page 2: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objectives1

pp2

Printf Function Printf Function3

The Other Function The Other Function4

Assignment #3 5

Scanf Function

Page 3: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

objectives

ร%&จั�กและเขี&าใจัค(าสั��งเพิ)�อใช้&ในการร�บค�าและแสัดงผลในภาษาซี�สัามารถร�บขี&อม%ลจัากค�ย�บอร�ดและแสัดงขี&อความออกที่างจัอภาพิได&สัามารถจั�ดร%ปแบบการแสัดงผลออกที่างจัอภาพิตามร%ปแบบต�างๆ ได&อย�างถ%กต&องเขี�ยนโปรแกรมประมวลผลต�วอ�กษรได&

Page 4: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objectives1

pp2

Printf Function Printf Function3

The Other Function The Other Function4

Assignment #3 5

Scanf Function

Page 5: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Receiving the Information from Keyboard

โปรแกรมโดยที่��วไปต&องม�การร�บค�าขี&อม%ลจัากผ%&ใช้&โปรแกรม เพิ)�อน(ามาห์าผลล�พิธ์�ตามกระบวนการที่(างานขีองโปรแกรม ห์ร)อตามความต&องการขีองผ%&ใช้&งานค(าสั��งที่��ใช้&สั(าห์ร�บการร�บค�าในภาษาซี�ม�ห์ลายค(าสั��ง แต�ที่��สัามารถใช้&งานได&ครอบคล/มและน�ยมใช้&ก�น ค)อ ค(าสั��งห์ร)อฟั1งก�ช้�น scanf()

Page 6: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Scanf Function (1)

scanf(“format”,argument1,argument2, … );

scanf (format-string, address-list);

scanf("control", &variable1,&variable2,…);

scanf() เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h ใช้&ร�บขี&อม%ลจัากผ%&ใช้&

Page 7: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

scanf ("%f", &a);

Format Code

Scanf Function (2)

Address list

Page 8: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format-string

format-string or format or control format เป2นร%ปแบบการร�บค�าที่��ถ%กก(าห์นดตามช้น�ด

ขีองขี&อม%ล (ค�าต�วแปร) แสัดงอย%�ในเคร)�องห์มาย Double

quotation mark (“_”) ร%ปแบบการร�บค�าต�วแปร (% ตามห์ล�งด&วย

อ�กขีระ) เม)�อร�นผลต&องป5อนค�าตามร%ปแบบที่��ก(าห์นด

ตามด&วยการกด enter

ห์มายเห์ต/ ห์ากผ%&ใช้&ป5อนขี&อม%ลไม�ตรงก�บช้น�ดขีองต�วแปร โปรแกรมจัะที่(างานผ�ดพิลาด (Runtime Error)

Page 9: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Address-list

address-list or argument or address of variable ค)อ ต(าแห์น�งขีองต�วแปรที่��ต&องการเก6บ

ขี&อม%ลไว& (การใช้&งานต(าแห์น�งขีองต�วแปรจัะใช้&เคร)�องห์มาย ampersand (&) น(าห์น&าช้)�อต�วแปร ยกเว&นต�วแปรช้น�ดขี&อความ)

ต�วอย�าง &variable_name

Page 10: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format Control of Scanf Function (1)

สั�วนแสัดงช้น�ดขี&อม%ล

การใช้&งาน

%d แสัดงผลขี&อม%ลช้น�ดจั(านวนเต6ม%u แสัดงผลขี&อม%ลช้น�ดจั(านวนเต6มบวก (ไม�

ค�ดเคร)�องห์มาย)

%o แสัดงผลขี&อม%ลเป2นเลขีฐานแปด%x แสัดงผลขี&อม%ลเป2นเลขีฐานสั�บห์ก%f แสัดงผลขี&อม%ลช้น�ดจั(านวนที่ศน�ยม (6

ต(าแห์น�ง)

%e แสัดงผลขี&อม%ลเป2นจั(านวนที่ศน�ยมและอย%�ในร%ปยกก(าล�ง

%c แสัดงผลขี&อม%ลช้น�ดอ�กขีระ%s แสัดงผลขี&อม%ลช้น�ดขี&อความ%p แสัดงผลขี&อม%ลช้น�ดต�วช้�8ต(าแห์น�ง

Page 11: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

ร%ปแบบการแสัดงผลซี4�งต&องก(าห์นดให์&ตรงก�บช้น�ดขีองต�วแปร

• int , unsigned int ก(าห์นดเป2น %d, %o, %x

• long ก(าห์นดเป2น %ld, %lo, %lx

• float, double ก(าห์นดเป2น %f, %e

• char ก(าห์นดเป2น %c

Input Format of Scanf and Printf Function(2)

Page 12: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Scanf ( )

float point; scanf("%f",&point);

char name[20]; scanf ("%s",name);

int day, month, year;scanf("%d/%d/%d", &day,&month,&year);

int age; scanf("%d", &age);

Page 13: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example 1

#include<stdio.h>int main(){ float point; char name[20]; printf ("Enter your name : "); scanf ("%s",name); printf ("Enter your point : "); scanf ("%f",&point); return 0;}

ต�วแปรขี&อความไม�ต&องม� & ห์น&าต�วแปร

ต�วแปรที่ศน�ยมต&องม� & ห์น&า

ต�วแปร

Page 14: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>int main(){ char first[20],last[20];

printf ("Enter your name and surname : "); scanf ("%s %s",first,last);

printf ("Hi %s %s\nHow are you?",first,last); return 0;}

การร�บต�วแปรมากกว�า 1 ต�วในค(าสั��งเด�ยว

ต&องป5อนขี&อม%ลให์&ม�ร%ปแบบเห์ม)อนก�น

Enter your name and surname : Jirasak SittigornHi Jirasak SittigornHow are you?

Example 2

Page 15: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>int main(){ char name[40]; float gpa; printf ("Enter your name : "); scanf ("%s",name); printf ("Enter your GPA : "); scanf ("%f",&gpa); printf ("Name : %s\n",name); printf ("Gpa : %f",gpa); return 0;}

Example 3

Page 16: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>int main(){ char name[40]; float gpa; printf ("Enter your name : "); scanf ("%[^\n]",name); printf ("Enter your GPA : "); scanf ("%f",&gpa); printf ("Name : %s\n",name); printf ("Gpa : %f",gpa); return 0;}

Example 4

Page 17: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objectives1

pp2

Printf Function Printf Function3

The Other Function The Other Function4

Assignment #3 5

Scanf Function

Page 18: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Show the Results Processing to User

โปรแกรมโดยที่��วไปต&องม�แสัดงผลล�พิธ์�ขีองการประมวลผลห์ร)อการที่(างานขีองโปรแกรม เพิ)�อให์&ผ%&ใช้&งานที่ราบผลล�พิธ์�ขีองการที่(างานค(าสั��งที่��ใช้&สั(าห์ร�บการร�บค�าในภาษาซี�ที่��น�ยมใช้&ก�น ค)อ ค(าสั��งห์ร)อฟั1งก�ช้�น printf()

Page 19: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Printf Function (1)

printf() เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h ใช้&สั(าห์ร�บแสัดงขี&อความ ค�าขีองต�วแปรห์ร)อผลการด(าเน�นการขีองน�พิจัน�ออกที่างห์น&าจัอ

printf (format-string, data-list);

printf(“format”, argument1, argument2, … );

printf(“message or control",variable list);

Page 20: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

printf ("Sum = %f \n", a+b);

Format Code

Format / Carriage Control

Value

Printf Function (2)

Page 21: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format-string

format-string or format or massage or control format เป2นร%ปแบบการแสัดงค�าที่��ถ%กก(าห์นดตามช้น�ด

ขีองขี&อม%ลห์ร)อต�วแปร แสัดงอย%�ในเคร)�องห์มาย Double

quotation mark (“_”) ร%ปแบบการร�บค�าต�วแปร (% ตามห์ล�งด&วย

อ�กขีระ อ�กขีระควบค/มห์ร)อ Escape Character

Page 22: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Data-list

data-list or argument or variable list ค)อ ต(าแห์น�งขีองต�วแปรที่��ต&องการจัะน(า

ขี&อม%ลออกไปแสัดง ถ&าม�มากกว�า 1 ต�วแปรห์ร)อ 1 พิจัน�ขี48นไปจัะต&องค��นระห์ว�างต�วแปรห์ร)อน�พิจัน�ด&วยเคร)�องห์มาย Comma (,)

ซี4�งขี&อม%ลในสั�วนน�8อาจัจัะเป2น ตั�วแปร, นิ�พจนิ� หร�อการดำ�าเนิ�นิการระหว�างตั�วแปรก�ไดำ�

Page 23: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Escape Character or Carriage Control

Format

การแสัดงผล

\0 ค�าว�าง (Null Character)

\a สั�งเสั�ยง 1 คร�8ง (Bell)

\n ขี48นบรรที่�ดให์ม� (New Line)

\t เว&นช้�องว�างเป2นระยะ 1 tab (Horizon Tab)

\r ให์&เคอร�เซีอร�กล�บไปอย%�ซี&ายสั/ดห์ร)อต&นบรรที่�ด (Carriage Return)

\f ขี48นห์น&าให์ม� (Form Feed)

\b ถอยกล�บไป 1 ต�วอ�กษร (Backspace)

\' พิ�มพิ�เคร)�องห์มาย '\" พิ�มพิ�เคร)�องห์มาย "\\ พิ�มพิ�เคร)�องห์มาย \

Page 24: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format Control of Printf Function

สั�วนแสัดงช้น�ดขี&อม%ล

การใช้&งาน

%d แสัดงผลขี&อม%ลช้น�ดจั(านวนเต6ม%u แสัดงผลขี&อม%ลช้น�ดจั(านวนเต6มบวก (ไม�

ค�ดเคร)�องห์มาย)

%o แสัดงผลขี&อม%ลเป2นเลขีฐานแปด%x แสัดงผลขี&อม%ลเป2นเลขีฐานสั�บห์ก%f แสัดงผลขี&อม%ลช้น�ดจั(านวนที่ศน�ยม (6

ต(าแห์น�ง)

%e แสัดงผลขี&อม%ลเป2นจั(านวนที่ศน�ยมและอย%�ในร%ปยกก(าล�ง

%c แสัดงผลขี&อม%ลช้น�ดอ�กขีระ%s แสัดงผลขี&อม%ลช้น�ดขี&อความ%p แสัดงผลขี&อม%ลช้น�ดต�วช้�8ต(าแห์น�ง

Page 25: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Printf Fuction

float g = 3.5; printf("GPA : %.2f",g);

char name[20]; printf ("Your name is :%s",name);

int day, month, year;printf(“Current date: %d/%d/%d",day,month,year);

int age; printf("Your age is :%d", age);

Page 26: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Order of Arguments (Data-list)

printf(“Today is %s , %d %s”.,”Tuesday”, 28, “October”);

printf(“\nTemperature is \”%f celcius.\” ”, 24.5);

Today is Tuesday , 28 October.

Temperature is “24.500000” celcius.

Page 27: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Format Control

#include <stdio.h>int x1 = 32, x2 = 0x7A, x3 = 0527;float y1 = -352.681, y2 = 9.23e04;char z = 'A';char name[11] = "University";void main(){ printf ("x1 = %d", x1); printf ("x2 = %x x3 = %o", x2, x3); printf ("y1 = %f y2 = %e", y1, y2); printf ("z = %c name = %s", z, name);}

Page 28: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Mismatch Example of Format Control

#include <stdio.h>int x1 = -456, x2 = 0.72, x3 = 1xe53;float y1 = -67.335, y2 = 5.21e03;char z = 'C';void main(){ printf ("x2 = %d x3 = %d", x2, x3); printf ("x1 = %u", x1); printf ("y1 = %e y2 = %f", y1, y2); printf ("z = %d", z);}

Page 29: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format Control : Amount Behind floating point

#include <stdio.h>float x1 = 17.983, x2 = 4.149e+03;void main(){ printf ("%.4f ", x1); printf ("%.1f ", x1); printf ("%.2e ", x2); printf ("%.7e ", x2);}

ห์มายเห์ต/ : จั(านวนห์ล�กห์ล�งจั/ดที่ศน�ยมสัามารถก(าห์นดได&โดยค(าสั��ง %.nf ห์ร)อ %.ne โดย n ค)อ จั(านวนห์ล�กห์ล�งจั/ดที่ศน�ยมที่��ต&องการแสัดง

Page 30: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Format Control for Message

#include <stdio.h>char message[] = "Engineering";void main(){ printf ("%.6sX\n", message); printf ("%12.6sX\n", message); printf ("%-12.6sX\n", message);}

ห์มายเห์ต/ : ขี&อความสัามารถถ%กแสัดงเพิ�ยงบางสั�วนเม)�อใช้&ค(าสั��ง %m.ns โดย n ค)อจั(านวนอ�กขีระที่��ต&องการให์&แสัดง และ m ค)อ พิ)8นที่��ที่��ใช้&แสัดงผล n ค)อ จั(านวนอ�กขีระที่��ต&องการให์&แสัดง %-m.ns ที่(าให์&ขี&อความที่��ถ%กแสัดงช้�ดขีอบด&านซี&าย

Page 31: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Format Control for Message

#include<stdio.h> main() {

printf(":%s:\n", "Hello, world!"); printf(":%15s:\n", "Hello, world!"); printf(":%.10s:\n", "Hello, world!"); printf(":%-10s:\n", "Hello, world!"); printf(":%-15s:\n", "Hello, world!"); printf(":%.15s:\n", "Hello, world!"); printf(":%15.10s:\n", "Hello, world!");

printf(":%-15.10s:\n", "Hello, world!");

}

Page 32: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Format Control for Numerical

#include<stdio.h> main() {

printf("The color: %s\n", "blue"); printf("First number: %d\n", 12345); printf("Second number: %04d\n", 25);

printf("Third number: %i\n", 1234); printf("Float number: %3.2f\n", 3.14159); printf("Hexadecimal: %x\n", 255); printf("Octal: %o\n", 255); printf("Unsigned value: %u\n", 150); printf("Just print the percentage sign %%\n", 10);

}

Page 33: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Result

Page 34: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Printf & Scanf Function

/* scanf example */#include <stdio.h>

int main (){ char str [80]; int i;

printf ("Enter your family name: "); scanf ("%s",str); printf ("Enter your age: "); scanf ("%d",&i); printf ("Mr. %s , %d years old.\n",str,i); printf ("Enter a hexadecimal number: "); scanf ("%x",&i); printf ("You have entered %#x (%d).\n",i,i); return 0;}

Page 35: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

The Use of Carriage Control

#include <stdio.h>void main(){ printf("Information\tTechnology\n"); printf ("C Lang"); printf ("\b\b\b\bProgramming");}

Page 36: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objectives1

pp2

Printf Function Printf Function3

The Other Function The Other Function4

Assignment #3 5

Scanf Function

Page 37: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

The Other Functions

putchar(ch);

puts(str);

ch = getchar();

ch = getch();

gets(str);

Page 38: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Show the Information : Putchar Function

putchar ( ch ) : เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h ใช้&สั(าห์ร�บอ�กขีระออกที่างห์น&าจัอคร�8งละต�วอ�กขีระ

ch : เป2นต�วแปรที่��เก6บขี&อม%ลช้น�ด char ที่��ต&องการแสัดงผล ซี4�งแสัดงอย%�ภายใน ‘_’

putchar(ch);

Page 39: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Putchar Function

#include <stdio.h>char Name = ‘A';void main(){ putchar (Name);

printf(“\n”); putchar ('b');}

Page 40: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Show the Information : Puts Function

puts( str ) :เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h ใช้&แสัดงขี&อความออกที่างห์น&าจัอ ซี4�งห์ล�งจัากแสัดงขี&อความแล&วจัะขี48นบรรที่�ดให์ม�

str : เป2นต�วแปรที่��เก6บขี&อม%ลช้น�ด string ที่��ต&องการแสัดงผล ซี4�งแสัดงอย%�ภายใน “_”

puts(str);

Page 41: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Puts Function

#include <stdio.h> char message[] = "C Programming";

void main(){ puts (message);

puts (“is very easy”);}

Page 42: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Putchar & Puts Function

#include <stdio.h>char ch = 'A', str[] = "Computer";

char message[] = "C Programming";void main(){

putchar (ch);putchar (' ');putchar (str[1]);putchar ('\n');puts (str);puts (message);

}

Page 43: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Receiving the Information : Getchar Function

getchar ( ) : เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h โดยร�บขี&อม%ลได&คร�8งละ 1 อ�กขีระเที่�าน�8น เม)�อป5อนห์ร)อร�บขี&อม%ลจัากค�ย�บอร�ดแล&วต&องกด Enter

ch : เป2นต�วแปรช้น�ด char ที่��ต&องการเก6บขี&อม%ล

ch = getchar();

Page 44: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include <stdio.h>#include <conio.h>

void main(){

char ch;ch = getchar();printf("Input Char is :%c",ch);}

}

Example of Getchar Function (1)

Page 45: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Getchar Function (2)

#include <stdio.h>#include <conio.h>char x;main(){

printf ("Enter your grade : ");x = getchar();printf ("Programming Language Grade : %c",x);

}

Page 46: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Receiving the Information : Getch Function

getch ( ) : เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ conio.h ใช้&ร�บขี&อม%ลคร�8งละ 1 อ�กขีระ และจัะที่(างานให์&ค(าสั��งต�อไปโดยไม�แสัดงค�าที่��ร�บเขี&ามา

ch : เป2นต�วแปรช้น�ด char ที่��ต&องการเก6บขี&อม%ล

ch = getch();

Page 47: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Getch Function (1)

#include <stdio.h>#include <conio.h>char x;main(){

printf ("Enter your grade : ");x = getch();printf ("Programming Language Grade : %c",x);

}

Page 48: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

What is the missing & what is the results?

void main (){

char ch1,ch2; printf ("Enter Character 1 : "); ch1 = getchar(); printf ("Enter Character 2 : "); ch2 = getch(); puts ("\n**** Output ****"); printf ("Char 1 = %c\nChar 2 = %c", ch1,ch2);}

Enter Character 1 : JEnter Character 2 : **** Output ****Char 1 = JChar 2 = O

พิ�มพิ� O แต�ไม�แสัดงออกมา

Page 49: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include <conio.h>#include <stdio.h>

void main(){

char ch;ch = getche();printf("Input Char is :%c",ch);

}

Example of Getche Function (1)

Page 50: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

#include<stdio.h>#include<conio.h>

main() {

printf("Waiting for a character to be pressed from the keyboard to exit.");

 getche(); return 0;}

Example of Getche Function (2)

Page 51: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Receiving the Information : Gets Function

gets ( str ) เป2นฟั1งก�ช้�นมาตรฐานในภาษา C ซี4�งอย%�ใน Library ที่��ช้)�อ stdio.h ใช้&ร�บขี&อม%ลช้น�ดขี&อความโดยเฉพิาะ อ�กที่�8งค(าสั��งที่��ใช้&ร�บขี&อม%ลช้น�ดขี&อความจัากผ%&ใช้&งาน โดยสัามารถใสั�ขี&อม%ลที่��ม�การเว&นช้�องว�างภายในได&

str : เป2นต�วแปรที่��ใช้&เก6บขี&อความ โดยเราต&องสัร&างเตร�ยมไว&ก�อนที่��จัะเร�ยกใช้&ฟั1งก�ช้�น gets()

gets(str);

Page 52: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Example of Gets Function (1)

#include <stdio.h>#include <conio.h>char message[30];main(){

clrscr();printf ("Enter your message : ");gets(message);printf ("Your message is %s", message);

}

Page 53: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

/* gets example */#include <stdio.h>

int main(){ char string [256]; printf ("Insert your full address: "); gets (string); printf ("Your address is: %s\n",string); return 0;}

Example of Gets Function (2)

Page 54: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Outline

Objectives1

pp2

Printf Function Printf Function3

The Other Function The Other Function4

Assignment #3 5

Scanf Function

Page 55: Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Assignment #3

ให์&เขี�ยนโปรแกรมเพิ)�อที่(างานด�งต�อไปน�8สัามารถร�บช้)�อและนามสัก/ลที่�8งห์มดพิร&อมก�น ด&วยค(าสั��ง scanf() และแสัดงผลค�าที่��ร�บเขี&ามาร�บค�าเกรดเฉล��ยขีองน�สั�ตเขี&ามา จัากน�8นแสัดงผลโดยเว&นช้�องว�างด&านห์น&าการแสัดงไว& 7 ช้�อง จั�ดผลการแสัดงช้�ดด&านขีวาร�บต�วเลขีฐานสั�บที่��ไม�ค�ดเคร)�องห์มาย จัากน�8นแสัดงเลขีจั(านวนด�งกล�าว ในแบบ ฐานแปด และฐานสั�บห์ก ต�วอย�างการแสัดง “o777”, “8AFFH”แสัดงผลค(าว�า “Please press any key.” จัากน�8นรอการกดป/:มใดๆ จั4งออกจัากการที่(างานขีองโปรแกรม