#include<iostream>usingnamespace std;intmain(){int x;int y;int sum;
cout <<"Please enter two number: ";
cin >> x>> y;int z = x;for(sum =0; x <= y; x++)
sum += x;
cout <<"The sum of "<< z <<" to "<< y <<" is: "<< sum << endl;return0;}
#include<iostream>#include<array>usingnamespace std;constint Arsize =100;intmain(){
array<longdouble, Arsize +1>factorials;//array 用法:array<类型;元素个数>数组名
factorials[0]= factorials[1]=1.0;for(int i =2; i < Arsize; i++)
factorials[i]= i * factorials[i -1];for(int i =0; i < Arsize; i++)
cout << i <<"! = "<< factorials[i]<< endl;return0;}
#include<iostream>usingnamespace std;intmain(){int x;int sum=0;
cout <<"Please enter the number: ";
cin >> x;while(x!=0){
sum += x;
cout <<"Untill now, the sum is: "<< sum << endl;
cin >> x;}return0;}
#include<iostream>usingnamespace std;intmain(){int x;double SumDa=100.0;double SumCl=100.0;double InCl;for(x =1; SumDa >= SumCl; x++){
SumDa =100.0+0.1*100.0* x;
InCl =0.05* SumCl;
SumCl += InCl;}
cout << x <<" years later, Cleo have :"<< SumCl <<"money, and Daphne have "<< SumDa <<" money. "<< endl;return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){
string month[12]={"January","February","March","April","May","June","July","August","Septempber","October","November","December"};int sel[12];int sum=0;int i =0;
cout <<"Enter the sales of book each month: ";while(i<12){
cout << month[i]<<" sell : ";
cin >> sel[i];
sum += sel[i];
i +=1;}
cout <<"The sum is: "<< sum;}
#include<iostream>#include<string>usingnamespace std;intmain(){
string month[12]={"January","February","March","April","May","June","July","August","Septempber","October","November","December"};int maxtemps[3][12];int totalsum =0;int sum[3]={0};
cout <<"Enter the sales of book each month: ";for(int year =0; year <3; year++){for(int mon =0; mon <12; mon++){
cout <<"Enter "<< year +1<<" year's "<< month[mon]<<": ";
cin >> maxtemps[year][mon];
sum[year]+= maxtemps[year][mon];}
totalsum += sum[year];}
cout <<"The sales of first year is "<< sum[0]<< endl;
cout <<"The sales of second year is "<< sum[1]<< endl;
cout <<"The sales of third year is "<< sum[2]<< endl;
cout <<"The total sales is "<< totalsum<< endl;return0;}
#include<iostream>#include<string>usingnamespace std;struct car
{
string company;int year;};intmain(){int i;
cout <<"How many cars do you wish to enter : ";
cin >> i;
cin.get();//使用get函数简单的暂停屏幕直到按回车键,且不需要储存字符struct car* p1 =newstruct car[i];for(int x =0; x < i; x++){
cout <<"Car # "<< x+1<<" : "<< endl;
cout <<"Please enter the make: ";getline(cin, p1[x].company);
cout <<"Please enter the year made: ";
cin >> p1[x].year;
cin.get();}
cout <<"Here is your collection: "<< endl;for(int x =0; x < i; x++){
cout << p1[x].year <<" "<< p1[x].company << endl;}return0;}
#include<iostream>#include<cstring>usingnamespace std;intmain(){int n =0;char input[128];
cout <<"Enter words (to stop, type the word done) : "<< endl;while(cin>>input){if(strcmp(input,"done")){
n++;}else{break;}}
cout <<"You entered a total of "<< n <<" words. "<< endl;return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){int n =0;
string input;
cout <<"Enter words (to stop, type the word done) : "<< endl;while(cin>>input){if(input !="done"){
n++;}else{break;}}
cout <<"You entered a total of "<< n <<" words. "<< endl;return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){int row ;
cout <<"Enter number of rows: ";
cin >> row;for(int i =1; i <= row; i++){for(int j = i; j < row; j++){
cout <<".";}for(int j =0; j < i; j++){
cout <<"*";}
cout << endl;}return0;}
三、编程练习答案#include<iostream>using namespace std;int main(){ int x; int y; int sum; cout << "Please enter two number: "; cin >> x>> y; int z = x; for (sum = 0; x <= y; x++) sum += x; cout << "The sum of " <