उन्नत कैलकुलेटर


28

आपको एक प्रोग्राम लिखना होगा जो एक स्ट्रिंग का मूल्यांकन करता है जिसे एक उन्नत कैलकुलेटर में दर्ज किया जाएगा।

कार्यक्रम को स्टडिन का उपयोग करके इनपुट को स्वीकार करना चाहिए और सही उत्तर का उत्पादन करना चाहिए। उन भाषाओं के लिए जिनके पास स्टडिन को स्वीकार करने के लिए फ़ंक्शन नहीं हैं, आप इन कार्यों को संभालने readLineऔर printइन कार्यों को संभालने के लिए मान सकते हैं ।

आवश्यकताएँ:

  • किसी भी तरह के "eval" फ़ंक्शन का उपयोग नहीं करता है
  • फ्लोटिंग पॉइंट और नेगेटिव नंबर को हैंडल कर सकते हैं
  • कम से कम +, -, *, /, और ^ ऑपरेटरों का समर्थन करता है
  • सामान्य आदेश को ओवरराइड करने के लिए कोष्ठक और कोष्ठक का समर्थन करता है
  • ऑपरेटरों और नंबरों के बीच एक या अधिक रिक्त स्थान वाले इनपुट को संभाल सकते हैं
  • संचालन के मानक क्रम का उपयोग करके इनपुट का मूल्यांकन करता है

परीक्षण के मामलों

इनपुट

10 - 3 + 2

उत्पादन

9


इनपुट

8 + 6 / 3 - 7 + -5 / 2.5

उत्पादन

1


इनपुट

4 + [ ( -3 + 5 ) * 3.5 ] ^ 2 - 12

उत्पादन

41

1
.0यदि वे पूर्णांक हैं , तो क्या यह ठीक है कि आउटपुट संख्याओं के अंत में एक अनुगामी है ? भी: कैलकुलेटर होना कितना सही है (फ्लोटिंग पॉइंट प्रिसिजन के बारे में और ऐसा)?
sepp2k

1
आउटपुट .0के अंत में एक अनुगामी हो सकता है । मैं सटीक के बारे में निश्चित नहीं हूं, लेकिन अधिक बेहतर है।
केविन ब्राउन

1
स्टैक ओवरफ्लो संस्करण गणितीय अभिव्यक्ति मूल्यांकनकर्ता (पूर्ण PEMDAS) था । हालांकि उस एक के जवाब में से कई लाइनों (?) की गिनती कर रहे हैं। फिर भी c में कई कॉम्पैक्ट उत्तर हैं।
dckckee

PN / RPN कैलकुलेटर के लिए बोनस?
मतीन उल्हाक

जवाबों:


8

सी ++, 640 583

string k="[]()+-*/^";stack<double> m;stack<char> n;
#define C(o,x,y) ('^'==o?x<y:x<=y)
#define Q(a) double a=m.top();m.pop();
#define R(o) {Q(b)Q(a)m.push(o=='+'?a+b:o=='-'?a-b:o=='*'?a*b:o=='/'?a/b:o=='^'?pow(a,b):0);n.pop();}
while(!cin.eof()){string s;getline(cin,s,' ');if(s.empty())continue;if('\n'==*--s.end())s.erase(--s.end());(s.size()==1&&s.npos!=k.find(s[0]))?({char c=s[0]=='['?'(':s[0]==']'?')':s[0];while(!n.empty()&&'('!= c&&C(c,k.find(c),k.find(n.top())))R(n.top());')'==c?n.pop():n.push(c);}):m.push(strtod(s.c_str(),0));}while(!n.empty())R(n.top());cout<<m.top()<<endl;

दांतेदार बना हुआ

string k="[]()+-*/^";
stack<double> m;
stack<char> n;
#define C(o,x,y) ('^'==o?x<y:x<=y)
#define Q(a) double a=m.top();m.pop();
#define R(o) {Q(b)Q(a)m.push(o=='+'?a+b:o=='-'?a-b:o=='*'?a*b:o=='/'?a/b:o=='^'?pow(a,b):0);n.pop();}
while(!cin.eof())
{
    string s;
    getline(cin,s,' ');
    if(s.empty())continue;
    if('\n'==*--s.end())s.erase(--s.end());
    (s.size()==1&&s.npos!=k.find(s[0]))?({
        char c=s[0]=='['?'(':s[0]==']'?')':s[0];
        while(!n.empty()&&'('!= c&&C(c,k.find(c),k.find(n.top())))
            R(n.top());
        ')'==c?n.pop():n.push(c);
    }):m.push(strtod(s.c_str(),0));
}
while(!n.empty())
    R(n.top());
