← ppnm

Exercise "input/output"

Tasks:

  1. 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.txt
    	
    and 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.txt
    
    or, 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
    
    The 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).

  2. 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.txt
    
    or
    echo 1 2 3 4 5 > input.txt
    mono main.exe < input.txt 2> out.txt
    
    or
    echo 1 2 3 4 5 > input.txt
    cat input.txt | mono stdin.exe 2> out.txt
    
  3. 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.txt
    
    Something 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();