Created
February 29, 2016 18:22
-
-
Save Kiso-blg/b3cde2f1bab4f033afa0 to your computer and use it in GitHub Desktop.
Chapter 09 Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_01_Send_Greeting | |
{ | |
public class SendGreeting | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.Write("Enter a name: "); | |
string name = Console.ReadLine(); | |
PrintGreeting(name); | |
} | |
static void PrintGreeting(string name) | |
{ | |
Console.WriteLine("Hello, {0}!", name); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_02_Find_Biggest_Number | |
{ | |
class FindBiggestNumber | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter the first number: "); | |
string input = Console.ReadLine(); | |
int number1; | |
if (int.TryParse(input, out number1)) | |
{ | |
number1 = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number"); | |
return; | |
} | |
Console.Write("Enter the second number: "); | |
input = Console.ReadLine(); | |
int number2; | |
if (int.TryParse(input, out number2)) | |
{ | |
number2 = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number"); | |
return; | |
} | |
Console.Write("Enter the third number: "); | |
input = Console.ReadLine(); | |
int number3; | |
if (int.TryParse(input, out number3)) | |
{ | |
number3 = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number"); | |
return; | |
} | |
Console.WriteLine(GetMax(number1, number2, number3)); | |
} | |
static int GetMax(int num1, int num2, int num3) | |
{ | |
int maxNumber = Math.Max(num1, Math.Max(num2, num3)); | |
return maxNumber; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_03_Get_Last_Number | |
{ | |
class Program | |
{ | |
static string GetLastNumber(long number) | |
{ | |
string numInDigits; | |
number = Math.Abs(number); | |
switch (number % 10) | |
{ | |
case 0: | |
numInDigits = "zero"; | |
break; | |
case 1: | |
numInDigits = "one"; | |
break; | |
case 2: | |
numInDigits = "two"; | |
break; | |
case 3: | |
numInDigits = "three"; | |
break; | |
case 4: | |
numInDigits = "four"; | |
break; | |
case 5: | |
numInDigits = "five"; | |
break; | |
case 6: | |
numInDigits = "six"; | |
break; | |
case 7: | |
numInDigits = "seven"; | |
break; | |
case 8: | |
numInDigits = "eight"; | |
break; | |
case 9: | |
numInDigits = "nine"; | |
break; | |
default: | |
Console.WriteLine("error"); | |
return null; | |
} | |
return numInDigits; | |
} | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter a number : "); | |
string input = Console.ReadLine(); | |
long number; | |
if (long.TryParse(input, out number)) | |
{ | |
number = long.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number."); | |
return; | |
} | |
Console.Write("The last digit is: "); | |
Console.WriteLine(GetLastNumber(number)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_04_Find_Repeating_Number | |
{ | |
class FindRepeatingNumber | |
{ | |
static int CountRepeatingNumber(int[] numbers, int num) | |
{ | |
int count = 0; | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
if (numbers[i] == num) | |
{ | |
count++; | |
} | |
} | |
return count; | |
} | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter the number to find: "); | |
string input = Console.ReadLine(); | |
int numberToFind; | |
if (int.TryParse(input, out numberToFind)) | |
{ | |
numberToFind = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number."); | |
return; | |
} | |
int[] numbers = new int[] { 10, 3, 5, -1, 0, 12, 8, 10, 3, 5, -1, 0, 12, 8 }; | |
Console.WriteLine("The number {0} has been foud {1} times in the array", | |
numberToFind, CountRepeatingNumber(numbers, numberToFind)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_05_Check_Element_In_Array | |
{ | |
class CheckElementInArray | |
{ | |
static string CheckElement(int index, int[] array) | |
{ | |
if (index == 0) | |
{ | |
if (array[index] < array[index + 1]) | |
{ | |
return "lower"; | |
} | |
else if (array[index] > array[index + 1]) | |
{ | |
return "bigger"; | |
} | |
else | |
{ | |
return "equal"; | |
} | |
} | |
else if (index == array.Length - 1) | |
{ | |
if (array[index] < array[index - 1]) | |
{ | |
return "lower"; | |
} | |
else if (array[index] > array[index - 1]) | |
{ | |
return "bigger"; | |
} | |
else | |
{ | |
return "equal"; | |
} | |
} | |
else | |
{ | |
if (array[index] < array[index + 1] && array[index] < array[index - 1]) | |
{ | |
return "lower"; | |
} | |
else if (array[index] > array[index + 1] && array[index] > array[index - 1]) | |
{ | |
return "bigger"; | |
} | |
else | |
{ | |
return "equal"; | |
} | |
} | |
} | |
static void PrintElement(string attitude) | |
{ | |
Console.WriteLine("The element is {0} to its neighbours", attitude); | |
} | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter an element number (1 - 20):"); | |
string input = Console.ReadLine(); | |
int element; | |
if (int.TryParse(input, out element)) | |
{ | |
element = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number."); | |
return; | |
} | |
int[] array = new int[] { 9, 8, 5, 20, 13, 7, 6, 18, 11, 19, 1, 3, 12, 2, 16, 15, 4, 10, 14, 17 }; | |
PrintElement(CheckElement(element, array)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_06_Find_First_Bigger_Element | |
{ | |
class FindFirstBiggerElement | |
{ | |
static int FindElement(int[] arr) | |
{ | |
int index = -1; | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
if (i == 0 && arr[i] > arr[i + 1]) | |
{ | |
index = arr[i]; | |
break; | |
} | |
else if (i > 0 && i < arr.Length - 1 && (arr[i] > arr[i + 1] && arr[i] > arr[i - 1])) | |
{ | |
index = arr[i]; | |
break; | |
} | |
else if (i == arr.Length - 1 && arr[i] > arr[i - 1]) | |
{ | |
index = arr[i]; | |
break; | |
} | |
} | |
return index; | |
} | |
static void Main(string[] args) | |
{ | |
int[] array = new int[] { 8, 8, 5, 20, 13, 7, 6, 18, 11, 19, 1, 3, 12, 2, 16, 15, 4, 10, 14, 17 }; | |
Console.Write("The first bigger element is: "); | |
Console.WriteLine(FindElement(array)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_07_Reverse_Number | |
{ | |
class ReverseNumber | |
{ | |
static int ReverseNum(int number) | |
{ | |
int reverse = 0; | |
int sign = number > -1 ? 1 : -1; | |
number = Math.Abs(number); | |
while (number > 0) | |
{ | |
reverse += (number % 10); | |
number /= 10; | |
reverse *= 10; | |
} | |
reverse /= 10 * sign; | |
return reverse; | |
} | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter a number: "); | |
string input = Console.ReadLine(); | |
int number; | |
if (int.TryParse(input, out number)) | |
{ | |
number = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid number;"); | |
return; | |
} | |
int reversedNumber = ReverseNum(number); | |
Console.WriteLine(reversedNumber); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
namespace Task_08_Add_Two_Numbers | |
{ | |
class AddTwoNumbers | |
{ | |
static void Main(string[] args) | |
{ | |
Stream buffer = Console.OpenStandardInput(10000); | |
Console.SetIn(new StreamReader(buffer, Console.InputEncoding, false, 10000)); | |
Console.Write("Enter the first number: "); | |
string firstInput = Console.ReadLine(); | |
Console.Write("Enter the second number: "); | |
string secondInput = Console.ReadLine(); | |
if (firstInput.Length < secondInput.Length) | |
{ | |
string swap = firstInput; | |
firstInput = secondInput; | |
secondInput = swap; | |
} | |
List<int> number1 = new List<int>(GetNumber(firstInput)); | |
List<int> number2 = new List<int>(GetNumber(secondInput)); | |
if (number1.Count != firstInput.Length || number2.Count != secondInput.Length) | |
{ | |
Console.WriteLine("Invalid number."); | |
return; | |
} | |
List<int> sumNumbers = SumNumbers(number1, number2); | |
sumNumbers.Reverse(); | |
Console.WriteLine("The sum is: "); | |
sumNumbers.ForEach(Console.Write); | |
Console.WriteLine(); | |
} | |
private static List<int> SumNumbers(List<int> number1, List<int> number2) | |
{ | |
int plusOne = 0; | |
List<int> result = new List<int>(); | |
for (int i = number1.Count - 1, j = number2.Count - 1; i > -1; i--, j--) | |
{ | |
int num1 = number1[i]; | |
int num2 = j > -1 ? number2[j] : 0; | |
int sum = num1 + num2 + plusOne; | |
result.Add(sum % 10); | |
plusOne = sum / 10; | |
} | |
if (plusOne == 1) | |
{ | |
result.Add(plusOne); | |
} | |
return result; | |
} | |
private static List<int> GetNumber(string strNumber) | |
{ | |
List<int> newList = new List<int>(); | |
for (int i = 0; i < strNumber.Length; i++) | |
{ | |
if ((strNumber[i] - 48) > -1 && (strNumber[i] - 48) < 10) | |
{ | |
int num = (strNumber[i] - 48); | |
newList.Add(num); | |
} | |
} | |
return newList; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Task_09_Find_Biggest_Element_And_Sort_Array | |
{ | |
class FindBiggestElementAndSortArray | |
{ | |
static void Main(string[] args) | |
{ | |
int[] array = new int[] { 9, 8, 5, 20, 13, 7, 6, 18, 11, 19, 1, 3, 12, 2, 16, 15, 4, 10, 14, 17 }; | |
Console.Write("Enter the starting position (0 - 19): "); | |
string input = Console.ReadLine(); | |
int startPosition; | |
if (int.TryParse(input, out startPosition)) | |
{ | |
startPosition = int.Parse(input); | |
if (startPosition < 0 || startPosition > 19) | |
{ | |
Console.WriteLine("Position out of range."); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
Console.Write("Enter the ending position (0 - 19): "); | |
input = Console.ReadLine(); | |
int endPosition; | |
if (int.TryParse(input, out endPosition)) | |
{ | |
endPosition = int.Parse(input); | |
if (startPosition < 0 || endPosition > 19) | |
{ | |
Console.WriteLine("Position out of range."); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
if (endPosition < startPosition) | |
{ | |
int swap = endPosition; | |
endPosition = startPosition; | |
startPosition = swap; | |
} | |
Console.Write("Choos sorting order. Enter 1 for intcreesing." + | |
"enter 2 for decreasing. "); | |
input = Console.ReadLine(); | |
int order; | |
if (int.Parse(input) == 1 || int.Parse(input) == 2) | |
{ | |
order = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
int biggestElement = FindBiggestElement(array, startPosition, endPosition); | |
Console.WriteLine("The biggest element in range {0} - {1} is {2}", | |
startPosition, endPosition, biggestElement); | |
PrintArray(order == 1 ? SortIncreasingArray(array) : SortDecreasingArray(array)); | |
} | |
private static int FindBiggestElement(int[] arr, int start, int end) | |
{ | |
int element = arr[start]; | |
for (int i = start; i < end; i++) | |
{ | |
element = Math.Max(element, arr[i]); | |
} | |
return element; | |
} | |
private static int[] SortDecreasingArray(int[] array) | |
{ | |
for (int i = 0; i < array.Length - 1; i++) | |
{ | |
for (int j = i + 1; j < array.Length; j++) | |
{ | |
if (array[i] < array[j]) | |
{ | |
int swap = array[i]; | |
array[i] = array[j]; | |
array[j] = swap; | |
} | |
} | |
} | |
return array; | |
} | |
private static int[] SortIncreasingArray(int[] array) | |
{ | |
for (int i = 0; i < array.Length - 1; i++) | |
{ | |
for (int j = i + 1; j < array.Length; j++) | |
{ | |
if (array[i] > array[j]) | |
{ | |
int swap = array[i]; | |
array[i] = array[j]; | |
array[j] = swap; | |
} | |
} | |
} | |
return array; | |
} | |
private static void PrintArray(int[] arr) | |
{ | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
Console.Write(arr[i]); | |
if (i < arr.Length - 1) | |
{ | |
Console.Write(", "); | |
} | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Numerics; | |
namespace Task_10_N_Factorial_From_1_To_100 | |
{ | |
class FactorialFromOneToHundred | |
{ | |
static void Main(string[] args) | |
{ | |
Stream buffer = Console.OpenStandardInput(1000); | |
Console.SetIn(new StreamReader(buffer, Console.InputEncoding, false, 1000)); | |
string input = Console.ReadLine(); | |
BigInteger number; | |
if (BigInteger.TryParse(input, out number)) | |
{ | |
number = BigInteger.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
PrintFactorial(number); | |
} | |
private static void PrintFactorial(BigInteger number) | |
{ | |
for (int i = 1; i < 100; i++) | |
{ | |
number *= i; | |
Console.WriteLine("Factorial n{0} is {1}", i, number); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Task_11_Few_Tasks | |
{ | |
class FewTasks | |
{ | |
static void Main(string[] args) | |
{ | |
System.Threading.Thread.CurrentThread.CurrentCulture = | |
System.Globalization.CultureInfo.InvariantCulture; | |
Console.WriteLine("Choose an operation."); | |
Console.WriteLine("Press 1 to reverse number."); | |
Console.WriteLine("Press 2 to calculate the average of given sequence"); | |
Console.WriteLine("Press 3 to solve the linear equation a * x + b = 0"); | |
string input = Console.ReadLine(); | |
int optionNumber; | |
if (int.TryParse(input, out optionNumber)) | |
{ | |
if (int.Parse(input) == 1 || int.Parse(input) == 2 || int.Parse(input) == 3) | |
{ | |
optionNumber = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
if (optionNumber == 1) | |
{ | |
PrintReversedNumber(); | |
} | |
else if (optionNumber == 2) | |
{ | |
GetAverageValue(); | |
} | |
else if (optionNumber == 3) | |
{ | |
SolveLinearEquation(); | |
} | |
} | |
private static void SolveLinearEquation() | |
{ | |
Console.Write("Enter number a (a != 0): "); | |
string input = Console.ReadLine(); | |
double numberA; | |
if (double.TryParse(input, out numberA)) | |
{ | |
numberA = double.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
if (numberA == 0) | |
{ | |
Console.WriteLine("Number a must not be 0."); | |
return; | |
} | |
Console.Write("Enter number b: "); | |
input = Console.ReadLine(); | |
double numberB; | |
if (double.TryParse(input, out numberB)) | |
{ | |
numberB = double.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
double numberX = (numberB * -1) / numberA; | |
Console.WriteLine("Number x is : {0:F3}", numberX); | |
} | |
private static void GetAverageValue() | |
{ | |
Console.WriteLine("Enter a sequence of numbers separate by single space."); | |
try | |
{ | |
double[] sequence = Console | |
.ReadLine() | |
.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(item => double.Parse(item)) | |
.ToArray(); | |
double sum = 0.0; | |
foreach (var num in sequence) | |
{ | |
sum += num; | |
} | |
Console.WriteLine("The avarage value of the sequence is: {0:F5}", | |
sum / sequence.Length); | |
} | |
catch | |
{ | |
Console.WriteLine("Invalid sequence input."); | |
return; | |
} | |
} | |
private static void PrintReversedNumber() | |
{ | |
Console.WriteLine("Enter a number to reverse."); | |
string input = Console.ReadLine(); | |
int number; | |
if (int.TryParse(input, out number)) | |
{ | |
number = int.Parse(input); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
int reversedNumber = RverseNumber(number); | |
Console.WriteLine("The reversed number is: {0}", reversedNumber); | |
} | |
private static int RverseNumber(int number) | |
{ | |
int reversedNumber = 0; | |
while (number > 0) | |
{ | |
reversedNumber += number % 10; | |
reversedNumber *= 10; | |
number /= 10; | |
} | |
reversedNumber /= 10; | |
return reversedNumber; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Task_12_Calculate_Polynom | |
{ | |
class CalculatePolynom | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.WriteLine("Enter first polynom's coefficients starting by the lowest,\n" + | |
"separate by space (only numbers). Enter 0 to miss a power."); | |
int[] firstPolynom = GetPolynom(); | |
Console.WriteLine("Enter second polynom's coefficients starting by the lowest,\n" + | |
"separate by space (only numbers). Enter 0 to miss a power."); | |
int[] secondPolynom = GetPolynom(); | |
int[] resultPolynom = new int[firstPolynom.Length + secondPolynom.Length + 1]; | |
resultPolynom = AddPolynoms(resultPolynom, firstPolynom, secondPolynom); | |
PrintPolynom(firstPolynom); | |
Console.WriteLine(" + "); | |
PrintPolynom(secondPolynom); | |
Console.WriteLine(" == "); | |
PrintPolynom(resultPolynom); | |
} | |
catch | |
{ | |
Console.WriteLine("ERROR!"); | |
return; | |
} | |
} | |
private static int[] GetPolynom() | |
{ | |
int[] polynom = Console | |
.ReadLine() | |
.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(item => int.Parse(item)) | |
.ToArray(); | |
return polynom; | |
} | |
private static void PrintPolynom(int[] polynom) | |
{ | |
for (int i = polynom.Length - 1; i > -1; i--) | |
{ | |
if (polynom[i] != 0) | |
{ | |
Console.Write(polynom[i] < 0 ? "-" : ""); | |
break; | |
} | |
} | |
for (int i = polynom.Length - 1; i > 0; i--) | |
{ | |
if (polynom[i] != 0) | |
{ | |
Console.Write("{0}x^{1} {2} ", Math.Abs(polynom[i]), i, | |
polynom[i - 1] != 0 ? polynom[i - 1] < 0 ? " - " : " + " : ""); | |
} | |
} | |
if (polynom[0] != 0) | |
{ | |
Console.WriteLine(Math.Abs(polynom[0])); | |
} | |
else | |
{ | |
Console.WriteLine(); | |
} | |
} | |
private static int[] AddPolynoms(int[] result, int[] polynom1, int[] polynom2) | |
{ | |
for (int i = 0; i < result.Length; i++) | |
{ | |
int num1 = i < polynom1.Length ? polynom1[i] : 0; | |
int num2 = i < polynom2.Length ? polynom2[i] : 0; | |
result[i] = num1 + num2; | |
} | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Task_13_Multiply_Polynoms | |
{ | |
class MultiplyPolynoms | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.WriteLine("Enter first polynom's coefficients starting by the lowest,\n" + | |
"separate by space (only numbers). Enter 0 to miss a power."); | |
int[] firstPolynom = GetPolynom(); | |
Console.WriteLine("Enter second polynom's coefficients starting by the lowest,\n" + | |
"separate by space (only numbers). Enter 0 to miss a power."); | |
int[] secondPolynom = GetPolynom(); | |
int[] resultPolynom = new int[firstPolynom.Length + secondPolynom.Length + 1]; | |
resultPolynom = MultiPolynoms(resultPolynom, firstPolynom, secondPolynom); | |
PrintPolynom(firstPolynom); | |
Console.WriteLine(" * "); | |
PrintPolynom(secondPolynom); | |
Console.WriteLine(" == "); | |
PrintPolynom(resultPolynom); | |
} | |
catch | |
{ | |
Console.WriteLine("Invalid input."); | |
return; | |
} | |
} | |
private static int[] GetPolynom() | |
{ | |
int[] polynom = Console | |
.ReadLine() | |
.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(item => int.Parse(item)) | |
.ToArray(); | |
return polynom; | |
} | |
private static void PrintPolynom(int[] polynom) | |
{ | |
for (int i = polynom.Length - 1; i > -1; i--) | |
{ | |
if (polynom[i] != 0) | |
{ | |
Console.Write(polynom[i] < 0 ? "-" : ""); | |
break; | |
} | |
} | |
for (int i = polynom.Length - 1; i > 0; i--) | |
{ | |
if (polynom[i] != 0) | |
{ | |
Console.Write("{0}x^{1} {2} ", Math.Abs(polynom[i]), i, | |
polynom[i - 1] != 0 ? polynom[i - 1] < 0 ? " - " : " + " : ""); | |
} | |
} | |
if (polynom[0] != 0) | |
{ | |
Console.WriteLine(Math.Abs(polynom[0])); | |
} | |
else | |
{ | |
Console.WriteLine(); | |
} | |
} | |
private static int[] MultiPolynoms(int[] result, int[] polynom1, int[] polynom2) | |
{ | |
for (int p1 = 0; p1 < polynom1.Length; p1++) | |
{ | |
for (int p2 = 0; p2 < polynom2.Length; p2++) | |
{ | |
result[p1 + p2] += polynom1[p1] * polynom2[p2]; | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment