练习代码为本人所写,欢迎大家学习交流
若有疑问请留言,我将尽快回复!!!!
题5.1:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int numb1, numb2, sum=0, temp;
cout << "please enter two number: ";
cin >> numb1;
cin >> numb2;
if (numb1 > numb2) //使得先输入数字更大也能实现
{
temp = numb1;
numb1 = numb2;
numb2 = temp;
}
for (int i = numb1; i <= numb2; i++)
{
sum += i;
}
cout << "sum = " << sum;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.2:
#include <iostream>
#include <ctime>
#include <array>
using namespace std;
int main()
{
array<long double, 101> q;
q[0] = q[1] = 1;
for (int i = 2; i <= 100; i++)
{
q[i] =i * q[i-1];
cout << i << "! = " << q[i] << endl;
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.3:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
long long numb, sum = 0;
cout << "please input one number at a time, and it will show the sum of the numbers you have inputed";
cout << endl << "Enter 0 to exit" << endl;
cin >> numb;
while (numb)
{
sum += numb;
cout << "sum = " << sum <<endl;
cin >> numb;
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.4:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
double profit_Daphne, profit_Cleo = 100;
int i=0;
do
{
i++;
profit_Daphne = 10*i +100;
profit_Cleo = profit_Cleo * 0.05 + profit_Cleo;
} while (profit_Daphne > profit_Cleo);
cout << "After " << i << " years, the profits of Cleo are more than Daphne's" << endl;
cout << "the profits of Cleo: " << profit_Cleo << endl;
cout << "the profits of Daphne: " << profit_Daphne;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.5:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int sell[12];
string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August",
"Septempber", "October", "November", "December" };
int total_sell = 0;
for (int i = 0; i < 12; i++)
{
cout << "enter the sell of " << month[i] << ": ";
cin >> sell[i];
total_sell += sell[i];
}
cout << "the total sell in this year: " << total_sell;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.6:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int sell[3][12];
string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August",
"Septempber", "October", "November", "December" };
int total_sell_oneyear[3] = {0};
int total_sell_allyears = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++)
{
cout << "enter the sell of " << month[j] << " in the " << i+1 << "th year: ";
cin >> sell[i][j];
total_sell_oneyear[i] += sell[i][j];
}
cout << "the total sell in the " << i+1 << "th year: " << total_sell_oneyear[i] << endl;
total_sell_allyears += total_sell_oneyear[i];
}
cout << "the total sell of three years: " << total_sell_allyears;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.7:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
struct car
{
string manufacturer;
int year;
};
int main()
{
int numb;
cout << "How many cars do you wish to catalog? ";
cin >> numb;
car * ps = new car [numb];
for (int i = 0; i < numb; i++)
{
cin.get(); //数字和字符串一起,一定要注意把回车给吃掉
cout << "CAR #" << i+1 << endl << "please enter the make: ";
getline(cin, ps[i].manufacturer);
cout << "please enter the year made: ";
cin >> ps[i].year;
}
cout << "Here is your collection" << endl;
for (int i = 0; i < numb; i++)
{
cout << ps[i].year << " " << ps[i].manufacturer << endl;
}
delete [] ps; //记住是数组的指针删除要用[],而不是delete ps
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.8:
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
int main()
{
char word[20]; //输入必须小于20个字符
int numb=-1;
do
{
cin >> word;
numb++;
} while(strcmp("done", word));
cout << "there have " << numb << " words before \"done\"";
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.9:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main()
{
string word; //随意多少字符数
int numb=-1;
do
{
cin >> word;
numb++;
} while(word != "done");
cout << "there have " << numb << " words before \"done\"";
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}
题5.10:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int numb_Rows;
cout << "Enter the number of rows:";
cin >> numb_Rows;
for (int i = 1; i <= numb_Rows; i++)
{
for (int j = 1; j <= (numb_Rows - i); j++)
cout << ".";
for (int j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
;
cin.get();
cin.get();
return 0;
}