Transcript include

การรับค่าและแสดงผล
1
Function
แสดงผลออกทางจอภาพ
2
printf(control,argument list);
• control
อยูภ่ ายใต้เครื่ องหมาย “” ประกอบด้วย
– ข้อความ
– รหัสควบคุมการแสดงผล
– Format code หลังเครื่ องหมาย %
• argument list
เครื่ องหมาย ,
ตัวแปร, ค่าคงที่ ถ้า > 1 ตัว คัน่ ด้วย
3
รหัสควบคุมการแสดงผล
•
•
•
•
•
•
•
•
\n
\t
\r
\f
\b
\a
\”
\’
ตัวนี้ใช้บ่อย
ขึ้นบรรทัดใหม่
เว้นระยะ 6 ตัวอักษร
cursor ชี้อยูต่ น้ บรรทัด
เว้นการแสดงผลไป 1 หน้า
cursor ถอยหลังไป 1 ตัวอักษร พร้อมลบตัวอักษรไปด้วย
มีเสี ยง
พิมพ์เครื่ องหมาย ”
พิมพ์เครื่ องหมาย ‘
4
Format Code
•
•
•
•
•
%d
%f
%c
%s
%%
ข้อมูลที่เป็ นตัวเลข (เลขฐาน10)
ข้อมูลที่เป็ นตัวเลขทศนิยม (float,double)
ข้อมูลที่เป็ น ตัวอักษร
ข้อมูลที่เป็ นข้อความ
เครื่ องหมาย %
5
แสดง Output
printf(“Number is %d“,500);
ตอบ Number is 500
int age=20;
printf(“You are %d year\’s old.”,age);
ตอบ You are 20 year’s old.
int num1=3,num2=10,ans;
ans=num1+num2;
printf(“Summary of %d and %d = %d”,num1,num2,ans);
ตอบ Summary of 3 and 10 = 13
6
ผลการ Run
#include<stdio.h>
void main()
{
int number = 10;
float tax = 5.5, rate = 40.25;
printf(“Number = %d\nTax = %f\nRate = %.2f\n”,
number, tax, rate);
}
ตอบ
Number = 10
Tax = 5.500000
Rate = 40.25
7
ตัวอย่างเช่น
int num = 100;
printf(“num = %5d\n”,num);
num = ^^100
printf(“num = %2d\n”,num);
num = 100
ตัวอย่างเช่น
int num = 100;
printf(“num = %-8d\n”,num);
num = 100^^^^^
printf(“num = %-2d\n”,num);
num = 100
8
ผลการแสดงผล
#include <stdio.h>
#include <conio.h>
int main()
{
int x = 123;
clrscr();
printf("%d\n", x);
printf("%2d\n", x);
printf("%10d\n", x);
printf("%-10d\n", x);
return 0;
}
123
123
^^^^^^^123
123^^^^^^^
9
ตัวอย่างเช่น
int num = 100;
printf(“num = %05d\n”,num);
num = 00100
10
กรณีเลขทศนิยม
float rate = 13.7582;
printf(“%f”,rate);
13.758200
printf(“%.3f”,rate);
13.758
printf(“%.2f”,rate);
13.76
printf(“%.0f”,rate);
14
11
การแสดงผลข ้อความ
#include <stdio.h>
#include <conio.h>
#define TEXT "Business Computer"
int main()
{
clrscr();
printf("%2s\n", TEXT);
printf("%20s\n", TEXT);
printf("%20.8s\n", TEXT);
printf("%-20.8s\n", TEXT);
printf("%20.12s\n", TEXT);
return 0;
}
Business^Computer
^^^Business^Computer
^^^^^^^^^^^^Business
Business^^^^^^^^^^^^
^^^^^^^^Business^Com
12
Function
รับค่าข้อมูลจากทางแป้ นพิมพ์
13
function getchar()
• เป็ นข ้อมูลรับอักขระ 1 ตัวอักษรจากแป้ นพิมพ์และ
ต ้องเคาะแป้ น enter
• ในกรณีกาหนดตัวแปรรับค่าเป็ น int จะไม่มก
ี าร
ฟ้ องข ้อผิดพลาด
14
function getch()
• ใชรั้ บตัวอักขระ 1 ตัวจากแป้ นพิมพ์ แต่ขณะรับ
ไม่แสดงทางจอภาพและไม่ต ้องเคาะแป้ น
enter
• ในกรณีกาหนดตัวแปรรับค่าเป็ น int จะไม่มก
ี าร
ฟ้ องข ้อผิดพลาด
15
function get()
ื่ ตัว
• ใชรั้ บข ้อความจากแป้ นพิมพ์ มาเก็บไว ้ในชอ
แปรทีก
่ าหนด
• รู ปแบบ คือ gets(string_var);
• เช่น
char str[20];
gets(str);
ใช้กบั ข้อความ และมีช่องว่าง
16
ตัวอย่าง gets();
#include<stdio.h>
void main()
{
char name[40];
printf(“Enter Subject : “);
gets(name);
printf(“This subject is %s\n”,name);
getch();
}
Enter Subject :Computer Programming
This subject is Computer Programming

17
function scanf()
รู ปแบบ
scanf(control,argument list);
• control “ ” มี Format code เหมือน printf
• argument list รับค่าจากแป้ นพิมพ์มาเก็บไว้
- คัน่ ด้วย ,
- นาหน้าด้วยเครื่ องหมาย &
18
การรับข้ อมูล
int age ;
float gpa;
scanf(“%d”,&age);
scanf(“%f”, &gpa);
scanf(“%d%f”,&age, &gpa);
ตัวอย่างการรับข้อมูล
20
3.75
20 3.75
19
ผลทีแ
่ สดงหน ้าจอ
#include <stdio.h>
#include <conio.h>
int main()
{
char name[30];
clrscr();
printf("Enter your name: ");
scanf("%s", name);
printf(“Your name is %s \n", name);
return 0;
}
Enter your name: Peter
Your name is Peter
20
ผลทีแ
่ สดงหน ้าจอ
#include <stdio.h>
#include <conio.h>
int main()
{
int age,salary;
clrscr();
printf("Enter your age : ");
scanf("%d ",&age);
printf("Enter your salary: ");
scanf("%d ",&salary);
printf(“Your age is %d years old and earn %d bath \n", age, salary);
return 0;
}
Enter your age: 25
Enter your salary: 32000
Your age is 25 years old and earn 32000 bath
21
ผลทีแ
่ สดงหน ้าจอ
#include <stdio.h>
#include <conio.h>
int main()
{
int age,salary;
clrscr();
printf("Enter your age, salary : ");
scanf("%d %d",&age,&salary);
printf(“Your age is %d years old and earn %d baht \n", age, salary);
return 0;
}
Enter your age, salary : 25 32000
Your age is 25 years old and earn 32000 baht
22
EX1 จงเขียนโปรแกรมรับค่าความยาวเป็ นฟุตจากแป้ นพิมพ์ แล้ว
เปลี่ยนค่านั้นให้เป็ นนิ้ว และแสดงผลที่ได้ออกทางหน้าจอ
Input :
Output :
ความยาวเป็ นฟุต(feet)
ความยาวเป็ นนิว้ (inch)
หมายเหตุ 1 ฟุต เท่ากับ 12 นิ้ว
23
#include<stdio.h>
#include<conio.h>
void main()
{
int feet, inch;
clrscr();
printf(“Enter number of feet : “);
scanf(“%d”,&feet);
inch = feet * 12;
printf(“ %d feet = %d inch\n“, feet, inch);
getch();
}
24
EX2
จงเขี ย นโปรแกรมรั บ ตั ว เลขทศนิ ยม 2 ค่ า จาก
แป้ นพิ ม พ์ จากนั้ น ให้ ห าผลบวกและผลคู ณ ของ 2
ตัวเลขนั้น พร้อมทั้งแสดงผลลัพธ์ออกทางจอภาพ โดย
กาหนดทศนิยม 2 ตาแหน่ง
Input
Enter 2 number : 12.50 20.25
Output
12.50 + 20.25 = 32.75
12.50 x 20.25 = 253.125
25
#include<stdio.h>
#include<conio.h>
void main()
{
float Num1,Num2,Add,Mul;
clrscr();
printf(“Enter number1 and 2 : “);
scanf(“%f %f”,&Num1,&Num2);
Add = Num1+Num2;
Mul=Num1*Num2;
printf(“%.2f + %.2f = %.2f\n” ,Num1,Num2,Add);
printf(“%.2f * %.2f = %.2f\n” ,Num1,Num2,Mul);
getch();
}
26
แบบฝึ กหัด
1. จงเขียนโปรแกรมเก็บข ้อมูลเกีย
่ วกับภาพยนตร์
ื่ จานวน
DVD โดยเก็บข ้อมูลคือ รหัส ชอ
output
Code : 003
Name : Titanic
Amount : 30 copies
27
้
2. จงเขียนโปรแกรมหาพืน
้ ทีว่ งกลมและเสนรอบวง
- รับข ้อมูล รัศมี
- พืน
้ ทีว่ งกลม คือ ¶r2
้
- เสนรอบวง
คือ 2 ¶ r
output
Radius : ….
Area : ….
Perimeter :….
28
3. จงเขียนโปรแกรมหาเกรดเฉลีย
่ ทัง้ 3 เทอม
- รับข ้อมูลเกรด เทอม 1 , เทอม 2, เทอม3
output
Grade Term 1 :….
Grade Term 2 :….
Grade Term 3 :….
Average is …
29