2017-03-28 16 views
5

मैं विश्वविद्यालय में रसायन शास्त्र का अध्ययन कर रहा हूं और पर्ल 6 या पर्ल में पाठ्यपुस्तक उदाहरण लिखना चाहता हूं, जैसे कि रासायनिक सूत्र या अन्य प्रक्रियाओं को संतुलित करना!पर्ल 6 कस्टम ऑपरेटरों का उपयोग

तब मुझे समस्या का सामना करना पड़ा perl6 कस्टम ऑपरेटर पर। मुझे लगता है कि जब मैं सुविधा का उपयोग करता हूं तो मैं अपना कोड दोहरा रहा हूं। पढ़ना और लिखना मुश्किल है। मैं इसे कैसे सरल बना सकता हूं?

#!/usr/bin/env perl6 
use v6; 
#basic SI(International System of Units) type 


role MetricPrefix { 
    method baseOn (Str $base , Numeric $input) { 
     given $base { 
      when 'pico' { return $input * 10**-12 } 
      when 'namo' { return $input * 10**-9 } 
      when 'micro' { return $input * 10**-6} 
      when 'milli' { return $input * 10**-3 } 
      when 'centi' { return $input * 10**-2 } 
      when 'hecto' { return $input * 10**2 } 
      when 'kilo' { return $input * 10**3 } 
      when 'mega' { return $input * 10**6 } 
      when 'giga' { return $input * 10**9 } 
      when 'tera' { return $input * 10**12 } 
      default { fail "you must input a metric prefix which allow pico to tera" } 
     } 
    } 
} 



class Mass does MetricPrefix { 
    #basic Mass is g is different form si statda 
    has $.g; 

    submethod BUILD (:$!g ) { 
    } 

} 

class Length does MetricPrefix { 
    has $.Length ; 

    submethod BUILD (:$!Length ) { 
    } 
} 



multi postfix:<(kg)>($input) { 
    return Mass.new(g => Mass.baseOn("kilo",$input)) or fail "you Must input a number"; 
} 

multi postfix:<(g)>($input) { 
    return Mass.new(g => $input) or fail "you Must input a number"; 
} 

multi infix:<+>(Mass $inputOne , Mass $inputTwo) is assoc<right> { 
    return Mass.new(g => $inputOne.g + $inputTwo.g) or fail "error in there "; 
} 

multi infix:<->(Mass $inputOne , Mass $inputTwo) is assoc<right> { 
    return Mass.new(g => $inputOne.g - $inputTwo.g) or fail "error in there "; 
} 

multi infix:<*>(Mass $inputOne , Mass $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) is tighter(&infix:</>) { 
    return Mass.new(g => $inputOne.g * $inputTwo.g) or fail "error in there "; 
} 

multi infix:</>(Mass $inputOne , Mass $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) { 
    return Mass.new(g => $inputOne.g/$inputTwo.g) or fail "error in there "; 
} 





#the meterLeng 
multi postfix:<(km)>($input) { 
    return Length.new(Length => Length.baseOn("kilo",$input)) or fail "you Must input a number"; 
} 

multi postfix:<(m)>($input) { 
    return Length.new(Length => $input) or fail "you Must input a number"; 
} 

multi infix:<+>(Length $inputOne , Length $inputTwo) is assoc<right> { 
    return Length.new(Length => $inputOne.Length + $inputTwo.Length) or fail "error in there "; 
} 

multi infix:<->(Length $inputOne , Length $inputTwo) is assoc<right> { 
    return Length.new(Length => $inputOne.Length - $inputTwo.Length) or fail "error in there "; 
} 

multi infix:<*>(Length $inputOne , Length $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) is tighter(&infix:</>) { 
    return Length.new(Length => $inputOne.Length * $inputTwo.Length) or fail "error in there "; 
} 

multi infix:</>(Length $inputOne , Length $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) { 
    return Length.new(Length => $inputOne.Length/$inputTwo.Length) or fail "error in there "; 
} 


#just a test 
say 10(kg) + 1(g); 
say 10(m) + 1(m); 
+2

यह आपको समस्या की आवश्यकता के बजाय कोड समीक्षा प्रश्न की तरह लगता है। आप * (* और पहले उनके सहायता केंद्र की जांच करें) इसे हमारी बहन साइट https://codereview.stackexchange.com – IMSoP

+0

पर बेहतर फिट ढूंढें, 'सबमिशन बिल्ड' अनावश्यक हैं क्योंकि आपने गुणों को सार्वजनिक रूप से घोषित किया है। –

+0

हां, यह सबमिशन बिल्ड अनावश्यक है जब मैंने किसी ऑब्जेक्ट निर्माण का उपयोग नहीं किया; –

उत्तर

2

मैंने इनपुट पर घोषित प्रकारों के साथ असफल संदेशों को प्रतिस्थापित किया। इससे पर्ल 6 चिंता करेगा कि संख्याएं इनपुट हैं या नहीं, यदि वे नहीं हैं तो उचित त्रुटि संदेश दें। मैंने यह भी धारणा में बनाया कि सभी लंबाई और जन सकारात्मक हैं।

#!/usr/bin/env perl6 
use v6; 
#basic SI(International System of Units) type 

role MetricPrefix { 
    method baseOn (Str $base , Numeric $input) { 
     given $base { 
      when 'pico' { return $input * 10**-12 } 
      when 'namo' { return $input * 10**-9 } 
      when 'micro' { return $input * 10**-6 } 
      when 'milli' { return $input * 10**-3 } 
      when 'centi' { return $input * 10**-2 } 
      when 'hecto' { return $input * 10**2 } 
      when 'kilo' { return $input * 10**3 } 
      when 'mega' { return $input * 10**6 } 
      when 'giga' { return $input * 10**9 } 
      when 'tera' { return $input * 10**12 } 
      default { fail "you must input a metric prefix within the range of pico to tera" } 
     } 
    } 
} 

class Mass does MetricPrefix { 
    #basic Mass is g is different form si statda 
    has $.g where * > 0; 
} 

class Length does MetricPrefix { 
    has $.Length where * > 0; 
} 

# Mass 
multi postfix:<(kg)>($input where * > 0) { 
    return Mass.new(g => Mass.baseOn("kilo",$input)); 
} 

multi postfix:<(g)>($input where * > 0) { 
    return Mass.new(g => $input); 
} 

multi infix:<+>(Mass $inputOne , Mass $inputTwo) is assoc<right> { 
    return Mass.new(g => $inputOne.g + $inputTwo.g); 
} 

multi infix:<->(Mass $inputOne , Mass $inputTwo) is assoc<right> { 
    return Mass.new(g => $inputOne.g - $inputTwo.g); 
} 

multi infix:<*>(Mass $inputOne , Mass $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) is tighter(&infix:</>) { 
    return Mass.new(g => $inputOne.g * $inputTwo.g); 
} 

multi infix:</>(Mass $inputOne , Mass $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) { 
    return Mass.new(g => $inputOne.g/$inputTwo.g); 
} 

#Length 
multi postfix:<(km)>($input where * > 0) { 
    return Length.new(Length => Length.baseOn("kilo",$input)); 
} 

multi postfix:<(m)>($input where * > 0) { 
    return Length.new(Length => $input); 
} 

multi infix:<+>(Length $inputOne , Length $inputTwo) is assoc<right> { 
    return Length.new(Length => $inputOne.Length + $inputTwo.Length); 
} 

multi infix:<->(Length $inputOne , Length $inputTwo) is assoc<right> { 
    return Length.new(Length => $inputOne.Length - $inputTwo.Length); 
} 

multi infix:<*>(Length $inputOne , Length $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) is tighter(&infix:</>) { 
    return Length.new(Length => $inputOne.Length * $inputTwo.Length); 
} 

multi infix:</>(Length $inputOne , Length $inputTwo) is assoc<right> is tighter(&infix:<+>) is tighter(&infix:<->) { 
    return Length.new(Length => $inputOne.Length/$inputTwo.Length); 
} 


#just a test 
say 10(kg) + 1(g); 
say 10(m) + 1(m); 
संबंधित मुद्दे