Difference between revisions of "User:Piphi/Asymptote/piphi"

m (pigments)
 
Line 3: Line 3:
 
My own unofficial Asymptote module with some useful functions:
 
My own unofficial Asymptote module with some useful functions:
  
 +
==Documentation==
 
<math>\texttt{string [] array (string s);}</math>
 
<math>\texttt{string [] array (string s);}</math>
 
:This function is actually part of newer versions of Asymptote, but is not supported in 2.32 so I decided to make it.  What it does is it takes a string and splits it into individual characters.
 
:This function is actually part of newer versions of Asymptote, but is not supported in 2.32 so I decided to make it.  What it does is it takes a string and splits it into individual characters.
<syntaxhighlight lang="Asymptote">string [] array (string s) {
+
 
string[]strarray;
+
<math>\texttt{int length (int i);}</math>
for (int i=0;i<length(s);++i) {
+
:Returns the number of digits in the given integer.
    strarray[i]=substr(s,i,1);
+
 
 +
<math>\texttt{int int (string s);}</math>
 +
:Converts strings into integers.
 +
 
 +
==Source Code==
 +
<syntaxhighlight lang="Asymptote" line>
 +
/**
 +
* The piphi module
 +
*
 +
* Made by piphi
 +
*/
 +
 
 +
string [] array (string s) {
 +
    string[]strarray;
 +
    for (int i=0;i<length(s);++i) {
 +
        strarray[i]=substr(s,i,1);
 
     }
 
     }
 
     return strarray;
 
     return strarray;
}</syntaxhighlight>
+
}
  
<math>\texttt{int length (int i);}</math>
+
int length (int i) {
:returns the number of digits in the given integer.
+
    int j=0;
<syntaxhighlight lang="Asymptote">int length (int i) {
 
int j=0;
 
 
     bool brk=false;
 
     bool brk=false;
 
     do {
 
     do {
    ++j;
+
        ++j;
 
         if (i/(10**j) < 1) {
 
         if (i/(10**j) < 1) {
        brk=true;
+
            brk=true;
 
         }
 
         }
 
     } while (!brk);
 
     } while (!brk);
 
     return j;
 
     return j;
}</syntaxhighlight>
+
}
  
<math>\texttt{int int (string s);}</math>
+
int int (string s) {
:converts strings into integers.
+
    string[]a=array(s);
<syntaxhighlight lang="Asymptote">int int (string s) {
+
    int b;
string[]a=array(s);
 
int b;
 
 
     for (int i=0;i<a.length;++i)
 
     for (int i=0;i<a.length;++i)
    b+=(ascii(a[i])-48)*10**(a.length-i-1);
+
        b+=(ascii(a[i])-48)*10**(a.length-i-1);
return b;
+
    return b;
 
}</syntaxhighlight>
 
}</syntaxhighlight>
 
More to come...
 

Latest revision as of 22:22, 30 October 2020

[asy]label(scale(2)*"The \texttt{piphi} module");[/asy]

My own unofficial Asymptote module with some useful functions:

Documentation

$\texttt{string [] array (string s);}$

This function is actually part of newer versions of Asymptote, but is not supported in 2.32 so I decided to make it. What it does is it takes a string and splits it into individual characters.

$\texttt{int length (int i);}$

Returns the number of digits in the given integer.

$\texttt{int int (string s);}$

Converts strings into integers.

Source Code

 1 /**
 2 * The piphi module
 3 * 
 4 * Made by piphi
 5 */
 6 
 7 string [] array (string s) {
 8     string[]strarray;
 9     for (int i=0;i<length(s);++i) {
10         strarray[i]=substr(s,i,1);
11     }
12     return strarray;
13 }
14 
15 int length (int i) {
16     int j=0;
17     bool brk=false;
18     do {
19         ++j;
20         if (i/(10**j) < 1) {
21             brk=true;
22         }
23     } while (!brk);
24     return j;
25 }
26 
27 int int (string s) {
28     string[]a=array(s);
29     int b;
30     for (int i=0;i<a.length;++i)
31         b+=(ascii(a[i])-48)*10**(a.length-i-1);
32     return b;
33 }