📌 ปักหมุด

แบบฝึกหัดตัวอย่าง PASCAL ที่หลายๆคนขอมาครับ

แบบฝึกหัดตัวอย่าง PASCAL อยากให้เขียนโปรแกรมอะไรเป็นตัวอย่างขอได้นะครับ

ขอได้ที่ PM ในเว็ปนะครับ PM หา Admin เลยครับผม

แล้วเดียวผมจะเอามาลงให้ครับผม

💬 การตอบกลับ 68

#1
Moshi
Moshi
16,996 โพสต์
โปรแกรม หาพื้นที่วงกลม
-------------------

ลอง copy ไปรันดูนะครับ

program it4x;
uses crt;
var r:integer;
    area:real;
begin
    clrscr;
    write('Input Your Radar = ');
    readln(r);
    area:=3.14*r*r;
    writeln('This Area Of Circel = ',area:1:2);
    readln;
end.

ลองไปใช้กันดูครับผม

อธิบาย :1:2 - 1 คือจำนวนเว้นค้างหน้านะครับ 2 คือจำนวนทศนิยมที่ต้องการให้โชว์

✏️ แก้ไขล่าสุด: 8 สิงหาคม 2552

#2
Moshi
Moshi
16,996 โพสต์
โปรแกรมรับค่าชื่อ สกุล รหัสประจำตัว สาขาวิชา แล้วออกจอภาพ
------------------------------------------------

program it4x;
uses crt;
var id:integer;
    name,surname,branch:string;
begin
    clrscr;
    write('What Is Your ID ? = ');
    readln(id);
    write('What Is Your Name ? = ');
    readln(name);
    write('What Is Your Surname ? = ');
    readln(surname);
    write('What Is Your Branch ? = ');
    readln(branch);
    clrscr;
    writeln('Your Id = ',id);
    writeln('Your Name = ',name,' ',surname);
    write('Your Branch = ',branch);
    readln;
end.

ลอง Copy ไปรันใน Pascal ดูนะครับ ใช้ตัวแปร id เพื่อเป็นรหัส name เป็นชื่อ surname นามสกุล branch สาขา ครับผม
#3
Moshi
Moshi
16,996 โพสต์
โปรแกรมคำนวณพื้นที่ สีเหลี่ยม คางหมู 
-----------------------------

สูตรข้อนี้คือ = 1/2 x ผลบวกด้านคู่ขนาน x สูง

program it4x;
uses crt;
var high,parallel:integer;
begin
    clrscr;
    write('Input Your Parallel = ');
    readln(parallel);
    write('Input Your High = ');
    readln(high);
    clrscr;
    write('Area Of Trapezoid = ',0.5*(parallel+parallel)*high:1:2);
    readln;
end.

ตัวแปล High แปลว่าความสูง parallel แปลว่า ด้านคู่ขนาน Trapezoid = สี่เหลี่ยมคางหมู

สำหรับพวกคำนวณพื้นที่ อื่นๆ ก็ใช้คล้ายกันครับ เพียงเปลี่ยนสูตร เท่านั้นเอง
#4
Moshi
Moshi
16,996 โพสต์
โปรแกรม เช็คว่าอายุคุณแก่หรือยัง
--------------------------

โดยรับค่า ชื่อ สกุล และ อายุของคุณ จากนั้นใช้เงื่อนไขโดย

กำหนดว่า ถ้าอายุ น้อยกว่า 0 = "ERROR"
          ถ้าอายุ 0-10 = "Baby"      = เด็กน้อย
          ถ้าอายุ 11-18 = "teens"    = วัยรุ่น
          ถ้าอายุ 19-30 = "Workers" = วัยทำงาน
          ถ้าอายุ 31-60 = "Elderly" = วัยทอง
          มากกว่า 60 = "Geezer" = คนแก่

จะสามารถทำได้ 2 รูปแบบนะครับ แบบแรกคือ ทำโดยใช้ IF แบบที่สอง คือทำโดยใช้ Case

แบบแรก If

program it4x;
uses crt;
var name,surname:string;
    old:integer;
