1.4 符号常量
在程序中使用300、20等类似的“幻数”并不是一个好习惯,它们几乎无法向以后阅读该程序的人提供什么信息,而且使程序的修改变得更加困难。处理这一类幻数的一种方法是赋予它们有意义的名字。
#include 名字 替换文本
在该定义之后,程序中出现的所有的【名字】都会被相应的【替换文本】替换。注意,替换文本可以是任意的字符序列,而不仅限于数字。
// Created by Liu Xianmeng on 2022/11/28
#include <bits/stdc++.h>
using namespace std;
#define NAME "BigXMeng" // 可以定义字符串
#define ABLE true // 可以定义bool变量true
#define UNABLE false // 可以定义bool变量false
#define NUMBER 100 // 可以定义整型
#define DOUBLE 266.88 // 可以定义浮点型
#define CHAR 'a'
int main(){
string name = NAME;
bool able = ABLE;
bool unable = UNABLE;
int money = NUMBER;
double dou = DOUBLE;
char ch = CHAR;
printf("string name = %s;\n"
"bool able = %s;\n"
"bool unable = %s;\n"
"int money = %d;\n"
"double dou = %f;\n"
"char ch = %c;", &NAME[0], &("true")[0], &("false")[0], NUMBER, DOUBLE, CHAR);
/*
* [打印结果]
* string name = BigXMeng;
* bool able = true;
* bool unable = false;
* int money = 100;
* double dou = 266.880000;
* char ch = a;
*/
return 0;
}
1.5 字符输入输出
标准库提供了一次读写一个字符的函数 getchar() 和 putchar():
char ch = getchar(); // 从键盘(标准输入)读取一个字符
putchar(ch); // 将此字符打印到标准输出
将输入复制到输出:
#include <bits/stdc++.h>
using namespace std;
int main(){ /** 将输入复制到输出 */
int c; // 定义为int以便保证能存储下任何可能的字符以及EOF
// EOF(End Of File)文件结束标志 //
c = getchar();
while (c != EOF){
putchar(c);
c = getchar();
}
return 0;
}
更精简的写法:
#include <bits/stdc++.h>
using namespace std;
int main(){ /** 将输入复制到输出 */
int c;
while ((c=getchar()) != EOF){
putchar(c);
}
return 0;
}
1.5.4 单词计数
/**
* Created by Liu Xianmeng on 2022/11/28
* 统计单词的行数、单词数和字符数
*/
#include <bits/stdc++.h>
using namespace std;
#define IN 1
#define OUT 0
int main(){
int c;
int nl; // 多少行
int nw; // 多少单词
int nc; // 多少字符
int state; // 状态(当前遍历字符是否在单词内)
state = OUT;
nl = nw = nc = 0;
while ((c=getchar())!=')'){
++nc; // 字符数自增
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if(state == OUT){ // state==IN 单词数不增加(在程序中不做处理)
state = IN;
++nw;
}
}
printf("%d %d %d", nl, nw, nc);
return 0;
}
1.9 字符数组
编写一个程序,实现输入一组文本行,打印出其中的最长文本
/** 伪代码如下 **/
while(还有未处理的行){
if(该行比已经处理的最长行还要长){
保存该行;
保存该行的长度;
}
}
打印最长的行
/**
* Created by Liu Xianmeng on 2022/11/28
* 编写一个程序,实现输入一组文本行,打印出其中的最长文本
*/
#include <bits/stdc++.h>
using namespace std;
#define MAXLINE 1000 // 允许输入行的最大长度
int getline(char line[], int maxline); // 读取新行
void copy(char to[], char from[]); // 如果新行的长度大于最大长度的行长度 则保存此新行
int main(){
int len; // 当前行长度
int max; // 最大行长度
char line[MAXLINE]; // 当前输入行
char longest[MAXLINE]; // 用于保存最长的输入行
max = 0; // 初始化
// 存储最大长度的行 如果len等于1 则存储的是一个换行 会造成无限循环
// 所以这里修改为 > 1 当输入一个纯粹的换行时 while循环就会结束 然后打印结果
while((len = getline(line, MAXLINE)) > 1){
if(len > max){
max = len;
copy(longest, line);
}
}
// 打印结果
if(max > 0) printf("%s", longest);
return 0;
}
// 读取新行
int getline(char s[], int lim){
int c, i;
// 为什么i<lim-1 因为s[lim-1]用于存储'\0'
for(i=0; i<lim-1 && (c= getchar()) != EOF && c!='\n'; ++i){
s[i] = c;
}
// 程序运行到此处 可能i===lim-1 也可能c==='\n'
if(c == '\n'){ // 如果因 c==='\n' 而结束for循环 则将'\n'存入
s[i] = c;
++i;
}
// 不论是因为i===lim-1 还是 c==='\n'退出for循环 最后都要存储'\0' 这样便于在main函数中打印printf()
s[i] = '\0';
return i;
}
// 如果新行的长度大于最大长度的行长度 则保存此新行from到to字符数组
void copy(char to[], char from[]){
int i;
i=0;
// 将from字符数组拷贝进to字符数组(包含'\0')
while((to[i] = from[i]) != '\0'){
++i;
}
}
1.10 外部变量与作用域
char line[MAXLINE]; // 当前输入行 [声明为外部变量]
char longest[MAXLINE]; // 用于保存最长的输入行 [声明为外部变量]
/**
* Created by Liu Xianmeng on 2022/11/28
* 编写一个程序,实现输入一组文本行,打印出其中的最长文本
*/
#include <bits/stdc++.h>
using namespace std;
#define MAXLINE 1000 // 允许输入行的最大长度
int getline(); // 读取新行
void copy(); // 如果新行的长度大于最大长度的行长度 则保存此新行
int max; // 最大行长度[声明为外部变量]
char line[MAXLINE]; // 当前输入行[声明为外部变量]
char longest[MAXLINE]; // 用于保存最长的输入行[声明为外部变量]
int main(){
int len; // 当前行长度
extern int max; // [声明为外部变量]
max = 0; // 初始化
// 存储最大长度的行 如果len等于1 则存储的是一个换行 会造成无限循环
// 所以这里修改为 > 1 当输入一个纯粹的换行时 while循环就会结束 然后打印结果
while((len = getline()) > 1){
if(len > max){
max = len;
copy();
}
}
// 打印结果
if(max > 0) printf("%s", longest);
return 0;
}
// 读取新行
int getline(){
int c, i;
// 为什么i<lim-1 因为s[lim-1]用于存储'\0'
for(i=0; i<MAXLINE-1 && (c= getchar()) != EOF && c!='\n'; ++i){
line[i] = c;
}
// 程序运行到此处 可能i===lim-1 也可能c==='\n'
if(c == '\n'){ // 如果因 c==='\n' 而结束for循环 则将'\n'存入
line[i] = c;
++i;
}
// 不论是因为i===lim-1 还是 c==='\n'退出for循环 最后都要存储'\0' 这样便于在main函数中打印printf()
line[i] = '\0';
return i;
}
// 如果新行的长度大于最大长度的行长度 则保存此新行from到to字符数组
void copy(){
int i;
i=0;
// 将from字符数组拷贝进to字符数组(包含'\0')
while((longest[i] = line[i]) != '\0'){
++i;
}
}