iT邦幫忙

2022 iThome 鐵人賽

DAY 22
0
Software Development

C++超級菜鳥也可以懂的物件導向攻略系列 第 22

Day 22 C++ 繼承Inheritance 刷題練習、如何顯示現在時間與Crypto雜談

  • 分享至 

  • xImage
  •  

前言:
暨之前聊到繼承,今天來實作一下Hackerank上的兩個題目
Inheritance IntroductionInheritance
然後之後開始準備進入Crypto bot,文章後面有些簡單介紹。

進入第一個題目Inheritance Introduction,這是簡單介紹版本的繼承,主要是method的操作。
程式碼如下:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


class Triangle{
    public:
    	void triangle(){
     		cout<<"I am a triangle\n";
    	}
};

class Isosceles : public Triangle{
    public:
    	void isosceles(){
    		cout<<"I am an isosceles triangle\n";
    	}
  		//Write your code here.
          void description(){
              cout << "In an isosceles triangle two sides are equal" << endl;
          }
        
};

int main(){
    Isosceles isc;
    isc.isosceles();
  	isc.description();
    isc.triangle();
    return 0;
}

第二題Inheritance使用到constructor和char function。

#include <iostream>
#include <vector>

using namespace std;


class Person{
	protected:
		string firstName;
		string lastName;
		int id;
	public:
		Person(string firstName, string lastName, int identification){
			this->firstName = firstName;
			this->lastName = lastName;
			this->id = identification;
		}
		void printPerson(){
			cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n"; 
		}
	
};

class Student :  public Person{
	private:
		vector<int> testScores;  
	public:
        /*	
        *   Class Constructor
        *   
        *   Parameters:
        *   firstName - A string denoting the Person's first name.
        *   lastName - A string denoting the Person's last name.
        *   id - An integer denoting the Person's ID number.
        *   scores - An array of integers denoting the Person's test scores.
        */
        // Write your constructor here
        Student(string fname, string lname, int id, vector<int> score): Person(fname, lname, id){
            this->testScores = score;
        }
        /*	
        *   Function Name: calculate
        *   Return: A character denoting the grade.
        */
        // Write your function here
        char calculate(){
            int sum;
            for(int i = 0; i < textScores.size();i++){
                sum= sum +testScores[i];            
            }
        int average = sum / testScores.size();
        if(average >= 90 && average <=100){
            return 'O';
        }else if(80<=average && average<90){
            return 'E';
        }else if(70<=average && average<80){
            return 'A';
        }else if(55<=average && average<70){
            return 'P';
        }else if(40<=average && average<55){
            return 'D';
         }else {
            return 'T';
        } 
    }
};

int main() {
	string firstName;
  	string lastName;
	int id;
  	int numScores;
	cin >> firstName >> lastName >> id >> numScores;
  	vector<int> scores;
  	for(int i = 0; i < numScores; i++){
	  	int tmpScore;
	  	cin >> tmpScore;
		scores.push_back(tmpScore);
	}
	Student* s = new Student(firstName, lastName, id, scores);
	s->printPerson();
	cout << "Grade: " << s->calculate() << "\n";
	return 0;
}


關於crypto的交易有鑒於小的也是剛入門,在這邊引用一下基本的一些交易術語:

Definitions
Bid: An order listed on the ‘buy’ side of the order book.
Ask: An order listed on the ‘sell’ side of the order book.
Bid/Ask Spread: The difference between the highest bid and the lowest ask on the order book.
Bid/Ask Depth: Represents the cumulative volume of buy and sell orders at a set price. The bid depth is the cumulative volume of current ‘buy’ orders at the price or higher. The ask depth is the cumulative volume of ‘sell’ orders at that price or lower.
引用自Coin Rivet


順便附上覺得挺有趣的一個小功能 - 現在時間
有空來把這個小功能加上去。

#include <iostream>
#include <chrono>
#include <ctime>    

int main()
{
    auto start = std::chrono::system_clock::now();
    // Some computation here
    auto end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s"
              << std::endl;
}

引用自stackOverflow連結與Hackerank


上一篇
Day21 C++ 循序搜尋Linear Search 和二元搜尋法Binary Search
下一篇
Day23 C++虛擬貨幣Crypto Bot menu與功能雜談
系列文
C++超級菜鳥也可以懂的物件導向攻略30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言