Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace P___Ruffini
  8. {
  9.     class Program
  10.     {
  11.         static char[] numbers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  12.         static char[] symbols = new char[] { '+', '-', '*', '/' };
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Title = "(P) - Ruffini ~ Programmato da Mario Calandra";
  17.             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");
  18.             string str = null;
  19.             while (true)
  20.             {
  21.                 int esp = 0;
  22.                 int num = 0;
  23.                 int result = 0;
  24.                 int value_x = 0;
  25.                 int indexLastChar = 0;
  26.                 int number_x = 0;
  27.                 int counter_x = 0;
  28.                 int loop = 0;
  29.                 char lastOperator = ' ';
  30.                 Console.Write("Scrivi l'equazione: ");
  31.                 str = Console.ReadLine();
  32.                 str = str.Replace(" ", "");
  33.                 for (int i = 0, j = str.Length; i < j; i++)
  34.                 {
  35.                     if (str[i] == 'x') number_x++;
  36.                 }
  37.                 counter_x = number_x;
  38.                 if (counter_x == 0)
  39.                 {
  40.                     Console.WriteLine("Quest'equazione non contiene variabili.");
  41.                     continue;
  42.                 }
  43.                 for (int i = 0, j = str.Length; i < j; i++)
  44.                 {
  45.                     if (indexLastChar == -1) break;
  46.                     if (str[i] == 'x' && lastOperator == ' ')
  47.                     {
  48.                         if (str[i + 1] == '^')
  49.                         {
  50.                             num = GetNumberFromIndex(str, indexLastChar);
  51.                             esp = Convert.ToInt32(Char.ToString(str[i + 2]));
  52.                             result = num * PowerNum(value_x, esp);
  53.                             // if (GetNumberFromIndex(str, indexLastChar) == 0) result = PowerNum(value_x, esp);
  54.                             Console.WriteLine("Numero prima di X: " + num);
  55.                             counter_x--;
  56.                             foreach (char k in symbols) // 1x^3-1x^1-24
  57.                             {
  58.                                 if ((i + 3) < j && str[(i + 3)] == k)
  59.                                 {
  60.                                     // Console.WriteLine("Foreach eseguito.");
  61.                                     lastOperator = k;
  62.                                     if ((i + 4) < j) indexLastChar = i + 4;
  63.                                     else indexLastChar = -1;
  64.                                     break;
  65.                                 }
  66.                             }
  67.                         }
  68.                         else
  69.                         {
  70.                             Console.WriteLine("Si è verificato un errore nella forma.");
  71.                             break;
  72.                         }
  73.                     }
  74.                     else if (str[i] == 'x')
  75.                     {
  76.                         if (str[i + 1] == '^')
  77.                         {
  78.                             esp = Convert.ToInt32(Char.ToString(str[i + 2]));
  79.                             switch (lastOperator)
  80.                             {
  81.                                 case '+':
  82.                                     result += GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
  83.                                     break;
  84.                                 case '-':
  85.                                     result -= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
  86.                                     break;
  87.                                 case '*':
  88.                                     result *= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
  89.                                     break;
  90.                                 case '/':
  91.                                     result /= GetNumberFromIndex(str, indexLastChar) * PowerNum(value_x, esp);
  92.                                     break;
  93.                             }
  94.                             counter_x--;
  95.                             foreach (char k in symbols)
  96.                             {
  97.                                 if ((i + 3) < j && str[(i + 3)] == k)
  98.                                 {
  99.                                     lastOperator = k;
  100.                                     if ((i + 4) < j) indexLastChar = i + 4;
  101.                                     else indexLastChar = -1;
  102.                                     break;
  103.                                 }
  104.                             }
  105.                         }
  106.                         else
  107.                         {
  108.                             Console.WriteLine("Si è verificato un errore nella forma.");
  109.                             break;
  110.                         }
  111.                     }
  112.                     else if (counter_x == 0)
  113.                     {
  114.                         // Console.WriteLine("Risultato primario: " + result);
  115.                         int lastNumber = GetNumberFromIndex(str, indexLastChar);
  116.                         switch (lastOperator)
  117.                         {
  118.                             case '+':
  119.                                 result += lastNumber;
  120.                                 break;
  121.                             case '-':
  122.                                 result -= lastNumber;
  123.                                 break;
  124.                             case '*':
  125.                                 result *= lastNumber;
  126.                                 break;
  127.                             case '/':
  128.                                 result /= lastNumber;
  129.                                 break;
  130.                         }
  131.                         // Console.WriteLine("Informazioni\n\nLastNumber: " + lastNumber + "\nLastOperator: " + lastOperator + "\nIndexLastChar: " + indexLastChar);
  132.                         lastOperator = ' ';
  133.                         indexLastChar = 0;
  134.                         if (result == 0) break;
  135.                         else
  136.                         {
  137.                             value_x++;
  138.                             counter_x = number_x;
  139.                             i = 0;
  140.                             loop++;
  141.                             if (loop == 25)
  142.                             {
  143.                                 Console.WriteLine("Impossibile risolvere l'equazione con il metodo di Ruffini.");
  144.                                 break;
  145.                             }
  146.                         }
  147.                     }
  148.                 }
  149.                 if (loop < 25) Console.WriteLine("Il valore di P è " + value_x);
  150.             }
  151.         }
  152.  
  153.         static int GetNumberFromIndex(string expression, int index)
  154.         {
  155.             string output = null;
  156.             string end = null;
  157.             int i = index;
  158.             int j = expression.Length;
  159.             int gotNumber = 0;
  160.             bool control = true;
  161.             while (i < j)
  162.             {
  163.                 foreach (char k in symbols)
  164.                 {
  165.                     if (expression[i] == k || expression[i] == 'x') control = false;
  166.                 }
  167.                 if (!control) break;
  168.                 foreach (char k in numbers)
  169.                 {
  170.                     if (expression[i] == k)
  171.                     {
  172.                         output = String.Format("{0}", k);
  173.                         end += output;
  174.                     }
  175.                 }
  176.                 i++;
  177.             }
  178.             gotNumber = Convert.ToInt32(end);
  179.             if (end == null) gotNumber = 1; //
  180.             return gotNumber;
  181.         }
  182.  
  183.         static int PowerNum(int number, int pow)
  184.         {
  185.             int result = 1;
  186.             for (int i = 0; i < pow; i++)
  187.             {
  188.                 result *= number;
  189.             }
  190.             return result;
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement