第五章
第一题:
#include <iostream>
using namespace std;
int main()
{
int a=0;
int b=0;
int SUM=0;
cout<<"Please enter two numbers a b(a<b):\n";
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
for(int i=a;i<=b;i++)
{
SUM+=i;
}
cout<<"The SUM of the numbers from a to b is: "<<SUM;
}
第二题:
#include <iostream>
#include <array>
using namespace std;
const int ArSize=101;
int main()
{
array<long double,ArSize> factorials;
factorials[1]=factorials[0]=1;
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;
}
第三题:
#include <iostream>
using namespace std;
const int MaxSize=100;
int main()
{
double num[MaxSize]={};
double SUM=0;
bool stop=false;
cout<<"Please enter a number(Enter zero to stop): ";
for(int i=0;(i<MaxSize)&&(stop==false);i++)
{
//Initialize one number in one loop in order to identify the stop signal
num[i]=1;
cin>>num[i];
SUM+=num[i];
if(num[i]!=0)
cout<<"Up to now, the SUM of the numbers is: "<<SUM<<endl;
else
stop=true;
}
cout<<"Loop exit.";
}
第四题:
#include <iostream>
using namespace std;
int main()
{
int year=0;
double Init=100;
double DaphneTotal=100;
double CleoTotal=100;
for(year=1;DaphneTotal>=CleoTotal;year++)
{
DaphneTotal+=Init*0.1;
CleoTotal+=CleoTotal*0.05;
}
cout<<year-1
<<" years later, the investment value of Cleo exceed that of Daphne."<<endl;
cout<<"Cleo: "<<CleoTotal<<endl;
cout<<"Daphne: "<<DaphneTotal<<endl;
}
第五题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sales[12]={};
int SUM=0;
string month[12]=
{"Jan","Fen","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
cout<<"Please enter the sales of <C++ For Fools> every month."<<endl;
for(int i=0;i<12;i++)
{
cout<<month[i]<<": ";
cin>>sales[i];
SUM+=sales[i];
}
cout<<"The sales of the whole year is: "<<SUM<<endl;
}
第六题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sales[3][12]={};
int SUM[3]={};
string month[12]=
{"Jan","Fen","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
cout<<"Please enter the sales of <C++ For Fools> every month."<<endl;
cout<<"Fisrt year: "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<12;j++)
{
cout<<month[j]<<": ";
cin>>sales[i][j];
SUM[i]+=sales[i][j];
}
cout<<"This year's sales is: "<<SUM[i]<<endl;
if(i!=2)
cout<<"Next year: "<<endl;
else
cout<<"The total sales is: "<<SUM[0]+SUM[1]+SUM[2]<<endl;
}
}
第七题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
struct car
{
string make;
int yearmade;
};
cout<<"How many cars do wish to catalog? ";
cin>>num;
cin.get();
car *carlist=new car[num];
for(int i=0;i<num;i++)
{
cout<<"Car #"<<i+1<<":"<<endl;
cout<<"Please enter the make: ";
getline(cin,carlist[i].make);
cout<<"Please enter the year made: ";
cin>>carlist[i].yearmade;
cin.get();
}
cout<<"Here is your collection:"<<endl;
for(int i=0;i<num;i++)
{
cout<<carlist[i].yearmade<<" "<<carlist[i].make<<endl;
}
}
第八题:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ch[20];
int count=0;
cout<<"Enter words; enter 'done' to quit."<<endl;
while(cin>>ch)
{
if(strcmp(ch,"done"))
count++;
else
break;
}
cout<<"You entered "<<count<<" words";
}
第九题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string st;
int count=0;
cout<<"Enter words; enter 'done' to quit."<<endl;
while(cin>>st)
{
if(st!="done")
count++;
else
break;
}
cout<<"You entered "<<count<<" words";
}
第十题:
#include <iostream>
using namespace std;
int main()
{
int line;
cout<<"How many line do you want to show? ";
cin>>line;
for(int i=1;i<=line;i++)
{
for(int j=0;j<line-i;j++)
cout<<".";
for(int k=0;k<i;k++)
cout<<"*";
cout<<endl;
}
}