Read a set of numbers from the command-line argument in the form of a
comma-separated list (we follow the option convention of
mcs
) like this,
mono main.exe -numbers:1,2,3,4,5 > Out.txtand print these numbers together with their sines and cosines (in a table form) to the standard output. Something like
public static void Main(string[] args){ foreach(var arg in args){ var words = arg.Split(':'); if(words[0]=="-numbers"){ var numbers=words[1].Split(','); foreach(var number in numbers){ double x = double.Parse(number); WriteLine($"{x} {Sin(x)} {Cos(x)}"); } } } }The program can get the numbers from the command-line directly,
mono main.exe -numbers:1,2,3,4,5 > out.txtor, alternatively, if the line of numbers is too long, one can write the numbers into a file and then "cat" the file into the command line,
echo "-numbers:1,2,3,4,5" > inputfile mono main.exe $$(cat inputfile) > out.txt # double dollar-sign if in MakefileThe length of the command line is limited though, so the size of the file should not exceed your command line limit (probably about 2 megabytes).
Read a sequence of numbers, separated by a combination of blanks (' '), tabs ('\t'), and newline characters ('\n'), from the standard input and print these numbers together with their sines and cosines (in a table form) to the standard error. Something like
char[] split_delimiters = {' ','\t','\n'}; var split_options = StringSplitOptions.RemoveEmptyEntries; for( string line = ReadLine(); line != null; line = ReadLine() ){ var numbers = line.Split(split_delimiters,split_options); foreach(var number in numbers){ double x = double.Parse(number); Error.WriteLine($"{x} {Sin(x)} {Cos(x)}"); } }The program can be run as
echo 1 2 3 4 5 | mono main.exe 2> out.txtor
echo 1 2 3 4 5 > input.txt mono main.exe < input.txt 2> out.txtor
echo 1 2 3 4 5 > input.txt cat input.txt | mono stdin.exe 2> out.txt
Read a set of numbers separated by newline characters from "inputfile" and write them together with their sines and cosines (in a table form) to "outputfile". The program must read the names of the "inputfile" and "outputfile" from the command-line,
mono main.exe -input:input.txt -output:out.txtSomething like
string infile=null,outfile=null; foreach(var arg in args){ var words=arg.Split(':'); if(words[0]=="-input")infile=words[1]; if(words[0]=="-output")outfile=words[1]; } if( infile==null || outfile==null) { Error.WriteLine("wrong filename argument"); return 1; } var instream =new System.IO.StreamReader(infile); var outstream=new System.IO.StreamWriter(outfile,append:false); for(string line=instream.ReadLine();line!=null;line=instream.ReadLine()){ double x=double.Parse(line); outstream.WriteLine($"{x} {Sin(x)} {Cos(x)}"); } instream.Close(); outstream.Close();