第五章…
1.编写一个要求用户输入两个整数的程序,计算并输出两个整数之间(包括这两个整数)所有整数的和
#include<iostream>
#include<array>
using namespace std;
int main()
{
int first,last;
int result = 0;
cout<<"请输入第一个整数:";
cin>>first;
cout<<"请输入最后一个整数:";
cin>>last;
for(int i = first;i<=last;i++)
{
result = result + i;
}
cout<<"最后的和为:"<<result;
}
实验结果:
2.使用array对象重新编辑程序清单5.4
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<long long,17> Array; //记住array的声明类型 array<数组类型,长度> 名称
Array[0] = 1;
for(int i=1;i<=17;i++)
{
Array[i] = i * Array[i-1];
cout<<i<<"!="<<Array[i]<<endl;
}
}
实验结果:
3.编写一个要用户输入数字的程序,每次输入将报告到目前为止,所有的输入的累积和
#include<iostream>
using namespace std;
int main()
{
int number; //构思需要两个数字,一个是输入的数字number,另一个代表最后计算的和total
int total = 0;
cin >> number;
while(number!=0)
{
total += number;
cout << "total: " << total << endl;
cin >> number;
}
}
实验结果:
4.Daphne以10%的单利投资了100美元。每一年的利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元,按照书中描述编写程序
#include<iostream>
using namespace std;
int main()
{
double num_d,num_c;
num_d = 100;
num_c = 100;
int count = 0;
do
{
num_d = num_d +10;
num_c = num_c *1.05;
count++;
} while(num_d>num_c); //先执行循环体在进行判断,判断成立在去执行循环体,直到条件不成立
cout<<"经过"<<count<<"年之后,"<<"Cleo的投资价值才能超过Daphne的投资价值"<<endl;
cout<<"此时Cleo的投资价值为:"<<num_c<<endl;
cout<<"此时Daphne的投资价值为:"<<num_d<<endl;
}
实验结果:
5.输入全年中每个月的销售量(图书数量),初始化月份逐月进行提示,将数据存储在一个int数组里面,最后计算一整年的销售总量
#include<iostream>
using namespace std;
int main()
{
int sale[12];
int allsale = 0;
string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for(int i=0;i<12;i++)
{
cout<<month[i]<<"的销售量为:";
cin>>sale[i];
}
for(int i=0;i<12;i++)
allsale +=sale[i];
cout<<"本年度总销售量为:"<<allsale<<endl;
}
实验结果:
6.这次使用一个二维数组,来存储输入——3年中每个月的销量,程序报告每年的销量以及三年的总销量
#include<iostream>
#include<string>
using namespace std;
int main()
{
string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int sales[3][12];
int total1 = 0, total2 = 0, total3 = 0; //每年总销量
cout << "Enter sales per month" << endl;
for(int i=0; i<3; i++){
for(int j=0; j<12; j++){
cout << i+1 << " year " << months[j] << ": ";
cin >> sales[i][j]; //注意在for里面设置的一个带有条件性的累加求和!
if(i==0)
total1 += sales[i][j];
if(i==1)
total2 += sales[i][j];
if(i==2)
total3 += sales[i][j];
}
cout << endl; //将每一年的分开,使终端结果更加清晰
}
cout << "first year: " << total1 << endl;
cout << "second year: " << total2 << endl;
cout << "third year: " << total3 << endl;
cout << "total: " << total1 + total2 + total3;
}
实验结果:
7.设计一个名为car的结构,用来存数书中信息
#include<iostream>
#include<string>
using namespace std;
struct car
{
string company;
int year;
};
int main()
{
int n;
cout<<"How many cars do you wish to catalog?";
cin>> n;
car *cars=new car[n]; //类型名 声明指针 = new 数组名[数组个数]
cin.ignore();
for(int i=0;i<n;i++)
{
cout<<"Car #"<<i+1<<":"<<endl;
cout<<"Please enter the make:";
getline(cin,cars[i].company);
cout<<"Please enter the year made:";
cin>>cars[i].year;
cin.ignore();
}
for(int i=0;i<n;i++)
{
cout<<cars[i].year<<" "<<cars[i].company<<endl;
}
}
实验结果:
8.编写一个程序,使用一个char数组和循环来每次读取一个单词
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char word[20];
int num = 0;
cout << "Enter words (to stop, type the word done):" << endl;
cin >> word;
while(strcmp(word , "done") != 0) //strcmp对输入的两个字符进行比较,因为done是字符串所以要用双引号
{
if(bool(cin >> word) == true) //bool强制转换,将从键盘输入字符串传入到word中,输入有效则继续循环输入,遇到结束符、输入无效或者流异常,则退出循环
{
num++;
}
}
cout << "You entered a total of " << num << " words." << endl;
return 0;
}
实验结果:
9.问题同上,使用string而不是使用char数组
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string word;
int num = 0;
cout << "Enter words (to stop, type the word done):" << endl;
cin >> word;
while(word!="done") //strcmp对输入的两个字符进行比较,因为done是字符串所以要用双引号
{
if(bool(cin >> word) == true) //bool强制转换,将从键盘输入字符串传入到word中,输入有效则继续循环输入,遇到结束符、输入无效或者流异常,则退出循环
{
num++;
}
}
cout << "You entered a total of " << num << " words." << endl;
return 0;
}
实验结果:
10.编写一个使用嵌套循环的程序
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n; //首先1个for循环来控制行数,再有两个for循环来控制每一行输出几个*和.
for(int i=0; i<n; i++){
for(int j=0; j<n-i-1; j++)
cout << ".";
for(int j=0; j<i+1; j++)
cout << "*";
cout << endl;
}
}
实验结果: