Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.Write("Fakultätberechner von Jakob Horak\n");
- string val, val0;
- bool finish;
- do
- {
- do
- {
- Console.Write("\nGeben sie bitte den Wert ein, von dem\ndie Fakultät berechnet werden soll: ");
- } while (Read(out val));
- val0 = val;
- if (val == "0")
- Console.Write("Die Fakultät von 0 beträt 1\n");
- else
- {
- Console.WriteLine("Berechnen...");
- StringFactorialref(ref val);
- Console.Write("Die Fakultät von {0} beträt {1}\n", val0, val);
- }
- Console.Write("Wollen Sie das Programm wiederholen? (Y/N): ");
- } while (Console.ReadLine().ToUpper() == "Y");
- }
- static bool Read(out string value)
- {
- int temp;
- if (!int.TryParse(Console.ReadLine(), out temp) || temp < 0)
- {
- Console.WriteLine("Ungültige Eingabe!");
- value = "0";
- return true;
- }
- else
- value = temp.ToString();
- return false;
- }
- static void StringFactorialref(ref string value)
- {
- string temp = "1";
- for (string i = "1"; StringGoE(value, i); i = StringAdd(i, "1"))
- {
- temp = StringMulti(temp, i.ToString());
- }
- value = temp;
- }
- //static string StringFactorial(string value)
- //{
- // if (value == "0")
- // return "1";
- // else
- // return StringMulti(StringFactorial(StringDecrease(value)), value);
- //}
- static string StringDecrease(string value)
- {
- char[] arr = value.ToCharArray();
- Array.Reverse(arr);
- for (int i = arr.Length - 1; i >= 0; i--)
- {
- if (arr[i] == '0')
- continue;
- else
- {
- arr[i] = Convert.ToChar(Convert.ToString(Convert.ToInt32(arr[i].ToString()) - 1));
- break;
- }
- }
- return new string(arr);
- }
- static string StringAdd(string a, string b)
- {
- char[] arra = a.ToCharArray(), arrb = b.ToCharArray(), temp, output;
- int i, tempint;
- int[] intarr;
- string stringout;
- Array.Reverse(arra);
- Array.Reverse(arrb);
- if (arra.Length < arrb.Length)
- {
- temp = arra;
- arra = arrb;
- arrb = temp;
- }
- if (arra.Length != arrb.Length)
- {
- temp = new char[arra.Length];
- for (i = 0; i < arrb.Length; i++)
- temp[i] = arrb[i];
- arrb = temp;
- }
- intarr = new int[arra.Length + 1];
- for (i = 0; i < arra.Length; i++)
- {
- int.TryParse(arra[i].ToString(), out tempint);
- intarr[i] = tempint;
- int.TryParse(arrb[i].ToString(), out tempint);
- intarr[i] += tempint;
- }
- bool carry = false;
- output = new char[intarr.Length];
- for (i = 0; i < intarr.Length; i++)
- {
- if (carry)
- intarr[i] += 1;
- carry = intarr[i] >= 10;
- output[i] = Convert.ToChar(Convert.ToString(intarr[i] % 10));
- }
- Array.Reverse(output);
- stringout = new string(output);
- while (stringout[0] == '0')
- stringout = stringout.Substring(1);
- return stringout;
- }
- static string StringMulti(string a, string b)
- {
- string temp = "0";
- for (string i = "1"; StringGoE(b, i); i = StringAdd(i, "1"))
- {
- temp = StringAdd(temp, a);
- }
- return temp;
- }
- static bool StringGoE(string a, string b)
- {
- while (a[0] == '0')
- a = a.Substring(1);
- while (b[0] == '0')
- b = b.Substring(1);
- if (a.Length > b.Length)
- return true;
- else if (a.Length < b.Length)
- return false;
- else
- {
- for (int i = a.Length - 1; i >= 0; i--)
- {
- if (Convert.ToInt32(a[i]) > Convert.ToInt32(b[i]))
- return true;
- if (Convert.ToInt32(a[i]) < Convert.ToInt32(b[i]))
- return false;
- }
- return true;
- }
- }
- //static string StringAdd(string[] arr)
- //{
- // string temp = StringAdd(arr[0], arr[1]);
- // for (int i = 2; i < arr.Length; i++)
- // {
- // temp = StringAdd(temp, arr[i]);
- // }
- // return temp;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement