5.8-6
#include<iostream>
int main()
{
using namespace std;
int i=1;
while(i<=64)
{
cout<<i<<"、";
i=i*2;
}
cout<<endl;
system("pause");
return 0;
}
5.9-1
#include<iostream>
int main()
{
using namespace std;
int number1,number2;
int sum=0;
cout<<"Enter two numbers:";
cin>>number1>>number2;
for (int i=number1;i<=number2;i++)
{
sum=sum+i;
}
cout<<"sum = "<<sum<<endl;
system("pause");
return 0;
}
5.9-2
#include<iostream>
#include<array>
const int ArSize=101;
int main()
{
using namespace std;
array<long double,ArSize> arr;
arr[1]=arr[0]=1;
for (int i=2;i<ArSize;i++)
arr[i]=i*arr[i-1];
for (int i=0;i<ArSize;i++)
cout<<i<<"! = "<<arr[i]<<endl;
system("pause");
return 0;
}
5.9-3
#include<iostream>
using namespace std;
int main()
{
int oneInt;
int sum=0;
do
{
cin>>oneInt;
sum+=oneInt;
}while(oneInt!=0);
cout<<"sum of all inputs:"<<sum<<endl;
system("pause");
return 0;
}
5.9-4
#include<iostream>
#include<string>
using namespace std;
int main()
{
double Daphne=100,Cleo=100;
int year=0;
while (Daphne>=Cleo)
{
Daphne+=10;
Cleo*=1.05;
year++;
}
cout<<year<<" year later,"<<"Cleo>Daphne"<<endl;
cout<<"Daphne:"<<Daphne<<endl;
cout<<"Cleo:"<<Cleo<<endl;
system("pause");
return 0;
}
5.9-5
#include<iostream>
using namespace std;
const int oneInt=12;
int main()
{
int arry[oneInt];
int sum=0;
string months[oneInt]=
{
"January", "February", "March", "April", "May", "June",
"July", "August", "Septemper", "October", "November", "December"
};
cout<<"Enter monthly sales for the year:";
char ch[oneInt];
cin.getline(ch,oneInt);
for(int i=0;i<12;i++)
{
cout<<months[i]<<":";
cin>>arry[i];
sum=sum+arry[i];
}
cout<<"sum = "<<sum<<endl;
system("pause");
return 0;
}
5.9-6
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arry[3][12];
int sum[3]={};
string year[3] = {"The First Year", "The Second Year;", "The Third Year;"};
string month[12] =
{
"january", "February", "March", "April", "May", "June",
"July", "August", "Septemper", "October", "November", "December"
};
for (int i=0;i<3;i++)
{
cout<<"sales of "<<year[i]<<endl;
for (int j=0;j<12;j++)
{
cout << "please input the sales of " << month[j] << ": ";
cin>>arry[i][j];
sum[i]+=arry[i][j];
}
cout<<"the sales of "<<year[i]<<" is "<<sum[i]<<endl;
}
cout << "the sales of three years is: " << sum[0]+sum[1]+sum[2] << endl;
system("pause");
return 0;
}
5.9-7
#include<iostream>
#include<string>
using namespace std;
struct car
{
string maker;
int year;
};
int main()
{
int numbcar;
cout<<"How many cars do you wish to catalog? ";
cin>>numbcar;
car* pt=new car[numbcar];
cin.get();
//处理换行符,不处理则编译系统第二次调用时,看到的一个字符便是换行符,认为已达到行尾,会导致不接下去读取字符
for (int i=0;i<numbcar;i++)
{
cout<<"Car #"<<i+1<<":\n";
cout<<"Please enter the make:";
getline(cin,pt[i].maker);
cout<<"Please enter the year make:";
cin>>pt[i].year;
cin.get();
}
cout<<"Here is your collection:\n";
for (int j=0;j<numbcar;j++)
{
cout<<pt[j].year<<" "<<pt[j].maker<<endl;
}
system("pause");
return 0;
}
5.9-8
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch[24];
int count=0;
cout<<"Enter words (to stop,type,the word done):\n";
cin>>ch;
while (strcmp(ch,"done")!=0)
{
++count;
cin>>ch;
}
cout <<"You entered a total of "<<count<<" words."<<endl;
system("pause");
return 0;
}
5.9-9
#include<iostream>
#include<string>
using namespace std;
int main()
{
string ch;
int count=0;
cout<<"Enter words (to stop,type,the word done):\n";
cin>>ch;
while (ch!="done")
{
++count;
cin>>ch;
}
cout <<"You entered a total of "<<count<<" words."<<endl;
system("pause");
return 0;
}
5.9-10
#include<iostream>
int main()
{
using namespace std;
cout << "Enter number of row:";
int rows;
cin >> rows;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
cout << ".";
for (int k = 0; k < i; k++)
cout << "*";
cout << endl;
}
system("pause");
return 0;
}
posted on
浙公网安备 33010602011771号