<span style="font-size:18px;">#include <iostream>using namespace std; int main(){int number;cout<<"Enter a decimal number:";cin>>number;cout<<"value in octal = 0"<<oct<<number<<endl;cout<<"value in hex = 0x"<
Fillvector.cpp
<span style="font-size:18px;">#include <iostream>#include <fstream>#include <vector>using namespace std; int main(){vector<string> v;ifstream in("Fillvector.cpp");string line;while(getline(in,line)){v.push_back(line);}for(int i = 0;i < v.size();++i){cout<<i<<":"<<v[i]<<endl;}return 0;}</span>
2-1 modify Hello. cpp so that he can print your name and age (or your shoes, dog's age, etc., as long as you like ). Compile and run the modified program.
<span style="font-size:18px;">#include <iostream>using namespace std;int main() { cout << "Hello, World! I am ZY." << endl; cout << "I am 19 years:" << endl; return 0;}</span>
For example, Stream. cpp and Numconv. cpp are used as two examples to compile a program so that it can obtain the circular area and print it based on the input radius value. You can use the "*" operator to calculate the square of the diameter. Note: Do not print in octal or hexadecimal format (they only apply to integer types ).
#include <iostream>using namespace std;int main() { const float pi = 3.141592654; cout << "Enter the radius: "; float radius; cin >> radius; cout << "The area is " << pi * radius * radius << endl; return 0;}
2-3 compile a program to open the file and count the number of words separated by spaces in the file.
#include <string>#include <iostream>#include <fstream>#include <string>using namespace std;int main() { ifstream f("test.cpp"); int nwords = 0; string word; while (f >> word) { ++nwords; } cout << "Number of words = " << nwords << endl; return 0;}
2-4 compile a program to count the number of occurrences of a specific word in the file (use the string operator "=" to find the word ).
#include <iostream>#include <fstream>#include <string>using namespace std;int main(int argc, char* argv[]) { // Process command-line arguments: if (argc < 3) { cerr << "usage: WordCount word file\n"; return -1; } string word(argv[1]); ifstream file(argv[2]); // Count occurrences: long wcount = 0; string token; while (file >> token) if (word == token) ++wcount; // Print result: cout << '"' << word << "\" appeared " << wcount << " times\n";}
2-5 modify Fillvector. cpp to print the rows from the back to the front.
Method 1:
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() { vector<string> v; ifstream in("Fillvector.cpp"); string line; while(getline(in, line)) { v.push_back(line); } // Print backwards: for(int i = v.size()-1; i > 0; --i) { cout << v.size()-1 << ": " << v[i] << endl; } return 0;}
Method 2:
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() { vector<string> v; ifstream in("Fillvector.cpp"<span style="font-family: Arial, Helvetica, sans-serif;">);</span> string line; while(getline(in, line)) v.push_back(line); // Print backwards: int nlines = v.size(); for(int i = 0; i < nlines; i++) { int lineno = nlines-i-1; cout << lineno << ": " << v[lineno] << endl; } return 0;}
2-6 modify Fillvector. cpp to connect all elements in the vector into a separate string and print it, but do not add a row number.
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;int main() { vector<string> v; ifstream in("Fillvector.cpp"); string line; while(getline(in, line)) { v.push_back(line); } // Put lines into a single string: string lines; for(int i = 0; i < v.size(); i++) { lines += v[i] + "\n"; } cout << lines; return 0;}
2-7 compile a program, display a line of the file at a time, and then wait for the user to press the Enter key to display the next line.
#include <string>#include <iostream>#include <fstream>using namespace std;int main() { ifstream in("test.cpp"); string line; while(getline(in, line)) { cout << line; // No endl! cin.get(); } return 0;}
From 2 to 8, create a vector <float> and input 25 floating point numbers to it using a for loop statement to display the results of the vector.
#include <iostream>#include <vector>using namespace std;int main() { // Fill vector: vector<float> v; for (int i = 0; i < 25; ++i) { v.push_back(i + 0.5); } // Display for (int i = 0; i < v.size(); ++i) { cout << v[i]<<" "; } cout << endl; return 0;}
2-9 create three vector <float> objects, and fill in the first two objects as in question 8. Compile a for loop, add each of the first two vectors to the corresponding element, and put the result into the corresponding element of the third vector. The result of the three vectors is displayed.
Method 1:
#include <iostream>#include <vector>using namespace std;int main(){ vector<float> v1, v2; for (int i = 0; i < 25; ++i) { v1.push_back(i); v2.push_back(i + 0.2); } // Form sum: vector<float> v3; for (int i = 0; i < v1.size(); ++i) { v3.push_back(v1[i] + v2[i]); } // Display: for (int i = 0; i < v1.size(); ++i) { cout << v1[i] << " + " << v2[i] << " = " << v3[i] << endl; } return 0;}
Method 2:
#include <iostream>#include <vector>using namespace std;int main(){ vector<float> v1, v2; for (int i = 0; i < 25; ++i) { v1.push_back(i); v2.push_back(i + 0.2); } // Form sum: vector<float> v3; v3.resize(v1.size()); // pre-allocate space for (int i = 0; i < v1.size(); ++i) { v3[i] = v1[i] + v2[i]; } // Display: for (int i = 0; i < v1.size(); ++i) { cout << v1[i] << " + " << v2[i] << " = " << v3[i] << endl; } return 0;}
2-10 compile a program, create a vector <float>, and enter 25 numbers as in the previous exercise. Calculate the square of each number and place them in the same position of the vector. Displays the vector before and after the operation.
#include <iostream>#include <vector>using namespace std;int main() { // Fill and print: vector<float> v; for (int i = 0; i < 25; ++i) { v.push_back(i); } for (int i = 0; i < v.size(); ++i) { cout << v[i]<<" "; } cout<<endl; // Square and print: for (int i = 0; i < v.size(); ++i) { v[i] = v[i] * v[i]; } for (int i = 0; i < v.size(); ++i) { cout << v[i]<<" "; } cout<<endl; return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.