begin
    clrscr;
    write('Input Your Name = ');
    readln(name);
    write('Input Your SurName = ');
    readln(surname);
    write('How Old Are You ? = ');
    readln(old);
    clrscr;
    writeln('Your name = ',name,' ',surname);
    if old<=0 then
    writeln('Your old Error')
    else
    if old<=10 then
    writeln('Your old Baby')
    else
    if old<=18 then
    writeln('Your old Teens')
    else
    if old<=30 then
    writeln('Your old Workers')
    else
    if old<=60 then
    writeln('Your old Elgerly')
    else
    if old>=61 then
    writeln('Your old Workers');
    ;;;;;;
    readln;
end.
ลอง Copy ไปรันดูครับ

แบบที่สองแบบ Case

program it4x;
uses crt;
var name,surname:string;
    old:integer;
begin
    clrscr;
    write('Input Your Name = ');
    readln(name);
    write('Input Your SurName = ');
    readln(surname);
    write('How Old Are You ? = ');
    readln(old);
    clrscr;
    writeln('Your name = ',name,' ',surname);
    case old of
    0..10:writeln('Your Old Baby');
    11..18:writeln('Your Old Teens');
    19..30:writeln('Your Old Workers');
    31..60:writeln('Your Old Elderly');
    61..100:writeln('Your Old Geezer');
    else
    writeln('Your Old ERROR');
    end;
    readln;
end.

ลองนำไปใช้กันดูนะครับ ^^
#5
Moshi
Moshi
16,996 โพสต์
โปรแกรม คำนวณพื้นที่ สามเหลี่ยม
--------------------------

ถูกร้องขอโดย น้องหม่อน ในบอร์ดเราเองนะครับ

สูตรการหาคือ พื้นที่สามเหลี่ยม = 1/2 x ฐาน x สูง

program it4x;
uses crt;
var high,base:integer;
begin
    clrscr;
    write('Input Your Base = ');
    readln(base);
    write('Input Your High = ');
    readln(high);
    clrscr;
    write('Area Of Triangle = ',0.5*base*high:1:2);
    readln;
end.

ิตัวแปรที่ใช้ base คือ ฐาน high คือสูง  Triangle สามเหลี่ยม
#6
Moshi
Moshi
16,996 โพสต์
โปรแกรม ตัดเกรดนักเรียน
--------------------

เงือ่นไขมีอยู่ว่า

คะแนน  0-49 เกรด  f
50-59 เกรด  d
60-69 เกรด  c
70-79 เกรด  b
80-100 เกรด  a

อื่นๆ Error

สามารถทำได้2แบบนะครัยโดยใช้ If และ case

แบบที่ 1 ใช้ IF

Program Cut_Grade;
uses crt;
var score:integer;
begin
    clrscr;
    write('Input Your Score = ');
    readln(score);
    if score>0 then
    begin
        if score<50 then
begin
writeln('Your Grade F');
end
else
begin
if score<60 then
begin
writeln('************');
writeln('Your Grade D');
end
else
begin
if score<70 then
begin
writeln('************');
writeln('Your Grade C');
end
else
begin
if score<80 then
begin
writeln('************');
writeln('Your Grade B');
end
else
begin
if score<101 then
begin
writeln('************');
writeln('Your Grade A');
end
else
begin
writeln('***********');
writeln('Error Score');
end;
end;
end;
end;
end;
        end
    else
    begin
writeln('***********');
        writeln('Error Score');
    end;
writeln;
writeln;
writeln('Press Any Key To Exit');
    readln;
end.



แบบที่ 2 ใช้ CASE

program Cut_Grade_Case;
uses crt;
var score:integer;
begin
clrscr;
write('Input Your Score = ');
                readln(score);
writeln('******************');
                writeln;
                writeln;
case score of
                        0..49  : writeln('Your Grade F');
50..59 : writeln('Your Grade D');
60..69 : writeln('Your Grade C');
70..79 : writeln('Your Grade B');
81..100: writeln('Your Grade A');
else
        writeln('Score Error');
end;
writeln;
writeln;
writeln('Press Any Key To Exit');
readln;
end.

ลองนำไปดัดแปลงแล้วแก้ไขกันเอาเองนะครับ ^^
#7
Moshi
Moshi
16,996 โพสต์
โปรแกรม คำนวณค่าเฉลี่ยของเลข
--------------------------

เช่น ค่าเฉลี่ย ของเลข 1,2,3,4,5,...100 ... นี่คือ ถึง 100 นะครับ

สูตรของมันคือ เอาเลขทั้งหมดมา+กัน หาร จำนวนตัวที่ + จะ = ค่าเฉลี่ย

ลองดูนะครับ หาค่าเฉลี่ยของเลข 1-100

program it4x;
uses crt;
var i,sum:integer;
begin
    clrscr;
    i:=1;
    repeat
    sum:=sum+i;
    i:=i+1;
    until i>=101;
    writeln('Average of 1 to 100 = ',sum/i:1:2);
    readln;
end.

ตัวแปร sum มีหน้าที่เก็บค่า 1 ถึง 100 + กัน ส่วน i มีหน้าที่เป็นตัวเลข + ขึ้นเรื่อยๆ ตั้งแต่ 1 - 100 สมมุตอยากหาเกินจาก 100 เป็น 200 ก็ แก้ตรง until i>=101; เป็น 201 แค่นี้เองครับ ลองรันกันเล่นๆดู


ลองดูกันอีกซักข้อนะครับ

หาผลรวมและคำนวนและค่าเฉลี่ยของ 91 88 85 82....,16 ลดลงทีละ 3 จนถึง 16

program it4x;
uses crt;
var i,x,sum:integer;
begin
    clrscr;
    i:=91;
    repeat
    writeln(i);
    sum:=sum+i;
    i:=i-3;
    x:=x+1;
    until i<=13;
    writeln('Average of 91 to 16 = ',sum/x:1:2);
    readln;
end.

ตัวแปร sum มีหน้าที่เก็บค่า ตัวเลขที่นำมา + กัน ส่วน i มีหน้าที่เป็นตัวเลข -ทีละ 3 เรื่อยๆ โดยเริ่มจาก 91 ส่วนตัวแปร x มีหน้าที่เก็บว่า เรา + ตัวเลขไปทั้งหมดกี่ครั้ง

✏️ แก้ไขล่าสุด: 8 สิงหาคม 2552

#8
Moshi
Moshi
16,996 โพสต์
โปรแกรมคำนวณ ภาษี และยอดเงินสุทธิ
------------------------------

ให้รับค่ารหัสประจำตัว ชื่อสกุล และ เงินเดือน

ถ้า เงินเดือนน้อยกว่า10000บาท  ไม่เสียภาษี
ถ้า เงินเดือน 10001-30000  เสียภาษี 3%
ถ้า เงินเดือน 30001-50000  5%
ถ้า เงินเดือน 50001 ขึ้นไป    7%

ภาษี=เงินเดือน*อันตราภาษี

เงินเดือน=เงินเดือน-ภาษี

ตัวแปรที่ใช้นะครับ

name ไว้รับชื่อ id ไว้รับรหัส salary:ไว้รับเงินเดือน กำหนดเป็น longint เพราะจำนวนเลขเยอะ tax ภาษี total เงินเดือนเมื่อหักภาษีแลว้

โปรแกรมนี้ไม่สามารถเขียนโดนใช้ case ได้นะครับ

เขียนโดยใช้ IF

program it4x;
uses crt;
var id,name:string;
    salary:longint;
    tax,total:real;
begin
    clrscr;
    write('What Is Your Id ? = ');
    readln(id);
    write('What Is Your Name ? = ');
    readln(name);
    write('Input Salary = ');
    readln(salary);
    clrscr;
    writeln('Your Id = ',id);
    writeln('Your Name = ',name);
    if salary<=10000 then
    begin
    writeln('Your Salary = ',salary)
    end
    else
    begin
    if salary<=30000 then
    begin
                      tax:=salary*3/100;
                      writeln('Your Tax 3% = ',tax:1:2);
                      total:=salary-tax;
                      writeln('Your Total Salary = ',total:1:2);
    end
    else
    begin
    if salary<=50000 then
    begin
                      tax:=salary*5/100;
                      writeln('Your Tax 5% = ',tax:1:2);
                      total:=salary-tax;
                      writeln('Your Total Salary = ',total:1:2);
    end
    else
        begin
                      tax:=salary*7/100;
                      writeln('Your Tax 7% = ',tax:1:2);
                      total:=salary-tax;
                      writeln('Your Total Salary = ',total:1:2);
        end;end;end;
        readln;
    end.

ลอง Copy แล้วไปวางใน pascal ดูครับ

✏️ แก้ไขล่าสุด: 9 สิงหาคม 2552

#9
Moshi
Moshi
16,996 โพสต์
โปรแกรม โชว์เลขที่ 5 หารลงตัว โดยใช้ while , for , repeat
-------------------------------------------------

กำหนดจาก 0 ถึง 100 พอนะครับ อยากแก้ให้เกิน 100 ก็ลองแก้ x ดูครับ ^^

คำสั่ง While

Program While_five_cut;
uses crt;
var i,x,loop:integer;
begin
        clrscr;
x:=100;
i := 0;
        loop:=1;
        writeln('Program 5 cut 100');
        writeln('*****************');
        writeln;
while i<=x  do
begin
i:=i+1;
if i mod 5 = 0 then
                begin
                    writeln(loop,')' ,i,' ');
                    loop:=loop+1;
                end;
end;
readln;
end.

คำสั่ง repeat

program repeat1_100;
uses crt;
var i,x:integer;
begin
clrscr;
x:=1;
i:=1;
writeln('Program 1_100/5 Repeat');
                writeln('**********************');
                writeln;
repeat
if (i mod 5)=0 then
begin
writeln(x,') Number ',i);
x:=x+1;
end;
                i:=i+1;
until i>101;
readln;
end.

คำสั่ง For

program For_1_100;
uses crt;
var i,x:integer;
begin
clrscr;
x:=1;
                writeln('Program 1_100/5 Use For');
                writeln('***********************');
                writeln;
for i:=1 to 100 do
begin
if(i mod 5)=0 then
                        begin
writeln(x,') Number = ',i);
x:=x+1;
                        end;
end;
readln;
end.


#10
Moshi
Moshi
16,996 โพสต์
อธิบายคร่าวๆ ใช้ Repeat นะครับเพื่อให้มันวนลุป แล้วให้มัน + ทีละ 2 จากเลขขี่ใช่ไหมครับ ก็เริ่ม จาก i เป็น 1 + 2 ไปเรื่อยๆในแต่ละรอบก็จะเป็นเลขขี่ไปเรื่อยๆครับ แล้วก็เอา มา + ลง Sum ก็จะเป็นคำตอบ

โปรแกรมบวกเลข 1-100 เป็นเลขขี่  อยากให้เกินร้อยก็แก้ตรง until ครับ

program it4x;
uses crt;
var i,sum:integer;
begin
    clrscr;
    i:=1;
    repeat
    sum:=sum+i;
    writeln('sum + ',i,' = ',sum);
    i:=i+2;
    until i>100;
    readln;
end.


โปรแกรมบวกเลข 1-100 เป็นเลขคู่  อยากให้เกินร้อยก็แก้ตรง until ครับ แตกต่างจากด้านบนคือ i เริ่มที่ 0 ให้เป็นเลขคู่

program it4x;
uses crt;
var i,sum:integer;
begin
    clrscr;
    i:=0;
    repeat
    sum:=sum+i;
    writeln('sum + ',i,' = ',sum);
    i:=i+2;
    until i>100;
    readln;
end.
#11
Moshi
Moshi
16,996 โพสต์
โปรแกรม คำนวณ สี่เหลี่ยม สามเหลี่ยม วงกลม แบบเลือก Choice ใช้ Case นะครับ

ถูกขอมาโดยคุณ ศตวรรษ ทองศรี  มีไฟล์คำถามโหลดได้ที่นี่  คลิกเล๊ย!!

Code program

program it4x;
uses crt;
var x,i,base,high:integer;
begin
    clrscr;
    writeln('Welcome To Program Calculator');
    writeln('-----------------------------');
    writeln('1.Rectangle');
    writeln('2.Circle');
    writeln('3.Triangle');
    writeln;
    write('Please Select Choice : ');
    readln(i);
    case i of
    1 :  begin
              clrscr;
              writeln('Program Rectangle');
              writeln('-----------------');
              writeln;
              write('Input Your High = ');readln(high);
              write('Input Your Base = ');readln(base);
              writeln('Area Of Rectangle is = ',high*base);
          end;
    2 : begin
              clrscr;
              writeln('Program Circle');
              writeln('--------------');
              writeln;
              write('Input Your Radius = ');readln(x);
              writeln('Area Of Circle is = ',3.14*x*x:1:2);
          end;
    3 : begin
              clrscr;
              writeln('Program Triangle');
              writeln('----------------');
              writeln;
              write('Input Your High = ');readln(high);
              write('Input Your Base = ');readln(base);
              writeln('Area Of Rectangle is = ',0.5*high*base:1:2);
          end;
    else
        begin
              clrscr;
              writeln('Your Unkwon Choice!!')
        end;
    end;
    writeln;
    write('Press Any Key To Exit');
    readln;
end.
#12
Moshi
Moshi
16,996 โพสต์
โปรแกรมคำนวณ น้ำมัน ดีเซล

มีคนขอมา นะครับ ช่วยนี้เขียนตอบช้าหน่อย พอดียังไม่ค่อยว่าง หุหุ

อธิบายโปรแกรม ประมาณว่า กรอก ลิตรที่ต้องการจะซ์อ แล้วมันจะคำนวณเป็นราคาที่ต้องจ่ายทั้งหมด ตัวแปร desel คือราคาน้ำมัน ตัวแปร x รับค่าจำนวนลิตร ที่จะซื้อ

program it4x;
uses crt;
var desel,x:real;
begin
    clrscr;
    desel:=26.79;
    writeln('Program Desel Calculator');
    writeln('------------------------');
    writeln('Desel = 26.79/lit ');
    writeln;
    write('Enter Your Lit = ');readln(x);
    writeln;

    if x<0 then
    writeln('Error Of Lit')
    else
    writeln('Your Bath = ',x*desel:1:2)
    readln;
end.
#13
Moshi
Moshi
16,996 โพสต์
โปรแกรม ถามก่อนว่าจะออกจากโปรแกรมยัง Yes/No

โปรแกรมนี้จะไม่หยุดทำงาน จนกว่าจะกด n ครับผม หุหุ ลองนำไปประยุกต์ใช้ดู

program it4x;
uses crt;
var ans:char;
begin
    clrscr;
repeat
Write('Do you want to continue (Y/N) = '); readln(ans);
ans:=upcase(ans);
until ans ='N';
end.
#14
Moshi
Moshi
16,996 โพสต์
โปรแกรมถาม ต้องการออกจากโปรแกรมไหม Yes/no

program it4x;
uses crt;
var ans:char;
begin
    clrscr;
repeat
Write('Do you want to Exit (Y/N) = '); readln(ans);
ans:=upcase(ans);
until ans ='Y';
end.
#15
Moshi
Moshi
16,996 โพสต์
โปรแกรม เลือกแบบเมนู หลังใช้โปรแกรมเส็ด มีถามก่อนออก

หลังใช้โปรแกรมเสร็จ จะมีถามก่อนออก หุหุ จะออกก็ต่อเมื่อกด Y  โปรแกรมนี้โดนขอมาอีกตามเคย หุหุ

program it4x;
uses crt;
var x,ans:char;
begin
repeat
clrscr;
writeln('Select Choice');
writeln('-------------');
writeln('1.');
writeln('2.');
writeln('3.');
writeln('4.Exit');
write('Please Select Choice = ');readln(x);
clrscr;
case x of
'1' : writeln('1');
'2' : writeln('2');
'3' : writeln('3');
end;

