วันอาทิตย์ที่ 18 มกราคม พ.ศ. 2558

โค้ดการคำนวนแบบใช้ ตัวแปรเข้าช่วย

โค้ดการคำนวนแบบใช้ ตัวแปรเข้าช่วย ยังไงลองทำดูนะครับ

#include <iostream>
using namespace std;
int main()
{
    int 
s_width; //กำหนดตัวแปร 2 ตัว
    int 
s_long;
    cout << "
Find Area Program " << endl; ----------------// แสดงค่าทางหน้าจอเพื่อถามเราว่า หาพื้นที่
    cout << "Width : "; -------------------------------------------// ค่าความกว้าง
    cin >> s_width; ------------------------------------------------// รับค่าทางคีย์บอร์ด
    
cout << "Long : "; -------------------------------------------// ค่าความยาว
    cin >> s_long; -------------------------------------------------// รับค่าทางคีย์บอร์ด
    cout << "Area : " <<
 (s_width * s_long) << " unit " << endl; // คำนวนในโยคนี้

    system("pause");
}

   ตัวแปร หมายถึงชื่อเรียกแทนพื้นที่เก็บของข้อมูลในหน่วยความจำ ซึ่งจะมีชนิดของข้อมูลมีชนิดของข้อมูล หรือรูปแบบของตัวแปรคือ char, int, long, float, double, unsigned int, unsigned long int โดยชื่อของตัวแปรนี้ เราสามารถกําหนดเป็นกลุ่มตัวอักษรที่ผู้เขียนโปรแกรมสามารถกําหนดขึ้นเองได้ โดยมีข้อกําหนดดังนี้ • ตัวอักษรภาษาอังกฤษตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ในภาษาC++ จะถือว่าเป็นตัวแปรคนละตัวกัน เช่น test และ Test จะเป็นตัวแปรคนละตัวกัน • ตัวอักษรตัวแรกต้องเป็นตัวอักษรหรือ _ (underline character) จะเป็นตัวเลขไม่ได้ • ตัวอักษรที่ไม่ใช่ตัวแรกจะเป็นตัวอักษรหรือ _ หรือตัวเลขก็ได้ ชื่อของตัวแปรที่ห้ามตั้งซ้ำกับคําสงวนของ C++ asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t การประกาศตัวแปร 



การเขียนโปรแกรมขนาดเล็กในแต่ละครับควรศึกษาวางแผนการดีๆสะก่อนว่าตัวแปรที่เราใช้นั้นควรใช้ชนิดใดนั้นเอง (อ้วกกกกกกก)

Program Flowchart

ผังงาน (Flowchart)
ผังงาน (Flowchart)
   คือ แผนภาพแสดงลำดับขั้นตอนการทำงาน โดยใช้สัญลักษณ์ต่าง ๆ ในการเขียนผังงาน เพื่อช่วยลำดับแนวความคิดในการเขียนโปรแกรม เป็นวิธีที่นิยมใช้เพราะทำให้เห็นภาพในการทำงานของโปรแกรมง่ายกว่าใช้ข้อความ หากมีข้อผิดพลาด สามารถดูจากผังงานจะทำให้การแก้ไขหรือปรับปรุงโปรแกรมทำได้ง่ายขึ้น



อ้างอิง https://kru-it.com/cs-p5/flowchart/

Dance on the float

Float ลอย

Float แปลว่า ลอยจากพื้นครับ พอเราใช้ค่านี้แล้วมันจะลอยได้ครับ
ตอนผมเด็กๆผมก็คิดแบบนั้นครับ แต่ตอนนี้ก็ยังคิดอยู่ครับ (คุณครูเอสไม่ได้กล่าวไว้)


Float(จริงๆ) ในภาษาซีหมายความว่า ทศนิยมครับ และเรายังสามารถปรับให้มันปัดค่าเป็นทศนิยมกี่ตำแหน่ง

