Зміст курсу
Introduction to .NET with C#
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
Introduction to .NET with C#
Task - File Based Calculator
You can download the required folder for this task. The folder includes two files called 1.txt
and 2.txt
.
After the user specifies an operation, the program should read the numbers from 1.txt
and 2.txt
and apply the operation on each corresponding number from the 1.txt
and 2.txt
files. The results should be stored in the corresponding lines of a new file called result.txt
.
For example, if the user enters the operator +
, the numbers in the first lines of 1.txt
and 2.txt
will be added (925 + 789 = 1714
), and the result will be stored in the first line of a new file called result.txt
. The same should happen for the remaining lines.
File.ReadAllLines
to read 1.txt
and 2.txt
. 2.
StreamWriter
will be used in writing the output file.
using System;
public class Calculator
{
// Modify these paths if needed
// These terms are set to 'public' and 'static' to make them accessible for the Unit Tests
public static string f1Path = "C:\\Users\\Public\\Documents\\1.txt";
public static string f2Path = "C:\\Users\\Public\\Documents\\2.txt";
public static string outputPath = "C:\\Users\\Public\\Documents\\result.txt";
public static void Main(string[] args)
{
char opr;
while (true)
{
Console.Write("Enter an operator (+, -, /, *): ");
// Simplified code using TryParse
if (char.TryParse(Console.ReadLine(), out opr) && "/-+*".Contains(opr))
break;
Console.WriteLine("Invalid operator.");
}
// Write code below this line
string[] numbers_1 = File.ReadAllLines(f1Path);
string[] numbers_2 = File.ReadAllLines(f2Path);
float result = 0.0f, n1, n2;
using (StreamWriter writer = new StreamWriter(outputPath))
{
for (int i = 0; i < numbers_1.Length; i++)
{
n1 = float.Parse(numbers_1[i]);
n2 = float.Parse(numbers_2[i]);
if (opr == '+')
result = n1 + n2;
else if (opr == '-')
result = n1 - n2;
else if (opr == '/')
result = n1 / n2;
else if (opr == '*')
result = n1 * n2;
writer.WriteLine(result);
}
}
Console.WriteLine($"Results saved in {outputPath}");
// Write code above this line
}
}
Все було зрозуміло?
Зміст курсу
Introduction to .NET with C#
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
Introduction to .NET with C#
Task - File Based Calculator
You can download the required folder for this task. The folder includes two files called 1.txt
and 2.txt
.
After the user specifies an operation, the program should read the numbers from 1.txt
and 2.txt
and apply the operation on each corresponding number from the 1.txt
and 2.txt
files. The results should be stored in the corresponding lines of a new file called result.txt
.
For example, if the user enters the operator +
, the numbers in the first lines of 1.txt
and 2.txt
will be added (925 + 789 = 1714
), and the result will be stored in the first line of a new file called result.txt
. The same should happen for the remaining lines.
File.ReadAllLines
to read 1.txt
and 2.txt
. 2.
StreamWriter
will be used in writing the output file.
using System;
public class Calculator
{
// Modify these paths if needed
// These terms are set to 'public' and 'static' to make them accessible for the Unit Tests
public static string f1Path = "C:\\Users\\Public\\Documents\\1.txt";
public static string f2Path = "C:\\Users\\Public\\Documents\\2.txt";
public static string outputPath = "C:\\Users\\Public\\Documents\\result.txt";
public static void Main(string[] args)
{
char opr;
while (true)
{
Console.Write("Enter an operator (+, -, /, *): ");
// Simplified code using TryParse
if (char.TryParse(Console.ReadLine(), out opr) && "/-+*".Contains(opr))
break;
Console.WriteLine("Invalid operator.");
}
// Write code below this line
string[] numbers_1 = File.ReadAllLines(f1Path);
string[] numbers_2 = File.ReadAllLines(f2Path);
float result = 0.0f, n1, n2;
using (StreamWriter writer = new StreamWriter(outputPath))
{
for (int i = 0; i < numbers_1.Length; i++)
{
n1 = float.Parse(numbers_1[i]);
n2 = float.Parse(numbers_2[i]);
if (opr == '+')
result = n1 + n2;
else if (opr == '-')
result = n1 - n2;
else if (opr == '/')
result = n1 / n2;
else if (opr == '*')
result = n1 * n2;
writer.WriteLine(result);
}
}
Console.WriteLine($"Results saved in {outputPath}");
// Write code above this line
}
}
Все було зрозуміло?