iT邦幫忙

0

資料人員來學C++ #隨堂筆記 Day1

前情提要: 本人從事數據處理的工作大約四年之久,主要的語言為R、SQL and Python,身為數據程式人這幾年這麼競爭,不時還是會上線上課程,近來的課程剛好有C++的需求,註冊一門課程直接上下去。
以下是課程的筆記,本人非程式本科出身主要會使用我寫別程式語言的邏輯來說明。
一、基本格式
1.include就像是 R 裡面的library 、python 裡面的 import ,iostream就是套件了。
2.有一個要注意的 帶入套件的時候 可以使用 <套件> 和 "套件",<>會直接去抓標準套件,而""會先抓你本地端的套件。
3.int main 就是主要的寫法去執行程式,裡面就是輸入要執行的內容,cout就c out 的意思打印出後面的文字"Hello world, I am ready for C++"。

 #include <iostream>

 int main() 
{
     std::cout << "Hello world, I am ready for C++";
     return 0;
 }

二、文字註解
總共有兩種
1.單行 // 只能一行
2.多行 /* 好幾行
第二行*/

三、using namespace
主要就是在指定一些功能的時候要選擇要使用的模塊類似 std::cout 中 前面的 std,但是如果你前面加了using namespace std; 代表之後的程式碼只要打count,前面不需要再加std::。
老師解說他有時會這樣使用有時候不會,有時候你的project太大可能很複雜就比較不會用using namespace 的方式,因地制宜。

四、變數
1.const 不能修改
前面如果加了const不能後面再寫入其他的值。
const int weightGoal = 100;
weightGoal = 200;
2.輸出
可以使用文字加上變數輸出,A為變數。
PS \n 換行
\t 按下一次tab鍵一樣功能
cout<<"int size = "<< A <<"\n";

五、IO(輸入、輸出)
使用 fstream library

這裡他只有稍微簡單介紹一下,大概就是ifstream(in)和ofstream(out)的兩種功能。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  //create an output stream to write to the file
  //append the new lines to the end of the file
  ofstream myfileI ("input.txt", ios::app);
  if (myfileI.is_open())
  {
      myfileI << "\nI am adding a line.\n";
      myfileI << "I am adding another line.\n";
      myfileI.close();
  }
  else cout << "Unable to open file for writing";

  //create an input stream to read the file
  ifstream myfileO ("input.txt");
  //During the creation of ifstream, the file is opened. 
  //So we do not have explicitly open the file. 
  if (myfileO.is_open())
  {
      while ( getline (myfileO,line) )
      {
          cout << line << '\n';
      }
      myfileO.close();
  }

  else cout << "Unable to open file for reading";

  return 0;
}

六、Header Files
把一些設定放在Header Files裡面,再把他include進來,
例如 #include ""main.hpp"

七、cin和input.txt
可以使用cin和input.txt來作為參數的輸入
PS 其中cin 中間不能有空白,有空白就要使用getline。

/*This program accepts inputs from the input.txt file*/ 

#include <iostream>
#include <string>


int main()
{
   std::string userName; 
   std::cout<<"Tell me your nickname?: ";
   std::getline(std::cin, userName);
   std::cout<<"Hello "<<userName<<"\n";
   return 0;
}

八、資料格式轉換
使用sstream,然後用stringstream轉成inches
#include
std::getline (std::cin,stringLength);
std::stringstream(stringLength) >> inches;
std::cout<<"You entered "<<inches<<"\n";

九、最後介紹了資料格式

main.cpp - the main program
main.hpp - the header file
mainFunctions.cpp - a file for any functions

input.txt - for user inputs. Delete the first line of the file,
otherwise the line will be seen as an input.


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言