Revised: 2023/4/11
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
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
. 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
. 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;
. \ // Backslash as a line continuation character
a = 1.0 + 2.0;
or
a = 1.0 + \
2.0;
. logical operators: &&, ||, !
. system("pause") // Pause
. if
for ()
. 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;
*/
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;
}
沒有留言:
張貼留言