Algorithms for linear and branched(Линейные и разветвленные алгоритмы)


Algorithms for linear and branched(Линейные и разветвленные алгоритмы)

ပရိုဂမ္မ်ားသည္ linear ,  branch , cyclic(loop) ႏွင့္ complex မ်ားၿဖစ္ၾကသည္။Program အမ်ားစုသည္ complex မ်ားၿဖစ္ၾကသည္။အၿခား က်န္ရွိေနေသာ ပရိုဂမ္အမ်ိဳးအစားမ်ားသည္ စိတ္ကူးအရတည္ေဆာက္ထားေသာ type မ်ားၿဖစ္ၾကသည္။ဆိုလိုသည္မွာ ပရိုဂမ္တိုင္းအား linear, branch (သို႕) cyclic(loop) တစ္ခုခု အစိတ္အပိုင္းအၿဖစ္ခြဲၿခား ၾကည့္ရွဳ႕နိဳင္သည္ ဟုသတ္မွတ္နိဳင္သည္။ မ်ားေသာအားၿဖင့္ linear program မ်ားအား အရိုးရွင္းဆံုးအၿဖစ္ (သို႕) complex programs မ်ားအား linear အစိတ္အပိုင္းမ်ားအၿဖစ္ ခြဲၿခမ္းၾကည့္ရွဳ႕ၾကသည္။

Linear algorithm(Линейные алгоритмы)
Linear ဆိုသည္မွာ program တစ္ခုၿဖစ္ၿပီး ထို program ထဲတြင္ operator မ်ားသည္ တစ္ခုၿပီးတစ္ခု အစမွစ၍ အဆံုးထိ အစဥ္လိုက္ operate ၿပဳလုပ္သြားမည္ၿဖစ္သည္။ထိုသို႕ operate ၿပဳလုပ္ရာတြင္ အစဥ္အလိုက္ၿပဳလုပ္မည္ၿဖစ္ၿပီး ထပ္ခါထပ္ခါ(ဥပမာ loop) ၿပဳလုပ္မည္မဟုတ္ေပ။
Linear ပရိုဂမ္မ်ားသည္ assignment operators မ်ား,mathmetic functions မ်ား,arithmetic operation မ်ား,input/output နွင့္သက္ဆိုင္ေသာ လုပ္ေဆာင္ခ်က္မ်ား ႏွင့္ အၿခား operator မ်ား ၿဖင့္ ဖြဲ႕စည္းတည္ေဆာင္ထားသည္။ထို အၿခား operator မ်ားဆိုရာတြင္ ပရိုဂမ္မ်ားတြင္ operator မ်ား၏လုပ္ေဆာင္ခ်က္မ်ားအား အစဥ္အလိုက္ၿပဳလုပ္ေနၿခင္းအား ေၿပာင္းလဲမည့္ operator မ်ားမၿဖစ္ေစရေပ။
C++ language တြင္ အသံုးၿပဳေသာ အေၿခခံ mathematic function မ်ားသည္ ေအာက္ပါဇယားတြင္ ေဖာ္ၿပထားသည့္ list အတိုင္းပင္ၿဖစ္သည္။

  
The basic mathematical functions (Основные математические функции)

Titile
Symbol(sign) of
Type
Declaration filename
function
the function
return to a func tion
argument
Absolute value
abs (x)
cabs (x)
fabs (x)
int
double
float
int
double
float
<stdlib.h>
<math.h>
<math.h>
Arc cosin
acos (x)
double
double
<math.h>
Arc sine
a sin (x)
double
double
<math.h>
Arc tangent
atan (x)
double
double
<math.h>
Cosine
c os (x)
double
double
<math.h>
Sine
s in (x)
double
double
<math.h>
The exponent e x
e xp (x)
double
double
<math.h>
Power function of x in
pow (x, y)
double
double
<math.h>
Natural logarithm
log (x)
double
double
<math.h>
Decimal logarithm
l og10 (x)
double
double
<math.h>
Square root
s qrt (x)
double
double
<math.h>
Tangent
tan (x)
double
double
<math.h>

