ตัวอย่างการเปรียบเทียบโครงสร้างโปรแกรมภาษา C กับ โปรแกรมภาษา C++ Ex โปรแกรมภาษาC /*Program by Jormpon:Do not copy*/ /*Body Mass index*/ #include"stdio.h" main() { float weight,height; float bmi; printf("Enter your weight (Kg): "); scanf("%f",&weight); printf("Enter your height (Metre): "); scanf("%f",&height);
bmi=weight/(height*height);
if (bmi>40) { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Morbidly obese.\n\n\n\n"); printf("By Jormpon"); } else if (bmi>=27) { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Obese.\n\n\n\n"); printf("By Jormpon"); } else if (bmi>=23) { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Overweight.\n\n\n\n"); printf("By Jormpon"); } else if (bmi>=18) { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Normal.\n\n\n\n"); printf("By Jormpon"); } else if (bmi>=15) { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Underweight.\n\n\n\n"); printf("By Jormpon"); } else { printf("Your Body Mass Index is %.3f\n",bmi); printf("You are Starvation.\n\n\n\n"); printf("By Jormpon"); } }
Ex โปรแกรมภาษา C++ /*Program by Jormpon:Do not copy*/ /*Body Mass index*/ #include"iostream.h" #include"iomanip.h" main() { float weight,height; float bmi;
cout<<"Enter your weight (Kg): "; cin>>weight; cout<<"Enter your height (Metre): "; cin>>height;
bmi=weight/(height*height);
if (bmi>40) { cout<<"Your Body Mass Index is : "<<>=27) { cout<<"Your Body Mass Index is : "<<>=23) { cout<<"Your Body Mass Index is : "<<>=18) { cout<<"Your Body Mass Index is : "<<>=15) { cout<<"Your Body Mass Index is : "<< setprecision(5)<< bmi; cout<<"\nYou are Underweight."; cout<<"\n\n\n\nBy Jormpon"; } else { cout<<"Your Body Mass Index is : "<< setprecision(5)<< bmi; cout<<"\nYou are Starvation."; cout<<"\n\n\n\nBy Jormpon"; } }
3. จากค่าของ int a[2][3] = {{6,5,4},{3,2,1}};ให้นักศึกษา หาค่าของ a[1][0] และ a[0][2]
ค่าของa[1][0]=3
ค่าของa[0][2]=4
4. ให้นักศึกษากำหนด Structure ที่มีค่าของข้อมูลอย่างน้อย6 Records
#include "stdio.h"
int i;
long sum1,sum2,sum3;
main()
{
struct National_Product
{
long quality;
long price;
}np;
struct National_Income
{
long rent;
long payment;
long interest;
long profit;
}ni;
struct National_Expenditure
{
long consume;
long invest;
long government;
long net_export;
}ne;
{
printf("1 National product,2 National income,3 National expenditure\n");
printf("Choose your choice between 1 and 3 : ");
scanf("%d",&i);
switch(i)
{
case 1: printf("National income data\n");
printf("Enter number of product: ");
scanf("%d",&np.quality);
printf("Enter price : ");
scanf("%d",&np.price);
sum1 =(np.quality*np.price);
printf("National income of product is %d\n",sum1);
break;
case 2: printf("National income data\n");
printf("Enter all of rent : ");
scanf("%d",&ni.rent);
printf("Enter all of payment : ");
scanf("%d",&ni.payment);
printf("Enter all of interest : ");
scanf("%d",&ni.interest);
printf("Enter all of profit : ");
scanf("%d",&ni.profit);
sum2 =(ni.rent+ni.payment+ni.interest+ni.profit);
printf("National income of income is %d\n",sum2);
break;
case 3: printf("National income data\n");
printf("Enter all of consumption : ");
scanf("%d",&ne.consume);
printf("Enter all of investment : ");
scanf("%d",&ne.invest);
printf("Enter all of government : ");
scanf("%d",&ne.government);
printf("Enter all of net export : ");
scanf("%d",&ne.net_export);
sum3 =(ne.consume+ne.invest+ne.government+ne.net_export);
printf("National income of expenditure is %d\n",sum3);
break;
default:
printf("U N K N O W N C O M M A N D ! ! ");
}
}
}