forked from pezy/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
11 changed files
with
257 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// read a sequence of ints from cin and | ||
/// store those values in a vector. | ||
/// | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() | ||
{ | ||
std::vector<int> vec; | ||
int i; | ||
while (std::cin >> i) | ||
vec.push_back(i); | ||
|
||
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,21 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// read a sequence of strings from cin and | ||
/// store those values in a vector. | ||
/// | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
std::vector<std::string> vec; | ||
std::string str; | ||
while (std::cin >> str) | ||
vec.push_back(str); | ||
|
||
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,86 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// print the size and contents of the vectors from exercise 3.13. | ||
/// Check whether your answers to that exercise were correct. | ||
/// If not, restudy § 3.3.1 (p. 97) until you understand why you were wrong. | ||
/// | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
using std::vector; | ||
using std::string; | ||
using std::cout; | ||
using std::endl; | ||
|
||
int main() | ||
{ | ||
vector<int> v1; | ||
cout << "{\n \"v1\":{\"size\":\"" << v1.size() << "\",\"value\":["; | ||
for (auto i : v1) | ||
cout << i << ","; | ||
if (!v1.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<int> v2(10); | ||
cout << " \"v2\":{\"size\":\"" << v2.size() << "\",\"value\":["; | ||
for (auto i : v2) | ||
cout << i << ","; | ||
if (!v2.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<int> v3(10, 42); | ||
cout << " \"v3\":{\"size\":\"" << v3.size() << "\",\"value\":["; | ||
for (auto i : v3) | ||
cout << i << ","; | ||
if (!v3.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<int> v4{10}; | ||
cout << " \"v4\":{\"size\":\"" << v4.size() << "\",\"value\":["; | ||
for (auto i : v4) | ||
cout << i << ","; | ||
if (!v4.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<int> v5{10, 42}; | ||
cout << " \"v5\":{\"size\":\"" << v5.size() << "\",\"value\":["; | ||
for (auto i : v5) | ||
cout << i << ","; | ||
if (!v5.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<string> v6{10}; | ||
cout << " \"v6\":{\"size\":\"" << v6.size() << "\",\"value\":["; | ||
for (auto i : v6) | ||
if (i.empty()) cout << "(null)" << ","; | ||
else cout << i << ","; | ||
if (!v6.empty()) cout << "\b"; | ||
cout << "]}" << endl; | ||
|
||
vector<string> v7{10, "hi"}; | ||
cout << " \"v7\":{\"size\":\"" << v7.size() << "\",\"value\":["; | ||
for (auto i : v7) | ||
if (i.empty()) cout << "(null)" << ","; | ||
else cout << i << ","; | ||
if (!v7.empty()) cout << "\b"; | ||
cout << "]}\n}" << endl; | ||
|
||
return 0; | ||
} | ||
|
||
/// | ||
///@Output(JSON-like format) | ||
///{ | ||
/// "v1":{"size":"0","value":[]} | ||
/// "v2":{"size":"10","value":[0,0,0,0,0,0,0,0,0,0]} | ||
/// "v3":{"size":"10","value":[42,42,42,42,42,42,42,42,42,42]} | ||
/// "v4":{"size":"1","value":[10]} | ||
/// "v5":{"size":"2","value":[10,42]} | ||
/// "v6":{"size":"10","value":[(null),(null),(null),(null),(null),(null),(null),(null),(null),(null)]} | ||
/// "v7":{"size":"10","value":[hi,hi,hi,hi,hi,hi,hi,hi,hi,hi]} | ||
///} | ||
/// |
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,50 +1,48 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// Read a sequence of words from cin and store the values a vector. | ||
/// After you’ve read all the words, process the vector and change each word to uppercase. | ||
/// Print the transformed elements, eight words to a line. | ||
/// | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <string> | ||
|
||
using namespace std; | ||
//using std::string; | ||
|
||
//using namespace std; | ||
|
||
using std::cin; | ||
using std::cout; | ||
using std::endl; | ||
using std::vector; | ||
using std::string; | ||
|
||
int main() | ||
{ | ||
|
||
string str; | ||
|
||
vector<string> vec_str; | ||
while(cin>>str) | ||
{ | ||
if(str.compare("e")==0) | ||
{ | ||
break; | ||
} | ||
|
||
|
||
if(str.compare("q")!=0) | ||
{ | ||
vec_str.push_back(str); | ||
} | ||
else | ||
{ | ||
for (unsigned int i=0; i<vec_str.size();i++) | ||
{ | ||
cout<<vec_str[i] | ||
<<" "; | ||
|
||
|
||
if(i%7==0 && i>0) | ||
{ | ||
cout<<"\n"; | ||
} | ||
} | ||
cout<<"\nthe size is " | ||
<<vec_str.size() | ||
<<"\n"; | ||
vec_str.clear(); | ||
} | ||
} | ||
|
||
return 0; | ||
vector<string> vec; | ||
string word; | ||
while (cin >> word) | ||
vec.push_back(word); | ||
|
||
for (auto &str : vec) | ||
for (auto &c : str) | ||
c = toupper(c); | ||
|
||
for (decltype(vec.size()) i=0; i<vec.size(); ++i) | ||
{ | ||
if (i%8 == 0) cout << endl; | ||
cout << vec[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
return 0; | ||
} | ||
|
||
/// | ||
///@Output | ||
///pezy is a good programmer you can ask any question to him all the time he is playing the codes | ||
///^D(EOF) | ||
///PEZY IS A GOOD PROGRAMMER YOU CAN ASK | ||
///ANY QUESTION TO HIM ALL THE TIME HE | ||
///IS PLAYING THE CODES | ||
/// |
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,10 @@ | ||
#include <vector> | ||
|
||
using std::vector; | ||
|
||
int main() | ||
{ | ||
vector<int> ivec{0}; | ||
ivec[0] = 42; | ||
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,24 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// List three ways to define a vector and give it ten elements, | ||
/// each with the value 42. | ||
/// Indicate whether there is a preferred way to do so and why. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
using std::vector; | ||
|
||
int main() | ||
{ | ||
vector<int> ivec1(10, 42); | ||
vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42}; | ||
vector<int> ivec3; | ||
for (decltype(ivec3.size()) i=0; i != 10; ++i) | ||
ivec3.push_back(42); | ||
|
||
std::cout << "The first one more better!" << std::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,47 @@ | ||
/// | ||
///@Author PEZY | ||
///@Date Aug. 2014 | ||
///@Brief | ||
/// Read a set of integers into a vector. | ||
/// Print the sum of each pair of adjacent elements. | ||
/// Change your program so that it prints the sum of the first and last elements, | ||
/// followed by the sum of the second and second-to-last, and so on. | ||
/// | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using std::vector; | ||
using std::cout; | ||
using std::endl; | ||
using std::cin; | ||
|
||
int main() | ||
{ | ||
vector<int> ivec; | ||
int i; | ||
while (cin >> i) | ||
ivec.push_back(i); | ||
|
||
for(decltype(ivec.size()) i=0; i!=ivec.size()-1; ++i) | ||
cout << ivec[i] + ivec[i+1] << " "; | ||
cout << endl; | ||
|
||
decltype(ivec.size()) size = ivec.size(); | ||
if (size%2 != 0) size = size/2 + 1; | ||
else size /= 2; | ||
|
||
for(decltype(ivec.size()) i=0; i!=size; ++i) | ||
cout << ivec[i] + ivec[ivec.size()-i-1] << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} | ||
|
||
/// | ||
///@Output | ||
///1 2 3 4 5 6 7 8 9 10 | ||
///EOF | ||
///3 5 7 9 11 13 15 17 19 | ||
///11 11 11 11 11 | ||
/// |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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