Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace P___Ruffini
- {
- class Program
- {
- static char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- static char[] symbols = new char[] { '+', '-', '*', '/' };
- static void Main(string[] args)
- {
- Console.Title = "(P) - Ruffini ~ Programmato da Mario Calandra";
- Console.Write("Programma per trovare il valore P nel metodo di Ruffini. Ricorda di disporre le x in modo decrescente e con esponente minore di 10.\n\n");
- string str = null;
- while (true)
- {
- int esp = 0;
- int num = 0;
- int result = 0;
- int value_x = 0;
- int indexLastChar = 0;
- int number_x = 0;
- int counter_x = 0;
- int loop = 0;
- char lastOperator = ' ';
- Console.Write("Scrivi l'equazione: ");
- str = Console.ReadLine();
- str = str.Replace(" ", "");
- for (int i = 0, j = str.Length; i < j; i++)
- {
- if (str[i] == 'x') number_x++;
- }
- counter_x = number_x;
- if (counter_x == 0)
- {
- Console.WriteLine("Quest'equazione non contiene variabili.");
- continue;
- }
- for (int i = 0, j = str.Length; i < j; i++)
- {
- if (indexLastChar == -1) break;
- if (str[i] == 'x' && lastOperator == ' ')
- {
- if (str[i + 1] == '^')
- {
- num = GetNumberFromIndex(str, indexLastChar);
- esp = Convert.ToInt32(Char.ToString(str[i + 2]));
- result = num * PowerNum(value_x, esp);
- // if (GetNumberFromIndex(str, indexLastChar) == 0) result = PowerNum(value_x, esp);
- Console.WriteLine("Numero prima di X: " + num);
- counter_x--;
- foreach (char k in symbols) // 1x^3-1x^1-24
- {
- if ((i + 3) < j && str[(i + 3)] == k)
- {
- // Console.WriteLine("Foreach eseguito.");
- lastOperator = k;
- if ((i + 4) < j) indexLastChar = i + 4;
- else indexLastChar = -1;
- break;
- }
- }
- }
- else
- {
- Console.WriteLine("Si è verificato un errore nella forma.");
- break;
- }
- }
- else if (str[i] == 'x')
- {
- if (str[i + 1] == '^')
- {
- esp = Convert.ToInt32(Char.ToString(str[i + 2]));
- switch (lastOperator)
- {
- case '+':
- result += GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
- break;
- case '-':
- result -= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
- break;
- case '*':
- result *= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
- break;
- case '/':
- result /= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
- break;
- }
- counter_x--;
- foreach (char k in symbols)
- {
- if ((i + 3) < j && str[(i + 3)] == k)
- {
- lastOperator = k;
- if ((i + 4) < j) indexLastChar = i + 4;
- else indexLastChar = -1;
- break;
- }
- }
- }
- else
- {
- Console.WriteLine("Si è verificato un errore nella forma.");
- break;
- }
- }
- else if (counter_x == 0)
- {
- // Console.WriteLine("Risultato primario: " + result);
- int lastNumber = GetNumberFromIndex(str, indexLastChar);
- switch (lastOperator)
- {
- case '+':
- result += lastNumber;
- break;
- case '-':
- result -= lastNumber;
- break;
- case '*':
- result *= lastNumber;
- break;
- case '/':
- result /= lastNumber;
- break;
- }
- // Console.WriteLine("Informazioni\n\nLastNumber: " + lastNumber + "\nLastOperator: " + lastOperator + "\nIndexLastChar: " + indexLastChar);
- lastOperator = ' ';
- indexLastChar = 0;
- if (result == 0) break;
- else
- {
- value_x++;
- counter_x = number_x;
- i = 0;
- loop++;
- if (loop == 25)
- {
- Console.WriteLine("Impossibile risolvere l'equazione con il metodo di Ruffini.");
- break;
- }
- }
- }
- }
- if (loop < 25) Console.WriteLine("Il valore di P è " + value_x);
- }
- }
- static int GetNumberFromIndex(string expression, int index)
- {
- string output = null;
- string end = null;
- int i = index;
- int j = expression.Length;
- int gotNumber = 0;
- bool control = true;
- while (i < j)
- {
- foreach (char k in symbols)
- {
- if (expression[i] == k || expression[i] == 'x') control = false;
- }
- if (!control) break;
- foreach (char k in numbers)
- {
- if (expression[i] == k)
- {
- output = String.Format("{0}", k);
- end += output;
- }
- }
- i++;
- }
- gotNumber = Convert.ToInt32(end);
- if (end == null) gotNumber = 1; //
- return gotNumber;
- }
- static int PowerNum(int number, int pow)
- {
- int result = 1;
- for (int i = 0; i < pow; i++)
- {
- result *= number;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement