2025.4.6--start
技巧:
1.字体颜色的改变方法,以及背景颜色:
首先,如果你想要实现这样的效果:
ASCII 艺术字:
有多种风格和形式,可以根据需求选择不同的呈现方式。以下是几种不同风格的 ASCII 艺术字示例:
1. 经典块状风格(Bold Block)
███████╗ █████╗ ███████╗██████╗ ████████╗ ██████╗ ██████╗
╚════██║██╔══██╗ ██╔════╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗
██║███████║ ███████╗██████╔╝ ██║ ██║ ██║██████╔╝
██╔╝██╔══██║ ╚════██║██╔══██╗ ██║ ██║ ██║██╔══██╗
██╔╝ ██║ ██║ ███████║██║ ██║ ██║ ╚██████╔╝██║ ██║
╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
(适合现代感强的 UI 界面)
2. 斜体手写风格(Italic Handwriting)
复制
____ _ _ _ ___ _ _
/ __ \ (_) | | | | / _ \ | | | |
/ / _` | | | _ | |_ | | / /_\ \| |_ ___| |___
| | (_| | | | | | | __| | | / ___ | __/ _ \ / __|
\ \__,_| | | |_| | |_ | |/ / \ | || (_) |\__ \
\____/ |_|\__,_|\__| |_/_/ \_\__\___/ |___/
(适合轻松、活泼的店铺风格)
3. 复古像素风格(Retro Pixel)
▄▄▄▄▄▄▄ ▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▄▄▄ ▄▄▄▄▄▄ ▄▄ ▄▄▄▄▄
█ ▄▄▄ █ █▄▄ █ █ █▄▄▄ █ █▄▄ █ ▄▄▄ █ █ █▄▄▄ █
█ ███ █ ███ █ █ ████ █ ███ █ ███ █ █ ████ █
█▄▄▄▄▄█ █▄▄ █ █▄▄▄▄▄▄█ █▄▄ █▄▄▄▄▄█ █▄▄▄▄▄▄█
(适合复古游戏或像素风设计)
4. 极简线条风格(Minimalist Line)
.-. .-..-. .-. .-. .--. .-.
| { } |\ \ / /| { } |/ {} \| |
| {_} } \ \/ / | {_} }\ {} /| |
`----' `--' `----' `----'`-'
(适合简洁、现代的设计)
5. 3D 立体风格(3D Shadow)
____ ___ _ _____ ____ _____ ______
/ ___|/ _ \| | |___ / / ___|| ____|__ /
| | | | | | | |_ \| | | _| / /
| |___| |_| | |___ ___) | |___ | |___ / /_
\____|\___/|_____||____/ \____||_____/____|
(适合强调品牌名称)
6. 圆形泡泡风格(Bubble Letters)
.-') ('-. .-') _ .-') _
( OO ). ( OO ).-. ( OO) ) ( OO) )
(_)---\_)/ . --. /,(_)----. ,(_)----.
/ _ | | \-. \ | | | |
\ :` `..-'-' | )'--. / '--. /
'..`''.)\| |_.' ( `--' / (`--' /
.-._) \ | .-. |`---' `---'
\ / | | | |
`-----' `--' `--'
(适合可爱、亲和力强的风格)
范例:
步骤一:包含头文件 #include <windows.h>,并使用 SetConsoleTextAttribute 函数
#include <windows.h>
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
步骤二:在你的代码中,这些数字是 Windows 控制台 API 的 颜色属性值(通过 SetConsoleTextAttribute
设置),具体对应关系如下:
Windows 控制台的颜色值是一个 8-bit 属性,按以下格式组合:
-
低 4 位(0-3):前景色(文字颜色)
-
高 4 位(4-7):背景色
-
每种颜色 4-bit(16 色),常见值如下:(意思就是每种颜色对应一个值)
值 | 颜色 | 值 | 颜色(高亮版) |
---|---|---|---|
0 | 黑色(Black) | 8 | 深灰色(Dark Gray) |
1 | 蓝色(Blue) | 9 | 亮蓝色(Bright Blue) |
2 | 绿色(Green) | 10 | 亮绿色(Bright Green) |
3 | 青色(Cyan) | 11 | 亮青色(Bright Cyan) |
4 | 红色(Red) | 12 | 亮红色(Bright Red) |
5 | 紫色(Purple) | 13 | 粉红色(Pink) |
6 | 黄色(Yellow) | 14 | 亮黄色(Bright Yellow) |
7 | 灰色(Gray) | 15 | 白色(White) |
示例:
-
63
的二进制是00111111
:-
背景色:
0011
(3,深青色) -
前景色:
1111
(15,白色) -
实际表现为 蓝底白字(具体效果可能因终端配置略有差异)。
-
然后为自己的颜色设定为对应的常量不变。
const int COLOR_white = 15; // 白色
const int COLOR_yellow = 14; // 黄色
const int COLOR_HIGHLIGH = 11; // 浅青色
const int COLOR_green = 10; // 绿色
const int COLOR_red = 12; // 红色
setColor(COLOR_BUTTON_ACTIVE);
cout << R"(
██████ ██████ ██████ ██ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ █████ █████ ████
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██████ ██████ ██ ██ ███████ ██
/\_/\
( o.o ) 欢迎使用商品销售系统2.0
> ^ <
/ ▽ \ <2025/4/13 星期天>
/ \
/ \
( )
\ ||||| /
~~~~~~~~~
)" << endl;
setColor(COLOR_white);//设定完颜色后记得恢复成白色
cout << R"(...)" 的 R"(...)" 是一种 原始字符串字面量(Raw String Literal) 的语法,它的作用是:会原样输出括号内的所有内容(包括换行和 █
符号),无需手动添加 \n
或转义 \
。即:
1. 原始字符串字面量的作用
-
忽略转义字符:字符串中的
\
、"
、换行符等会被直接输出,不会被当作特殊字符处理。 -
保留所有格式:包括换行、缩进、空格等,适合输出多行文本(如 ASCII 艺术字)。
对比普通字符串
如果不用原始字符串,普通写法需要大量转义:
cout <<
" ██████ ██████ ██████ ██ ██ ███████ ██ ██ \n"
" ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ \n"
" ██ ██ ██ ██ ██ █████ █████ ████ \n"
" ██ ██ ██ ██ ██ ██ ██ ██ ██ \n"
" ██████ ██████ ██████ ██ ██ ███████ ██ \n"
<< endl;
2.你好奇这个 █ 符号怎么打出来的吗?
按住ALT键,数字键输入219。
其他变体:220
(▄)、223
(▀)。还有很多。。。
3.patorjk.com
这个网站提供了不少艺术字,只需要你打入内容,选择字体就行了。
2.c++的文件操作
为什么建议使用c++的文件操作?因为更加面向对象啊
1.<<
和 >>
运算符自动匹配类型,编译器会检查类型错误。
2.C语言需要手动管理文件指针(FILE*
),容易忘记关闭文件或发生资源泄漏,而c++文件流对象(ifstream
/ofstream
)在析构时自动关闭文件,你找谁说理去
3.c:
-
读写时需反复指定格式和变量地址
int num;
char name[20];
fscanf(file, "%d %s", &num, name); // 需要取地址符 &
fprintf(file, "%d %s", num, name);
c++:
- 使用
<<
和>>
运算符,代码更直观:
int num;
std::string name;
file >> num >> name; // 自动处理类型和地址
file << num << " " << name;
4.c++与 STL 容器无缝集成
与 C 语言文件操作对比
操作 | C++ (<fstream> ) |
C (<stdio.h> ) |
---|---|---|
打开文件 | ifstream/ofstream/fstream |
fopen |
关闭文件 | file.close() |
fclose |
写入文本 | file << "text" |
fprintf(file, "text") |
读取文本 | file >> var |
fscanf(file, "%s", buf) |
二进制写 | file.write(data, size) |
fwrite(data, size, 1, file) |
二进制读 | file.read(data, size) |
fread(data, size, 1, file) |
文件定位 | seekg()/seekp() |
fseek() |
获取位置 | tellg()/tellp() |
ftell() |
错误检查 | good()/eof()/fail() |
feof()/ferror() |
C++ 文件流可以指定打开模式,类似于 C 语言的 fopen
模式:
模式 | 含义 |
---|---|
ios::in |
只读(默认 ifstream ) |
ios::out |
只写(默认 ofstream ) |
ios::app |
追加模式(不覆盖原文件) |
ios::binary |
二进制模式 |
ios::trunc |
清空文件(默认 ofstream ) |
ios::ate |
打开后定位到文件末尾 |
ofstream file("data.txt", ios::out | ios::app); // 追加写入
ifstream infile("data.txt", ios::in | ios::binary); // 二进制读取
fstream ioFile("data.txt", ios::in | ios::out); // 可读可写
4.7第一版:
包含基础功能
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <windows.h>
#include <conio.h>
#include <commctrl.h>
#include <cmath>
#include <malloc.h>
typedef long long LL;
using namespace std;
const int white = 15; // 白色
const int yellow = 14; // 黄色
const int HIGHLIGH = 11; // 浅青色
const int green = 10; // 绿色
const int red = 12; // 红色
const int blue = 9; // 蓝色
const int BUTTON_ACTIVE = 63;// 蓝底白字高亮
const int grey = 7; // 灰色输入框
void setColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
//清理屏幕
void clearScreen() {
system("cls");
}
//首页面
void background(){
setColor(yellow);
cout << R"(
██████ ██████ ██████ ██ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ █████ █████ ████
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██████ ██████ ██ ██ ███████ ██
/\_/\
( o.o ) 欢迎使用商品销售系统2.0
> ^ <
/ ▽ \ <2025/4/13 星期天>
/ 来 财 \
/ 超 市 \
( ) --Made by Cai
\ ||||| / 如有疑问请联系
~~~~~~~~~
)" << endl;
setColor(white);
}
//定义结构体数组,商品信息,交易信息
struct Product {
string id;
string name;
LL stock;
double purchasePrice;
double salePrice;
};
struct Deal{
char inout;
string name;
string id;
LL n;
double money;
};
vector<Product> products;
vector<Deal> deals;
//读取文档数据
void Loadpro(){
ifstream file("products.txt");
Product p;
LL i=0;
while(file>>p.id>>p.name>>p.stock>>p.purchasePrice>>p.salePrice){
products.push_back(p);}
file.close();
}
void Loaddeal(){
ifstream file("deal.txt");
Deal d;
while(file>>d.inout>>d.name>>d.id>>d.n>>d.money){
deals.push_back(d);
}
file.close();
}
//更新商品数据
void saveProducts() {
ofstream file("products.txt");
Product p;
for(LL i=0;i<products.size();i++){
p=products[i];
file << p.id << " " << p.name << " " << p.stock << " "
<< p.purchasePrice << " " << p.salePrice << endl;
// cout << p.id << " " << p.name << " " << p.stock << " "
//<< p.purchasePrice << " " << p.salePrice << endl;
}
file.close();
}
//更新交易记录
void saveDeal(){
ofstream file("deal.txt");
Deal d;
for(LL i=0;i<deals.size();i++){
d=deals[i];
file<<d.id<<" "<<d.name<<" "<<d.inout<<" "<<d.n<<" "<<d.money<<endl;
}
file.close();
}
//添加商品
void addProduct(){
clearScreen();
Product p;
cout<<"<添加商品>"<<endl;
cout << endl<<"请输入商品编号: ";
cin >> p.id;
// 检查编号是否重复
for (LL i=0;i < products.size(); ++i) {
if (products[i].id == p.id) {
setColor(yellow);
clearScreen();
cout << "商品编号已存在!" << endl;
setColor(HIGHLIGH);
return;
}
}
cout << "请输入商品名称: ";
cin >> p.name;
cout << "请输入初始库存: ";
cin >> p.stock;
cout << "请输入进货价: ";
cin >> p.purchasePrice;
cout << "请输入销售价: ";
cin >> p.salePrice;
products.push_back(p);
saveProducts();
setColor(green);
clearScreen();
cout << "商品添加成功!" << endl;
setColor(HIGHLIGH);
}
//修改商品
void modifyProduct(){
clearScreen();
cout<<"<修改商品>"<<endl;
cout<<endl<<"请输入要修改的商品编号(如果不知道编号可以使用查询功能):";
Product p;
LL i;
int f=0;
char choice;
cin>>p.id;
for( i=0;i<products.size();i++){
if(p.id==products[i].id){
p=products[i];
f=1;
setColor(green);
clearScreen();
cout<<"已找到该商品!"<<endl;
setColor(HIGHLIGH);
cout<<"商品编号:"<<p.id<<endl<<"商品名称:"<<p.name<<endl<<"库存:"<<p.stock<<endl<<"进货价:"<<p.purchasePrice<<endl<<"销售价:"<<p.salePrice<<endl;
break;
}
}
if(!f){
setColor(red);
clearScreen();
cout<<"未找到该商品,请重新确认!"<<endl;
setColor(HIGHLIGH);
return;
}
setColor(blue);
cout<<R"(
请选择你想更改的数据:
1.商品名称
2.库存
3.进货价
4.销售价
0.返回)"<<endl;
setColor(HIGHLIGH);
while (true) {
cout << "请输入要执行的操作: ";
cin>>choice;
if(choice<'0'||choice>'4'){
clearScreen();
setColor(red);//红色
cout << "输入无效,请重新输入数字(0-4)!" << endl;
setColor(HIGHLIGH);
return;
}
if (choice == '0') break;
switch (choice-'0') {
case 1:
cout << "请输入新名称: ";
cin >> products[i].name;
break;