diff --git a/cpp/coroutine/c1.cpp b/cpp/coroutine/c1.cpp index f3ed0e0..5c36f0a 100644 --- a/cpp/coroutine/c1.cpp +++ b/cpp/coroutine/c1.cpp @@ -71,7 +71,7 @@ struct example { value =v; return std::suspend_never{}; } - std::suspend_always final_suspend(){ + std::suspend_always final_suspend() noexcept{ cout<<"@ finale_suspend is called\n"; return std::suspend_always{}; } @@ -110,4 +110,4 @@ int main(){ cout<std::suspend_always{ + auto final_suspend() noexcept ->std::suspend_always{ return std::suspend_always{}; } void unhandled_exception(){ @@ -67,7 +67,7 @@ struct awaitble_obj { example await_routine(){ awaitble_obj a = awaitble_obj(); - for (int i=1;i<5;i++){ + for (int i=1;i<100;i++){ auto v = co_await a; co_yield v; } diff --git a/cpp/coroutine/c5.cpp b/cpp/coroutine/c5.cpp index eb0efd5..4e4e9e4 100644 --- a/cpp/coroutine/c5.cpp +++ b/cpp/coroutine/c5.cpp @@ -145,7 +145,7 @@ struct task { task get_return_object() { return {}; } std::suspend_never initial_suspend() { return {}; } - std::suspend_never final_suspend() { return {}; } + std::suspend_never final_suspend() noexcept { return {}; } void return_void() {} void unhandled_exception() {} }; @@ -186,4 +186,4 @@ task consumer() int main(){ auto c = consumer(); producer(); -} \ No newline at end of file +} diff --git a/cpp/pi.cpp b/cpp/pi.cpp index 2f58f39..cc923e2 100644 --- a/cpp/pi.cpp +++ b/cpp/pi.cpp @@ -1,132 +1,136 @@ -// this is a plain translation from pi.d +// this is a plain translation from pi.d // all rights reserved -# include -# include -# include -# include +// g++ pi.cpp -O3 -std=c++11 +#include +#include +#include +#include using namespace std; -const int LONG_TIME =4000; +const int LONG_TIME = 4000; vector p; vector t; int q; -int tiszero(){ +int tiszero() { int k; - for (k=0;k<=q;k++){ - if (t[k]!=0) return false; + for (k = 0; k <= q; k++) { + if (t[k] != 0) + return false; } return true; } -void mul4(){ - int i,c,d; - d=c=0; - for (i=q;i>=0;i--){ - d = (p[i]*4+c)%10; - c= (p[i]*4+c)/10; - p[i]= static_cast(d); +void mul4() { + int i, c, d; + d = c = 0; + for (i = q; i >= 0; i--) { + d = (p[i] * 4 + c) % 10; + c = (p[i] * 4 + c) / 10; + p[i] = static_cast(d); } } -void div4(){ - int i,c,d=0; - for( int i=0;i<=q;i++){ - c = (10*d +p[i])/4; - d= (10*d +p[i])%4; - p[i]= static_cast(c); +void div4() { + int i, c, d = 0; + for (int i = 0; i <= q; i++) { + c = (10 * d + p[i]) / 4; + d = (10 * d + p[i]) % 4; + p[i] = static_cast(c); } } -void div( int divisor){ - int i,b; - int quotient,remainder=0; - for ( auto & x:t){ - b= (10* remainder +x); - quotient = b/divisor; - remainder= b%divisor; - x= static_cast(quotient); +void div(int divisor) { + int i, b; + int quotient, remainder = 0; + for (auto &x : t) { + b = (10 * remainder + x); + quotient = b / divisor; + remainder = b % divisor; + x = static_cast(quotient); } } -void mul(int multiplier){ - int b,i,carry=0,digit=0; - for ( i=q;i>=0;i--){ - b= (t[i]*multiplier+carry); - digit=b%10; - carry= b/10; - t[i]= static_cast(digit); +void mul(int multiplier) { + int b, i, carry = 0, digit = 0; + for (i = q; i >= 0; i--) { + b = (t[i] * multiplier + carry); + digit = b % 10; + carry = b / 10; + t[i] = static_cast(digit); } } -void sub(){ +void sub() { int j; - for ( j=q;j>=0;j--){ - if (p[j]= 0; j--) { + if (p[j] < t[j]) { + p[j] -= t[j] - 10; + p[j - 1] -= 1; + } else { + p[j] -= t[j]; } } } -void add( ){ +void add() { int j; - for (j=q;j>=0;j--){ - if ( t[j]+p[j]>9){ - p[j]+=t[j]-10; - p[j-1]+=1; - }else { - p[j]+=t[j]; + for (j = q; j >= 0; j--) { + if (t[j] + p[j] > 9) { + p[j] += t[j] - 10; + p[j - 1] += 1; + } else { + p[j] += t[j]; } } } -void arctan(int s){ +void arctan(int s) { int n; - t[0]=1; + t[0] = 1; div(s); add(); - n=1; + n = 1; do { mul(n); - div(s*s); - div(n+=2); - if ( ((n-1)/2)%2==0){ + div(s * s); + div(n += 2); + if (((n - 1) / 2) % 2 == 0) { add(); - }else { + } else { sub(); } - }while (!tiszero()); + } while (!tiszero()); } - -int main(int argc,char * argv[]){ - if (argc==2){ - q= atoi(argv[1]); - }else { - cout<< "usage: pi [precision]\n"; +int main(int argc, char *argv[]) { + if (argc == 2) { + q = atoi(argv[1]); + } else { + cout << "usage: pi [precision]\n"; exit(55); } - if (q<0){ - cout<<" precision too low\n"; - q=3; - }else if (q>LONG_TIME){ - cout<<" be patient to wait a while \n"; + if (q < 0) { + cout << " precision too low\n"; + q = 3; + } else if (q > LONG_TIME) { + cout << " be patient to wait a while \n"; } q++; - p.resize(q+1); - t.resize(q+1); + p.resize(q + 1); + t.resize(q + 1); auto start_time = std::chrono::system_clock::now(); arctan(2); arctan(3); mul4(); - auto end_time = std::chrono::system_clock::now(); + auto end_time = std::chrono::system_clock::now(); q--; - cout<<"pi = "<< static_cast(p[0])<<"."; - for( int i=1;i<=q;i++){ - cout<(p[i]); + cout << "pi = " << static_cast(p[0]) << "."; + for (int i = 1; i <= q; i++) { + cout << static_cast(p[i]); } - cout<<"\n time is "<< std::chrono::duration_cast(end_time-start_time).count()<< "s\n"; + cout << "\n time is " + << std::chrono::duration_cast(end_time - + start_time) + .count() + << "s\n"; return 0; - -} \ No newline at end of file +} diff --git a/d/pi.d b/d/pi.d index bab7b9c..faac700 100644 --- a/d/pi.d +++ b/d/pi.d @@ -1,5 +1,6 @@ -// this is from example of dmd -// all rights reserved to dmd +// this is from example of dmd +// all rights reserved to dmd +// ldc2 pi.d import std.stdio; import std.conv; import core.stdc.stdlib; @@ -11,179 +12,154 @@ byte[] p; byte[] t; int q; -int main(string[] args) -{ - time_t startime, endtime; - int i; +int main(string[] args) { + time_t startime, endtime; + int i; - if (args.length == 2) - { - q = to!int(args[1]); - } - else - { - writeln("Usage: pi [precision]"); - exit(55); - } + if (args.length == 2) { + q = to !int(args[1]); + } else { + writeln("Usage: pi [precision]"); + exit(55); + } - if (q < 0) - { - writeln("Precision was too low, running with precision of 0."); - q = 0; - } + if (q < 0) { + writeln("Precision was too low, running with precision of 0."); + q = 0; + } - if (q > LONG_TIME) - { - writeln("Be prepared to wait a while..."); - } + if (q > LONG_TIME) { + writeln("Be prepared to wait a while..."); + } - // Compute one more digit than we display to compensate for rounding - q++; + // Compute one more digit than we display to compensate for rounding + q++; - p.length = q + 1; - t.length = q + 1; + p.length = q + 1; + t.length = q + 1; - /* compute pi */ - core.stdc.time.time(&startime); - arctan(2); - arctan(3); - mul4(); - core.stdc.time.time(&endtime); + /* compute pi */ + core.stdc.time.time(&startime); + arctan(2); + arctan(3); + mul4(); + core.stdc.time.time(&endtime); - // Return to the number of digits we want to display - q--; + // Return to the number of digits we want to display + q--; - /* print pi */ + /* print pi */ - writef("pi = %d.", cast(int) (p[0])); + writef("pi = %d.", cast(int)(p[0])); - for (i = 1; i <= q; i++) - writef("%d", cast(int) (p[i])); + for (i = 1; i <= q; i++) + writef("%d", cast(int)(p[i])); - writeln(); - writefln("%s seconds to compute pi with a precision of %s digits.", endtime - startime, q); + writeln(); + writefln("%s seconds to compute pi with a precision of %s digits.", + endtime - startime, q); - return 0; + return 0; } -void arctan(int s) -{ - int n; - - t[0] = 1; - div(s); /* t[] = 1/s */ - add(); - n = 1; - - do - { - mul(n); - div(s * s); - div(n += 2); - - if (((n - 1) / 2) % 2 == 0) - add(); - else - sub(); - } while (!tiszero()); +void arctan(int s) { + int n; + + t[0] = 1; + div(s); /* t[] = 1/s */ + add(); + n = 1; + + do { + mul(n); + div(s * s); + div(n += 2); + + if (((n - 1) / 2) % 2 == 0) + add(); + else + sub(); + } while (!tiszero()); } -void add() -{ - int j; - - for (j = q; j >= 0; j--) - { - if (t[j] + p[j] > 9) - { - p[j] += t[j] - 10; - p[j - 1] += 1; - } - else - p[j] += t[j]; - } +void add() { + int j; + + for (j = q; j >= 0; j--) { + if (t[j] + p[j] > 9) { + p[j] += t[j] - 10; + p[j - 1] += 1; + } else + p[j] += t[j]; + } } -void sub() -{ - int j; - - for (j = q; j >= 0; j--) - { - if (p[j] < t[j]) - { - p[j] -= t[j] - 10; - p[j - 1] -= 1; - } - else - p[j] -= t[j]; - } +void sub() { + int j; + for (j = q; j >= 0; j--) { + if (p[j] < t[j]) { + p[j] -= t[j] - 10; + p[j - 1] -= 1; + } else + p[j] -= t[j]; + } } -void mul(int multiplier) -{ - int b; - int i; - int carry = 0, digit = 0; - - for (i = q; i >= 0; i--) - { - b = (t[i] * multiplier + carry); - digit = b % 10; - carry = b / 10; - t[i] = cast(byte) digit; - } +void mul(int multiplier) { + int b; + int i; + int carry = 0, digit = 0; + + for (i = q; i >= 0; i--) { + b = (t[i] * multiplier + carry); + digit = b % 10; + carry = b / 10; + t[i] = cast(byte) digit; + } } /* t[] /= l */ -void div(int divisor) -{ - int i, b; - int quotient, remainder = 0; - - foreach (ref x; t) - { - b = (10 * remainder + x); - quotient = b / divisor; - remainder = b % divisor; - x = cast(byte) quotient; - } +void div(int divisor) { + int i, b; + int quotient, remainder = 0; + + foreach (ref x; t) { + b = (10 * remainder + x); + quotient = b / divisor; + remainder = b % divisor; + x = cast(byte) quotient; + } } -void div4() -{ - int i, c, d = 0; +void div4() { + int i, c, d = 0; - for (i = 0; i <= q; i++) - { - c = (10 * d + p[i]) / 4; - d = (10 * d + p[i]) % 4; - p[i] = cast(byte) c; - } + for (i = 0; i <= q; i++) { + c = (10 * d + p[i]) / 4; + d = (10 * d + p[i]) % 4; + p[i] = cast(byte) c; + } } -void mul4() -{ - int i, c, d; +void mul4() { + int i, c, d; - d = c = 0; + d = c = 0; - for (i = q; i >= 0; i--) - { - d = (p[i] * 4 + c) % 10; - c = (p[i] * 4 + c) / 10; - p[i] = cast(byte) d; - } + for (i = q; i >= 0; i--) { + d = (p[i] * 4 + c) % 10; + c = (p[i] * 4 + c) / 10; + p[i] = cast(byte) d; + } } -int tiszero() -{ - int k; +int tiszero() { + int k; - for (k = 0; k <= q; k++) - if (t[k] != 0) - return false; + for (k = 0; k <= q; k++) + if (t[k] != 0) + return false; - return true; + return true; } diff --git a/pdf/cpp/p080.cc b/pdf/cpp/p080.cc deleted file mode 100644 index aae6b48..0000000 --- a/pdf/cpp/p080.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include - -// gcc p080.cc -lmpfr -lgmp -lm -int main(){ - mpfr_t s; - mpfr_init2(s,400); - int ans =0; - for (int d=2;d<=100;d=d+1){ - int v= sqrt(d*1.0); - if (v*v==d) continue; - mpfr_set_d(s,d,MPFR_RNDD); - mpfr_sqrt(s,s,MPFR_RNDD); - //mpfr_out_str(stdout,10,0,s,MPFR_RNDD); - char *root2; - mpfr_exp_t e; - root2=mpfr_get_str(NULL,&e,10,0,s,MPFR_RNDD); - for(int i=0;i<100;i++) { - ans =ans + (root2[i]-'0'); - } - - } - printf("%d\n",ans); - mpfr_clear(s); -}