float a = 3.3559;
printf("\n the number is %.3f ",a);
ผลที่ได้คือ 3.356 ครับ (Reroyd ได้กล่าวไว้)

หาค่าพื้นที่ สามเหลี่ยม
#include <iostream>
using namespace std;
int main() 
{
float base , height , area;  ------------------> รับค่าด้วย ลอย ถุ้ยไม่ใช้ float รับค่าเป็นทศนิยม ฐาน สูง พื้นที่
cout << "Enter Base : ";   ------------------> รับค่าด้วย  Base แปลว่า ฐาน
cin >> base; 
cout << "Enter Height : "; ------------------> รับค่าด้วย  Height แปลว่า ความสูง
cin >> height; 
area = (0.5) * base * height; ------------------> area = (0.5) คือ ครึ่งหนึ่ง * base * height
cout << "Triangle Area Is " << area << endl;
cout << endl; 
system("pause");
 return 0; 
}

หลักการง่ายๆคือการ ใช้ตัวแปร คิดไว้ว่า คำตอบที่ได้ถ้าออกมาเป็นทศนิยม ก็ใช้ตัวแปรที่ถูกต้อง
ส่วนการคำนวน ควรรู้สูตรการคำนวนนั้นๆเพื่อเวลาเขียนจะเขียนโปรแกรมได้ง่ายขึ้น
(เขียนโฟว์ชาร์ทไว้ก่อนจะดีมาก
)


---------------------------------------------------------------------------------------------------------------------

วันพฤหัสบดีที่ 20 พฤศจิกายน พ.ศ. 2557

else if # 3

else if # 3

  คำสั่งที่ใช้ในการตรวจสอบเงื่อนไขในภาษาซี คำสั่งแรกที่เราจะเรียนรู้กันก็คือ if (แปลว่า “ถ้า”) นั่นคือเราจะใช้ if เพื่อตรวจสอบผลของการเปรียบเทียบว่าเป็น “จริง” หรือ “เท็จ” ถ้า เป็นจริง จะให้ทำอะไรต่อไป และถ้าเป็นเท็จ จะให้ทำอะไรต่อไป (ออกสอบ)

   

ขอบคุณที่มาจาก http://www.cprogramming.com/tutorial/lesson2.html

>     greater than              5 > 4 is TRUE
<     less than                 4 < 5 is TRUE
>=    greater than or equal     4 >= 4 is TRUE
<=    less than or equal        3 <= 4 is TRUE
==    equal to                  5 == 5 is TRUE
!=    not equal to              5 != 4 is TRUE

The structure of an if statement is as follows:
if ( TRUE )
  Execute the next statement

Here is a simple example that shows the syntax:
if ( 5 < 10 )
  cout<<"Five is now less than ten, that's a big surprise";


Else

if ( TRUE ) {
  // Execute these statements if TRUE
}
else {
  // Execute these statements if FALSE
}

Else If

if ( <condition> ) {
  // Execute these statements if <condition> is TRUE
}
else if ( <another condition> ) {
  // Execute these statements if <another condition> is TRUE and
  // <condition> is FALSE
}

ตัวอย่างโปรแกรม รูปแบบเงื่อนไข (ออกสอบ)

#include <iostream>

using namespace std;

int main()                                                        // Most important part of the program!
{
  int age;                                                        // Need a variable...
  
  cout<<"Please input your age: ";                  // Asks for age
  cin>> age;                                                  // The input is put in age
  cin.ignore ( );                                                 // Throw away enter
  if ( age <= 30 ) {                                         // If the age is less than 30
     cout<<"You are pretty young!\n";              // Just to show you it works...
  }
  else if ( age > 31 ) {                                      // I use else just to show an example 
     cout<<"You are old\n";                              // Just to show you it works...
  }
  else {
    cout<<"You are really old\n";                     // Executed if no other statement is
  }
  cin.get( );
}

thank you : http://www.cprogramming.com/tutorial/lesson2.html

วันพฤหัสบดีที่ 30 ตุลาคม พ.ศ. 2557