Write('Do you want to Exit (Y/N) = '); readln(ans);
ans:=upcase(ans);
until ans ='Y';
end.
#16
Moshi
Moshi
16,996 โพสต์
โปรแกรม หาร 3 ลงตัวโดยกำหนดเอง และ วนซ้ำได้

ถูกขอมาจากนี่นะครับ http://www.it4x.com/forum/index.php?topic=1825.0

โค๊ดโปรแกรม  อันนี้ผมยังไม่ได้ลองนะครับ ผิดพลาดไงรายงานผลด้วยเพราะผมยังไม่ได้ลง PASCAL ไว้

Program it4x;
uses crt;
var i,x,loop:integer;
begin
repeat
  clrscr;
  writeln('Input Your Start Num = ');readln(i);
  writeln('Input Your End Num = ');readln(x);
       loop:=1;
       writeln('Program 5 cut 100');
       writeln('*****************');
       writeln;
  while i<=x  do
      begin
      i:=i+1;
      if i mod 3 = 0 then
                begin
                    writeln(loop,')' ,i,' ');
                    loop:=loop+1;
                end;
      end;
writeln('Do you Want To Counting (Y/N)');readln(ans);
ans:=upcase(ans);
until ans ='N';

end.

✏️ แก้ไขล่าสุด: 27 กันยายน 2552

#17
Moshi
Moshi
16,996 โพสต์
โปรแกรม คำนวณหาปี Leaf Year

ปี Leaf Year ก็คือ ปีที่เดือนกุมภาพันธ์มี 29 วันครับ


Program it4x_leafyear;
uses crt;
var year:integer;
begin
    clrscr;
    write('In Put Your Year = ');readln(year);
    clrscr;
    if (Year mod 4 = 0)
    and ((Year mod 100 <> 0)
    or (Year mod 400 = 0)) then
       writeln ('Leaf Year')
else
       writeln ('Not Leaf Year');
       readln;
end.

✏️ แก้ไขล่าสุด: 15 ธันวาคม 2552

#18
alist
alist
3 โพสต์
ช่วยหน่อยนะคะ
ถ้าเราต้องการแปลงหน่วยที่ดินจากตารางวาเป็นตารางเมตร
ขอบคุณคะ
#19
Moshi
Moshi
16,996 โพสต์
— alist link=topic=1138.msg16279#msg16279 date=1282545088

ช่วยหน่อยนะคะ
ถ้าเราต้องการแปลงหน่วยที่ดินจากตารางวาเป็นตารางเมตร
ขอบคุณคะ


เอาสูตรมาดิครับ
#20
alist
alist
3 โพสต์
อืมขอโทษด้ยนะตัวเองก้อยังงงอยู่เลยไม่รู้ทำไงดีเลยขอให้ช่วย
สูตรแบบนี้หรือเปล่า
  1  ไร่                          =  4  งาน  หรือ  400  ตารางวา

    1  วา                          =  2  เมตร

    1  ตารางวา                  =  4  ตารางเมตร

    1  ไร่                          =  1,600  ตารางเมตร

    1  เมตร                      =  100  เซนติเมตร

    1  ตารางเมตร              =  10,000  ตารางเซนติเมตร

    1  ลูกบาศก์เมตร            =  1,000,000  ลูกบาศก์เซนติเมตร

    1  กิโลเมตร                  =  1,000  เมตร

    1  ตารางกิโลเมตร          =  1,000,000  ตารางเมตร

    1  ตารางกิโลเมตร          =  625  ไร่

    1  เอเคอร์                    =  2.53  ไร่  หรือ  4,046.86  ตารางเมตร

    1  เมตร                      =  39.37  นิ้ว

    1  ฟุต                        =  30.48  เซนติเมตร

    1  หลา                        =  91.44  เซนติเมตร

    1  ไมล์                        =  1,609.34  เมตร

    1  เฮกตาร์                  =  10,000  ตารางเมตร  =  6.25  ไร่
ขอโทษด้วยนะถ้าไม่ถู้