C++程序设计第四章笔记
/*
c++程序设计第四章笔记
1.数学函数
头文件为<cmath>
三角函数:
sin();cos();tan();asin();acos();atan()
指数函数:
exp(x)--e^x
log(x)--ln(x)
log10(x)--lg(x)
pow(a,b)--a^b
sprt(x)--x的平方根
近似函数(double):
ceil()向上取整
floor()向下取整
min,max,abs(绝对值):
GUN c++中,这些函数定义在<cstdlib>
vs定义在<algorithm>
2.字符数据类型和操作符
char代表一个单独的字符,"为字符串",'字符'
ASCII码:
计算机最底层使用的二进制数字
从键盘读去一个字符
特殊字符的转义序列
\b回退,\t制表,\n换行,\f换页,\r回车,\\反斜线,\"双引号
数值类型和字符类型之间的相互转换
字符可以转数字,但数字转字符,只能低8位可以使用
static_cast<char>(value)
比较和测试字符
可以使用关系运算符
3.字符函数:
头文件<cctype>
isdigit
isalpha
isalnum
islower
isupper
issapce,判断是否为空白字符
tolower
toupper
4.字符串类型:
string实际上是一个对象,定义在头文件<string>
字符串索引和下标操作符:
索引范围0~length-1
s.at(index)重写某一字符或是stringName[index]
连接字符串
+;但直接连接两个字符串是非法的,必须和变量相链接
比较字符串
==;!=,<=,>=,>,<,按照字典顺序
读字符串
getline(cin,stringName,delimitCharacter),可以读取一个字符串
5.格式化控制台输出
流操作;包含在<iomanip>头文件
setprecision(n) 浮点数的精度
fixed 显示指定小数位数的浮点数
showpoint 即使没有小数部分也显示以0补足的小数点后位数
setw(width) 指定打印字段的宽度
left 调整输出到左边
right 右边
6.简单的文件输入输出
写入文件
ifstream input;
input.open("number.txt");
input >> sc1 >> sc2 >> sc3;
input.close;
读取文件
ofstream output;
output.open("number.txt");
output << 95 << " " << 56 << " " << 34 << endl;
output.close;
*/
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string as = "fjasgdj";
cout << as[0];
//setprecision(n)的用法
double amount = 12628.68;
double interestRate = 21.234;
double interest = amount * interestRate;
cout << "interest is " << fixed << setprecision(2) <<
interest << endl;
//showpoint
cout << setprecision(6);
cout << 1.23 << endl;
cout << showpoint << 1.23 << endl;
cout << showpoint << 12.3 << endl;
//setw(width) 指定打印字段的宽度
cout << setw(8) << "C++" << setw(6) << "101" << endl;
cout << setw(8) << "Java" << setw(6) << "101" << endl;
cout << setw(8) << "Python" << setw(6) << "101" << endl;
//left 调整输出到左边;right 右边
cout << left;
cout << setw(8) << "C++" << setw(6) << "101" << endl;
cout << setw(8) << "Java" << setw(6) << "101" << endl;
}
内容较多,知识较为基础