1.项目实现C++打印风格
#include 头文件
#include<Windows.h> 窗口
//实现打印需求:
//1.网站 404 攻击
//2.网站篡改攻击
//3.网站攻击记录
//4. DNS 攻击
//5.服务器重启攻击
int main(void) {
std::cout << “1.网站404攻击” << std::endl;
std::cout << “2.网站篡改攻击” << std::endl;
std::cout << “3.网站攻击记录” << std::endl;
std::cout << “4.DNS 攻击” << std::endl;
std::cout << “5.服务器重启攻击” << std::endl;
system("pause");
return 0;
}
1.1.C语音打印风格
#include
#include<Windows.h>
//实现打印需求:
int main(void) {
printf(“你好!Cool”);
printf(“你好!Cool\n”);
printf("\n");
printf(“工资:30000”);
printf(“工资: % d”, 30000);
printf(“工资: % d 年假: % d”, 30000, 12);
printf(" % f", 3.1415);
printf(“圆周率: % f”, 3.1415);
system("pause");
return 0;
}
2.命名空间的使用
2.1第一种
#include
#include
//命名空间的使用
//中国
namespace China {
float population = 14.1; //单位:亿
std :: string capital = “北京”; //首都 capital
}
//日本
namespace Japan {
float population = 1.27; //单位:亿
std :: string capital = “日本”; //首都 capital
}
using namespace Japan;
int main(void) {
std::cout << “首都:” << capital << std::endl;
std::cout << “人口:” << population << std::endl;
std::cout << "首都:" << China::capital << std::endl;
std::cout << "人口:" << China::population << std::endl;
system("pause");
return 0;
}
2.2第二种
#include
#include
namespace China {
float population = 14.1; //单位: 亿
std::string capital = “北京”;
}
namespace Japan {
float population = 1.27; //单位: 亿
std::string capital = “东京”;
}
//注意:没有namespace
//直接指定命名空间中的标识符,而不是整个域名
using China::capital;
using Japan::population;
int main(void) {
std::cout << “首都:” << capital << std::endl;
std::cout << “人口:” << population << std::endl;
system("pause");
return 0;
}
2.3第三种
#include
#include
namespace China {
float population = 14.1; //单位: 亿
std::string capital = “北京”;
}
namespace Japan {
float population = 1.27; //单位: 亿
std::string capital = “东京”;
}
using namespace China;
using Japan::population;
int main(void) {
std::cout << “首都:” << capital << std::endl;
std::cout << “人口:” << population << std::endl; //出错!
system("pause");
return 0;
}
2.4解决方案:指定完整的域名(Japan::population )来表示。
int main(void) {
std::cout << “首都:” << capital << std::endl;
std::cout << “人口:” << Japan::population << std::endl; //出错!
system("pause");
return 0;
}
3.图形开发
#include<graphics.h> // 引用 EasyX 图形库
#include<Windows.h>
int main(void){
initgraph(640, 480); // 初始化图形界面, 画布大小为640x480
setbkcolor(RGB (64, 128, 128)); //设置背景色
cleardevice(); //用背景色清空整个屏幕(整个画布)
//画一个圆
setlinecolor(RGB (255, 0, 0)); //设置划线(画笔)的颜色
setlinestyle( PS_SOLID, 10); //设置线条为实线, 设置线宽为10像素
circle(320, 240, 200); //画圆,圆心(320, 240),半径为200
//输出文本
settextcolor(RGB(255,255,0)); //设置字体颜色
//设置字体样式: 字体高度为100像素, 宽度不指定, 字体名称为"微软雅黑"
settextstyle(100, 0, “微软雅黑”);
outtextxy(170, 190, (“无码高清”)); //在指定位置输出文本
//画一条斜线
line(180, 380, 460, 100); //从(180,380) 到 (460, 100)画一条直线
system("pause");
closegraph(); // 关闭图形界面
}
3.1图形界面实现
#include <graphics.h> // 引用 EasyX 图形库
#include <Windows.h>
int main(void){
initgraph(600, 400); // 初始化图形界面, 画布大小为640x480
loadimage(0, (“bg.jpg”));
settextcolor(RGB(255,255,0)); //设置字体颜色
settextstyle(30, 0, "微软雅黑");
rectangle(300, 40, 550, 80); //(左上角x,左上角y, 右下角x,右下角y
outtextxy(310, 45, "1-网站404攻击"); //在指定位置输出文本
rectangle(300, 100, 550, 140);
outtextxy(310, 105, "2-网站篡改攻击"); //在指定位置输出文本
rectangle(300, 160, 550, 200);
outtextxy(310, 165, "3-网站攻击修复"); //在指定位置输出文本
rectangle(300, 220, 550, 260);
outtextxy(310, 225, "4-查看攻击记录"); //在指定位置输出文本
rectangle(300, 280, 550, 320);
outtextxy(310, 285, "5-退出"); //在指定位置输出文本
system("pause");
closegraph(); // 关闭图形界面
}
3.2图形音频开发
#include <graphics.h> // 引用 EasyX 图形库
#include <Windows.h>
#include <mmsystem.h> //mci库头文件
#pragma comment(lib, “winmm.lib”)
int main(void) {
initgraph(800, 513);
loadimage(0, “jile.jpg”);
mciSendString(“play 极乐净土.mp3 repeat”, 0, 0, 0); //重复播放
system(“pause”);
closegraph();
}
4.增加功能菜单
#include
#include <Windows.h>
/*
新增需求:
DNS攻击
*/
int main(void) {
std::cout << “1.网站404攻击” << std::endl;
std::cout << “2.网站篡改攻击” << std::endl;
std::cout << “3.网站攻击记录” << std::endl;
std::cout << “4.DNS攻击” << std::endl;
std::cout << “5.服务器重启攻击” << std::endl;
// 新增功能:
std::cout << "6. DNS攻击" << std::endl;
system("pause");
return 0;
}
4.1增加图形功能实现
#include <graphics.h> // 引用 EasyX 图形库
#include <Windows.h>
/*
新增需求:
DNS攻击
*/
int main(void)
{
initgraph(600, 400); // 初始化图形界面, 画布大小为640x480
loadimage(0, (“bg.jpg”));
settextcolor(RGB(255,255,0)); //设置字体颜色
settextstyle(30, 0, "微软雅黑");
rectangle(300, 40, 550, 80); //(左上角x,左上角y, 右上角x,右上角
outtextxy(310, 45, "1-网站404攻击"); //在指定位置输出文本
rectangle(300, 100, 550, 140);
outtextxy(310, 105, "2-网站篡改攻击"); //在指定位置输出文本
rectangle(300, 160, 550, 200);
outtextxy(310, 165, "3-网站攻击修复"); //在指定位置输出文本
rectangle(300, 220, 550, 260);
outtextxy(310, 225, "4-查看攻击记录"); //在指定位置输出文本
// 新增功能 (x坐标不变, y坐标增加60)
rectangle(300, 280, 550, 320);
outtextxy(310, 285, "5-DNS攻击");
// 修改最后的"退出"菜单的位置, 和菜单序号
//rectangle(300, 280, 550, 320);
rectangle(300, 340, 550, 380);
//outtextxy(310, 285, "5-退出");
outtextxy(310, 345, "6-退出"); //在指定位置输出文本
system("pause");
closegraph(); // 关闭图形界面
}
5.账户输入实现
#include
#include <Windows.h>
int main(void) {
char name;
int pwd;
std::cout << "请输入账号:";
std::cin >> name;
std::cout << "请输入密码:";
std::cin >> pwd;
/*
std::cout << "1.网站404攻击" << std::endl;
std::cout << "2.网站篡改攻击" << std::endl;
std::cout << "3.网站攻击记录" << std::endl;
std::cout << "4.DNS攻击" << std::endl;
std::cout << "5.服务器重启攻击" << std::endl;
*/
system("pause");
return 0;
}
6.浮点数据的输出控制
#include
#include<Windows.h>
using namespace std;
int main(void){
double value = 12.3456789;
// 默认精度是6,所以输出为 12.3457
//(默认情况下,精度是指总的有效数字)
cout << value << endl;
// 把精度修改为4, 输出12.35, 对最后一位四舍五入
// 精度修改后,持续有效,直到精度再次被修改
cout.precision(4);
cout << value << endl;
// 使用定点法, 精度变成小数点后面的位数
// 输出12.3457
cout.flags(cout.fixed);
cout << value << endl;
// 定点法持续有效
// 输出3.1416
cout << 3.1415926535 << endl;
// 把精度恢复成有效数字位数
cout.unsetf(cout.fixed);
cout << value << endl; //输出12.35
cout << 3.1415926535 << endl; //输出3.142
system("pause");
return 0;
}
7.C++数据输入与类型表示
7.1第一种
#include
#include<Windows.h>
int main(void){
char girlType; //字符型
int salary; //整数型
float height; //浮点型
std::cout << "请输入您的理想类型:\n A:贤惠型 \n B:泼辣新 \n C: 文艺型 \n D:运动型" << std::endl;
std::cin >> girlType;
std::cout << "请输入您的月收入:" << std::endl;
std::cin >> salary;
std::cout << "请输入您的身高:[单位-米]" << std::endl;
std::cin >> height;
std::cout << "您的理想类型是: " << girlType << std::endl;
std::cout << "您的月收入是: " << salary << "元" << std::endl;
std::cout << "您的身高是: " << height << "米" << std::endl;
system("pause");
return 0;
}
7.2第二种
#include
#include<Windows.h>
using namespace std;
int main(void){
char girlType; //字符型
int salary; //整数型
float high; //浮点型
cout << "请输入您的理想类型:" << endl;
cout << "A:贤惠型" << endl;
cout << "B:泼辣型" << endl;
cout << "C:文艺型" << endl;
cout << "D:运动型" << endl;
cin >> girlType;
cout << "请输入您的月收入:";
cin >> salary;
cout << "请输入您的身高[单位米]:";
cin >> high;
cout << "您的理想类型是:" << girlType << endl;
cout << "您的月收入是:" << salary <<"元"<< endl;
cout << "您的身高是:" << high <<"米"<< endl;
system("pause");
return 0;
}
8.输入数据时,前面数据输入错误,导致后面无法输入
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
int a;
int b;
int c;
//std::cin >> a >> b >> c;
cout << "请输入a: ";
cin >> a;
if (cin.fail()) { //检查输入时是否发生了错误
cout << “输入错误,应该输入一个整数” << endl;
//清除错误标记,使得后续输入可以正常进行
//但是已经输入的数据还在输入缓冲区
cin.clear();
cin.sync(); //清空输入缓冲区
}
cout << "请输入b: ";
cin >> b;
if (cin.fail()) {//检查输入时是否发生了错误
cout << "输入错误,应该输入一个整数" << endl;
cin.clear(); //清除错误标记,使得后续输入可以正常进行
cin.sync(); //清空输入缓冲区
}
cout << "请输入c: ";
cin >> c;
if (cin.fail()) {//检查输入时是否发生了错误
cout << "输入错误,应该输入一个整数" << endl;
cin.clear(); //清除错误标记,使得后续输入可以正常进行
}
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
system("pause");
return 0;
}
9.练习计算,输入半径计算面的周长和面积输出保留2位小数
9.1练习题1:
#include
#include<windows.h>
using namespace std;
int main(void){
float R; //半径
float S; //面积
float len;//周长
cout << "请输入半径:";
cin >> R;
//数法公式
len = 2 * 3.14 * R;
S = 3.14 * R * R;
//控制精度
cout.precision(2);
cout.flags(cout.fixed);
//输出
cout << "这个圆的周长是:" << len << endl;
cout << "这个圆的面积是:" << S << endl;
system("pause");
return 0;
}
9.2 无符号数-练习题2:
#include
#include <Windows.h>
using namespace std;
int main(void) {
unsigned boyAge;
unsigned girlAge;
unsigned diff2 = boyAge2 - girlAge2;
cout << "美女,多大了?" << endl;
cin >> girlAge; //输入25
cout << "帅哥,多大了?" << endl;
cin >> boyAge; //输入22
diff = girlAge - boyAge;
cout << "美女比帅哥大" << diff <<"岁" << endl;
diff = boyAge - girlAge;
cout << "帅哥比美女大" << diff << "岁" << endl;
system("pause");
return 0;
}
10.实现账户密码的优化
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
//char name;
string name;
//int pwd;
string pwd;
cout << "请输入账号:";
cin >> name;
cout << "请输入密码:";
cin >> pwd;
system("pause");
return 0;
}
11.string变量定义和初始化
#include
#include
using namespace std;
int main(void)
{
//定义了字符串变量girFriend1,此时它的值是,空字符串
string girlFriend1;
//定义了girlFriendl字符串,王菲.
girlFriend1 = “王菲”;
cout << “girlFriend1=” << girlFriend1 << endl;
//定义了字符串变量girFriend2,此时它的值是,空字符串
string girlFriend2;
//定义了girlFriend2就是girlFriend1的字符串
girlFriend2 = girlFriend1;
cout << "girlFriend2=" << girlFriend2 << endl;
//定义一个变量字符串,同时设置了他的值.
string girlFriend3("周迅");
cout << "girlFriend3=" << girlFriend3 << endl;
//定义了gurlFriend4就是girlFriend3
string girlFriend4(girlFriend3);
cout << "girlFriend4=" << girlFriend4 << endl;
//等于10个A
string girlFriend5(10,'A');
system("pause");
return 0;
}
11.1string基本的输入输出
string job;
cout << “你是做什么工作的?” << endl;
cin >> job;
cout << “做” << job << “,收入一定不错吧?” << endl;
11.2string自动跳过空白字符
string university; //大学
string profession; //专业
cout << "你是哪个学习毕业的?学什么专业? ";
// 输入: 清华 考古 hello
// 自动跳过空白字符
cin >> university >> profession;
cout << university << "的" << profession << "专业不错哦!" << endl;
11.3string变量的输入和输出
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
string food;
int i = 0;
cout << "你喜欢什么美食:";
//需要循环语句
//使用cin>>输入时,如果遇到文件结束符是(Ctrl+z),就可以返回0
while (cin >> food){
i = i + 1;
cout << "你喜欢的第" << i << "美食是:" << food << endl;
cout << "你还喜欢吃什么美食:";
}
cout << "你最喜欢的美食有" << i << "种" << endl;
system("pause");
return 0;
}
11.4string变量读取一行
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
string addr;
cout << “你想到哪里去旅行:”;
//从标准输入设备(cin)读取一行字符串,保存到字符串变量addr中.
//读一行,直到遇到回车符,但并不包括回车符.如果直接回复,没有任何数据输入.
getline(cin, addr);
//empty()判断一个字符串是否为空,如果是空字符串,结果是true(真)否则结果为false(假).
if (addr.empty() == true) {
cout << "您输入了一个空行" << endl;
} else {
cout << "我也准备" << addr << "去哪旅游" << endl;
}
//size()和length()完全等效
//长度是指字符串占用的字节数,如果含有汉字,那么总的字节数和汉字个数不同
cout << "地址的长度是:" << addr.size() << endl;
cout << "地址的长度是:" << addr.length() << endl;
system("pause");
return 0;
}
12.string字符串的比较
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
string myGirl = “小芳”;
string yourGirl;
cout << "我喜欢的女孩子是" << myGirl << endl;
cout << "你喜欢的女孩是:";
cin >> yourGirl;
if (myGirl == yourGirl) {
cout << "我们都喜欢" << myGirl << "我们决斗吧!" << endl;
} else {
cout << "祝你幸福";
}
system("pause");
return 0;
}
12.1string字符串的加法
#include
#include<Windows.h>
#include
using namespace std;
int main(void){
string s1 = “武当派”;
string s2 = “张三丰”;
string s3 = “太极拳”;
string s4;
s4 = s1 + s2;//s4的值是:“武当派张三丰”
cout << “s4=” << s4 << endl;
//相当于s4 = s4 +"第一式"
s4 += "第一式";//变成了,武当派张三丰第一式.
cout << "s4" << s4 << endl;
system("pause");
return 0;
}
13.C语言字符串简介
#include
#include <Windows.h>
#include <stdio.h>
using namespace std;
int main(void) {
//C语言风格的字符串
char name[32];
/*
使用C语言的函数,来处理C语言字符串
printf("请输入您的名字:");
scanf("%s", name);
printf("%s, 你好!\n", name);
*/
// 使用C++的方式,来处理C语言字符串
cout << "请输入您的名字:";
cin >> name;
cout << name << ",你好!" << endl;
system("pause");
return 0;
}
14.C语言的字符串输入与输出
#include <stdio.h>
#include <Windows.h>
int main(void) {
char name[16];
char addr[64];
printf("姑娘芳名?\n");
scanf("%s", name);
//此时输入缓冲区中还有一个回车符
//清空输入缓冲区
fflush(stdin);
printf("姑娘家住何地?\n");
gets(addr); //读一行,包括一行中的空格
printf("家住%s 的%s, 我中意你!\n", addr, name);
system("pause");
return 0;
}
15.数组的初始化
#include
#include
using namespace std;
int main(void){
int a[8] = { 18,20,23,22,21,24,25,25 }; //数组是0开始到数,到8个数字.
cout << “a[3]=” << a[3];
//初始化方式1
int weights[8] = { 0 };
int weights2[8] = { 0,100,200 };
//初始化方式2
int b[] = { 1,2,3 }; //等效于:int b[3] = {1,2,3};
//高逼格初始化方式
//在定义时,仅确定了部分成员.
//注意:这个叫乱序方式,仅支持C编辑器支持,C++编辑器不支持.
int scores[50] = {
[10] = 90,
[20] = 100,
[25] = 120,
[30] = 155,
};
system("pause");
return 0;
}
16.数组元素的访问
#include
#include<Windows.h>
#include
using namespace std;
int main(void){
//下标:0,1,2,3,4,5,6,7,8,9
int girlSize[10];
//写数组元素
girlSize[5] = 38;
//读取数组元素
//C++写法
cout << "第" << 5 << "个girl的Size是" << girlSize[5] << endl;
//C语言写法
printf("第%d个girl的Size是%d\n", 5, girlSize[5]);
system("pause");
return 0;
}
17.C语言字符串存储,初始化
char name[10];
name[0] = ‘R’;
name[1] = ‘o’;
name[2] = ‘c’;
name[3] = ‘k’;
name[4] = 0; //字符串结束符0,就是 ‘\0’
printf(“姓名:%s”, name); //
name[2] = 0;
printf(“姓名:%s”, name); //
char name[10] = “Rock”; //相当于char name[10] = {‘R’, ‘o’, ‘c’, ‘k’, ‘\0’};
printf(“姓名:%s”, name);
char name[] = “Rock”; //相当于:name[5] = “Rock”
printf("%d", sizeof(name)); //5
18.C语言的字符串输入输出
#include
#include<Windows.h>
#include<string.h>
using namespace std;
int main(void){
char name[16];
char addr[64];
printf("姑娘芳名?\n");
scanf("%s", name);
//清除输入缓冲区
fflush(stdin);
printf("%s姑娘,家住哪里?\n",name);
gets(addr);//读一行
printf("家住%s的%s姑娘,我中意你!\n", addr, name);
system("pause");
return 0;
}
19.练习题1
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
string word;
int count = 0;
int length = 0;
cout << “请输入任意多个单词:”;
while (1) {
// 输入成功时,返回 cin 对象本身
// 遇到文件结束符(ctrl+z),而导致输入失败是,返回 0
if ((bool)(cin >> word) == false){
break;
}
count++;
length += word.length();
}
cout << “一共有” << count << “单词” << endl;
cout << “总长度:” << length << endl;
system(“pause”);
return 0;
}
19.1C语言版本
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
char word[128];
int count = 0;
int length = 0;
printf( "请输入任意多个单词:\n");
while (1) {
if (scanf("%s", word) == -1) {
break;
}
count++;
length += strlen(word);
}
printf("一共有%d个单词\n", count);
printf("总长度:%d", length);
system("pause");
return 0;
}
19.2练习题2
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
string line;
int lineCount = 0;
int length = 0;
cout << "请输入任意多行:";
while (1){
// 遇到文件结束符时, 返回NULL(0)
if (getline(cin, line) == 0) {
break;
}
lineCount++;
length += line.length();
}
cout << "一共有" << lineCount << "行" << endl;
cout << "总长度: " << length << endl;
system("pause");
return 0;
}
19.3C语言版本
#include <stdio.h>
#include <string.h>
#include <Windows.h>
int main(void) {
char line[2048];
int lineCount = 0;
int length = 0;
printf("请输入任意多行:");
while (1) {
if ( gets(line) == 0) {
break;
}
lineCount++;
length += strlen(line);
}
printf("一共有%d行\n", lineCount);
printf("总长度:%d\n", length);
system("pause");
return 0;
}
20.string字符串的比较
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
string name;
string pwd;
cout << "请输入账号:";
cin >> name;
cout << "请输入密码:";
cin >> pwd;
if (name == "cool" && pwd == "a63829149") {
cout << "1.网站404攻击" <<endl;
cout << "2.网站篡改攻击" << endl;
cout << "3.网站攻击记录" <<endl;
cout << "4.DNS攻击" << endl;
cout << "5.服务器重启攻击" <<endl;
} else {
cout << "用户名或密码错误!" << endl;
}
system("pause");
return 0;
}
21.C语言字符串比较
#include <stdio.h>
#include <string.h>
#include <Windows.h>
int main(void) {
char addr[32];
int ret;
printf("美女,你是哪里人?");
scanf("%s", addr);
if (strcmp(addr, "湖南") == 0) {
printf("美女,我们是老乡啊!\n");
} else {
printf("美女,你和我的同学是老乡啊!\n");
}
system("pause");
return 0;
}
22.数据类型的比较与运算
#include
#include <Windows.h>
using namespace std;
int main(void) {
int weight;
printf("美女, 你多重啊?\n");
cin >> weight;
if (weight >= 120){
cout << "美女, 如此丰满, 真有福气!" << endl;
} else {
cout <<"美女, 这么瘦, 身材不错啊!" << endl;
}
system("pause");
return 0;
}
22.1逻辑&&
#include
#include <Windows.h>
using namespace std;
int main(void) {
int money;
int days;
cout << "你有多少钱?";
cin >> money;
cout << "你看多少天的假期?";
cin >> days;
//如果存款大于10万,而且假期大于10天, 就去旅游
if (money > 100000 && days > 10) {
cout << "我要去旅游!" << endl;
} else {
cout << "还是在家里呆着" << endl;
}
system("pause");
return 0;
}
22.2逻辑||
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
int money;
string love;
cout << "你有多少钱?" << endl;
cin >> money;
cout << "你爱我吗?" << endl;
cin >> love; //回答: 爱 或者 不爱
if (money > 1000000 || love == "爱" ) {
cout << "我们结婚吧!" << endl;
} else {
cout << "你是一个好人!" << endl;
}
system("pause");
return 0;
}
22.3逻辑非!
#include
#include <Windows.h>
using namespace std;
int main(void) {
int money;
int days;
cout << "你的月薪多少?";
cin >> money;
if (!(money >= 30000)){
cout << "我是菜鸟, 薪资不到3万, 我要努力修炼!\n" << endl;
}else{
cout << "我要接外包\n" << endl;
}
system("pause");
return 0;
}
24.三目运算符
#include
#include <Windows.h>
using namespace std;
int main(void) {
int years;
int days;
cout << "请输入你的工作年限:";
cin >> years;
cout << (years >= 5 ? "老鸟" : "菜鸟") << ",晚上好!" << endl;
system("pause");
return 0;
25.if语句的一种判断
#include
#include <Windows.h>
using namespace std;
int main(void) {
int salary;
cout << "你的月薪是多少:";
cin >> salary;
if (salary >= 10000){
cout << "我们在一起吧" << endl;
}else{
cout << "我觉得我们不太合适" << endl;
}
system("pause");
return 0;
}
25.1if语句的二种判断
#include
#include
#include <stdio.h>
using namespace std;
int main(void){
int salary;
string houseRet; //是否有房
string carRet; //是否有车
cout << "你的月薪是多少?" << endl;
cin >> salary;
cout << "你有房吗?" << endl;
cin >> houseRet;
cout << "你有车吗?" << endl;
cin >> carRet;
if (salary < 20000) {
cout << "你是一个好人, 我现在还不想谈恋爱." << endl;
}else if (houseRet == "有") {
cout << "我其实没有什么要求,只要你对我好." << endl;
}else if (carRet == "有") {
cout << "还不错哦, 以后再联系." << endl;
}else{
cout << "有缘再见." << endl;
}
system("pause");
return 0;
}
26.if语句的嵌套
#include
#include <windows.h>
using namespace std;
int main(void) {
int x, y, z;
cout << "请输入 3 个整数: " << endl;
cin >> x >> y >> z;
if (x > y) {
if (x > z) {
cout << "最大值是: " << x << endl;
}else{
cout << "最大值是: " << z << endl;
}
}else{
if (y > z) {
cout << "最大值是: " << y << endl;
}else{
cout << "最大值是: " << z << endl;
}
}
system("pause");
return 0;
}
27.swintch语句基本使法
#include
#include <windows.h>
using namespace std;
int main(void) {
int num;
cout << "请输入一个数字: ";
cin >> num;
switch (num) {
case 1:
cout << “星期一:包子” << endl;
break;
case 2:
cout << “星期二:馒头” << endl;
break;
case 3:
cout << “星期三:稀饭” << endl;
break;
case 4:
cout << “星期四:白菜” << endl;
break;
case 5:
cout << “星期五:土豆” << endl;
break;
case 6:
cout << “星期六:面条” << endl;
break;
case 7:
cout << “周末:休息” << endl;
break;
default:
cout << “请输入 1-7” << endl;
break;
}
system(“pause”);
return 0;
}
28.练习题1
#include
#include <windows.h>
using namespace std;
int main(void) {
char Transformation;
cout << "请输入一个字符:";
cin >> Transformation;
if (Transformation >= 'a' && Transformation <= 'z') {
Transformation = Transformation - 'a' + 'A';
}
else if (Transformation >= 'A' && Transformation <= 'Z') {
Transformation = Transformation - 'A' + 'a';
}
cout << Transformation << endl;
system("pause");
return 0;
}
28.1练习题2
#include
#include
#include <Windows.h>
using namespace std;
//零 壹 贰 叁 肆 伍 陆 柒 捌 玖
int main(void) {
int num;
string ret;
cout << "请输入一个数字[0-9]: ";
cin >> num;
switch (num) {
case 0:
cout << “零”;
break;
case 1:
cout << “壹”;
break;
case 2:
cout << “贰”;
break;
case 3:
cout << “叁”;
break;
case 4:
cout << “肆”;
break;
case 5:
cout << “伍”;
break;
case 6:
cout << “陆”;
break;
case 7:
cout << “柒”;
break;
case 8:
cout << “捌”;
break;
case 9:
cout << “玖”;
break;
default:
break;
}
cout << endl;
system(“pause”);
return 0;
}
28.2用空间换速度
#include
#include
#include <Windows.h>
using namespace std;
int main(void) {
int num;
string ret[10] = { “零”, “壹”, “贰”, “叁”, “肆”, “伍”, “陆”, “柒”, “捌”,
“玖” };
cout << "请输入一个数字[0-9]: ";
cin >> num;
if (num >= 0 && num <= 9) {
cout << ret[num] << endl;
}
system(“pause”);
return 0;
}
28.3练习题3
#include
#include <Windows.h>
#include
using namespace std;
/*
闰年的 2 月份有 29 天
普通闰年: 能被 4 整除但不能被 100 整除的年份为
世纪闰年: 能被 400 整除
*/
int main(void) {
int year;
int month;
bool flag = false;
int days;
cout << “请输入年份:”;
cin >> year;
cout << “请输入月份:”;
cin >> month;
if (year % 400 == 0) {
flag = true;
}
else if (year % 4 == 0 && year % 100 != 0) {
flag = true;
}
else {
flag = false;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
break;
case 2:
days = flag ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
std::cout << “无效月份” << std::endl;
break;
}
cout << year << “年” << month << “月一共有:” << days << “天” << endl;
system(“pause”);
return 0;
29.for循环语句
#include
#include <windows.h>
using namespace std;
int main(void) {
for (int i = 1; i <=9; i++){
cout << “射第” << i << “个太阳” << endl;
}
system(“pause”);
return 0;
}
30.do-while循环语句
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
int Calculation = 0;
int Calculation1 = 1;
do{
Calculation += Calculation1;
Calculation1++;
} while (Calculation1 <= 100);
cout << Calculation << endl;
system("pause");
return 0;
}
31.循环特殊控制:continue和break
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
string Putquestionsto;
for (int i = 0; i < 3; i++){
cout << "第" << i + 1 << "次相亲" << endl;
cout << "你喜欢玩游戏吗,回答YES或NO:";
cin >> Putquestionsto;
if (Putquestionsto !="yes"){
continue;
}
cout << "一起玩吧" << endl;
}
system("pause");
return 0;
}
32.goto语句,不太建议使用
#include
#include <Windows.h>
#include
using namespace std;
void happy(void) {
cout << “开启浪漫生活” << endl;
}
int main(void) {
string Putquestionsto;
for (int i = 0; i < 3; i++){
cout << “第” << i + 1 << “次相亲” << endl;
cout << “你喜欢玩游戏吗,回答YES或NO:”;
cin >> Putquestionsto;
if (Putquestionsto !=“yes”){
continue;
}
cout << “一起玩吧” << endl;
cout << "我喜欢你哦,你喜欢我吗?" << endl;
cin >> Putquestionsto;
if (Putquestionsto == "yes"){
//goto happy;
happy();
system("pause");
return 0;
}
}
system("pause");
return 0;
}
33.循环嵌套
#include
#include <Windows.h>
#include
using namespace std;
// 一天想我几次?
// 24 小时,每秒一次
int main(void) {
int missyou = 0;
for (int hour = 0; hour < 24; hour++){
for (int Minute = 0; Minute < 60; Minute++){
for (int second = 0; second < 60; second++){
missyou++;
cout << hour << “:” << Minute << “:” << second << endl;
cout<< " 想你第" << missyou << “次” << endl;
Sleep(1000);
}
}
}
system(“pause”);
return 0;
}
34.暴力破解
#include
using namespace std;
int main(void) {
char pwd[7];
char dict[64]; //10+26+26+1 = 63;
char tmp[32];
int index = 0;
for (int i = 0; i < 10; i++) {
dict[index++] = '0' + i;
}
/*
for (int i=0; i<26; i++) {
dict[index++] = 'a' + i;
}
for (int i=0; i<26; i++) {
dict[index++] = 'A' + i;
}
dict[index++] = '_';
*/
dict[index] = '\0';
int n = index; // 字符个数
for (int p1 = 0; p1 < n; p1++) {
for (int p2 = 0; p2 < n; p2++) {
for (int p3 = 0; p3 < n; p3++) {
for (int p4 = 0; p4 < n; p4++) {
for (int p5 = 0; p5 < n; p5++) {
for (int p6 = 0; p6 < n; p6++) {
tmp[0] = dict[p1];
tmp[1] = dict[p2];
tmp[2] = dict[p3];
tmp[3] = dict[p4];
tmp[4] = dict[p5];
tmp[5] = dict[p6];
tmp[6] = '\0';
cout << tmp << endl;
}
}
}
}
}
}
return 0;
}
35乘法口诀表
#include
#include<Windows.h>
#include
using namespace std;
int main(void){
for (int i = 1; i <= 9; i++){
for (int j = 1; j <= i; j++){
if (j==1){
cout << j << “" << i << “=” << setw(1) << std::left << i * j << " ";
}else{
cout << j << "” << i << “=” << setw(2) << std::left << i * j << " ";
}
}
cout << endl;
}
system(“pause”);
return 0;
}
36.倒除法,二进制转换
#include
#include <Windows.h>
#include
using namespace std;
int main(void) {
int n;
int ret[32];
int i;
cout << “请输入一个整数:”;
cin >> n;
if (n <= 0){
cout << “请你重新输入大于0的数值” << endl;
system(“pause”);
return 1;
}
i = 0;
while (n != 0){
ret[i] = n % 2;
n = n / 2;
i++;
}
for (i–; i >= 0; i–){
cout << ret[i];
}
cout << endl;
system(“pause”);
return 0;
}
37.项目优化-菜单选择
#include
#include <Windows.h>
#include
using namespace std;
void login(void) {
string name;//账户
string pwd; //密码
while (1) {
system("cls");
std::cout << "请输入账号:";
std::cin >> name;
std::cout << "请输入密码:";
std::cin >> pwd;
if (name == "cool" && pwd == "shuao365") {
//break;
return;
}
else {
cout << "用户名或密码错误!" << endl;
system("pause");
}
}
}
void menuShow(void) {
system(“cls”);
std::cout << “1.网站404攻击” << std::endl;
std::cout << “2.网站篡改攻击” << std::endl;
std::cout << “3.网站攻击修复” << std::endl;
std::cout << “4.查看攻击记录” << std::endl;
std::cout << “5.退出” << std::endl;
}
int menuChoise(void) {
int n = 0;
//循环判断
while (1) {
cin >> n;
if (cin.fail()) {
cin.clear();
cin.sync();
cout << “无效输入. 请重新输入.” << endl;
system(“pause”);
}else {
break; //输入成功跳出
}
}
return n;
}
void attack404(void) {
system(“cls”);
cout << “404攻击…”;
system(“pause”);
}
void siteEdit(void) {
system(“cls”);
cout << “网站篡改攻击…”;
system(“pause”);
}
void siteRepair(void) {
system(“cls”);
cout << “网站修复…”;
system(“pause”);
}
void attckRecord(void) {
system(“cls”);
cout << “查看攻击记录”;
system(“pause”);
}
int main(void) {
// 登录
login();//封装函数
while (1) {
menuShow();
int n = menuChoise();
switch (n) {
case 1:
attack404();
break;
case 2:
siteEdit();
break;
case 3:
siteRepair();
break;
case 4:
attckRecord();
break;
case 5:
//break;
return 0;
default:
cout << "无效输入. 请重新输入." << endl;
system("pause");
break;
}
}
system("pause");
return 0;
}
38.菜单居中显示
#include
#include <Windows.h>
#include
using namespace std;
#define WIDTH 40 //宽度
#define HEIGHT 15 //高度
void login(void) {
string name;//账户
string pwd; //密码
while (1) {
system("cls");
std::cout << "请输入账号:";
std::cin >> name;
std::cout << "请输入密码:";
std::cin >> pwd;
if (name == "cool" && pwd == "shuao365") {
//break;
return;
}
else {
cout << "用户名或密码错误!" << endl;
system("pause");
}
}
}
void menuShow(void) {
//数组居中
string menu[] = {
“1.网站404攻击”,
“2.网站篡改攻击”,
“3.网站攻击修复”,
“4.查看攻击记录”,
“5.退出”
};
int max = 0;
int menuCount = sizeof(menu) / sizeof(menu[0]);
for (int i = 0; i < menuCount; i++){
if (menu[i].length()>max){
max = menu[i].length();
}
}
int leftSpace = (WIDTH - max) / 2;
for (int i = 0; i < menuCount; i++){
for (int i = 0; i < leftSpace; i++){
cout << " ";
}
cout << menu[i] << endl;
}
}
int menuChoise(void) {
int n = 0;
//循环判断
while (1) {
cin >> n;
if (cin.fail()) {
cin.clear();
cin.sync();
cout << “无效输入. 请重新输入.” << endl;
system(“pause”);
}else {
break; //输入成功跳出
}
}
return n;
}
void attack404(void) {
system(“cls”);
cout << “404攻击…”;
system(“pause”);
}
void siteEdit(void) {
system(“cls”);
cout << “网站篡改攻击…”;
system(“pause”);
}
void siteRepair(void) {
system(“cls”);
cout << “网站修复…”;
system(“pause”);
}
void attckRecord(void) {
system(“cls”);
cout << “查看攻击记录”;
system(“pause”);
}
void init(void) {
//设置窗口大小
char cmd[128];
sprintf(cmd, “mode con cols=%d lines=%d”, WIDTH, HEIGHT);
system(cmd);
}
int main(void) {
init();
// 登录
login();//封装函数
while (1) {
menuShow();
int n = menuChoise();
switch (n) {
case 1:
attack404();
break;
case 2:
siteEdit();
break;
case 3:
siteRepair();
break;
case 4:
attckRecord();
break;
case 5:
//break;
return 0;
default:
cout << “无效输入. 请重新输入.” << endl;
system(“pause”);
break;
}
}
system("pause");
return 0;
}
39.隐藏密码
#include<conio.h>
void input_pwd(char pwd[], int max_len) {
char c;
int i = 0;
while (1) {
c = getch();
// getch不从输入缓冲区中读取
//在getch中,把回车按键输入,识别为回车符'\r'
//在getchar中,把回车按键输入,识别为换行符'\n'
if (c == '\r' || i >= max_len) {
pwd[i] = 0;
break;
}
pwd[i++] = c;
printf("*", c);
}
printf("\n");
}
void login(void) {
string name;
//string pwd;
char pwd[16];
while (1) {
system("cls");
std::cout << "请输入账号:";
std::cin >> name;
std::cout << "请输入密码:";
//std::cin >> pwd;
input_pwd(pwd, sizeof(pwd));
if (name == "54hk" && !strcmp(pwd, "123456")) {
//break;
return;
} else {
cout << "用户名或密码错误!" << endl;
system("pause");
}
}
}
40.二维数组,初始化
#include
#include<windows.h>
using namespace std;
int main(void) {
//一维数组
int a[5]; //一维数组a包含{0,1,2,3,4}组成, 共5个元素
//二维数组
int b[5][5]; //二维数组b包含 5 行 5 列,共 25 元素
//二维数组,初始化 第一种方式
int c[3][4] = { //等效于 int a[][4]
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
//二维数组,第二种方式 初始化时从头开始,依次序进行
int d[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
//只初始化第一个,其他得默认置零
int e[3][4] = { 1 };
//定义一个二维数组
int i = 0, j = 0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 4; j++){
cout << c[i][j];//访问c这个二维数组和循环的i和j
}
cout << endl;
}
system("pause");
return 0;
}
41.二维数组的访问
#include
#include<windows.h>
using namespace std;
int main(void) {
//给二维数组成员赋值
int i = 0, j = 0;
int a[3][4];
//使用一个循环给二维数组赋值
//i=0的时候,对应的就是a[0][0],如果i=1的时候对应a[0][1],i=2那么a[0][3].i=3 a[0][4]以此类推.
for (int i = 0; i < 12; i++) {
a[i / 4][i % 4] = i + 1;
}
//第二种方式
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = 4 * i + j + 1;
}
}
//输出
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << a[i][j];
}
cout << endl;
}
}
43.项目实现-地形导航
#include
#include
#include
#include<Windows.h>
#include<stdlib.h>
using namespace std;
//判断峰点,判断真假
bool isPeak(int grid[64][64], int r, int c);
int main(void) {
int nrows, ncols; //行:nrows,列:ncols
int map[64][64];
string filename;
ifstream file;
cout << "请输入文件名称:";
cin >> filename;
file.open(filename.c_str());
if (file.fail()){
cout << "打开输入的文件出错" << endl;
exit(1);//结束
}
file >> nrows >> ncols;
if (nrows > 64 || ncols > 64){
cout << "网格超出预算,请重新调整" << endl;
exit(1);//结束
}
//从数据文件读取到二维数组
for (int i = 0; i < nrows; i++){
for (int j = 0; j < ncols; j++){
file >> map[i][j];
}
}
//判断打印峰值的数值的位置
for (int i = 1; i < nrows-1; i++){
for (int j = 1; j < ncols-1; j++){
if (isPeak(map,i,j)){
cout << "峰点出现在行:" << i << "列:" << j << endl;
}
}
}
system("pause");
//关闭文件
file.close();
//结束程序
return 0;
}
bool isPeak(int grid[64][64], int r, int c) {
if ((grid[r][c] > grid[r - 1][c]) &&
(grid[r][c] > grid[r + 1][c]) &&
(grid[r][c] > grid[r][c - 1]) &&
(grid[r][c] > grid[r][c + 1])) {
return true;
}else{
return false;
}
}
43.杨灰三角形
#include
#include <stdlib.h>
#include
using namespace std;
#define N 10
int main(void) {
int a[N][N] = { 0 }; //二维数组所有元素清零
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {//j<=i,第 i 行,仅有 i 个数字
if (j == 0 || i == j) {//第一列和对角线置为 1
a[i][j] = 1;
}
else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < N; i++) {
cout << setw((N - i) * 4) << a[i][0];
for (int j = 1; j <= i; j++) {
cout << setw(8) << a[i][j];
}
cout << endl;
}
system(“pause”);
return 0;
}