练习一:
#include<iostream>
using namespace std;
int main()
{
int i,j;
int sum=0;
cout<<"Enter two integers: ";
cin>>i>>j;
for(int n=i; n<=j; ++n)
{
sum+=n;
}
cout<<i<<"——"<<j<<"之间所有整数之和为: "<<sum<<endl;
return 0;
}
练习二:软件不可实现!!!
练习三:
#include<iostream>
using namespace std;
int main()
{
int i;
int sum=0;
cout<<"Enter a figure:";
cin>>i;
while(i!=0)
{
sum+=i;
cout<<"输入累计和为: "<<sum<<endl;
cout<<"Enter a figure:";
cin>>i;
}
return 0;
}
练习四:
#include<iostream>
using namespace std;
int main()
{
int i=0;
double interest1=0;
double interest2=0; //利息
double cd=100; //当前存款
while(interest2<=interest1)
{
interest1+=10;
cd=((0.05*cd)+cd);
interest2=cd-100;
++i;
}
cout<<"经过"<<i<<"年可以超过!!!"<<endl;
return 0;
}
练习五:
#include<iostream>
using namespace std;
int main()
{
int i,j;
int sum=0;
const int months=12;
int month[12];
const char * remind[12]=
{
"Please enter the january sales: ",
"Please enter the February sales: ",
"Please enter the March sales: ",
"Please enter the April sales: ",
"Please enter the May sales: ",
"Please enter the June sales: ",
"Please enter the July sales: ",
"Please enter the August sales: ",
"Please enter the September sales: ",
"Please enter the October sales: ",
"Please enter the November sales: ",
"Please enter the December sales: "
};
for(i=0, j=0; j<months; ++i,++j)
{
cout<<remind[i];
cin>>month[j];
sum+=month[i];
}
cout<<"The year sale number is: "<<sum<<endl;
return 0;
}
练习六:
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
int Sum=0; //三年总销量
const years=3;
const int months=12;
int month[years][months]; //那一年那一月
int sum[years]={0,0,0,}; //每一年的总销量,必须进行初始化
const char * remind[12]=
{
"Please enter the january sales: ",
"Please enter the February sales: ",
"Please enter the March sales: ",
"Please enter the April sales: ",
"Please enter the May sales: ",
"Please enter the June sales: ",
"Please enter the July sales: ",
"Please enter the August sales: ",
"Please enter the September sales: ",
"Please enter the October sales: ",
"Please enter the November sales: ",
"Please enter the December sales: "
};
for(i=0; i<years; ++i) //对三年进行循环
{
cout<<"Please enter 第"<<i+1<<"的年销售量: "<<endl;
for(n=0,j=0; n<months; ++n,++j) //一年十二月循环
{
cout<<remind[j]; //提醒那一月
cin>>month[i][n];
sum[i]+=month[i][n];
}
}
Sum=(sum[0]+sum[1]+sum[2]);
cout<<"The year sale number is: "<<Sum<<endl;
return 0;
}
练习七:
#include<iostream>
#include<string>
using namespace std;
struct car
{
string producter;
int year;
};
int main()
{
int i;
cout<<"How many cars do you wish to catalog? ";
(cin>>i).get();
car * p=new car[i];
for(int j=0; j<i; ++j)
{
cout<<"Car #"<<j+1<<endl;
cout<<"Please enter the make: ";
getline(cin,p[j].producter);
//cin>>p[j].producter;
cout<<"Please enter the year made: ";
(cin>>p[j].year).get();
}
cout<<"here is your collection:"<<endl;
for(int n=0; n<i; ++n)
{
cout<<p[n].year<<" "<<p[n].producter<<endl;
}
delete [] p;
return 0;
}
练习八:
include<iostream>
#include<cstring>
using namespace std;
int main()
{
char word[20];
int i=0;
cout<<"Enter words (to stop, type the word done): ";
cin>>word;
while(strcmp(word,"done")) //进行比较
{
cin>>word;
++i;
}
cout<<"You entered a total of "<<i<<" words!"<<endl;
return 0;
}
练习九:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
int i=0;
cout<<"Enter words (to stop, type the word done): ";
cin>>word;
while(word != "done")
{
cin>>word;
++i;
}
cout<<"your entered a total of "<<i<<" words"<<endl;
return 0;
}
练习十:
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"Enter number of rows: ";
cin>>size;
for(int j=0; j<size; ++j) //打印多少行
{
for(int i=1; i<(size-j); ++i)//每一行应打印多少个"."
{
cout<<".";
}
for(int t=0; t<(j+1); ++t) //每一行应打印多少个"*"
{
cout<<"*";
}
cout<<endl; //进行换行
}
return 0;
}