cout<<m.top()<<endl;

मेरा पहला कोड गोल्फ, इसलिए टिप्पणियों और आलोचनाओं की प्रतीक्षा कर रहा है!


घातांक संचालक की सही-संगति संभालता है, जो जेबी के पर्ल समाधान नहीं लगता है।
drspod

न तो समस्या कथन और न ही लिंक किए गए विकिपीडिया पृष्ठ उल्लेख घातांक को सही-सहयोगी होना चाहिए। इसके अलावा, विकिपीडिया पृष्ठ स्पष्ट रूप से कहता है कि दोनों तरीके वाणिज्यिक कैलकुलेटर पर पाए जाते हैं।
जेबी

1
+1 अच्छी तरह से गोल्फ, लेकिन ... बस छोड़ने में शामिल है, using namespace stdऔर एक मुख्य कार्य वास्तव में ठीक नहीं है, क्या यह है?
बंद हो गया

2

PHP - 394 354 312 अक्षर

<?=e(!$s=preg_split('#\s+#',`cat`,-1,1),$s);function e($P,&$s){$S='array_shift';if(($a=$S($s))=='('|$a=='['){$a=e(0,$s);$S($s);}while($s&&($p=strpos(' +-*/^',$o=$s[0]))&&$p>=$P){$b=e($p+($S($s)!='^'),$s);if($o=='+')$a+=$b;if($o=='-')$a-=$b;if($o=='*')$a*=$b;if($o=='/')$a/=$b;if($o=='^')$a=pow($a,$b);}return$a;}

इंडेंट:

<?
preg_match_all('#\d+(\.\d+)?|\S#',`cat`,$m);
$s=$m[0];
function e($P) {
        global $s;
        if (strpos(" ([",$s[0])){
                array_shift($s);
                $a=e(0);
                array_shift($s);
        } else {
                $a=array_shift($s);
                if ($a=='-')$a.=array_shift($s);
        }
        while ($s && ($p=strpos(' +-*/^',$o=$s[0])) && $p >= $P) {
                array_shift($s);
                $b = e($p+($o!='^'));
                switch($o){
                case'+':$a+=$b;break;
                case'-':$a-=$b;break;
                case'*':$a*=$b;break;
                case'/':$a/=$b;break;
                case'^':$a=pow($a,$b);
                }
        }
        return $a;
}
echo e(0);

2

पोस्टस्क्रिप्ट, 446

यह शंटिंग यार्ड एल्गोरिथ्म का उपयोग करता है।

[/*[/p
2/e{mul}>>/d[/p
2/e{div}>>/+[/p
1/e{add}>>/-[/p
1/e{sub}>>/o[/p
9/e{}>>/c[/p
-1/e{}>>/^[/p
3/e{exp}>>/p
0>>begin/s(%stdin)(r)file 999 string readline pop def
0 1 s length 1 sub{s exch[0 1 255{}for]dup[(\(o)([o)(\)c)(]c)(/d)]{{}forall
put dup}forall
pop
3 copy pop
get
get
put}for{s token not{exit}if
exch/s exch store{cvr}stopped{load
dup/p get
p
le{currentdict end
exch begin/e get exec}{begin}ifelse}if}loop{{e end}stopped{exit}if}loop
=

अन-गोल्फ और टिप्पणी:

% We associate the operators with their precedence /p and the executed commend /e
[
  (*)[/p  2 /e{mul}>>
  (d)[/p  2 /e{div}>> % This is division
  (+)[/p  1 /e{add}>>
  (-)[/p  1 /e{sub}>>
  (o)[/p  9 /e{   }>> % This is open bracket
  (c)[/p -1 /e{   }>> % This is close bracket
  (^)[/p  3 /e{exp}>>
  /p 0
>>begin

% Let's read the input string
/s(%stdin)(r)file 999 string readline pop def

% If we want to use the token operator, we have to replace (, [, ), ] and / to get meaningful results
% We use kind of an encoding array (familiar to PostScripters) to map those codes to o, c, and d.
0 1 s length 1 sub{        % index
  s exch                   % string index
  [0 1 255{}for] dup       % string index translationArray translationArray
  [(\(o)  ([o)  (\)c)  (]c)  (/d)] % string index translationArray translationArray reencodeArray
  {                        % string index translationArray translationArray translationString
    {}forall               % string index translationArray translationArray charCode newCharCode
    put dup                % string index translationArray translationArray
  }forall                  % string index translationArray translationArray
  pop                      % string index translationArray
  3 copy pop               % string index translationArray string index
  get                      % string index translationArray charCode
  get                      % string index translatedCharCode
  put                      % -/-
}for

% Now we can actually start interpreting the string
% We use the stack for storing numbers we read and the dictionary stack for operators that are "waiting"
{                          % number*
  s token not{exit}if      % number* string token
  exch /s exch store       % number* token
  % We try to interpret the token as a number
  {cvr}stopped{            % number* token
    % If interpretation as number fails, we have an operator
    load                   % number* opDict
    % Compare operator precedence with last operator on dictstack
    dup /p get             % number* opDict opPrec
    p                      % number* opDict opPrec prevOpPrec
    le {                   % number* opDict
      % If the last operator on the stack has at least the same precedence, execute it
      currentdict end      % number* opDict prevOpDict
      exch begin           % number* prevOpDict
      /e get exec          % number*
    }{                     % number* opDict
      % If last operator doesn't have higher precedence, put the new operator on the dictstack as well
      begin
    }ifelse
  }if
}loop
% If we're finished with interpreting the string, execute all operators that are left on the dictstack
{{e end}stopped{exit}if}loop
=

TODO : घातांक की सही-संगति


आह ... तानाशाही: शानदार!
लूसर ड्रग

मुझे स्टैकओवरफ्लो पर टिप्पणी करने की अनुमति है, इसलिए मैं थोड़ा भ्रमित था। क्या यह सामान्य है कि प्रतिष्ठा अलग से प्रबंधित की जाती है, या मैंने अपने लॉगिन को खराब कर दिया है?
थॉमस डब्ल्यू।

मैं आपको बताना चाहता था कि आपके समाधान में कुछ गड़बड़ होनी चाहिए क्योंकि ऊपर दिए गए सभी तीन परीक्षण मामले विफल होते हैं। हालाँकि, मैंने यह समझने की कोशिश नहीं की कि आप अभी तक क्या कर रहे हैं (कुछ टिप्पणियाँ शांत होंगी; ;-))।
थॉमस डब्ल्यू।

1) किसी भी साइट पर 200 हिट करने के बाद, आप हर साइट पर 101 पर शुरू करेंगे। या यहाँ 50 मारा। 2) AUGHH! मुझे लगा कि यह बेसिक कैलकुलेटर का एक त्वरित विस्तार था। मैंने यह भी नहीं देखा कि कोष्ठक की आवश्यकता है! और मैंने इसे बहुत अच्छी तरह से परीक्षण नहीं किया। ओह अच्छा; नीचे पैंट, कम से कम मेरी undies साफ कर रहे हैं!
लूसर ड्रोग

@luserdroog: "@ThomasW" मुझे टिप्पणी करने के लिए आमंत्रित नहीं करता है।
थॉमस डब्ल्यू।

1

पायथन 2 , 339 335 बाइट्स

import re
x,s=input(),re.sub
def f(y):
 y,r=s('- ','+ -',y).split(),['^','*','/','+','-']
 for c in r:
  while c in y:d=y.index(c)-1;a,b=map(float,[y[d],y[d+2]]);y=y[:d]+[((a,-a)[a<0]**b,a*b,a/b,a+b,a-b)[r.index(c)]]+y[d+3:]
 return`y[0]`
w=lambda b:s("[([]+[\d+\-*/^ .]*[)\]]",lambda m:f(m.group()[1:]),s(' +',' ',b))
print f(w(w(x)))

