circulation Chapitre V et les expressions relationnelles
référence Q8 à quelqu'un d'autre blog de O (∩_∩) O
//第一题
# include<iostream>
int main()
{
using namespace std;
int small;
int big;
int sum = 0;
cout<<"please input the smaller number:"<<endl;
cin>>small;
cout<<"please input the bigger number:"<<endl;
cin>>big;
for(int i = small;i<=big;++i)
sum += i;
cout<<"sum = "<<sum;
return 0;
}
```cpp
//第二题 用array
# include<iostream>
# include<array>//使用array别忘了include
using namespace std;
const int artsize = 16;
int main()
{
array<long long, artsize> factor;//arrar<type,num> a1,array是动态数组的替代
factor[0] = 1; //顺便再记一下vector<type>a1(num),vector是定长数组的替代
factor[1] = 1;
for (int i = 2;i<artsize;++i)
factor[i] = i*factor[i-1];
for(int i = 0;i<artsize;i++)
cout<<i<<"!= "<<factor[i]<<endl;
return 0;
}
```cpp
//第二题 用long double
# include<iostream>
using namespace std;
const int artsize = 16;
int main()
{
cout.setf(ios_base::fixed,ios_base::floatfield);//为了了解精度,防止把较大的数转化为E表示法,使程序显示到小数点后6位
long double factor[artsize];
factor[0] = 1;
factor[1] = 1;
for(int i =2;i<artsize;++i)
factor[i] = i*factor[i-1];
for(int i = 0;i<artsize;++i)
cout<<i<<"!= "<<factor[i]<<endl;
return 0;
}
//第三题
# include<iostream>
using namespace std;
int main()
{
int num;
int sum;
cout<<"input 0 to quit,please input numbers:";
cin>>num;
while(num!=0)
{
sum+=num;
cout<<sum<<endl;
cin>>num;
}
}
//第四题
# include<iostream>
using namespace std;
int main()
{
const double daph_rate = 0.1;
const double cleo_rate = 0.05;
const double ori_money = 100;
double daph = ori_money+daph_rate*ori_money;
double cleo = ori_money+cleo_rate*ori_money;
int i;
for(i = 1;daph>=cleo;++i)
{
daph += daph_rate*ori_money;
cleo += cleo*cleo_rate;
}
cout<<"第"<<i<<"年 cleo 的存款超过 daph"<<endl;
cout<<"daph的存款为:"<<daph<<"$"<<endl;
cout<<"cleo的存款为:"<<cleo<<"$"<<endl;
return 0;
}
//第五题
# include<iostream>
# include<string>
using namespace std;
int main()
{
long sum = 0;
string month[12]={"January","February","March", "April", "May", "June", "July",
"August", "September","October","November", "December"};
//将从键盘输入的数据存入int数组中
int mon[12];
for(int i = 0;i<=11;++i)
{
cout<<"please input "<<month[i]<<"卖出多少册书"<<":"<<endl;
cin>>mon[i];
}
for(int j = 0;j<=11;++j)
{
sum+=mon[j];
}
for(int k = 0;k<=11;++k)
cout<<month[k]<<"卖出"<<mon[k]<<"册书"<<endl;
cout<<"全年卖出"<<sum<<"册书";
return 0;
}
// 第六题 记录三年每个月的销售量构造二维数组
# include<iostream>
const int m = 3;
const int n = 12;
int main()
{
using namespace std;
//将指针数组初始化为字符串常量
const char * month[n] =
{
"January","February","March","April","May ",
"June ","July ","August","September","October",
"November","December"
};
int num[m][n];
long sum=0;
long sum_year = 0;
cout<<"the numbers of sold books:2018-2020\n\n";
for(int p=0;p<m;++p)
{
int i;
for (i = 0;i<n;++i)
{
cout<<"请输入"<<2018+p<<"年"<<month[i]<<"的销售量"<<endl;
cin>>num[p][i];
sum+=num[p][i];
}
}
for(int j = 0;j<n;++j)
{
cout<<month[j]<<": \t";
for (int k = 0; k<m; ++k)
{
cout<<num[k][j]<<"\t";
}
cout<<endl;
}
cout<<"三年的总销售量为:"<<sum;
return 0;
}
//第七题 用new 创建动态数组
# include<iostream>
# include<string>
using namespace std;
struct car{
string make;
int year;
};
int main()
{
int size;
cout<<"how many cars do you want to catalog?";
cin>>size;
car *point = new car[size] ;//用new分配的内存没有名字,只能通过指针访问。
for (int i=0;i<size;++i)
{
cout<<"Car #"<<i+1<<endl;
cout<<"please enter the make:";
cin.get();
getline(cin, point[i].make);
cout<<"please enter the year made:";
cin>>point[i].year;
}
cout<<"here is your collection:"<<endl;
for (int j=0;j<size;++j)
{
cout<<point[j].year<<" "<<point[j].make<<endl;
}
return 0;
}
//第八题
// https://blog.csdn.net/wyl1813240346/article/details/80361734
#include<iostream>
# include<cstring>
using namespace std;
int main()
{
cout<<"Enter words(to stop enter the word done):"<<endl;
int count = 0;
char ch[20];
while(cin>>ch&&strcmp(ch,"done"))
++count;
cout<<count<<endl;
return 0;
}
//第九题 用string对象
# include<iostream>
# include<string>
using namespace std;
int main()
{
int count = 0;
string word;
cout<<"please input a text:"<<endl;
while(cin>>word&&word!="done")
++count;
cout<<count;
return 0;
}
//第十题
# include<iostream>
using namespace std;
int main()
{
cout<<"enter number of rows:";
int n;
cin>>n;
for (int i = 1;i<=n;++i)
{
for (int j = 1;j<=n-i;++j)
cout<<".";
for (int k = 1;k<=i;++k)
cout<<"*";
cout<<"\n";
}
return 0;
}