1、
#include<iostream> using namespace std; int main() { int i, j; cout << "Enter two numbers: "; cin >> i >> j; //cout << "Sum between " << i << " and " << j << " is " << (i + j) * (j - i + 1) / 2 << endl; int sum = 0; while (i++ <= j) sum += i; cout << "sum: " << sum << endl; }
2、
#include<iostream> #include<array> using namespace std; int main() { const int ArSize = 100; array<long double, ArSize> factorials; factorials[1] = factorials[0] = 1; for (int i = 2; i < ArSize; i++) factorials[i] = factorials[i - 1] * i; for (int i = 0; i < ArSize; i++) cout << "factorials[" << i << "]=" << factorials[i] << endl; }
3、
#include<iostream> #include<array> using namespace std; int main() { int i, sum = 0; cout << "Enter a series of integer; quit when input 0\n"; cin >> i; while (i != 0) { sum += i; cout << "You entered " << i << ", and the sum is: " << sum << endl; cin >> i; } cout << "QUIT\n"; }
4、
#include<iostream> using namespace std; int main() { double Daphe = 100.0, Celo = 100.0; double totalD = Daphe, totalC = Celo; int years = 0; while (totalC <= totalD) { totalD += 0.1 * Daphe; totalC *= 1.05; years++; } cout << "After " << years << " years, Celo has $" << totalC << " assets, more than Daphe with $" << totalD << endl; }
5、
#include<iostream> using namespace std; int main() { int sales[12]; const char* months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int sum = 0; cout << "Enter the books sales each month\n"; for (int i = 0; i < 12; i++) { cout << months[i] << ": \t"; if (strlen(months[i]) < 6) cout << "\t"; cin >> sales[i]; sum += sales[i]; } cout << "The total sales is: " << sum << endl; }
6、
#include<iostream> using namespace std; int main() { int sales[3][12]; const char* Months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int sumEachYear, sumTotal = 0; for (int year = 0; year < 3; year++) { sumEachYear = 0; cout << "Enter the books sales each month in the " << year+1<< "th year\n"; for (int month = 0; month < 12; month++) { cout << Months[month] << ": \t"; if (strlen(Months[month]) < 6) cout << "\t"; cin >> sales[year][month]; sumEachYear += sales[year][month]; } cout << "In the "<<year+1 << "th year, the total sales is: " << sumEachYear << endl; sumTotal += sumEachYear; } cout << "Total sales is " << sumTotal << " for three years\n"; }
7、
#include<iostream> #include<string> using namespace std; //const int CharSize = 20; struct car { string make;//char make[CharSize]; int year; }; int main() { int n; cout << "How many cars do you wish to catalog? "; (cin >> n).get(); car* cars = new car[n]; for (int i = 0; i < n; i++) { cout << "Car #" << i + 1 << endl; cout << "Please enter the make: "; getline(cin, cars[i].make);//cin.getline(cars[i].make, CharSize); cout << "Please enter the year made: "; (cin >> cars[i].year).get(); } cout << "Here is your collection:\n"; for (int i = 0; i < n; i++) cout << cars[i].year << " " << cars[i].make << endl; }
8、
#include<iostream> using namespace std; const int Size = 20; int main() { const char* obj = "done"; char word[Size]; cout << "Enter words (to stop, type the word done)\n"; cin >> word; int count = 0; while (strcmp(word, obj)) { count++; cin >> word; }; cout << "You entered " << count << " words\n"; }
9、
#include<iostream> using namespace std; const int Size = 20; int main() { string obj = "done"; string word; cout << "Enter words (to stop, type the word done)\n"; cin >> word; int count = 0; while (word != obj) { count++; cin >> word; }; cout << "You entered " << count << " words\n"; }
10、
#include<iostream> using namespace std; int main() { int n; cout << "Enter number of rows:"; cin >> n; for (int i = 0; i < n; i++) { string s1(n - i - 1, '.'), s2(i + 1, '*'); cout << s1 << s2 << endl; } }
标签:main,PrimerPlus,cout,int,namespace,C++,第六版,using,include From: https://www.cnblogs.com/xinmind/p/16884774.html