c++ primer 5.9 编程练习

mac2024-03-19  21

#include <iostream> int main() { using namespace std; int a, b, sum; cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; sum = 0; for(int i = a, j = b; i <= j; i++) sum += i; cout << "Sum between " << a << " to " << b << " is " << sum << endl; system("pause"); return 0; } #include <iostream> #include <array> const int ArSize = 101; int main() { using namespace std; array<long double, ArSize> factorials = {1,1}; for (int i = 2; i < ArSize; i++) factorials[i] = i * factorials[i-1]; for (int i= 0; i < ArSize; i++) std::cout << i << "! = " << factorials[i] << std::endl; system("pause"); return 0; } #include <iostream> int main() { using namespace std; int a, sum; sum = 0; do { cout << "Enter a number: "; cin >> a; sum += a; cout << "Total sum is " << sum << endl; }while(a != 0); system("pause"); return 0; } #include <iostream> int main() { using namespace std; double Daphnc = 100, Clco = 100; int year = 0; do { year++; Daphnc = 100 + year * 10; Clco = Clco * 1.05; }while(Daphnc >= Clco); cout << "After " << year << " year " << "Clco beyond Daphn." << endl; system("pause"); return 0; } #include <iostream> #include <string> int main() { using namespace std; string month[12] = {"January","February","March","April","May","June","July","August","September", "October","November","December"}; int sold[12], sum = 0; for (int i = 0; i < 12; i++) { cout << "Enter the " << month[i] << " month sold:"; cin >> sold[i]; sum += sold[i]; } cout << "The anuual sale is " << sum << endl; system("pause"); return 0; } #include <iostream> #include <string> int main() { using namespace std; string month[12] = {"January","February","March","April","May","June","July","August","September", "October","November","December"}; string year[3] = {"first", "second", "third"}; int sold[3][12], sum = 0; for (int j = 0; j < 3; j++) { cout << "The " << year[j] << " data:\n"; for (int i = 0; i < 12; i++) { cout << "Enter the " << month[i] << " month sold:"; cin >> sold[j][i]; sum += sold[j][i]; } } cout << "The three years sales are " << sum << endl; system("pause"); return 0; } #include <iostream> #include <string> using namespace std; struct car { string manufature; int year; }; int main() { int number; cout << "How many cars do you wish to catalog? "; cin >> number; car * data = new car[number]; for (int i = 0; i < 2;i++) { cout << "Car #" << i + 1 << ":\n"; cout << "Please enter the make: "; cin.get(); getline(cin, data[i].manufature); cout << "Please enter the year made:"; cin >> data[i].year; } cout << "Here is your collection:\n"; for (int i = 0; i < 2; i++) { cout << data[i].year << " " << data[i].manufature << endl; } system("pause"); return 0; #include <iostream> #include <cstring> int main() { using namespace std; char words[100]; int count = 0; cout << "Enter words (to stop, type the word done):\n"; do { cin >> words; count++; }while(strcmp(words, "done")); cout << "Your entered a total of " << count - 1 << " words.\n"; system("pause"); return 0; } #include <iostream> #include <string> #include <cstring> int main() { using namespace std; string words; int count = 0; cout << "Enter words (to stop, type the word done):\n"; do { cin >> words; count++; }while(words != "done"); cout << "Your entered a total of " << count - 1 << " words.\n"; system("pause"); return 0; } #include <iostream> int main() { using namespace std; int number; cout << "Enter number of rows: "; cin >> number; for (int t = 0; t < number; t++) { for(int i =number - t - 1; i > 0; i--) cout <<"."; for(int j = 0 ;j <= t;j++) cout << "*"; cout << endl; } system("pause"); return 0; }
最新回复(0)