計算機程式 2020 fall
===
###### tags: `計算機程式`
[TOC]
## WEEK 1
### 1-2 DEC->BIN
``` cpp=1
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num = 0; //param for DEC
int bin_counter = 0; //param to count
int bin[100]={0}; //array to save BIN value
cin >> num; //input number
if (num == 0) //deal with number if it's zero
bin_counter = 1;
else
{
for(bin_counter = 0 ; num > 0 ; bin_counter++ ) //break if num greater than zero , it means it can't be devided by 2 anymore
{
bin[bin_counter] = num % 2; //get r
num /= 2; // num = num/2;
}
}
for (int j = bin_counter - 1; j >= 0; j--) //print bin value inversely
cout << bin[j] << " ";
system("paunse");
return 0;
}
```
>[name=逸男]
## WEEK 2
### 2-1 Calendar
```cpp=1
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void){
int month, date, output=0;
int days[] = {31, 29 ,31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cout << "Please input month and date" << endl << "month: ";
cin >> month;
cout << "date: ";
cin >> date;
if( month >= 1 && month <= 12 && date > 0 && date <= days[month-1] ){
for(int i=0; i < month-1; i++){
output += days[i];
}
output += date;
cout << "output: " << output;
}else{
cout << "Your input is wrong. haha";
}
return 0;
}
```
>[name=D暉]
### 2-2 Sum
```cpp=1
# include <iostream>
# include <cstdlib>
using namespace std;
void Sum(long long int &a, long long int &b); // declare the function with reference arguments
int main(void){
int a, b;
cin >> a >> b;
Sum(a, b); // call the function
cout << a << endl;
system("pause");
return 0;
}
void Sum(long long int &a, long long int &b){
a = a + b; // save the result to a
}
```
>[name=吉他之神]
### 2-3 Sort
```cpp=1
# include <iostream>
using namespace std;
void Sort(int &a, int &b){
if ( b > a ){
int tmp = a ;
a = b;
b = a;
}
}
int main(void){
int a, b;
cin >> a >> b;
Sort(a, b); // call the function
cout << a << " " << b << endl;
}
```
>[name=大神]
## WEEK 3
### 3-1 randomly choose 4 different numbers
```cpp=1
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
# define NUMBER_OF_DIFFFERENT_DIGITS 4 // can be customized.
int main(void){
srand(time(NULL)); // set the random seed.
int check[10] = {0}; // check whether the digit is exist. 1 means exist, 0 means not.
int number;
for (int i = 0; i < NUMBER_OF_DIFFFERENT_DIGITS; i++){
if (i == 0){ // first digit can not be zero.
number = rand() % 9 + 1; // number within 1~9.
}
else{
number = rand() % 10; // number within 0~9.
while (check[number] != 0){ // while number is exist then randomly generate again.
number = rand() % 10;
}
}
check[number] = 1; // mark the number to prevent using again.
cout << number;
}
cout << "\n";
return 0;
}
```
>[name=吉他之神]
## WEEK 4
### 4-3 encoding
``` C++=1
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
string str;
int num;
cin >> str;
cin >> num;
for (int i=0 ;i<str.length();i++)
cout <<(char)(str[i] + num);
}
```
``` cpp=1
# include <iostream>
# include <cmath>
using namespace std;
void print_log(int num){
cout << log10(num) << endl; // call log10 function in <cmath> library
}
int main(void){
int num;
cin >> num;
print_log(num); // call self-defined function
return 0;
}
```
>[name=神宗翰]
### 4-2 power
```cpp=1
#include <iostream>
float pow(const double a,int b){
float data=1;
for(int i = 0;i<b;i++){
data*=a;
}
return data;
}
int pow(const int a,int b){
int data=1;
for(int i = 0;i<b;i++){
data*=a;
}
return data;
}
using namespace std;
int main(){
int a, c;
float b;
cout << "輸入正整數:";
cin >> a ;
cout << "輸入浮點數:";
cin >> b;
cout << "輸入指數:";
cin >> c;
cout<<pow(a,c)<<endl;
cout<<pow(b,c);
}
```
>[name=大哥翰]
### 4-3 Array and Random
```cpp=1
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
void find_max(int arr[3], int* Max){
for (int i = 0; i < 3; i++){
if (*Max < *(arr+i)){
*Max = *(arr+i);
}
}
}
int main(void){
int arr[3], Max=0;
srand(time(NULL));
for(auto offset = begin(arr); offset !=end(arr); offset++){
*offset = rand()%10 + 1;
cout << *offset << " ";
}
cout <<endl;
find_max(arr, &Max);
cout << Max << endl;
return 0;
}
```
>[name=QQ龍的小夫]
## week 5
``` C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
string str[3] ;
int bill_line , flag;
for(int i = 0 ;i<3;i++)
{
getline(cin,str[i]);
}
for(bill_line = 0 ;bill_line<3;bill_line++)
{
flag = str[bill_line].find("BILL");
if (flag != -1 ){
break;
}
}
cout << bill_line+1 << endl;
cout << flag <<endl;
}
```
>[name=NCRL狗]
## Midterm
### week1_rectangle
```cpp=1
# include <iostream>
using namespace std;
int main(void){
int length = 0, width = 0;
cout << "length: " << endl;
cin >> length;
cout << "width: " << endl;
cin >> width;
for (int i=0; i < length; i++)
{
if (i == 0 || i == (length-1))
{
for (int j=0; j < width; j++) cout << "*";
cout << endl;
}
else
{
for (int j=0; j < width; j++)
{
if (j == 0 || j == (width-1))
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
return 0;
}
```
### week1_scorePassFailed
```cpp=1
# include <iostream>
using namespace std;
typedef struct classes{
int pass;
int failed;
classes()
{
pass = 0;
failed = 0;
}
}classes;
int main(void){
int num = 5;
int score[num];
classes myClass;
while (num--)
{
cin >> score[num];
if (score[num] >= 60)
{
myClass.pass++;
}else
myClass.failed++;
}
cout << "pass: " << myClass.pass << endl;
cout << "failed: " << myClass.failed;
}
```
### week1_Armstrong
```cpp=1
# include <iostream>
# include <cmath>
using namespace std;
int count_digit(int num){
if (num == 0) return 1;
int digit = 0;
while(num){
digit++;
num /= 10;
}
return digit;
}
int Armstrong(int num, int digit){
int arr[digit], ans = 0;
for (int i = 0; i < digit; i++){
arr[i] = num % 10;
num /= 10;
}
for (int i = 0; i < digit; i++){
ans += pow(arr[i], digit);
}
return ans;
}
int main(void){
int low, high, exist = 0;
cin >> low >> high;
for (int i = low; i < high; i++){
int digit = count_digit(i);
if (Armstrong(i, digit) == i) {
cout << i << " ";
exist = 1;
}
}
if (exist == 0) cout << "none";
cout << endl;
return 0;
}
```
### Week1 Simple substitution cipher
```cpp=1
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
int main(){
char map_array[27];
char key[26];
char plain[50];
int flag = 0;
int n = 0;
cin>>key;
strcpy (map_array,key);
for(int i = strlen(map_array) ; i < 26 ; i++){
do{
flag = 0;
for(int j = 0 ; j<i ; j++ ){
if(('a'+ n) == map_array[j]){
n++;
flag = 1;
break;
}
}
}while(flag == 1);
map_array[i] = 'a'+n;
}
map_array[26] = '\0';
cout <<map_array<<" "<<strlen(map_array)<<endl;
cin >> plain;
for(int i = 0 ;i<strlen(plain);i++){
plain[i] = map_array[plain[i]-'a'];
}
cout << plain;
}
```
<!--
### week2_triangle
```cpp=1
# include <iostream>
using namespace std;
int main(void){
int width = 0;
cout << "width: " << endl;
cin >> width;
for (int i=0; i <= (width-1)/2; i++)
{
for (int j=0; j < (width-1)/2 - i; j++)
{
cout << " ";
}
for (int j=0; j < 1 + 2*i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
```
### week2_student score
```cpp=1
#include <iostream>
#include <cmath>
using namespace std;
typedef struct student{
string id;
double score = 0;
}student;
int main(void){
int num = 5;
student students[num];
for (int i=0; i < num; i++)
{
cin >> students[i].id >> students[i].score;
students[num].score = pow(students[num].score, 0.5)*10;
}
for (int i=0; i < num; i++)
{
cout << "id: " <<students[i].id << "\t score: " << students[i].score << endl;
}
return 0;
}
```
### Week2 bouble sort with student score
```c++=1
#include <iostream>
using namespace std;
typedef struct student{
int student_id = 0;
int score = 0;
}student;
void swap(student &a,student &b){
student tmp ;
tmp = a;
a = b;
b = tmp;
}
int main()
{
int student_num = 0;
cin>>student_num;
student class_student[20];
for(int i = 0 ; i<student_num ; i++){
cin >> class_student[i].student_id;
cin >> class_student[i].score;
}
for(int i=student_num ; i>=0 ; i--){
for(int j = 0 ; j<i-1 ; j++){
if(class_student[j].score>class_student[j+1].score)
swap(class_student[j+1],class_student[j]);
}
}
for (int i = 0 ; i<student_num; i ++){
cout << class_student[i].student_id<<" ";
}
return 0;
}
```
-->
## final
```cpp=1
# include <iostream>
using namespace std;
void fun(int b){
int a = 8;
a++;
b--;
}
int main(void){
int a = 0;
a += 1;
a *= 2;
--a;
fun(a);
cout << a << endl; // a = ?
return 0;
}
```
```cpp=1
# include <iostream>
using namespace std;
int sum(int num1, int num2){
return num1 + num2;
}
float sum(float num1, float num2){
return num1 + num2;
}
int main(void){
int a = 1, b = 2, ans1;
float c = 1.1, d = 2.2, ans2;
ans1 = sum(a, b);
ans2 = sum(c, d);
cout << ans1 << endl; // ans1 = ?
cout << ans2 << endl; // ans2 = ?
return 0;
}
```
```cpp=1
# include <iostream>
using namespace std;
int sum(float num1, float num2){
return num1 + num2;
}
float sum(float num1, float num2){
return num1 + num2;
}
int main(void){
int a = 1, b = 2, ans1;
float c = 1.1, d = 2.2, ans2;
ans1 = sum(a, b);
ans2 = sum(c, d);
cout << ans1 << endl; // ans1 = ?
cout << ans2 << endl; // ans2 = ?
return 0;
}
```

```cpp=1
# include <iostream>
# include <cstdlib>
using namespace std;
int main(void)
{
int a = 5, b = 10;
int *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &b;
*ptr1 = 7;
*ptr2 = 32;
a = 17;
ptr1 = ptr2;
*ptr1 = 9;
ptr1 = &a;
a = 64;
*ptr2 = *ptr1 + 5;
ptr2 = &a;
system("pause");
return 0;
}
```
## week 12
```cpp = 1
#include <iostream>
#include <math.h>
using namespace std;
class shape
{
public:
shape(string Obj, int Num_edges): obj(Obj), num_edges(Num_edges)
{
cout << "create a " << obj << endl;
edges = new double[num_edges];
}
void set_edges(double* Edges)
{
edges = Edges;
}
double* get_edges() const
{
return edges;
}
int get_num_edges() const
{
return num_edges;
}
virtual double area() const = 0;
~shape()
{
delete [] edges;
}
private:
string obj;
int num_edges;
double* edges;
};
class triangle: public shape
{
public:
triangle(double* Edges): shape("triangle", 3)
{
set_edges(Edges);
}
double area() const
{
double* ptr = get_edges();
int num_edges = get_num_edges();
return ptr[0]*ptr[1]/2;
}
};
class circle: public shape
{
public:
circle(double Edge): shape("circle", 1)
{
set_edges(&Edge);
set_radius(Edge);
}
void set_radius(double edge)
{
radius = edge / (2 * M_PI);
}
double area() const
{
return radius*radius*M_PI;
}
private:
double radius;
};
int main()
{
double tt_edges[] = {30.0, 40.0, 50.0};
triangle tt(tt_edges);
cout << tt.area() << endl;
double cc_edge = 2*M_PI;
circle cc(cc_edge);
cout << cc.area() << endl;
return 0;
}
```
{"metaMigratedAt":"2023-06-15T13:44:36.031Z","metaMigratedFrom":"Content","title":"計算機程式 2020 fall","breaks":true,"contributors":"[{\"id\":\"bf7833a3-c42d-44c3-b7b8-d5f491dc5a19\",\"add\":3397,\"del\":46},{\"id\":\"08738f89-e5bf-43c6-9ce1-a28798c06fbd\",\"add\":5691,\"del\":1841},{\"id\":\"47601b27-13e9-4cff-ab9f-353000759668\",\"add\":7546,\"del\":1727},{\"id\":\"4120d7ce-d442-477c-8fea-1100480d3776\",\"add\":4,\"del\":2}]"}