# 師大資工 2022 Fall Comuter Programming(I) HW02 --- [TOC] ## HW0201 >分成兩個函數分開處理, --- ![](https://i.imgur.com/9Kole3t.png) ![](https://i.imgur.com/3firLQI.png) ```c=1 #include<stdio.h> #include<stdint.h> double leibniz(int32_t n){ double ret = 0; for(int32_t i=1; i<=n; i++){ if(i%2 == 1){ ret += 4.0/(2*i-1); }else{ ret -= 4.0/(2*i-1); } } return ret; } double nila(int32_t n){ double ret = 3; if(n == 1){ return ret; } for(int32_t i = 2; i<=n; i++){ if(i%2 == 1){ ret -= 4.0/((2*i-2)*(2*i-1)*2*i); }else{ ret += 4.0/((2*i-2)*(2*i-1)*2*i); } } return ret; } double d(double n){ int32_t pi = 3.14159265358979323846; if(n > pi){ return n - pi; }else{ return pi - n; } } int main(){ uint16_t n = 0; printf("Please enter n(16-bits unsigned): "); scanf("%hd", &n); for(int32_t i=1; i<=n; i++){ printf("n = %d:\n", i); printf(" Grgory-Leibniz series: %lf (%lf)\n", leibniz(i), d(leibniz(i))); printf(" Nilakantha series: %lf (%lf)\n\n", nila(i), d(nila(i))); } } ``` ## HW0202 ![](https://i.imgur.com/zXDyIFa.png) ```c=1 #include<stdio.h> #include<stdint.h> #include<stdbool.h> int32_t m = 0, d = 0, y = 0; int32_t sy = 0, sm = 0, sd = 0; int32_t ey = 0, em = 0, ed = 0; void reverse(int32_t yr){ int32_t y1 = yr/1000; int32_t y2 = yr%1000/100; int32_t y3 = yr%100/10; int32_t y4 = yr%10; m = 10*y4 + y3; d = 10*y2 + y1; y = yr; } bool legal(int32_t y, int32_t m, int32_t d){ bool ret = true; if(y == 9220){ return true; } if(m < 1 || m > 12 || d < 1){ ret = false; }else{ if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){ if(d > 31){ ret = false; } }else if(m == 4 || m == 6 || m == 9 || m == 11){ if(d > 30){ ret = false; } }else{ if(d > 28){ ret = false; } } } return ret; } int main(){ printf("Start date: "); scanf("%d-%d-%d", &sm, &sd, &sy); printf("End date: "); scanf("%d-%d-%d", &em, &ed, &ey); bool sge = true, leg = true; if(!legal(sy, sm, sd) || !legal(ey, em, ed)) leg = false; if(sy > ey){ sge = false; }else{ if(sm > em){ sge = false; }else{ if(sd > ed){ sge = false; } } } if(!sge || !leg){ printf("error input"); }else{ int32_t counter = 0; for(int32_t i=sy; i<=ey; i++){ reverse(i); if(legal(y, m, d)){ printf("%02d-%02d-%04d\n", m, d, y); counter++; } } printf("%d\n", counter); } } ``` ## HW0203 ![](https://i.imgur.com/6GpAprd.png) ```c=1 #include<stdio.h> #include<stdint.h> #include<math.h> int main(){ uint32_t n = 0; printf("Please enter a number: "); scanf("%d", &n); int32_t sum = 0, ex = 0, t = 0; if(n < 10){ sum = n; }else{ while(n >= 10){ ex = 0; sum = 0; while((int32_t)(n / pow(10.0, ex)) != 0){ ex++; } ex--; for(; ex >= 0; ex--){ int32_t t = (int32_t)(pow(10.0, ex)); if(t != 1){ printf("%d + ", n/t); }else{ printf("%d ", n/t); } sum += n / t; n = n % t; } printf("= %d", sum); printf("\n"); n = sum; } } printf("Final: %d", sum); } ``` ## HW0204 ![](https://i.imgur.com/ylLiLcv.png) ![](https://i.imgur.com/G20Te0c.png) ```c=1 #include<stdio.h> #include<stdint.h> #include<math.h> double round(double n){ int t = 0; n *= 100; n += 0.5; t = (int32_t)n; return t/100.0; } int32_t decimal(double n){ int32_t ret = 0; ret = (int32_t)(n *100); return ret%100; } void printcomma(int n){ if(n < 1000){ printf("%d", n); return; } printcomma(n/1000); printf(",%03d", n%1000); } int main(){ int32_t ly = 0, lm = 0, lt = 0, pt = 0, i = 0, ap = 0; double r = 0, p = 0; printf("Loan Date: "); scanf("%d.%d", &ly, &lm); printf("Loan Price: "); scanf("%lf", &p); printf("Loan Term (yrs): "); scanf("%d", &lt); printf("Interest Rate: "); scanf("%lf", &r); printf("Property Tax: "); scanf("%d", &pt); printf("Insurance (/m): "); scanf("%d", &i); printf("Additional Payment (/m): "); scanf("%d", &ap); printf("--- Output ---\n"); if(p < 10000 || r < 0){ printf("error input"); } r = r / 1200.0; int32_t n = 0, total = 0; n = 12 * lt; total = (int32_t)(p * (r * pow(1+r, n))/(pow(1+r, n)-1) + pt + i); printf("Total monthly payment: %d\n", total); double interest = 0, principal = 0, remaining = 0; for(int32_t ctr = 0; ctr < n; ctr++){ interest = p * r; principal = total - (pt + i) - p*r + ap; remaining = p - principal; if(lm > 12){ lm = 1; ly += 1; } if(ctr == n-1){ principal = p * 100; principal = principal / 100.0; remaining = 0; } int32_t d_p = 0, d_i = 0, d_r = 0; printf("%04d,%02d) ", ly, lm); printf("Principal: "); d_p = decimal(principal); printcomma((int32_t)principal); printf(".%02d, ", d_p); printf("Interest: "); interest = round(interest); d_i = decimal(interest); printcomma((int32_t)interest); printf(".%02d, ", d_i); printf("Remaining: "); remaining = round(remaining); d_r = decimal(remaining); printcomma((int32_t)remaining); printf(".%02d\n", d_r); p = remaining; lm++; } } ``` ## HW0205 ![](https://i.imgur.com/3j8EB3B.png) ![](https://i.imgur.com/u4uDMYq.png) ![](https://i.imgur.com/5iy03XT.png) ![](https://i.imgur.com/ix3fbyP.png) ```c=1 #include<stdio.h> #include<stdint.h> #include<stdbool.h> int32_t l = 0, w = 0, h = 0, m = 0; void paloc(int32_t amount){ int32_t blank_counter = w-1, c_counter = 0; for(int32_t i = 0; i<amount; i++){ //first layer for(int32_t fl = 0; fl < blank_counter; fl++){ printf(" "); } for(int32_t sharp = 0; sharp < l*2; sharp++){ printf("#"); } printf(" "); } //next layer blank_counter -= 1; printf("\n"); // top layer bool h_end = false; int32_t dc = 0; while(blank_counter > 0){ for(int32_t i = 0; i<amount; i++){ for(int32_t ctr = 0; ctr < blank_counter; ctr++){ //print blank part printf(" "); } printf("#"); //first sharp for(int32_t ctr = 0; ctr < 2*l-2; ctr++){ //first color a printf("\033[47m \033[m"); } printf("#"); //second sharp for(int32_t ctr = 0; ctr < c_counter; ctr++){ //second color c printf("\033[41m \033[m"); } printf("# ");//next cuboid if(h_end){ for(int32_t ctr=0; ctr < dc-2; ctr++){ printf(" "); } } } blank_counter--; if(c_counter < h-2){ c_counter++; }else if(c_counter < h){ h_end = true; } printf("\n"); dc++; } //top side for(int32_t i = 0; i<amount; i++){ for(int32_t sharp = 0; sharp < l*2; sharp++){ printf("#"); } for(int32_t ctr = 0; ctr < c_counter; ctr++){ printf("\033[41m \033[m"); } printf("# "); if(w > h){ for(int32_t ctr = 0; ctr < dc-2 ; ctr++){ printf(" "); } } } printf("\n"); //front side if(w >= h){ c_counter--; } while(c_counter >= 0){ for(int32_t ctr = 0; ctr < amount; ctr++){ printf("#"); for(int32_t ctr = 0; ctr < 2*l-2; ctr++){ printf("\033[44m \033[m"); } printf("#"); for(int32_t ctr = 0; ctr < c_counter; ctr++){ printf("\033[41m \033[m"); } printf("#");//boarden for(int32_t ctr = 0; ctr < w-2-c_counter; ctr++){ printf(" "); } printf(" "); } printf("\n"); c_counter--; } //bottom side for(int32_t ctr = 0; ctr < amount; ctr++){ for(int32_t bl = 0; bl < l*2; bl++){ printf("#"); } for(int32_t bc = 0; bc < w-c_counter-1; bc++){ printf(" "); } } printf("\n\n");//end. } int main(){ printf("Welcome to Cuboid Super Infinity Exporter\n"); printf("Please enter Length, Width, and Height of the cuboid\n"); printf("Length: "); scanf("%d", &l); printf("Width: "); scanf("%d", &w); printf("Height: "); scanf("%d", &h); printf("How many cuboids do you want to generate?\n"); printf("Amount: "); scanf("%d", &m); //the space that a cuboid need is w+2*l-1. //and it needs one space between two cuboids. int32_t len = w + 2*l - 1; if(len > 80 || w < 4 || l < 4 || h < 4 || m < 0){ printf("error input"); } else{ int32_t m_pl = 0; if(w + 2*l != 81){ m_pl = 80/(len+1); //amount per line }else{ m_pl = 81/(len); //amount per line } if(m_pl == 0){ m_pl = 1; } int32_t lines = m/m_pl; //lines int32_t less = m%m_pl; //less for(int i = 0; i < lines; i++){ paloc(m_pl); } if(less != 0){ paloc(less); } } } ``` > [name=5xooooo] > [time=Sat, Oct 29, 2022 10:15 AM]