इसे ऑनलाइन आज़माएं!

  • -4 बाइट्स बदलकर str (x) बैकटिक्स के साथ ``!

0

पोस्टस्क्रिप्ट, 1000 695 665 494

थॉमसडब्ल्यू के विचारों को चुरा लिया। जोड़ा सुविधा: ऑपरेटरों के आसपास या रिक्त स्थान के साथ तार स्वीकार करता है।[फीचर हटाया गया]


उपयोग करने से बूट करने के लिए परीक्षण करने की ARGUMENTSतुलना में कम %stdinआसान है, और आसान है!


सिर्फ ब्रैकेट्स को पैरेंस से बदलने के लिए प्रतिस्थापन को सरल बनाया।

575(1)10:36 PM:ps 0> gsnd -q -- calc2bg.ps '10 - 3 + 2'
9
576(1)10:37 PM:ps 0> gsnd -q -- calc2bg.ps '8 + 6 / 3 - 7 + -5 / 2.5'
1.0
577(1)10:37 PM:ps 0> gsnd -q -- calc2bg.ps '4 + [ ( -3 + 5 ) * 3.5 ] ^ 2 - 12'
41.0

कोड:

/T[/^[/C{exp}/P 4/X{le}>>/*[/C{mul}/P 3/X{lt}>>/[/C{div}/P
3/X{lt}>>/+[/C{add}/P 2/X{lt}>>/-[/C{sub}/P
2/X{lt}>>>>def[/integertype{}/realtype{}/stringtype{V}/nametype{cvlit/N
exch store{P T N get dup/P get exch/X get exec{exit}if C end}loop T N get
begin}91 40 93 41>>begin/V{0 1 2 index length 1 sub{2 copy get
dup where{exch get}if 3 copy put pop pop}for[/N 0/R 0/P 0/C{}>>begin{token
not{exit}if exch/R exch store dup type exec R}loop{P 0 eq{end exit}if C
end}loop}def ARGUMENTS{V ==}forall

असंबद्ध और टिप्पणी:

%!
%Shunting-Yard Algorithm using dictstack for operators
%invoke with %gsnd -q -- calc2bg.ps [ 'expr1' ]*

%The operator table. C:code P:precedence X:test(implements associativity)
/T[
    /^[/C{exp}/P 4/X{le}>>
    /*[/C{mul}/P 3/X{lt}>>
    /[/C{div}/P 3/X{lt}>>
    /+[/C{add}/P 2/X{lt}>>
    /-[/C{sub}/P 2/X{lt}>>
>>def

%The type-dispatch dictionary
%numbers: do nothing
%string: recurse
%name: process op
[%/integertype{}/realtype{} %now uses `where` below
/stringtype{V}/nametype{
pstack()=
    cvlit/N exch store %stash cur-op
    {
        P %prec(tos)
        T N get %prec(tos) cur-op-dict
        dup/P get %prec(tos) cur-op-dict prec(cur-op)
        exch/X get %prec(tos) prec(cur-op) test(cur-op)
        exec{exit}if %exit if prec(tos) < || <= prec(cur-op)
/C load ==
        C %pop-and-apply
        end
pstack()=
    } loop
    T N get begin %push cur-op
}>>begin

%substitutions
[91 40 93 41>>begin %replace brackets with parens
/V {
    %pre-process
    0 1 2 index length 1 sub {
        2 copy get
        dup where { exch get } if
        3 copy put pop pop
    } for
dup ==

    [/N 0/R 0/P 0/C{}>>begin %dummy base operator and storage
    { token not{exit}if exch /R exch store %extract token, stash Remainder
pstack(>)=
        %dispatch type procedure
        dup type dup where { pop exec }{ pop } ifelse
    R }loop
pstack()=
    {
        P 0 eq{end exit}if %hit dummy op: exit
/C load ==
        C end %pop and apply
    } loop
} def

ARGUMENTS{V ==}forall %iterate through the command-line arguments

@ThomasW मुझे आश्चर्य है कि अगर यह आपको टिप्पणी करने के लिए आमंत्रित करने के लिए काम करता है । (?)
लूसर डॉग

मैंने comp.lang.postscript में एक ही विचार का एक पूर्ण कार्यान्वयन पोस्ट किया है ।
luser droog
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.