else if #2

else if #2
การกำหนดเงื่อนไขโดยกำหนดวันเป็นหมายเลข



#include <iostream>
#include <conio.h>
int main()
{
int x; char name[10];
 cout<<"Enter the name: ";
cin>>name;
cout<<"What is the first day of weeks?
 Please select the number 1-7.

1.Monday 2.Tuesday 3.Wednesday 4.Thursday 5.Friday 6.Saturday 7.Sunday ";
cin>>x;
switch(x)
{
case 1: cout<<"Monday";
break; case 2: cout<<"Tuesday";
break; case 3: cout<<"Wednesday";
break; case 4: cout<<"Thursday";
break; case 5: cout<<"Friday";
 break; case 6: cout<<"Saturday";
break; default: cout<<"Sunday";
break;
 }
if(x==1)
{
cout<<" Total score = 5"<< x;
}
else if(x==2)
{
cout<<" Total score = 0"<<x;
 }
else if(x==3)
{
cout<<" Total score = 0"<<x;
 }
else if(x==4)
{
cout<<" Total score = 0"<<x;
 }
else if(x==5)
{
cout<<" Total score = 0"<<x;
}
else if(x==6)
{
cout<<" Total score = 0"<<x;
 }
else if(x==7)
{
cout<<" Total score = 0"<<x;
}
getch( );
 }

else if #1

else if  #1(ไม่มีทศนิยม) If you, if you, if you love me or not

if ตัวนี้แปลว่า ถ้า ครับ แสดงค่าแบบ เงื่อนไข
เช่น ถ้าวันนี้ฝนตก ฉันจะไม่ไปโรงเรียน แต่ถ้าฝนไม่ตกฉันก็ไม่ไปโรงเรียน (ถุ้ย!!)ฉันจะไปโรงเรียน 
ดังนั้น if else ใช้ในกรณีมี 2 ทางเลือก
แต่ if else if คือใช้เมื่อมีมากกว่า2ทางเลือก เช่น ผลเกรดเป็นต้น (เพราะมีหลายทางเลือกการให้คะแนน)



 Thank you http://byterevel.com

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
main()
{
int score;
 printf("Plese enter your score : ");
 scanf("%d",&score);
if ((score>=0)&&(score<=50))        |-- กำหนดถ้าเงื่อนไขว่า คะแนน 0-50
{printf("You got grade : F");}      |-- แสดงเกรด F
 else if ((score>=51)&&(score<=60)) |-- แต่ถ้ามากกว่า 51-60
{printf("You got grade : D");}      |-- แสดงเกรด D
 else if ((score>=61)&&(score<=70))
{printf("You got grade : C");}
 else if ((score>=71)&&(score<=80))
{printf("You got grade : B");}
 else if ((score>=81)&&(score<=90))
{printf("You got grade : A");}
 else printf("INPUT ERROR");
getch();
}

วันจันทร์ที่ 27 ตุลาคม พ.ศ. 2557

Add two numbers

Add two numbers

add two numbers รับค่า 2 ตัวเลข

#include<iostream.h>
main()
{
int a;
int b;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number:";
cin>>b;
cout<<"REsult is = "<<a+b<<endl;
system ("pause") ;
}

-----------------------------------------------

โค้ดรับค่า 2 ตัว โดยกำหนดให้
int  a ค่าที่ 1
int  b ค่าที่ 2

แสดงค่าใน "....." ว่า "Enter First Number"
cout<<"Enter First Number: ";

เก็บค่าด้วยcin>>a;
cin>>b;

นำมาคำนวนcout<<"REsult is = "<<a+b<<endl;
** ลองปรับการใช้งานตามโจทย์ต่างๆ และเพื่มตัวแปรด้วยตัวเองนะครับ



เบื่อวันที่ฝนตกมากครับ ตากผ้าที่ไร ไม่เคยแห้งเลย // แมวบ้านผมบอกมา