Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. // cliarray.cpp - arrays in the .NET Common Language Runtime
  2.  
  3. #using <mscorlib.dll>
  4. #using <System.Numerics.dll>
  5.  
  6. using namespace System;
  7. using System::Numerics::BigInteger;
  8.  
  9. namespace {
  10.     __declspec(noreturn) void Die(String^ fmt, ...array<Object^>^ a)
  11.     {
  12.         Console::Error->Write(L"{0}: error: ",
  13.             IO::Path::GetFileNameWithoutExtension(
  14.                 Environment::GetCommandLineArgs()[0]));
  15.         Console::Error->WriteLine(fmt, a);
  16.         Environment::Exit(1);
  17.     }
  18.  
  19.     int ParseOrDie(String^ val)
  20.     {
  21.         try {
  22.             auto n = int::Parse(val);
  23.             if (n < 0) Die(L"{0} is negative", n);
  24.             return n;
  25.         }
  26.         catch (FormatException^) {
  27.             Die(L"\"{0}\" isn't an integer", val);
  28.         }
  29.         catch (OverflowException^) {
  30.             Die(L"\"{0}\" looks way too big (or small)", val);
  31.         }
  32.     }
  33.  
  34.     array<BigInteger>^ GetFactorials(int n)
  35.     {
  36.         if (n < 0) return gcnew array<BigInteger> {};
  37.         auto a = gcnew array<BigInteger> (n + 1);
  38.         a[0] = 1;
  39.         for (int i {1}; i != a->Length; ++i) a[i] = a[i - 1] * i;
  40.         return a;
  41.     }
  42. }
  43.  
  44. int main(array<String^>^ args)
  45. {
  46.     if (args->Length != 1) {
  47.         Die(L"too {0} arguments (1 needed, {1} passed)",
  48.             args->Length == 0 ? L"few" : L"many", args->Length);
  49.     }
  50.  
  51.     auto a = GetFactorials(ParseOrDie(args[0]));
  52.     for (int i {0}; i != a->Length; ++i)
  53.         Console::WriteLine(L"{0}! = {1}", i, a[i]);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement