การทดลองที่ 6 การจัดรูปแบบ การรับข้อมูลและการแสดงผลข้อมูล

Download Report

Transcript การทดลองที่ 6 การจัดรูปแบบ การรับข้อมูลและการแสดงผลข้อมูล

การทดลองที่ 6
การจัดรู ปแบบ
การรับข้อมูลและการแสดงผลข้อมูล
ฟังก์ชนั scanf()
• เป็ นฟังก์ชนั ทาหน้าที่ input ส่ วนมากใช้รับข้อมูลจากแป้ นพิมพ์
• ต้องกาหนดว่าจะรับข้อมูลชนิดใด และรับแล้วไปเก็บในตัวแปรตัวใด
• การกดแป้ น Enter เป็ นการสิ้ นสุ ดการรับข้อมูล
รู ปแบบการใช้งาน
scanf(“%รู ปแบบ”, &ตัวแปร )
ampersand
รู ปแบบ (format specifier)
เครือ
่ งหมาย
ความหมาย
%d
ิ
เลขจานวนเต็มฐานสบ
%f
เลขทศนิยม
%c
ตัวอักษรตัวเดียว (อักขระ (character))
%s
ข ้อความ (String)
%u
ตัวเลขไม่คด
ิ เครือ
่ งหมาย
ตัวอย่าง
#include <stdio.h>
void main() {
ประกาศตัวแปรก่อนใช้เก็บข้อมูล
int a;
printf(“Enter number: ”);
รับข้อมูลเลขจานวนเต็มฐานสิ บ
scanf(“%d”,&a);
แล้วนามาเก็บที่ตวั แปร a
printf(“Value of a is %d”, a);
}
printf(“Enter number :”);
scanf(“%d”, &a);
Enter number : 100
10
printf(“Enter number :”);
scanf(“%d”, &a);
memory
a
100
Enter number : 100
Value of a is 100
printf(“Value of a is %d”, a);
x
y
ถ้าป้ อนข้อมูลผิด
scanf(“%d”, &a);
Enter number : 123.45
Value of a is 123
ถ้าป้ อนข้อมูลผิด
scanf(“%d”, &a);
Enter number : aaa
Value of a is -28708
การป้ อนข้อมูลตัวเลขสองตัว
#include <stdio.h>
void main() {
ประกาศตัวแปร a, b เพื่อใช้เก็บข้อมูล
int a, b;
printf(“Enter number: ”);
scanf(“%d %d”,&a, &b);
printf(“Value of a is %d\n”, a);
printf(“Value of b is %d\n”, b);
}
ตัวอย่างการป้ อนข้อมูลเป็ นตัวเลขสองตัว
Enter number :10
Value of a is 10
Value of b is 20
อย่าลืมเว้นวรรคระหว่าง
ตัวเลขสองจานวน
20
a
b
10
20
x
y
ตัวอย่างการป้ อนข้อมูลเป็ นตัวเลขสองตัว
#include <stdio.h>
void main() {
int a, b;
ประกาศตัวแปร a, b เพื่อใช้เก็บข้อมูล
printf(“Enter number : ”);
input ตัวแปร a
scanf(“%d”,&a);
printf(“Enter number : ”);
input ตัวแปร b
scanf(“%d”,&b);
printf(“Value of a is %d\n”, a);
printf(“Value of b is %d\n”, b);
}
a
Enter number : 10
10
Enter number : 20
20
Value of a is 10
Value of b is 20
b
ตัวอย่างการคานวณพื้นที่สี่เหลี่ยมผืนผ้า
(สู ตรคานวณ พื้นที่ = กว้าง x สูง)
#include <stdio.h>
void main() {
double w;
double h;
double area;
printf(“Enter width : ”);
scanf(“%f”, &w);
printf(“Enter height: ”);
scanf(“%f”, &h);
area = w*h;
printf(“area is %.2f\n”, area);
}
Enter width : 5.6
Enter height : 3.4
Area is : 19.04
h
w
ลองดู
เขียนโปรแกรมคานวณพื้นที่สามเหลี่ยม
พื้นที่ = 0.5*w*h
h
w
เฉลย
#include <stdio.h>
void main() {
double w;
double h;
double area;
printf(“Enter width : ”);
scanf(“%f”, &w);
printf(“Enter height: ”);
scanf(“%f”, &h);
area = 0.5*w*h;
printf(“area is %.2f\n”, area);
}
เขียนโปรแกรมคานวณปริ มาตรทรงกระบอก
r
ปริ มาตร = 3.14*r*r*h
h
เฉลย
#include <stdio.h>
void main() {
double r;
double h;
double v;
printf(“Enter radius : ”);
scanf(“%f”, &r);
printf(“Enter height: ”);
scanf(“%f”, &h);
v = 3.14*r*r*h;
printf(“volume is %.2f\n”, v);
}
อักขระ (character)
• char เป็ นชนิดข้อมูลอักขระ (character)
• ข้อมูลอักขระ ต้องมีเครื่ องหมาย single quote กากับ เช่น ‘a’,
‘F’, ‘Y’, ‘N’, ‘ ’
• การประกาศ
char ch;
char sex;
char choice;
char m;
ตัวอย่าง
Enter character : F
#include <stdio.h>
You enter F
void main() {
char ch;
printf(“Enter character : ”);
scanf(“%c”, &ch);
printf(“You enter %c\n”, ch);
}
ถ้า input มากกว่า 1 ตัว จะรับเพียงตัวแรก
Enter character : Female
#include <stdio.h>
You enter F
void main() {
char ch;
printf(“Enter character : ”);
scanf(“%c”, &ch);
printf(“You enter %c\n”, ch);
}
ฟังก์ชนั getch()
• ทางานสะดวกกว่า ไม่ตอ้ งกด EnterEnter character : F
You enter F
#include <stdio.h>
void main() {
char ch;
printf(“Enter character : ”);
ไม่ตอ้ งกด Enter
ch = getch();
printf(“You enter %c\n”, ch);
}
อย่าลืม !!!
• ข้อมูลชนิด char นามาคานวณได้ เช่น
‘A’ + 10  65 + 10 
75
‘a’ + 1  97 + 1
 98
‘C’ * 2  67 * 2
 134
• ข้อมูลชนิด char กาหนดพิมพ์เป็ นตัวเลขได้โดยใช้ %d
printf(“%c %d\n”, ‘A’, ‘A’);
A 65
String
• String แปลว่า สายวลี เรี ยกง่าย ๆ ว่า ข้อความ
• String อาจประกอบด้วยตัวอักษร สัญลักษณ์ หรื อตัวเลข
• ข้อมูล String ต้องมีเครื่ องหมาย double quote กากับ
“Korat”, “Thailand”, “Hello”, “340 Suranarai
road” , “\“Nakhon Ratchasima\””
• ข้อมูล String ไม่สามารถนาไปคานวณได้ เช่น
“1234” * 2, “10” + “20”,
“340” - 300
String
• ตัวแปรรับข้อมูล string ให้ประกาศชนิดเป็ น array of char
เป็ นกลุ่มข้อมูลอักขระ
• ตัวอย่างการประกาศตัวแปร string
char name[20];
//รับได้ 19 ตัว
char program[30];
//รับได้ 29 ตัว
char n[];
//ปรับขนาดได้ตามขนาดข้อมูล
String
• การประกาศและกาหนดค่า
char name[10] = “Sangsuree”;
รหัสปิ ดท้าย string
0 1 2 3 4 5 6 7 8 9
S a n g s u r e e \0
char name[10] จะรับข้อมูลได้ 9 ตัว
String
• การประกาศและกาหนดค่า
char name[10] = “Somsree”;
รหัสปิ ดท้าย string
0 1 2 3 4 5 6 7 8 9
S o m s r e e
\0
ว่าง
String
• การประกาศโดยไม่กาหนดขนาด คอมไพเลอร์ จะกาหนดขนาดให้พอดี
char city[] = “Nakhon Ratchasima”;
0
1
2
3
4
5
N
a
k
h
o
n
6
7
8
9
10
11
12
13
14
15
16
17
R
a
t
c
h
a
s
i
m
a
\0
• เหมือนการประกาศ
char city[18] = “Nakhon Ratchasima”;
String
• การกาหนดค่าเกินขนาด จะเกิด error
char ch[5] = “abcdef”;
ข้อความ error “Too many initialize”
ระวัง
การรับข้อมูล
• ใช้ scanf() หรื อ gets()
char name[10];
printf(“Enter name : ”);
scanf(“%s”, name);
หรื อ
printf(“Enter name : ”);
gets(name);
ไม่จาเป็ นต้องใช้ &name
ระวัง !! อย่าป้ อนข้อมูลเกินจานวนที่ประกาศ
Enter string : Nakhon
#include <stdio.h>
•พิมพ์ขอ้ มูลขยะ
void main() {
•เกิด runtime error
char str[5];
printf(“Enter string : ”);
scanf(“%s”, str);
printf(“%s\n”, str);
}
ตัวอย่าง
Enter your name : Somsree
#include <stdio.h>
Hi, Somsree
void main() {
Nice to meet you!!!
char name[20];
printf(“Enter your name : ”);
gets(name);
printf(“Hi, %s\n”, name);
printf(“Nice to meet you!!!”);
}
ตัวอย่าง
Enter your name : Somchai
#include <stdio.h>
Hi, Somchai
void main() {
Nice to meet you!!!
char name[20];
printf(“Enter your name : ”);
gets(name);
printf(“Hi, %s\n”, name);
printf(“Nice to meet you!!!”);
}
ตัวอย่าง
Enter your name : Somsak
#include <stdio.h>
Hi, Somsak
void main() {
Nice to meet you!!!
char name[20];
printf(“Enter your name : ”);
gets(name);
printf(“Hi, %s\n”, name);
printf(“Nice to meet you!!!”);
}
ฟังก์ชนั printf()
• เป็ นฟังก์ชนั แสดงข้อมูล
• รู ปแบบ
printf(“control string”, variable list);
Format specifier
%d
%e
%f
%lf
%0
%u
%x
%p
แทนเลขจานวนเต็ม
ี ล (exponential form)
แทนเลขในรูปเอกซโ์ พเนนเชย
แทนเลขทศนิยมชนิด float
แทนเลขทศนิยมชนิด double
แทนเลขจานวนเต็มฐานแปด
แทนเลขจานวนเต็มไม่คด
ิ เครือ
่ งหมาย
ิ หก
แทนเลขจานวนเต็มฐานสบ
แทนข ้อมูลแบบพอยน์เตอร์ (pointer)
%s แทนข ้อความ
%c แทนตัวอักขระ
่ งหมาย %
%% แทนเครือ
ตัวอย่างการพิมพ์ตวั เลข
10 decimal = 20 octal = 24 hexadecimal = 14
#include
353.45
fixed<stdio.h>
point 353.450012 floating point = 3.534500e+02
void main() {
int a = 20;
float b = 353.45;
printf(“20 decimal = %d octal = %o hexadecimal
= %x\n ”, a, a, a);
printf(“353.45 fixed point = f% floating point =
%e\n”, b, b);
}
ตัวอย่างการพิมพ์ตวั เลข
fixed point 34.567890
#include <stdio.h>
floating point 3.456789e+01
void main() {
fixed point 34.567891
float a = 34.567891; floating point 3.456789e+01
double b = 34.567891; fixed point 34.57
fixed
printf("fixed point %f\n",
a); point 34.57
printf("floating point %e\n", a);
printf("fixed point %lf\n", b);
printf("floating point %e\n", a);
printf("fixed point %.2f\n", a);
printf("fixed point %.2lf\n", b);
}
ตัวอย่าง
#include <stdio.h>
Korat
void main() {
char a[] = "Korat"; Korat
Korat
printf("%s\n", a);
printf("%2s\n", a); Korat
Korat
printf("%6s\n", a);
Korat
printf("%7s\n", a);
printf("%8s\n", a);
printf("%9s\n", a);
}
ตัวอย่าง
#include <stdio.h>
void main() {
char ch = 'A';
printf("%c\n", ch);
printf("%2c\n", ch);
printf("%3c\n", ch);
printf("%4c\n", ch);
printf(“%c%5c\n", ch, ch);
}
A
A
A
A
A
A
A
ปฏิบตั ิ