ထို functions မ်ားအသံုးၿပဳရန္အတြက္ header file ေၾကၿငာရန္ <math.h> (သို႕) <cmath.h> သည္ မရွိမၿဖစ္လိုအပ္မည္ၿဖစ္သည္။
I/O (input/output) နွင့္သက္ဆိုင္ေသာ operation မ်ားအတြက္ standard input and output stream မ်ားမွာ သက္ဆိုင္ေသာ stream မ်ားမွေထာက္ပံၿပဳလုပ္မည္ၿဖစ္သည္။
Example.1)    Write a program to calculate the volume V and surface area S of a hollow sphere of a given external and internal radii R and r, if it is known that 


# include <iostream>
using namespace std;
# include <cmath.h>
const double Pi = 3.1415926;               / / Determine the number of pi
int main ()
{            double S, V, R, r;
      cout << "Enter the radius of the outer";
      cin >> R;
      cout << "Enter the inner radius";
      cin >> r;
              S = 4 * Pi * (R * R - r * r);
              V = 4.0 / 3 * Pi * (pow (R, 3)-pow (r, 3));
              cout << "S =" << S << endl;
              cout << "V =" << V << endl;
return 0;
}
  
Branching algorithms(Разветвленные алгоритмы) 
Branching algorithms မ်ားသည္ အခ်ိဳ႕ေသာ condition မ်ား၏ မွန္ကန္မွဳ႕(true) (သို႕) မွားယြင္းမွဳ႕(false) ေပၚမူတည္၍ program အား operate ၿပဳလုပ္ရန္ ေရြးခ်ယ္မွဳ႕ၿဖင့္ အဓိကထားေဆာင္ရြက္သည္။ထိုသို႕ၿပဳလုပ္ရာတြင္ program ထဲတြင္ရွိေသာ ေအာက္ေဖာ္ၿပပါ decision-making structures  ဟုေခၚေသာ special operator မ်ားအားၿဖင့္ တိက်ေစသည္။
  • if
  • if-else
  • switch

Conditional if statement (Условный оператор if)
if operator ၏ syntax(ေရးနည္းပံုစံ) သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
if (expression) statement;
အကယ္၍ operator သည္ condition ၏ expression အား မွန္ကန္၍ execute ၿပလုပ္ရာတြင္ တစ္ခုတည္းၿဖစ္ပါက အထက္ပါ syntax အားအသံုးၿပဳ၍ အကယ္၍တစ္ခုထက္ပိုပါက ေအာက္ပါ syntax အားအသံုးၿပဳသည္။
if (expression) {statement1;
    statement2;
     . . .
    operator N;
    } // End if,

Example .By number y> 0, a year to identify with - the number of his century. Considering that, for example, XXI century starts with 2001, not 2000.

# include <iostream>
using namespace std;
int main ()
{   int dig;
    cout << "Enter year:";
    cin >> y;
    c = y/100;
    if (y% 100! = 0) c += 1;
    cout << "This year belongs to the century" << c << endl;
    return 0;
}

The operator if-else(Оператор if - else)
else-if statement တြင္ branches(အခြဲ) နစ္ခု ရွိသည္။ထို branches နွစ္ခုထဲမွ တစ္ခုသည္ alternative ၿဖစ္ရမည္ၿဖစ္သည္။ if-else operator ၏ syntax(ေရးနည္းပံုစံ) သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
if  (expression)  statement1;
else  statement2;
အကယ္၍ if-else ၏ေနာက္တြင္ operator(statement) သည္ တစ္ခုသာတည္ရွိပါက အထက္ပါ syntax အားအသံုးၿပဳ၍ operator(statement ) သည္တစ္ခုထက္ပိုပါက ေအာင္ပါ syntax ကိုအသံုးၿပဳနိဳင္သည္။
if (expression) {   statement 1;
                          statement 2; }
else { statement 3;
         statement 4; },


Sub-branches(Вложенные ветвления)
ထို႕ထက္ပို၍ if operator အား ႏွစ္သက္ရာ condition မ်ားထပ္မံထည့္ဆိုၿပီး internal အၿဖစ္ အတြင္းပိုင္းတြင္ထပ္မံၿဖည့္စြပ္ အသံုးၿပဳနိဳင္သည္။ထိုသို႕အသံုးၿပဳရာတြင္ ပံုမွန္အားၿဖင့္ ေအာက္ေဖာ္ၿပပါအတိုင္း အသံုးၿပဳၾကသည္။
if (expr1) statement1;
else if (expr2) statement2;
else if (expr3) operator3;
else if (exprN) operatorN;
else            / / Optional part                    


Example.Compute d = max (a, b, c); - a maximum of three numbers entered from the keyboard.(keyboard မွ ရိုက္ထည့္လိုက္ေသာ ကိန္းသံုးလံုးမွ အၾကီးဆံုးကိန္းအားရွာေဖြရန္)
#include <iostream>
using namespace std;
int main( )
{  int a, b, c, d;
    cout << "a >  ";
    cin >> a;
    cout << "b >  ";
    cin >> a;
    cout << "c >  ";
    cin >> a;
    if (a>b && a>c) d = a;
       else if (b>c) d = b;
                 else d = c;
    cout << "max =  "<< d << endl;
     return 0;
}

The conditional operator(Условная операция)
conditional operator မ်ားသည္ ပံုမွန္အားၿဖင့္ ေအာက္ပါ syntax အတိုင္းၿဖစ္သည္။
variable = expression? expression1 : expression2
ထိုကဲ့သို႕ ေရးထားေသာ conditional operator ေဖာ္ၿပခ်က္အား ေအာက္ပါအတိုင္း အစားထိုးၾကည့္ရွဳ႕နိဳင္သည္။
if (expression) variable = value1;
    else variable = value2;

conditional operator နွင့္ condition experssion မ်ား၏ ရလဒ္မ်ားအား ေလ့လာၾကည့္ရွဳ႕ရာတြင္ ၎တို့၏ code မ်ားသည္လုပ္ေဆာင္ခ်က္အတူတူပင္ၿဖစ္သည္။သို႕ေသာ္ ကြဲၿပားၿခားနားမွဳ႕ကိုႏွိုင္းယွဥ္ၾကည့္လွ်င္ conditional operator ၏လုပ္ေဆာင္ခ်က္သည္ variavle ဆီသို႕ ႏွစ္ခါၿပန္လည္ သြားေ၇ာက္မည္ၿဖစ္ၿပီး ၎၏ address အားနွစ္ၾကိမ္ၿပန္လည္တြက္ခ်က္မည္ၿဖစ္မည္။condition experssion တြင္ အဆင့္လိုက္ တစ္ခါသာၿပဳလုပ္မည္ၿဖစ္သည္။အၿခားတစ္ဘက္အေနၿဖင့္ ပရိုဂမ္ အၿမင္အရ ၾကည့္ရွဳ႕ရလွ်င္ conditional operator သည္ ပိုမိုေကာင္းမြန္သည္ၿဖစ္သည္။
Example.
if (test = = ‘Y’)
     TestValue = 100;
     else
          TestValue = 0;
    …
    …
TestValue = (test = = ‘Y’)?100:0;
    …
အထက္ေဖာ္ၿပပါ condition သည္ ဘယ္ဘက္တြင္ေဖာ္ၿပထားေသာ if-else condition နွင့္တူညီသည္။

Example
...
if (a> b) max = a;
else max = b;
...
...
max = (a> b)? a: b;
    ...

The operator of multiple choice(Оператор множественного выбора)(switch operator)
အကယ္၍ မ်ားစြာေသာ ေရြးခ်င္ခြင့္မ်ားမွ တစ္ခုအားေရြးခ်ယ္ရမည္ဆိုလွ်င္ if-else structure မ်ားစြာသံုးမည့္အစား switch ဟုေခၚေသာ the operator of multiple choice ကိုအသံုးၿပဳနိဳင္သည္။၎၏ syntax သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
switch (expression)
{case value1: statement1;
break;
case value2: statement2;
break;
case value3: operator3;
break;
default:                             // Optional component
  ....................;                // If there was not a coincidence and Denia.
} / / end switch (expression)
Switch ၏ ေ၇းနည္းအရ switch ၏ body အတြက္ brackets သည္မၿဖစ္မေနလိုအပ္ေပသည္။ အထက္ေဖာ္ၿပပါ syntax တြင္ switch ၏ expression နွင့္ကိုက္ညီေသာ case အား ေရြးခ်ယ္၍ operate ၿပဳလုပ္သြားမည္ၿဖစ္သည္။ထိုသို႕ expression လိုအပ္သကဲ့သို႕ပင္ case တုိုင္းတြင္လည္း ထို expression နွင့္မွန္ကန္မွု႕၇ွိမရွိ စစ္ေဆးရန္အတြက္ values(labels) မ်ားလိုအပ္မည္ၿဖစ္သည္။
ထို ေရြးခ်ယ္လိုက္ေသာ case တစ္မ်ား၏ operator သည္ တစ္ခုတည္းၿဖစ္နိဳင္သလို အမ်ားအၿပားလည္းၿဖစ္နိဳင္သည္။ထိုကဲ့သို႕ case မ်ားအားပိုင္းၿခား၇န္အတြက္ brackets မ်ားအားအၿမဲလိုအပ္မည္မဟုတ္ပဲ break operator အားအသံုးၿပဳ၍ return ၿပန္ၿခင္းအားၿဖင့္ၿပဳလုပ္နိဳင္ၿဖစ္သည္။
Block(case) တိုင္းတြင္ရွိေသာ break statement သည္ switch operator ၏ exit ၿဖစ္သည္ဟုလည္းေၿပာဆိုနိဳင္သည္။အကယ္၍ break statement အားအသံုးမၿပဳပဲ switch operator အားေရးသားလွ်င္ ေအာက္ပါအတိုင္းေရးသားနိဳင္သည္။
switch (expression) {   case value1: statement1;
                                 case value2: statement2;
                                 case value3: operator3;
                             } // end switch (expression)
အထက္ေဖာ္ၿပပါ ပရိုဂမ္တြင္ ပထမအေနၿဖင့္ switch ေနာက္ရွိ expression နွင့္ မည္သည့္ case ၏ value နွင့္ မွန္ကန္မွဳ႕ရွိမရွိ တိုက္ဆိုင္စစ္ေဆးၿပီး မွန္ကန္ပါက ၎၏ statement အား operate(execute) လုပ္မည္ၿဖစ္ၿပီး ၿပီးဆံုးပါက အၿခား case မ်ားအား ထပ္မံ၍ မွန္ကန္မွဳ႕ရွိမရွိစစ္ေဆးၿပီး break statement ဆီသို႕မေရာက္မခ်င္း(သို႕) switch statement မၿပီးမခ်င္း အစဥ္အလိုက္ operate ၿပဳလုပ္မည္ၿဖစ္သည္။

Example 3.12. Enter the keyboard character. If it is a symbol of an arithmetic operation, indicate - what kind and to bring an example. If not, then display a message about it.
# include <iostream>
using namespace std;
int main ()
{char sym;
int op1, op2, res;
cout << "Enter the symbol of an arithmetic operation"
 cin >> sym;
switch (sym)
{case '+':               cout << "Adding" << endl;
                                          cout << "a term";
                                          cin >> op1;
                                          cout << "two term";
                                          cin >> op2;
                                          res = op1 + op2;
                                          cout << op1 << '+' << op2 << '=' << res << endl;
                                          break;
case '-':               cout << "Subtraction" << endl;
                                          cout << "decreases:";
                                          cin 1 >> op;
                                          cout << "subtrahend";
                                          cin >> op2;
                                          res = op1 - op2;
                                          cout << op1 << '-' << op2 << '=' << res << endl;
                                          break;
case '*':               cout << "multiplication" << endl;
                                          cout << "multiplicand";
                                          cin >> op1;
                                          cout << "multiplier";
                                          cin >> op 2;
                                          res = op1 * op2;
                                          cout << op1 << '*' << op2 << '=' << res << endl;
                                          break;
case '/':               cout << "division" << endl;
                                          cout << "dividend";
                                          cin >> op1;
                                          cout << "divider";
                                          cin >> op 2;
                                          if (op2! = 0)
{res = op1 / op2;
                                                cout << op1 << '/' << op2 << '=' << res << endl;
                                                  }
                                             else cout << "Division by 0 not allowed" << endl;
                                          break;
default: cout << "No arithmetic operation" << endl;
                 }                             // End switch (sym)
return 0;
}

 translated by zmk@miet51

No comments:

Post a Comment