c++学习笔记(1)
常用的c++库:
#include <iostream> // 输入输出
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
#include <string> // 字符串函数
#include <iomanip> // 格式化输出
#include <fstream> // 文件读写操作
#include <cmath> // 数学函数相关的函数
字符串读取:
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a char: ";
cin >> ch;
cout << "You input " << ch << endl;
cout << "He said \"program is interetsing\"." << endl;
int c = static_cast<int>(ch); //类型转换
cout << "num is " << c << endl;
return 0;
}
字符大小写转换:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a lowercase char: ";
char lowercase;
cin >> lowercase;
char uppercase = static_cast<char>('A' + lowercase - 'a'); //原理 'A'-'a'='C'-'c'
cout << "The corresponding letter is " << uppercase << endl;
return 0;
}
生成一个随机字母:
生成固定范围内的整数: 公式 a+rand()%(b-a+1) ----> (a, a+b)之间的随机数
#include <iostream>
#include <cstdlib> // 随机函数
int main()
{
int a = static_cast<int>('a');
int b = static_cast<int>('z');
srand(time(0));
char random_char = a + rand() % (b-a+1); //公式 a+rand()%(b-a+1) ----> (a, a+b)之间的随数
cout << "The random char is " << random_char << endl;
return 0;
}
随机在两个字符之间生成一个字符(ASCII 码 America standard code for information interchange)
#include <iostream>
#include <cstdlib> // 随机函数
int main()
{
cout << "input a char: ";
char ch1;
cin >> ch1;
cout << "Input a char: ";
char ch2;
cin >> ch2;
int a = static_cast<int>(ch1); // 类型转换
int b = static_cast<int>(ch2);
if(a<b)
{
char random_char = a + rand() % (b - a + 1);
cout << "The random char between " << ch1 << " and " << ch2 << " is " << random_char << endl;
}
else if(a>b)
{
char random_char = b + rand() % (a - b + 1);
cout << "The random char between " << ch2 << " and " << ch1 << " is " << random_char << endl;
}
else
{
cout << "You Inpit two same char!" << endl;
}
return 0;
}
猜生日code:
简单原理:
0~31之间的数字,可以用二进制11111表示,可以看到没一张表中的第一个数字分别是1,2,4,8,16,用二进制可以表示为
00001, 00010, 00100, 01000, 10000。五张表中的数字分布是有一定的规律的。0-31之间的任何一个数字都可以由前面
五个数字组成,将能够组成生日的数字所在的表中填入即可,直接判断自己的生日在不在表中,将得到的表第一个数字相加集
即可得到正确的数字。
#include <iostream>
using namespace std;
int main()
{
int day = 0;
char answer;
cout << "Is your birthday in set1" << endl;
cout << "1 3 5 7\n" <<
"9 11 13 15\n" <<
"17 19 21 23\n" <<
"25 27 29 31\n" << endl;
cout << "Enter your answer(y/n): ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
{
day += 1;
}
cout << "\nIs your answer in set2: " << endl;
cout << "2 3 6 7\n" <<
"10 11 14 15\n" <<
"18 19 22 23\n" <<
"26 27 30 31\n" << endl;
cout << "ENter your answer(y/n): ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
{
day += 2;
}
cout << "\nIs your answer in set3: " << endl;
cout << "4 5 6 7\n" <<
"12 13 14 15\n" <<
"20 21 22 23\n" <<
"28 29 30 31\n" << endl;
cout << "ENter your answer(y/n): ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
{
day += 4;
}
cout << "\nIs your answer in set4: " << endl;
cout << "8 9 10 11\n" <<
"12 13 14 15\n" <<
"24 25 26 27\n" <<
"28 29 30 31\n" << endl;
cout << "ENter your answer(y/n): ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
{
day += 8;
}
cout << "\nIs your answer in set5: " << endl;
cout << "16 17 18 19\n" <<
"20 21 22 23\n" <<
"24 25 26 27\n" <<
"28 29 30 31\n" << endl;
cout << "ENter your answer(y/n): ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
{
day += 16;
}
cout << "Your birth day is " << day << endl;
return 0;
}
大小写转换(cctype 里的函数)
如果输入是大写,则转换为小写,输入小写,转换为大写, 如果不是字母给出提示;
字符常用函数:
isdigit()
isalpha()
isalnum() : 是否为数字或者字母
islower(): 是否为小写
isupper()
isspace() : 是否是空白字符
tolower()
toupper()
#include <iostream>
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
using namespace std;
int main()
{
cout << "Enter a character: ";
char ch;
cin >> ch;
cout << "You Entered " << ch << endl;
if(islower(ch))
{
char temp;
cout << "It is a lowercase." << endl;
temp = static_cast<char>(toupper(ch));
cout << "The equilent is " << temp << endl;
}
else if(isupper(ch))
{
char temp;
cout << "It is a uppercase" << endl;
temp = static_cast<char>(tolower(ch));
cout << "The equilent is " << temp << endl;
}
else
{
cout << "It is not a alpha" << endl;
}
return 0;
}
字符串操作:
常用字符串函数入下:
length(), size() : 字符串长度
at(index): 字符串索引
#include <iostream>
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
#include <string> // 字符串函数
using namespace std;
int main()
{
string message = "Hello python";
string str1 = "ABCDEF";
cout << "string len is " << str1.length() << endl;
cout << "C is at " << str1.at(2) << endl;
return 0;
}
彩票:
生成两位的随机数与输入的两位数比较:
完全相同
次序相同
只有一个数字相同
涉及到:(1)随机数字生成, 字符串比较
#include <iostream>
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
#include <string> // 字符串函数
using namespace std;
int main()
{
// 彩票代码
string lottery; // string类型的变量可以直接用+连接
srand(time(0)); // time(0)返回格林尼治时间起经过的秒数,作为随机数的种子
int digit = rand() % 10; // 生成一位随机整数
lottery += static_cast<char>(digit + '0');
digit = rand() % 10;
lottery += static_cast<char>(digit + '0'); // 两个随机整数链接为字符串
cout << "Enter a guess(two numbers): ";
string guess;
cin >> guess;
if(lottery == guess)
{
cout << "You win $10 000" << endl;
}
else if(lottery[0]==guess[1] && lottery[1]==guess[0])
{
cout << "You win $3000" << endl;
}
else if(lottery[0]==guess[0] || lottery[0]==guess[1] || lottery[1]==guess[0]
|| lottery[1]==guess[1])
{
cout << "You win $1000" << endl;
}
else
{
cout << "You loss" << endl;
}
return 0;
}
格式化输出:
常用的格式化输出函数
setprecision(n)
fixed()
showpoint()
setw(width)
left() 左对齐
right() 右对齐
#include <iostream>
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
#include <string> // 字符串函数
#include <iomanip> // 格式化输出
using namespace std;
int main()
{
double amount = 3342.3456;
double rate = 0.0087;
double interest = amount * rate;
cout << "Interest is " << fixed << setprecision(3) <<interest << endl;
return 0;
}
文件读写:
写文件: ofstream output ;
读文件: ifstream input ;
程序从文件中读取数据相当于input,写入数据到文件中相当于output
#include <iostream>
#include <cstdlib> // 随机函数
#include <cctype> // 字符函数
#include <string> // 字符串函数
#include <iomanip> // 格式化输出
#include <fstream> // 文件读写操作
#include <cmath> // 数学函数相关
using namespace std;
int main()
{
//文件写操作
ofstream output;
output.open("numbers.txt"); // 创建文件
output << 23 << " " << 90 << " " << 56 << endl;
//output << 98 << " " << "hello" << " " << 98 <<endl;
output.close();
// 读取文件
ifstream input;
// 用Input对象的open()函数打开文件
input.open("numbers.txt");
int score1, score2, score3;
input >> score1;
input >> score2;
input >> score3;
cout << "Read from file: \n" << "score1== " << score1 << endl;
cout << "score2== " << score2 << endl;
cout << "score3== " << score3 << endl;
input.close();
return 0;
}