C++ Primer Plus 学习笔记 第五章 编程练习

时间:2023-02-14 11:57:04

第一题

#include <iostream>
using namespace std;

int main()
{
cout<<"请输入两个整数,将计算两个整数之间(包括这两个整数)所有整数的和\n";
int first,twice,last=0;
cin>>first>>twice;
for (int i=first;i<=twice;i++)
{
last=last+i;
}
cout<<"和为:"<<last<<endl;
return 0;
}

第二题

#include <iostream>
#include <array>
using namespace std;
const int ArSize = 100;
int main()
{
//array<int,n_elem> arr;
array<long double,ArSize> factorials;
factorials[1]=factorials[0]=1;
for (int i=2;i<ArSize;i++)
{
factorials[i]=i*factorials[i-1];
}

cout.setf(ios_base::fixed);
for (int i=0;i<ArSize;i++)
{
cout<<i<<"!="<<factorials[i]<<endl;
}
return 0;
}

第三题

#include <iostream>
using namespace std;
int main()
{
cout<<"请输入数字,将输出累计和,输入0停止\n";
int number,sum=0;
do
{
cin>>number;
sum=sum+number;
}while(number!=0);
cout<<"累计和为:"<<sum<<endl;
}

第四题

#include <iostream>
using namespace std;
const double simple_interest = 0.10;//
const double compound_interest = 0.05;
int main()
{
double Daphne,Cleo;
int number=0;
Daphne=Cleo=100;
while(Daphne>=Cleo)
{
Daphne = Daphne+10;
Cleo = Cleo+Cleo*compound_interest;
number++;
}
cout<<"Daphne: "<<Daphne<<"美元"<<endl;
cout<<"Cleo: "<<Cleo<<"美元"<<endl;
cout<<number<<"年之后Cleo比Daphne多"<<endl;
return 0;
}

第五题

#include <iostream>
using namespace std;
const int month = 12;
int main()
{
const char * Month[month]=
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"Aguest",
"September",
"October",
"November",
"December"

};
int Sales[month];
int sum=0;
cout<<"输入每个月的销量"<<endl;
for (int i=0;i<12;i++)
{
cout<<Month[i]<<":";
cin>>Sales[i];
sum=sum+Sales[i];
}
cout<<"今年的销量为:"<<sum<<endl
return 0;
}

第六题

#include <iostream>
using namespace std;
const int month = 12;
const int years = 3;
int main()
{
const char * Month[month]=
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"Aguest",
"September",
"October",
"November",
"December"
};
int Sales[years][month];
int annual[years]={0};
int sum=0;

for (int i=0;i<years;i++)
{
cout<<"第"<<i+1<<"年\n";
cout<<"输入每个月的销量"<<endl;
for (int j=0;j<month;j++)
{

cout<<Month[j]<<":";
cin>>Sales[i][j];
annual[i]=annual[i]+Sales[i][j];
}
cout<<"第"<<i+1<<"年的销量是:"<<annual[i]<<endl;
sum=sum+annual[i];
}

cout<<"3年的总销售量为:"<<sum<<endl;
return 0;
}

第七题

/*
设计一个car结构,存储汽车信息,使用new来创建一个由相应数量的car结构组成的动态数组。输入每辆车的生产商和年份(交替读取数值和字符串)显示每个结构的内容。
*/


#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct Car{
string producer;//生产商
int year;//生产年份
};
int main()
{
cout<<"How many cars do you wish to catalog?";
int num;
cin>>num;
Car *car=new Car [num];
for (int i=0;i<num;i++)
{
cout<<"Car #"<<i+1<<":"<<endl;
cout<<"Please enter the made: ";
cin.get();
getline(cin,car[i].producer);//数组名是指针,数组元素就相当于所指向的变量,所以用.

cout<<"Please enter the year made: ";
cin>>car[i].year;
}
cout<<"Here is your collection: \n";
for (int i=0;i<num;i++)
{
cout<<car[i].year<<" "<<car[i].producer<<endl;
}
delete [] car;
return 0;
}

第八题

/*编写一个程序,它使用一个char数组和循环来读取一个单词,直到用户输入done为止。随后指出用户输入了多少个单词。*/
#include <iostream>
#include <cstring>
using namespace std;
const int ArSize = 10;
int main()
{
cout<<"Enter words (to stop, type the word down)\n";

char words[ArSize];

int num=0;
cin>>words;
while (strcmp(words,"done"))
{
cin>>words;
num++;
}
cout<<"\nYou entered a total of "<<num<<"words\n";

return 0;
}

第九题

/*编写一个程序,它使用一个string和循环来读取一个单词,直到用户输入done为止。随后指出用户输入了多少个单词。*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"Enter words (to stop, type the word down)\n";

string words;

int num=0;
cin>>words;
while (words!="done")
{
cin>>words;
num++;
}
cout<<"\nYou entered a total of "<<num<<" words\n";

return 0;
}

第十题

/*
使用嵌套循环,要求用户输入一个值,指出要显示多少行。程序将显示相应行数的星号,第一行包括一个星号,第二行包括两个星号,在星号不够的情况下用.来凑
*/

#include <iostream>
using namespace std;

int main()
{
cout << "enter number of rows: ";
int num;
cin >> num;
int some = 1;

while (some <= num)
{
for (int j = 0; j < num - some; j++)
{

cout << ".";
}

for (int i = 0; i < some; i++) //此循环用来输出*号
{
cout << "*";
}
some++;
cout << endl;
}

return 0;
}