วันอังคารที่ 16 กันยายน พ.ศ. 2551

การเขียนโปรแกรม

1. พัฒนาโปรแกรมเครื่องคิดเลข โดยให้ผู้ใช้ป้อนตัวเลข 2 จำนวน และเลือกเครื่องหมาย แล้วทำการแสดงผลลัพธ์ออกทางจอภาพ ดังตัวอย่างจอภาพ (Filename : cal.cpp)
#include
#include
void main()
{
char op;
float a,b;
clrscr();
printf("Input First Number : ");
scanf("%f",&a);
printf("\nInput Operator(+,-,*,/) : ");
scanf("%s",&op);
printf("\nInput Second Number : ");
scanf("%f",&b); switch(op){
case '+': printf("\n%0.2f + %0.2f = %0.2f",a,b,a+b);break;
case '-': printf("\n%0.2f - %0.2f = %0.2f",a,b,a-b);break;
case '*': printf("\n%0.2f * %0.2f = %0.2f",a,b,a*b);break;
case '/': printf("\n%0.2f / %0.2f= %0.2f",a,b,a/b);break;
default: printf("\nPlease seleec choice (+,-,*,/)");
}
getch();
}
2. ให้เขียนโปรแกรม สำหรับคำนวณน้ำหนักมาตรฐาน โดยให้ป้อนข้อมูล ส่วนสูง น้ำหนัก เพศ[M , F] (Filename : weight.cpp)
#include
#include
void main()
{
char fm;
float h,w;
clrscr();
printf("Your Male(m) or Female(f) : ");
scanf("%c",&fm);printf("\nYour Weight : ");
scanf("%f",&w);printf("\nYour Hight : ");
scanf("%f",&h); switch(fm)
switch(fm) {
case 'm': if(h-100==w)
printf("\nYou Standardweight ");
else if(h-100)
printf("\nYour weight > Standard : %0.2fkg",w-(h-100));
else if(h-100>w)
printf("\nYour weight Less Than Standard : %0.2fkg",(h-110)-w);
break;
case 'f': if(h-110==w)
printf("\nYou Standard weight ");
else if(h-110printf("\nYour weight > Standard : %0.2fkg",w-(h-110));
else if(h-110>w)
printf("\nYour weight less Than Standard : %0.2fkg",(h-110)-w);
break;
getch();
}
3. ให้เขียนโปรแกรมรับตัวอักษรภาษาอังกฤษมาและบอกว่าเป็นสระ หรือ พยัญชนะ (Filename : eng.cpp)
#include
#include
void main()
{
char az;
clrscr();
printf("Please your input english word A-Z : ");scanf("%c",&az); switch(az)
{
case 'a': printf(" a is article"); break;
case 'e': printf(" e is article"); break;
case 'i': printf(" i is article"); break;
case 'o': printf(" o is article"); break;
case 'u': printf(" u is article"); break;
default : printf("%c is consonant",az);
}
getch();
}
4. ให้เขียนโปรแกรมเพื่อรับจำนวนเงินบาท และบอกว่าใช้ธนบัตรประเภทใดเป็นจำนวนเท่าใด และต้องใช้เหรียญประเภทใด จำนวนเท่าใด (Filename : bank.cpp)
#include
#include
void main()
{
int mn;clrscr();
printf("Input total money : ");
scanf("%i",&mn);
{ if(mn>=1000) printf("1000 bank :%i",mn/1000); mn=mn%1000;
}
if(mn>=500){ printf("\n500 bank :%i",mn/500); mn=mn%500;
}
if(mn>=100){ printf("\n100 bank :%i",mn/100); mn=mn%100;
} if(mn>=50){ printf("\n50 bank :%i",mn/50); mn=mn%50;
}
if(mn>=20){ printf("\n20 bank :%i",mn/20); mn=mn%20;
}
if(mn>=10){ printf("\n10 coin :%i",mn/10); mn=mn%10;
}
if(mn>=5){ printf("\n5 coin :%i",mn/5); mn=mn%5;
}
if(mn>=1){ printf("\n1 coin :%i",mn);
}
getch();
}

ไม่มีความคิดเห็น: