#include#include #include using std::cout; using std::cin; using std::string; void practice1(void) { int n1, n2, sum = 0; cout << "Input two numbers: "; cin >> n1 >> n2; for (int i = n1; i <= n2; i++) { sum += i; } cout << "Sum " << n1 << " to " << n2 << " is " << sum << std::endl; cout << std::endl; return; } void practice2(void) { const int ArSize = 16; std::array factorials{}; factorials[1] = factorials[0] = 1LL; for (int i = 2; i < ArSize; i++) { factorials[i] = i * factorials[i - 1]; } for (int i = 0; i < ArSize; i++) { cout << i << "! = " << factorials[i] << std::endl << std::fixed; cout.precision(0); } cout << std::endl; return; } void practice3(void) { cout << "Input the number( 0 is over ): "; int i = 100, sum = 0; while (i != 0) { cin >> i; sum += i; } cout << "Sum: " << sum << std::endl; cout << std::endl; return; } void practice4(void)//存款利息 { int i; double Daphne = 0.10 * 100, Cleo = 0.05 * 100, sumCleo = 100.0; for (i = 0; Daphne > Cleo; i++) { Cleo = 0.05 * sumCleo; sumCleo += Cleo; } cout << "Cleo need " << i << " years to exceed Daphne.n" << "Daphne : " << Daphne << "nCleo : " << Cleo << std::endl; cout << std::endl; return; } void practice5(void) { string month[12]={"Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."}; int sale[12], sum = 0; cout << "Enter the one year sale of < > : "; for (int i = 0; i < 12; i++) { cin >> sale[i]; sum += sale[i]; } cout << "Thg one year sale:n"; for (int j = 0; j < 12; j++) { cout << month[j] << " : " << sale[j] << " "; if ((j + 1) % 4 == 0) cout << std::endl; } cout << "Total sale: " << sum << std::endl; cout << std::endl; return; } void practice6(void) { string month[12] = { "Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec." }; int sale[3][12], sum = 0; for (int i = 0; i < 3; i++) { cout << "Enter the " << i + 1 << " year sale of << C++ For Fools >> : "; for (int j = 0 ; j < 12; j++) { cin >> sale[i][j]; sum += sale[i][j]; } } cout << "Thg the three years sale:n"; for (int n= 0; n < 3; n++) { cout << " The " << n + 1 << " year:n"; for (int m = 0; m < 12; m++) { cout << month[m] << ": " << sale[n][m] << " "; if ((m + 1) % 4 == 0) cout << std::endl; } cout << std::endl; } cout << "Total sale: " << sum << std::endl; cout << std::endl; return; } void practice7(void) { struct car { string carMake = {}; int carYearMade = 0; }; int carNumber; cout << "How many cars do you wish to catalog? "; cin >> carNumber; cin.get(); car* carINFO = new car [carNumber]; for (int i = 1; i <= carNumber; i++) { cout << "Car #" << i << std::endl << "Please enter the make: "; getline(cin, carINFO[i - 1].carMake); cout << "Please enter the year made: "; cin >> carINFO[i - 1].carYearMade; cin.get(); } cout << "Here is your collection:n"; for (int j = 1; j <= carNumber; j++) { cout << carINFO[j - 1].carYearMade << " " << carINFO[j - 1].carMake << std::endl; } cout << std::endl; delete[] carINFO; return; } void practice8(void) { char word[256] = {}; int i; cout << "Enter words (to stop, type the word done): n"; for (i = 0; strcmp(word, "done"); i++) { cin >> word; } cout << "You entered a total of " << i - 1 << " words.n"; cout << std::endl; return; } void practice9(void) { string word; int i; cout << "Enter words (to stop, type the word done): n"; for (i = 0; word != "done"; i++) { cin >> word; } cout << "You entered a total of " << i - 1 << " words.n"; cout << std::endl; return; } void practice10(void) { int i, j, k, l; cout << "Input the number: "; cin >> i; char s = '*', d = '.'; for (l = 1; l <= i; l++) { for (j = l; j < i; j++) { cout << d; } for (k = 0; k < l; k++) { cout << s; } cout << std::endl; } return; } int main() { practice10(); return 0; }
个人自写代码仅供参考,如有错误麻烦指出谢谢



