板凳——————————————————c++(104-1)

本文通过多个实例展示了C++中字符串处理和输入输出的常见操作,包括使用`std::cout`、`std::cin`、`std::getline()`、`std::putback()`、`std::setw()`、`std::setprecision()`等函数进行格式化输出、错误处理、输入检查等。还涉及到`std::list`、`std::vector`、`std::deque`等容器的迭代器使用,以及`std::for_each`、`std::find`等算法的应用。
摘要由CSDN通过智能技术生成

#if 0//17.1 write.cpp 2021年03月16日 星期二 19时05分03秒
#include
#include

int main(){
const char * state1 = “Florida”;
const char * state2 = “Kansas”;
const char * state3 = “Euphoria”;
int len = std::strlen(state2);
std::cout << “Increasing loop index:\n”;
int i;
for(i = 1; i <= len; i++){
std::cout.write(state2, i);
std::cout << std::endl;
}
std::cout << “Decreasing loop index:\n”;
for(i = len; i > 0; i–)
std::cout.write(state2, i) << std::endl;
std::cout << “Exceeding string length:\n”;
std::cout.write(state2, len + 5) << std::endl;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Increasing loop index:
K
Ka
Kan
Kans
Kansa
Kansas
Decreasing loop index:
Kansas
Kansa
Kans
Kan
Ka
K
Exceeding string length:
KansasEuph

#endif

#if 0//17.2 defaults.cpp 2021年03月16日 星期二 19时10分12秒
#include

int main(){
std::cout << “12345678901234567890\n”;
char ch = ‘K’;
int t = 273;
std::cout << ch << “:\n”;
std::cout << t << “:\n”;
std::cout << -t << “:\n”;
double f1 = 1.200;
std::cout << f1 << “:\n”;
std::cout << (f1 + 1.0/9.0) << “:\n”;

double f2 = 1.67E2;
std::cout << f2 << ":\n";
f2 += 1.0/9.0;
std::cout << f2 << ":\n";
std::cout << (f2 * 1.0e4) << ":\n";

double f3 = 2.3e-4;
std::cout << f3 << ":\n";
std::cout << f3 / 10 << ":\n";

return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
12345678901234567890
K:
273:
-273:
1.2:
1.31111:
167:
167.111:
1.67111e+06:
0.00023:
2.3e-05:

#endif

#if 0//17.3 manip.cpp 2021年03月16日 星期二 19时15分01秒
#include

int main(){
std::cout << "Enter an integer: “;
int n;
std::cin >> n;
std::cout << “n n*n\n”;
std::cout << n << " " << n * n << " (decimal)\n”;
std::cout << std::hex;
std::cout << n << " “;
std::cout << n * n << " (hexadecimal)\n”;

std::cout << std::oct << n << "   " << n * n << "  (octal)\n";
	
dec(std::cout);
std::cout << n << "   " << n * n << "  (decimal)\n";

return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter an integer: 13
n n*n
13 169 (decimal)
d a9 (hexadecimal)
15 251 (octal)
13 169 (decimal)

#endif

#if 0// 17.4 width.cpp 2021年03月16日 星期二 19时19分54秒
#include

int main(){
int w = std::cout.width(30);
std::cout << "default field width = "<< w << “:\n”;
std::cout.width(5);
std::cout << “N” << ‘:’;
std::cout.width(8);
std::cout << “N * N” << “:\n”;
for(long i = 1; i <= 100; i *= 10){
std::cout.width(5);
std::cout << i << ‘:’;
std::cout.width(8);
std::cout << i * i << “:\n”;
}
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
default field width = 0:
N: N * N:
1: 1:
10: 100:
100: 10000:

#endif

#if 0 //17.5 fill.cpp 2021年03月16日 星期二 19时23分43秒
#include

int main(){
std::cout.fill(’*’);
const char * staff[2] { “Waldo Whipsnade”, “Wilmarie Wooper”};
long bonus[2] {900, 1350};
for(int i = 0; i < 2; i++){
std::cout << staff[i] << ": KaTeX parse error: Undefined control sequence: \n at position 55: …< bonus[i] << "\̲n̲"; } return … ./hello
Waldo Whipsnade: $****900
Wilmarie Wooper: $***1350

#endif

#if 0//17.6 precise.cpp 2021年03月16日 星期二 19时26分54秒
#include

int main(){
float price1 = 20.40;
float price2 = 1.9 + 8.0/9.0;
std::cout << "“Furry Friends” is KaTeX parse error: Undefined control sequence: \n at position 18: …<< price1 << "!\̲n̲"; std::cout <…" << price2 << “!\n”;

std::cout.precision(2);	
std::cout << "\"Furry Friends\" is $" << price1 << "!\n";
std::cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
“Furry Friends” is $20.4!
“Fiery Fiends” is $2.78889!
“Furry Friends” is $20!
“Fiery Fiends” is $2.8!

#endif

#if 0//17.7 showpt.cpp 2021年03月16日 星期二 19时31分18秒
#include
int main(){
float price1 = 20.40;
float price2 = 1.9 + 8.0/9.0;
std::cout.setf(std::ios_base::showpoint);
std::cout << "“Furry Friends” is KaTeX parse error: Undefined control sequence: \n at position 18: …<< price1 << "!\̲n̲"; std::cout <…" << price2 << “!\n”;

std::cout.precision(2);	
std::cout << "\"Furry Friends\" is $" << price1 << "!\n";
std::cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
“Furry Friends” is $20.4000!
“Fiery Fiends” is $2.78889!
“Furry Friends” is $20.!
“Fiery Fiends” is $2.8!

#endif

#if 0//17.8 setf.cpp 2021年03月16日 星期二 19时34分35秒
#include

int main(){
int temperature = 63;
std::cout << "Today’s water temperature: ";
std::cout.setf(std::ios_base::showpos);
std::cout << temperature << std::endl;

std::cout << "For our programming friends, that's\n";
std::cout << std::hex << temperature << std::endl;
std::cout.setf(std::ios_base::uppercase);
std::cout.setf(std::ios_base::showbase);
std::cout << "or\n";
std::cout << temperature << std::endl;
std::cout << "How " << true << "! oops --How ";	
std::cout.setf(std::ios_base::boolalpha);
std::cout << true << "!\n";

return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Today’s water temperature: +63
For our programming friends, that’s
3f
or
0X3F
How 0X1! oops --How true!

#endif

#if 0//17.9 setf2.cpp 2021年03月16日 星期二 19时39分54秒
#include
#include

int main(){
std::cout.setf(std::ios_base::left, std::ios_base::adjustfield);
std::cout.setf(std::ios_base::showpos);
std::cout.setf(std::ios_base::showpoint);
std::cout.precision(3);
std::ios_base::fmtflags old = std::cout.setf(std::ios_base::scientific,
std::ios_base::floatfield);
std::cout << “Left Justification:\n”;
long n;
for(n = 1; n <= 41; n += 10){
std::cout.width(4);
std::cout << n << “|”;
std::cout.width(12);
std::cout << sqrt(double (n)) << “|\n”;
}

std::cout.setf(std::ios_base::internal, std::ios_base::adjustfield);
std::cout.setf(old, std::ios_base::floatfield);
std::cout << "Internal Justification:\n";
for(n = 1; n <= 41; n += 10){
	std::cout.width(4);
	std::cout << n << "|";
	std::cout.width(12);
	std::cout << sqrt(double (n)) << "|\n";
}	

std::cout.setf(std::ios_base::right, std::ios_base::adjustfield);
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout << "Right Justification:\n";
for(n = 1; n <= 41; n += 10){
	std::cout.width(4);
	std::cout << n << "|";
	std::cout.width(12);
	std::cout << sqrt(double (n)) << "|\n";
}	

return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Left Justification:
+1 |+1.000e+00 |
+11 |+3.317e+00 |
+21 |+4.583e+00 |
+31 |+5.568e+00 |
+41 |+6.403e+00 |
Internal Justification:

  • 1|+ 1.00|
  • 11|+ 3.32|
  • 21|+ 4.58|
  • 31|+ 5.57|
  • 41|+ 6.40|
    Right Justification:
    +1| +1.000|
    +11| +3.317|
    +21| +4.583|
    +31| +5.568|
    +41| +6.403|

#endif

#if 0//17.10 iomanip.cpp 2021年03月16日 星期二 19时48分39秒
#include
#include
#include

int main(){
std::cout << std::fixed << std::right;
std::cout << std::setw(6) << “N” << std::setw(14) << “square root”
<< std::setw(15) << “fourth root\n”;
double root;
for(int n = 10; n <= 100; n += 10){
root = sqrt(double(n));
std::cout << std::setw(6) << std::setfill(’.’) << n << std::setfill(’ ')
<< std::setw(12) << std::setprecision(3) << root
<< std::setw(14) << std::setprecision(4) << sqrt(root)
<< std::endl;
}
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
N square root fourth root
…10 3.162 1.7783
…20 4.472 2.1147
…30 5.477 2.3403
…40 6.325 2.5149
…50 7.071 2.6591
…60 7.746 2.7832
…70 8.367 2.8925
…80 8.944 2.9907
…90 9.487 3.0801
…100 10.000 3.1623

#endif
#if 0//17.11 check_it.cpp 2021年03月17日 星期三 19时03分00秒
#include

int main(){
std::cout << "Enter numbers: ";
int sum = 0;
int input;
while(std::cin >> input){
sum += input;
}
std::cout << "Last value entered = " << input << std::endl;
std::cout << "Sum = " << sum << std::endl;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter numbers: 200
10 -50 -123z 60
Last value entered = 0
Sum = 37

#endif

#if 0//17.12 cinexcp.cpp 2021年03月17日 星期三 19时06分29秒
#include
#include

int main(){
std::cin.exceptions(std::ios_base::failbit);
std::cout << "Enter numbers: ";
int sum = 0;
int input;
try{
while(std::cin >> input){
sum += input;
}
}catch(std::ios_base::failure & bf){
std::cout << bf.what() << std::endl;
std::cout << “O! the horror!\n”;
}
std::cout << "Last value entered = " << input << std::endl;
std::cout << "Sum = " << sum << std::endl;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter numbers: 20 30 40 pi 6
basic_ios::clear: iostream error
O! the horror!
Last value entered = 0
Sum = 90

#endif

#if 0//17.13 get_gun.cpp 2021年03月17日 星期三 19时11分31秒
#include
const int Limit = 255;

int main(){
char input[Limit];
std::cout << “Enter a string for getline() processing:\n”;
std::cin.getline(input, Limit, ‘#’);
std::cout << “Here is your input:\n”;
std::cout << input << “\nDone with phase 1\n”;

char ch;
std::cin.get(ch);
std::cout << "The next input character is " << ch << std::endl;
if(ch != '\n')
	std::cin.ignore(Limit, '\n');
std::cout << "Enter a string for get() processing:\n";
std::cin.get(input, Limit, '#');
std::cout << "Here is your input:\n";
std::cout << input << "\nDone with phase 2\n";

std::cin.get(ch);
std::cout << "The next input character is " << ch << std::endl;	
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter a string for getline() processing:
Please pass
me a #3 melon!
Here is your input:
Please pass
me a
Done with phase 1
The next input character is 3
Enter a string for get() processing:
I still
want my #3 melon!
Here is your input:
I still
want my
Done with phase 2
The next input character is #

#endif

#if 0//17.14 peeker.cpp 2021年03月17日 星期三 19时17分42秒
#include

int main(){
char ch;
while(std::cin.get(ch)){
if(ch != ‘#’)
std::cout << ch;
else{
std::cin.putback(ch);
break;
}
}
if(!std::cin.eof()){
std::cin.get(ch);
std::cout << std::endl << ch << " is next input character.\n";
}else{
std::cout << “End of file reached.\n”;
std::exit(0);
}

while(std::cin.peek() != '#'){
	std::cin.get(ch);	
	std::cout << ch;
}
if(!std::cin.eof()){
	std::cin.get(ch);
	std::cout << std::endl << ch << " is next input character.\n";
}else
	std::cout << "End of file reached.\n";

return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
I used a #3 pencil when I should have used a #2.
I used a

is next input character.

3 pencil when I should have used a

is next input character.

wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
I used a #3 pencil when I should have used a #2.
I used a

is next input character.

3 pencil when I should have used a

is next input character.

#endif

#if 0// 17.15 truncate.cpp 2021年03月17日 星期三 19时23分38秒
#include
const int SLEN = 10;
inline void eatline() { while (std::cin.get() != ‘\n’) continue; }

int main(){
char name[SLEN];
char title[SLEN];
std::cout << "Enter your name: ";
std::cin.get(name, SLEN);
if(std::cin.peek() != ‘\n’)
std::cout << "Sorry, we only have enough room for "
<< name << std::endl;
eatline();
std::cout << “Dear " << name << “, enter your title: \n”;
std::cin.get(title, SLEN);
if(std::cin.peek() != ‘\n’)
std::cout << " We were forcedd to truncate your title.\n”;
eatline();
std::cout << " Name: " << name << "\nTitle: " << title << std::endl;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter your name: Ella Fishsniffer
Sorry, we only have enough room for Ella Fis
Dear Ella Fis, enter your title:
Excutive Adjunct
We were forcedd to truncate your title.
Name: Ella Fis
Title: Excutive
#endif

#if 0// 17.16 fileio.cpp 2021年03月17日 星期三 19时30分42秒
#include
#include
#include

int main(){
std::string filename;
std::cout << "Enter name for new file: ";
std::cin >> filename;
std::ofstream fout(filename.c_str());
fout << “For your eyes only!\n”;
std::cout << "Enter your secret number: ";
float secret;
std::cin >> secret;
fout << "Your secret number is " << secret << std::endl;
fout.close();

std::ifstream fin(filename.c_str());
std::cout << "Here are the contents of " << filename << ":\n";
char ch;
while(fin.get(ch))
	std::cout << ch;
std::cout << "Done\n";
fin.close();	
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter name for new file: pythag
Enter your secret number: 3.14159
Here are the contents of pythag:
For your eyes only!
Your secret number is 3.14159
Done

#endif

#if 0//17.17 count.cpp 2021年03月17日 星期三 19时38分28秒
#include
#include
#include

int main(int argc, char * argv[]){
if(argc == 1){
std::cerr << “Usage: " << argv[0] << " filename[s]\n”;
exit(EXIT_FAILURE);
}
std::ifstream fin;
long count;
long total = 0;
char ch;
for(int file = 1; file < argc; file++){
fin.open(argv[file]);
if(!fin.is_open()){
std::cerr << “Could not open " << argv[file] << std::endl;
fin.clear();
continue;
}
count = 0;
while(fin.get(ch))
count++;
std::cout << count << " characters in " << argv[file] << std::endl;
total += count;
fin.clear();
fin.close();
}
std::cout << total << " characters in all files\n”;
return 0;
}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello paris rome
Could not open paris
Could not open rome
0 characters in all files
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Usage: ./hello filename[s]

#endif

#if 0// 17.18 append.cpp 2021年03月17日 星期三 19时48分13秒
#include
#include
#include
#include

const char * file = “guest.txt”;

int main(){
char ch;
std::ifstream fin;
fin.open(file);
if(fin.is_open()){
std::cout << “Here are the current contents of the "
<< file << " file:\n”;
while(fin.get(ch))
std::cout << ch;
fin.close();
}

std::ofstream fout(file, std::ios::out | std::ios::app);
if(!fout.is_open()){
	std::cerr << "Can't open " << file << " file for output.\n";
	exit(EXIT_FAILURE);
}
std::cout << "Enter guest names(enter a blank line to quit):\n";
std::string name;
while(getline(std::cin, name) && name.size() > 0){
	fout << name << std::endl;
}
fout.close();
	
fin.clear();	
fin.open(file);
if(fin.is_open()){
	std::cout << "Here are the new contents of the "
	<< file << " file:\n ";
	while(fin.get(ch))
		std::cout << ch;
	fin.close();
}
std::cout << "Done.\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter guest names(enter a blank line to quit):
Genghis Kant
Hank Attila
Charles Bigg
^Z
[2]+ 已停止 ./hello
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Here are the current contents of the guest.txt file:
Genghis Kant
Hank Attila
Charles Bigg
Enter guest names(enter a blank line to quit):
Greta Greppo
LaDonna Mobile
Fannie Mae
q
[3]+ 已停止 ./hello
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Here are the current contents of the guest.txt file:
Genghis Kant
Hank Attila
Charles Bigg
Greta Greppo
LaDonna Mobile
Fannie Mae
q
Enter guest names(enter a blank line to quit):

#endif
#if 0//17.19 binary.cpp 2021年03月18日 星期四 19时12分17秒
#include
#include
#include
#include

inline void eatline(){ while (std::cin.get() != ‘\n’) continue;}
struct planet{
char name[20];
double population;
double g;
};

const char * file = “planets.dat”;

int main(){
planet p1;
std::cout << std::fixed << std::right;

std::ifstream fin;
fin.open(file, std::ios_base::in | std::ios_base::binary);
if(fin.is_open()){
	std::cout << "Here are the current contents of the "
	<< file << " file:\n";
while (fin.read((char *)&p1, sizeof p1)){
	std::cout << std::setw(20) << p1.name<< ": "
	<< std::setprecision(0) << std::setw(12) << p1.population
	<< std::setprecision(2) << std::setw(6) << p1.g << std::endl;
}
fin.close();
}

std::ofstream fout (file, std::ios_base::out | std::ios_base::app |
	std::ios_base::binary);
if(!fout.is_open()){
	std::cerr << "Can't open " << file << " file for output:\n";
	exit(EXIT_FAILURE);
}
std::cout << "Enter planet name(enter a blank line to quit):\n";
std::cin.get(p1.name, 20);
while(p1.name[0] != '\0'){
	eatline();
	std::cout << "Enter planetary population: ";
	std::cin >> p1.population;
	std::cout << "Enter planet's acceleration of gravity: ";
	std::cin >> p1.g;
	eatline();
	fout.write((char *) &p1, sizeof p1);
	std::cout << "Enter planet name(enter a blank line " 
	"to quit):\n";
	std::cin.get(p1.name, 20);
}
fout.close();
	
fin.clear();
fin.open(file, std::ios_base::in | std::ios_base::binary);
if(fin.is_open()){
	std::cout << "Here are the new contents of the "
	<< file << " file:\n";
	while(fin.read((char *) & p1, sizeof p1)){
		std::cout << std::setw(20) << p1.name << ": "
	<< std::setprecision(0) << std::setw(12) << p1.population
	<< std::setprecision(2) << std::setw(6) << p1.g << std::endl;
	}
	fin.close();
}
std::cout << "Done.\n";
return 0;

}
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Enter planet name(enter a blank line to quit):
Earth
Enter planetary population: 6928198253
Enter planet’s acceleration of gravity: 9.81
Enter planet name(enter a blank line to quit):

Here are the new contents of the planets.dat file:
Earth: 6928198253 9.81
Done.
wannian07@wannian07-PC:~/Desktop/c++Primer Plus$ ./hello
Here are the current contents of the planets.dat file:
Earth: 6928198253 9.81
Enter planet name(enter a blank line to quit):
Jenny’s World
Enter planetary population: 32155648
Enter planet’s acceleration of gravity: 8.93
Enter planet name(enter a blank line to quit):

Here are the new contents of the planets.dat file:
Earth: 6928198253 9.81
Jenny’s World: 32155648 8.93
Done.

#endif

#if 0//17.20 random.cpp 20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值