forked from jieniyimiao/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// ex9_47_1.cpp | ||
// Exercise 9.47 | ||
// | ||
// Created by pezy on 12/5/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Write a program that finds each numeric character | ||
// and then each alphabetic character in the string "ab2c3d7R4E6". | ||
// Write two versions of the program. The first should use find_first_of, | ||
// and the second find_first_not_of. | ||
// @Version find_first_of | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
using std::string; using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
string numbers{"123456789"}; | ||
string alphabet{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; | ||
string str{"ab2c3d7R4E6"}; | ||
|
||
cout << "numeric characters: "; | ||
for (string::size_type pos = 0; (pos = str.find_first_of(numbers, pos)) != string::npos; ++pos) | ||
cout << str[pos] << " "; | ||
cout << "\nalphabetic characters: "; | ||
for (string::size_type pos = 0; (pos = str.find_first_of(alphabet, pos)) != string::npos; ++pos) | ||
cout << str[pos] << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// ex9_47_2.cpp | ||
// Exercise 9.47 | ||
// | ||
// Created by pezy on 12/5/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Write a program that finds each numeric character | ||
// and then each alphabetic character in the string "ab2c3d7R4E6". | ||
// Write two versions of the program. The first should use find_first_of, | ||
// and the second find_first_not_of. | ||
// @Version find_first_not_of | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
using std::string; using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
string numbers{"123456789"}; | ||
string alphabet{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; | ||
string str{"ab2c3d7R4E6"}; | ||
|
||
cout << "numeric characters: "; | ||
for (string::size_type pos = 0; (pos = str.find_first_not_of(alphabet, pos)) != string::npos; ++pos) | ||
cout << str[pos] << " "; | ||
cout << "\nalphabetic characters: "; | ||
for (string::size_type pos = 0; (pos = str.find_first_not_of(numbers, pos)) != string::npos; ++pos) | ||
cout << str[pos] << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,36 @@ | ||
//! @Alan | ||
//! | ||
//! Exercise 9.49: | ||
//! A letter has an ascender if, as with d or f, part of the | ||
//! letter extends above the middle of the line. A letter has | ||
//! a descender if, as with p or g, part of the letter extends | ||
//! below the line. | ||
//! Write a program that reads a file containing words and reports | ||
//! the longest word that contains neither ascenders nor descenders. | ||
//! | ||
// | ||
// ex9_49.cpp | ||
// Exercise 9.49 | ||
// | ||
// Created by pezy on 12/5/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief A letter has an ascender if, as with d or f, part of the letter extends | ||
// above the middle of the line. | ||
// A letter has a descender if, as with p or g, part of the letter extends below the line. | ||
// Write a program that reads a file containing words and reports the longest word | ||
// that contains neither ascenders nor descenders. | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <fstream> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
//! Exercise 9.47 | ||
std::string | ||
ex949(const std::string &fileName); | ||
using std::string; using std::cout; using std::endl; using std::ifstream; | ||
|
||
int main() | ||
{ | ||
std::cout << ex949("test.txt"); | ||
return 0; | ||
} | ||
|
||
|
||
std::string | ||
ex949(const std::string &fileName) | ||
{ | ||
std::string word; | ||
std::ifstream fin(fileName); | ||
ifstream ifs("../data/letter.txt"); | ||
if (!ifs) return -1; | ||
string longest_word; | ||
for (string word; ifs >> word; ) | ||
if (word.find_first_not_of("aceimnorsuvwxz") == string::npos && | ||
word.size() > longest_word.size()) | ||
longest_word = word; | ||
|
||
cout << longest_word << endl; | ||
|
||
//! store into a vector | ||
std::vector<std::string> v; | ||
while (fin >> word) | ||
{ | ||
if(word.find_first_not_of("aceimnorsuvwxz") == std::string::npos) | ||
{ | ||
v.push_back(word); | ||
} | ||
} | ||
ifs.close(); | ||
|
||
//! move the longest into the string result | ||
std::string result; | ||
if(v.size() != 0) | ||
{ | ||
for(auto it = v.begin(); it != v.end(); ++it) | ||
{ | ||
if((*it).size() > result.size()) result = *it; | ||
} | ||
} | ||
|
||
return result; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
In typography, an ascender is the portion of a minuscule letter in a Latin-derived alphabet that extends above the mean line of a font. That is, the part of a lower-case letter that is taller than the font's x-height. | ||
|
||
Ascenders, together with descenders, increase the recognizability of words. For this reason, British road signs no longer use all capital letters.[1] | ||
|
||
Studies made at the start of the construction of the British motorway network concluded that words with mixed-case letters were much easier to read than "all-caps" and a special font was designed for motorway signs. These then became universal across the U.K. See Road signs in the United Kingdom. |