←to practical programming

Homework "input/output"

Reading and writing numbers from/into files.
  1. Build a program that reads a sequence of numbers, separated by a combination of blanks (' '), tabs ('\t'), and newline characters ('\n'), from the standard input and prints these numbers together with their sines and cosines (in a table form) to the standard output. Something like
    char[] delimiters = {' ','\t','\n'};
    var options = StringSplitOptions.RemoveEmptyEntries;
    for( string line = ReadLine(); line != null; line = ReadLine() ){
    	var words = line.Split(delimiters,options);
    	foreach(var word in words){
    		double x = double.Parse(word);
    		WriteLine($"{x} {Sin(x)} {Cos(x)}");
                    }
            }
    
    The program can be run as
    echo 1 2 3 4 5 | mono stdin.exe > out.txt
    
    or
    echo 1 2 3 4 5 > input.txt
    mono stdin.exe < input.txt > out.txt
    
    or
    echo 1 2 3 4 5 > input.txt
    cat input.txt | mono stdin.exe > out.txt
    
  2. Build a program that reads a set of numbers from the command-line and prints 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){
    	double x = double.Parse(arg);
    	WriteLine($"{x} {Sin(x)} {Cos(x)}");
    	}
    }
    
    The program can be run as
    mono cmdline.exe 1 2 3 4 5 > out.txt
    
    or
    echo 1 2 3 4 5 > inputfile
    mono cmdline.exe $(cat inputfile) > out.txt
    
  3. Build a program that reads a set of numbers (separated by newlines) from "inputfile" and writes 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. Something like
    mono fileio.exe -input:input.txt -output:out.txt
    
    public static int Main(string[] args){
            string infile=null,outfile=null;
            foreach(var arg in args){
                    var words=arg.Split(':');
                    if(words[0]=="-input")infile=words[1];
                    else if(words[0]=="-output")outfile=words[1];
                    else { Error.WriteLine("wrong argument"); return 1; }
                    }
            var instream =new System.IO.StreamReader(infile);
            var outstream=new System.IO.StreamWriter(outfile);
            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();
    	return 0;
    }