0%

C++之分支语句与简单文件输入输出

C++之分支语句与简单文件输入输出

字符函数库cctype

函数名称 返回值
isalnum() 如果参数是字母数字,即字母或数字,该函数返回true
isalpha() 如果参数是字母,该函数返回true
iscntrl() 如果参数是控制字符,该函数返回true
isdigit() 如果参数是数字(0~9),该函数返回true
isgraph() 如果参数是除空格之外的打印字符,该函数返回true
islower() 如果参数是小写字母,该函数返回true
isprint() 如果参数是打印字符(包括空格),该函数返回true
ispunct() 如果参数是标点符号,该函数返回true
isspace() 如果参数是标准空白字符,如空格、换行符、回车、水平制表符等,该函数返回true
isupper() 如果参数是大写字母,该函数返回true
isxdigit() 如果参数是十六进制数字,该函数返回true
tolower() 如果参数是大写字符,则返回其小写,否则返回该参数
toupper() 如果参数是小写字符,则返回其大写,否则返回该参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<cctype>

using namespace std;

int main(void)
{
int whitespace=0;
int digits=0;
int chars=0;
int punct=0;
int others=0;

char ch;
cout<<"Enter text for analysis,and type @ to terminate the input"<<endl;
cin.get(ch);

while(ch!='@')
{
if(isalpha(ch))
chars++;
else if(isspace(ch))
whitespace++;
else if(isdigit(ch))
digits++;
else if(ispunct(ch))
punct++;
else
others++;
cin.get(ch);
}
cout<<chars<<" letters"<<endl;
cout<<whitespace<<" whitespace"<<endl;
cout<<digits<<" digits"<<endl;
cout<<punct<<" punctuations"<<endl;
cout<<others<<" others"<<endl;
return 0;
}

结果如下:

image-20221205145331903


Switch语句

  • switch语句中的每一个case标签都必须是一个单独的值,且这个值必须是整数(包括char),因此switch无法处理浮点测试
  • 若每个case语句后不加break,则程序会一直执行下一个case的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;

enum {red,orange,yellow,green,blue,violet,indigo};

//枚举量用作标签
int main(void)
{
int code;
cout<<"Enter colot code(0~6):";
cin>>code;

while(code>=red && code<=indigo)
{
switch(code)
{
case red:
cout<<"You choose red!"<<endl;
break;
case orange:
cout<<"You choose orange!"<<endl;
break;
case yellow:
cout<<"You choose yellow!"<<endl;
break;
case green:
cout<<"You choose green!"<<endl;
break;
case blue:
cout<<"You choose blue!"<<endl;
break;
case violet:
cout<<"You choose violet!"<<endl;
break;
case indigo:
cout<<"You choose indigo!"<<endl;
break;
default:
cout<<"Impossible!"<<endl;
break;
}
cout<<"Enter colot code(0~6):";
cin>>code;
}
cout<<"Bye!"<<endl;
return 0;
}

结果如下:

image-20221205152024353


简单文件输入/输出

1.输出内容至文件

  • 首先需包含头文件:#include<fstream>
  • 其次对输出文件流创建对象:ofstream outFile
  • 接着将此对象与某个文件关联:outFile.open("文件名")
  • 关闭文件:outFile.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<fstream>//需要包括<fstream>头文件

using namespace std;

int main(void)
{
char automobile[50];
int year;
double a_price;
double d_price;

ofstream outFile;//对输出文件流创建对象
outFile.open("carinfo.txt");//将outFile与某个文件相关联

cout<<"Enter the make and model of automobile:";
cin.getline(automobile,50);
cout<<"Enter the model year:";
cin>>year;
cout<<"Enter the orignal asking price:";
cin>>a_price;
d_price=a_price*0.913;

//输出到屏幕上
cout<<"Make and model:"<<automobile<<endl;
cout<<"Year:"<<year<<endl;
cout<<"Was asking:"<<a_price<<endl;
cout<<"Now asking:"<<d_price<<endl;

//输出到文件中
//cout怎么用outFile就怎么用
outFile<<"Make and model:"<<automobile<<endl;
outFile<<"Year:"<<year<<endl;
outFile<<"Was asking:"<<a_price<<endl;
outFile<<"Now asking:"<<d_price<<endl;

outFile.close();
return 0;
}

结果如下:

image-20221210025134082

image-20221210025343690

2.读取文件内容

  • 首先需包含头文件:#include<fstream>
  • 其次对输入文件流创建对象:ifstream inFile
  • 接着将此对象与某个文件关联:inFile.open("文件名")
  • 判断文件是否打开成功:inFile.is_open()
  • 判断从文件中读取数据是否成功:inFile.good()
  • 判断是否读取到文件尾部:inFile.eof()
  • 判断读取数据与变量是否匹配:inFile.fail()
  • 关闭文件:inFile.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
#include<fstream>
#include<cstdlib>//因为使用了exit
using namespace std;

const int SIZE=60;

int main(void)
{
char filename[SIZE];

cout<<"Enter the filename:";
cin.getline(filename,SIZE);

//文件输入流对象
ifstream inFile;
inFile.open(filename);//对象与文件名关联

//判断文件是否打开成功
if(!inFile.is_open())
{
cout<<"Could not open the file"<<endl;
cout<<"Program terminating"<<endl;
exit(EXIT_FAILURE);
}
cout<<"Sucess open the txt file."<<endl;

double value;
double sum=0.0;
int count=0;

inFile>>value;
while(inFile.good())//判断从文件中读取数据是否成功
{
count++;
cout<<value<<endl;
sum+=value;
inFile>>value;
}
if(inFile.eof())//读到文件末尾
{
cout<<"End of file reached"<<endl;
}
else if(inFile.fail())//读取的类型不匹配
{
cout<<"Input terminated by data mismachted"<<endl;
}
else//其他不知名原因
{
cout<<"Input terminated by unknow reason"<<endl;
}

//计算数据平均值
if(count==0)
{
cout<<"No data processed"<<endl;
}
else
{
cout<<"Items read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<sum/count<<endl;
}
//关闭文件
inFile.close();
return 0;
}

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter the filename:score.txt
Sucess open the txt file.
18
19
18.5
13.5
14
16
19.5
20
18
20
18
12
17.5
18.5
End of file reached
Items read:12
Sum:204.5Average:17.0417

输出控制iomanip

  • setw(n):每个数占n个字节的宽度,setw()只对紧接着的输出有效,紧接着的输出结束后又会变回默认的域宽

  • setiosflags(ios::right)

    • ios::right代表右对齐

    • iso::fixed 是操作符setiosflags 的参数之一,该参数指定的动作是以带小数点的形式表示浮点数,并且在允许的精度范围内尽可能的把数字移向小数点右侧;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们

#include <iostream>
#include <iomanip> //对输入输出操纵的头文件

using namespace std;

int main()
{
int a[3];
cin >> a[0] >> a[1] >> a[2];
cout << setiosflags(ios::right) //输出数据右对齐
<< setw(8) << a[0] << ' ' //setw(n) 设置字段宽度为n位
<< setw(8) << a[1] << ' ' //注意:setw()只对紧接着的输出有效,紧接着的输出结束后又会变回默认的域宽
<< setw(8) << a[2] << ' ' << endl;
return 0;
}

结果如下:

1
2
3
4
5
输入:123456789 -1 0
输出:123456789 -1 0

输入:876 12 0
输出: 876 12 0
  • setprecision(n) :控制小数点输出的精度,即是小数点右面的数字的个数为n。
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip> //对输入输出操纵的头文件

using namespace std;

int main()
{
double d;
cin>>d;
cout<<setiosflags(ios::fixed)<<setprecision(12)<<d<<endl;
return 0;
}

结果如下:

1
2
输入:3.7826478265786475864376783
输出:3.782647826579
欢迎来到ssy的世界