C# 0 The Complete Reference


n  is  returned as  GetParts( )



Download 4,07 Mb.
Pdf ko'rish
bet251/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   247   248   249   250   251   252   253   254   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

n

 is 


returned as 

GetParts( )

’s return value. Second, the fractional portion of 



n

 is passed back to 

the caller through the 

out

 parameter 



frac

. As this example shows, by using 



out

, it is possible 

for one method to return two values.

Of course, you are not limited to only one 



out

 parameter. A method can return as many 

pieces of information as necessary through 

out

 parameters. Here is an example that uses 

two

out

 parameters. The method 



HasComFactor( )

 performs two functions. First, it determines 

if two integers have a common factor (other than 1). It returns 

true

 if they do and 



false

otherwise. Second, if they do have a common factor, 



HasComFactor( )

 returns the least and 

greatest common factors in 

out

 parameters.

// Use two out parameters.

using System;

class Num {

  /* Determine if x and v have a common divisor.

www.freepdf-books.com



180

 

P a r t   I :  



T h e   C #   L a n g u a g e

     If so, return least and greatest common factors in

     the out parameters. */

  public bool HasComFactor(int x, int y,

                           out int least, out int greatest) {

    int i;

    int max = x < y ? x : y;

    bool first = true;

    least = 1;

    greatest = 1;

    // Find least and greatest common factors.

    for(i=2; i <= max/2 + 1; i++) {

      if( ((y%i)==0) & ((x%i)==0) ) {

        if(first) {

          least = i;

          first = false;

        }

        greatest = i;

      }

    }


    if(least != 1) return true;

    else return false;

  }

}

class DemoOut {



  static void Main() {

    Num ob = new Num();

    int lcf, gcf;

    if(ob.HasComFactor(231, 105, out lcf, out gcf)) {

      Console.WriteLine("Lcf of 231 and 105 is " + lcf);

      Console.WriteLine("Gcf of 231 and 105 is " + gcf);

    }

    else


      Console.WriteLine("No common factor for 35 and 49.");

    if(ob.HasComFactor(35, 51, out lcf, out gcf)) {

      Console.WriteLine("Lcf of 35 and 51 " + lcf);

      Console.WriteLine("Gcf of 35 and 51 is " + gcf);

    }

    else


      Console.WriteLine("No common factor for 35 and 51.");

  }


}

In

Main( )

, notice that 

lcf

 and 


gcf

 are not assigned values prior to the call to 



HasComFactor( )

. This would be an error if the parameters had been 



ref

 rather than 



out

. The method returns either 



true

 or 


false

, depending upon whether the two integers 

have a common factor. If they do, the least and greatest common factors are returned 

in the 


out

 parameters. The output from this program is shown here:

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   247   248   249   250   251   252   253   254   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish