简单来说,Clang是一个编译器,目前用来编译C、C++、Objective-C语言
。
where clang
D:\Swift\Toolchains\5.10.0+Asserts\usr\bin\clang.exe
clang -v
clang version 16.0.0
Target: x86_64-unknown-windows-msvc
Thread model: posix
计算 斐波那契数列(Fibonacci sequence),
mkdir testc ; cd testc ; 编写 fib.cpp 如下
// Fibonacci Series using Space Optimized Method
//#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
using namespace std;int fib(int n)
{int a = 0, b = 1, c, i;if (n == 0)return a;for (i = 2; i <= n; i++) {c = a + b;a = b;b = c;}return b;
}// Fibonacci
int main(int argc,char *argv[])
{if (argc > 1){int n = atoi(argv[1]);if (n >0) cout << fib(n) << std::endl;} else {cout << " usage: fib.exe n ";}return 0;
}
编译 clang fib.cpp -o fib.exe
运行 fib.exe 80
-285007387 这个结果溢出了
计算 斐波那契数列(Fibonacci sequence),不受长整型位数限制。
从 https://github.com/rgroshanrg/bigint 下载 bigint-main.zip 解压到当前目录
copy D:\Swift\testc\bigint-main\Big Integer Library in Single file\bigint.h to D:\Swift\testc\
编写 fibonacci.cpp 如下
// Fibonacci Series using Space Optimized Method
//#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
#include "bigint.h"
using namespace std;bigint fib(int n)
{bigint a(0), b(1), c(0);int i;if (n == 0)return a;for (i = 2; i <= n; i++) {c = a + b;a = b;b = c;}return b;
}// Fibonacci Series test
int main(int argc,char *argv[])
{if (argc > 1){int n = atoi(argv[1]);if (n >0) cout << fib(n) << std::endl;} else {cout << " usage: fibonacci.exe n ";}return 0;
}
编译 clang fibonacci.cpp -o fibonacci.exe
运行 fibonacci.exe 1024
4506699633677819813104383235728886049367860596218604830803023149600030645708721396248792609141030396244873266580345011219530209367425581019871067646094200262285202346655868899711089246778413354004103631553925405243
where g++
D:\Strawberry\c\bin\g++.exe
编译 g++ fib.cpp -o fib.exe
也是可以的。
参阅:BigInt (BIG INTEGERS) in C++ with Example - GeeksforGeeks