Welcome Message

「流行起於高分子,變化盡藏微宇宙」! 歡迎光臨「流變學好簡單 | The RheoMaster」部落格,成立於 2019.2.22,已於 2024 年初屆滿 5 年!旨在提供簡單的中文流變學知識,包括高分子流變學、輸送現象、高分子加工、流變量測等。您可至右方進行關鍵字搜尋,若有任何建議,請至文章留言或來信 yuhowen@gmail.com。 Welcome to "The RheoMaster" Blog. This website was established in Feb 2019, and has celebrated its 5th anniversary in eary 2024. In view of the lack of Chinese literature on rheology, here we offer basic knowledge relevant to polymer rheology, transport phenomena, polymer processing, rheometry, etc. If you have any suggestion, please leave a message on the post you are reading or email us at yuhowen@gmail.com.

精選文章

網誌作者近期國際期刊論文發表 (Recent SCI Journal Articles by the Blogger)

  Extensional Rheology of Linear and Branched Polymer Melts in Fast Converging Flows 線型、分支型高分子融體於高速收縮流之拉伸流變 Rheol. Acta 62 , 183–204 (2023)...

2019年12月24日

基本 C++ 程式碼整理 (Basic C++ Notes)

Revised: 2023/4/11

基本 C++ 程式碼整理如下


Reference: I Horton, Ivor Horton's Beginning Visual C++ 2013 (Wiley 2014).


Main Function

. #include <cstring>     // For strlen() ans strcpy()
. #include <iostream>  // Basic input and output library
. #include <utility>      // Define a set of templates for operator functions; For operator overload templates
. using namespace std::rel_ops;  // The templates are defined in the std::rel_ops namespace
. #include "CandyBox.h"            // For class CBox and class CCandyBox
. using namespace std;                // Any name in std namespace


Header File

. #pragma once  // Prevent the file contents from being included in the source code more than once 

. #include "Box.h"                           // In inherited class CCandyBox 

. #include "CandyBox.h"                // For CBox and CCandyBox

. class CCandyBox : public CBox  // In inherited class CCandyBox; private CBox or CBox

. explicit CBox(double side) : m_Length {side}, m_Width {side}, m_Height {side} {}  // Specific constructor

. #include <fstream>
      ofstream myfile;  // Write
      myfile.open ("example.txt");
      myfile << "Writing this to a file.\n";
      myfile.close()

. #include <fstream> 
  #include <string>
  string line;
  ifstream myfile("example.txt");  // Read
  while (getline(myfile, line))
  {
  cout << line << '\n';
  }
  myfile.close();

#include <iostream>
  int number;  // Or int number = int();
  cout << "Enter a number: ";
  cin >> number;
  cout << "The number is: " << number << endl;

#include <fstream>  // Output with file (file stream 的縮寫)
ofstream myfile;
ofstream myfile1;
myfile.open("data.txt");
myfile1.open("rate.txt");
myfile << x << endl;
myfile1 << y << endl;


Basic Commands
. log(x)           // Natural logarithm of x

. log10(x)       // Logarithm of x

. exp(5.0)       // = 148.413159

. abs(-1.0)      // = 1
  fabs(-1.0)     // = 1

. acos(-1.0)    // = pi

. sqrt(143.0)  // = 11.9583

. pow(5.0, 3)  // = 125

. \                  // Backslash as a line continuation character
      a = 1.0 + 2.0;
      or
      a = 1.0 + \
            2.0;

. logical operators: &&, ||, !

. system("pause")  // Pause

. if
       for ()
       {
           if ()
          {
              statement 1;
              statement 2;
          }
       }

      if (i > 7) x = 6;


      if (j % 2 == 0)  // j is even
          K2 = K2 + Q;
      else                  // j is odd
          K3 = K3 + Q;


      if (i == 0 || i == n)
          J1 = J1 + L; 
      else if (i % 2 == 0)
          J2 = J2 + L;
      else           
          J3 = J3 + L;

. for
      for (int i{0}; i <= n - 1; i++)  // or int i{}

. switch
      switch(dice)
      {
          case 1: cout << pstr << pstr1;
                      a = 1.0;
                      break;
          case 2: cout << pstr << pstr2;
                      break;
          case 3: cout << pstr << pstr3;
                      break;
          case 4: cout << pstr << pstr4;
                      break;
          case 5: cout << pstr << pstr5;
                      break;
          case 6: cout << pstr << pstr6;
                      break;       
          default: cout << "Sorry, you haven't got a lucky star.";

      }

. return
  (1) ok
        if (FP == 0.0 || ((b - a) / 2.0) < tol)
           return p;  // Output p; procedure completed successfully
  (2) ok
        if (FP == 0.0 || ((b - a) / 2.0) < tol)
        {
           return p;  // Output p; procedure completed successfully
        }  
  (3) ok
        if (FP == 0.0 || ((b - a) / 2.0) < tol)
       {
            cout << "Root: " << p << endl;
           return p;  // Output p; procedure completed successfully
       }
 (4) failed
       if (FP == 0.0 || ((b - a) / 2.0) < tol)
      {}
      return p;  // Output p; procedure completed successfully
 (5) failed
       if (FP == 0.0 || ((b - a) / 2.0) < tol)
           cout << "Root: " << p << endl;
          return p;  // Output p; procedure completed successfully

. to comment-out (multi-line comments)
   /*
   cout << "The message is disabled" << endl;
  */

. 變動的檔名
#include <iostream>
#include <fstream> // Output with file
#include <string>
using namespace std;

int main()
{
std::string HistoryFilename = "temperature (n=" + std::to_string(0.5) + ")";

ofstream myfile;
myfile.open(HistoryFilename + ".txt");

myfile << 123 << endl;

myfile.close();

return 0;
}

沒有留言:

張貼留言