Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cliarray.cpp - arrays in the .NET Common Language Runtime
- #using <mscorlib.dll>
- #using <System.Numerics.dll>
- using namespace System;
- using System::Numerics::BigInteger;
- namespace {
- __declspec(noreturn) void Die(String^ fmt, ...array<Object^>^ a)
- {
- Console::Error->Write(L"{0}: error: ",
- IO::Path::GetFileNameWithoutExtension(
- Environment::GetCommandLineArgs()[0]));
- Console::Error->WriteLine(fmt, a);
- Environment::Exit(1);
- }
- int ParseOrDie(String^ val)
- {
- try {
- auto n = int::Parse(val);
- if (n < 0) Die(L"{0} is negative", n);
- return n;
- }
- catch (FormatException^) {
- Die(L"\"{0}\" isn't an integer", val);
- }
- catch (OverflowException^) {
- Die(L"\"{0}\" looks way too big (or small)", val);
- }
- }
- array<BigInteger>^ GetFactorials(int n)
- {
- if (n < 0) return gcnew array<BigInteger> {};
- auto a = gcnew array<BigInteger> (n + 1);
- a[0] = 1;
- for (int i {1}; i != a->Length; ++i) a[i] = a[i - 1] * i;
- return a;
- }
- }
- int main(array<String^>^ args)
- {
- if (args->Length != 1) {
- Die(L"too {0} arguments (1 needed, {1} passed)",
- args->Length == 0 ? L"few" : L"many", args->Length);
- }
- auto a = GetFactorials(ParseOrDie(args[0]));
- for (int i {0}; i != a->Length; ++i)
- Console::WriteLine(L"{0}! = {1}", i, a[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement