学习时候的点:
1、用户来控制是否继续进行的模板:
char goonLoop=’y’;
while(goonLoop==’y’){
//logic
cout<<”输入y 来继续当前逻辑,否则退出。”<<endl;
cin>>goonLoop;
}
2、
这种写法:
cout<<”asdfasdfasdfasdfafs”
<<”qweqweqweqweqwe”<<endl;
3、不要在循环条件中使用浮点型进行相等判断。因为存在舍入误差,如果需要真么做,建议通过a-b<的方式。
4、在P82页里面提到了可以使用for循环来简化while 这个提法可以吸取一下。【overwrite或rewrite跟simplify必然是两个词】
习题:
4.1 下面的循环会执行几次循环体?循环体的打印结果是什么?
int i = 1;
while(i>10){
if((i++)%2==0)
cout<<i<<endl;
}
不执行循环体。【跟执行零次循环体貌似是两个答法。】
int i = 1;
while(i<10){
if((i++)%2==0)
cout<< i << endl;
}
2
4
6
8
4.2while循环 和do-while循环的不同之处在哪里?
while有可能一次也不执行,但是do-while 最少执行一次。
4.3 下面两个循环会计算出相同的sum值么?
for(int i=0;i<10;++i){
sum+=i;
}
for(int i=0;i<10;i++){
sum+=i;
}
会得到相同的值。
4.4 for循环控制结构的是哪个部分是什么?编写一个循环,打印从1~100的整数。
中间那个部分。
for(int i=1;i<=100;i++){
cout<<setw(3)<<i;
if(i%10==0)cout<<endl;
}
4.5下面语句会有什么行为?
for(;;){
do something;
}
程序一直在 do something,会死循环的吧。
4.6如果一个变量是在for循环的控制结构中声明的,那么它能够在循环退出后继续使用吗?
不能
4.7将下面的for循环分别转换为一个while循环和一个do-while循环:
long sum = 0;
for(int i=0;i<=1000;i++)
sum= sum+i;
转换:
int i=0;
long sum =0;
while(i<=1000){
sum+=i;
i++;
}
int i = 0;
long sum = 0;
do{
sum+=i;
i++;
}while(i<=1000);
4.9 可以的
4.10关键字break的作用是什么?continue呢?下面程序会结束吗?如果会,给出他们的输出结果。
A:
int balance= 1000;
while(true){
if(balance<9)
break;
balance = balance -9;
}
cout<<” Balance is ”<<balance<<endl;
B:
int balance = 1000;
while(true){
if(balance <9)
continue;
balance = balance -9;
}
cout<<”Balance is”<<balance <<endl;
解答:
break是脱离当前循环。
continue 是脱离当次循环。
1
出不来了吧。。。
4.11
一个while循环总能转换为一个for循环码?将下面的while循环转换为一个for循环:
int i= 1;
int sum = 0;
while(sum<10000){
sum = sum+i;
i++;
}
能!
int sum = 0;
for(int i = 0;i<10000;i++){
sum+=i;
}
4.12 下图左边的for循环转换为右边的while循环。有什么错误嘛?如果有,请指正。
for(int i =0;i<4;i++){
if(i%3 ==0) continue;
sum+=i;
}
转换为:
int i = 0;
while(i<4){
if(i%3==0)continue;
sum+=i;
i++;
}
死了啊~出不来了,
把后面的i++去掉。前面的if里面写成(i++)%3==0
4.13重写程序清单里面的两个程序:
sum>100就退出
int num =0;
int sum = 0;
while(num<20&&sum<100{
number++;
sum+=number;
}
cout<<sum;
程序是10 11的时候不往里面加,一直加到20
int num = 0;
int sum = 0;
while(number<20){
number++;
if(number!=10&&number!=11)
sum+=number;
}
4.14找出并修正代码a中的语法错误和代码b中的逻辑错误。
A:
for(int i=0;i<10;i++);//这里不该有分号
sum+=i;
if(i<j);//这里不该有分号
cout<<i//这里该有分号
else
cout<<j;
while(j<10);//这里不该有分号
{
j++;
};//这里不该有分号
do{
j++;
}while(j<10)//这里该有分号
B:
int total = 0,num = 0;//没错误吧
do{
//Read the next data
cout << ”Enter an int value,”<<
“ exit if the input is 0: “;
int num;//应该把这里的这句话注释掉。程序只能算这一次加法。
cin >> num;
total +=num;
}while(num!=0);
cout<<”Total is “ <<total<<endl;
4.15 给出下面4个程序的输出结果。
A:
for(int i=1;i<5;i++){
int j =0;
while(j<i){
cout << j << “ “;
j++;
}
}
0 0 1 0 1 2 0 1 2 3 0 1 2 3 4
B:
int i=0;
while(i <5){
for(int j = i;j>1;j--)
cout<< j<< “ “;
cout<< “****” <<endl;
i++;
}
****
****
2
****
3 2
****
4 3 2
C:
int i =5;
while(i>=1){
int num = 1;
for(int j = 1;j<=i;j++){
cout <<num <<”xxx”;
num *=2;
}
cout<<endl;
i--
}
1xxx2xxx4xxx8xxx16xxx
1xxx2xxx4xxx8xxx
1xxx2xxx4xxx
1xxx2xxx
1xxx
D:
int i =1;
do{
int num = 1;
for(int j=1;j<=i;j++){
cout<<num << “G”;
num+=2;
}
cout<<endl;
i++;
}while(i<=5);
1G
1G3G
1G3G5G
1G3G5G7G
1G3G5G7G9G
4.16按照2.14节中提出的程序设计风格和文档射界指南,重拍下面的程序格式。
#include <iostream>
using namespace std;
int main(){
int i = 0;
if(i>0)
i++;
else
i--;
char grade;
if(i>=90)
grade = ‘A’;
else
if(i>=80)
grade=’B’;
}
重排:
#include <iostream>
using namespace std;
int main(){
int i =0;
if(i>0)
i++;
else
i--;
char grade;
if(i>=90)
grade = ‘A’;
else if(i>=80)
grade= ‘B’;
}
题二:
#include <iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++)
if(i>0)
i++;
else
i--;
}
重排:
#include <iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
if(i>0)
i++;
else
i--;
}
}
4.17统计下面每个循环的迭代次数。
A:
int count = 0;
while(count<n){
count++;
}
n次
B:
for(int count = 0;count<n;count++){
}
n次
C:
int count=5;
while(count<n){
count++;
}
n-5次
D:
int count = 5;
while(count <n){
count = count +3;
}
n-5/3次
程序设计练习:
4.1加法练习工具。要求生成10个题,运算数在1-15之间。显示正确题目数量,和测试所用时间。
#include <iostream>
#include <ctime>
using namespace std;
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
/*得到用来相加的两个数*/
int *getNumberForPlus(void){
srand(time(0));
int *array = new int[2];
int a = rand() % 14+1;
int b = rand() % 14+1;
array[0] = a;
array[1] = b;
return array;
}
/*得到用来相减的两个数*/
int *getNumberForMinus(void){
srand(time(0));
int *array = new int[2];
int a = rand() % 10;
int b = rand() % 10;
if (a < b){
swap(a, b);
}
array[0] = a;
array[1] = b;
return array;
}
bool checkMinusResult(int *array, int result){
if (result == array[0] - array[1])
return true;
return false;
}
int getMinusResult(int a, int b){
return a - b;
}
int getPlusResult(int a, int b){
return a - b;
}
bool checkPlusResult(int *array, int result){
if (result == array[0] + array[1]){
return true;
}
return false;
}
int main(){
cout << "改进的数学学习工具:(加法)" << endl;
//int a = 3, b = 4;
int count = 0;
int correctCount = 0;
int startTime = time(0);
while (count < 10){
int *array = getNumberForMinus();
cout << array[0] << " + " << array[1] << "是多少?" << endl;
int result = 0;
cout << "请输出结果" << endl;
cin >> result;
if (checkPlusResult(array, result)){
cout << "计算正确!" << endl;
correctCount++;
}
else
cout << "计算错误,正确的结果是:" << getPlusResult(array[0], array[1]) << endl;
count++;
}
int endTime = time(0);
cout << "正确数量是: " << correctCount << " 个。" << endl
<< "耗时是 " << getMinusResult(endTime, startTime) << " 秒。" << endl;
system("pause");
return 0;
}
4.2统计正数 负数的数目 并计算平均值。输0退出,如果输入 1 2 0平均值为1.5。
#include <iostream>
using namespace std;
double getAvg(double sum,int count){
return sum / count;
}
int main(){
cout << "统计正数 负数的数目 并计算平均值" << endl;
int number = 0;
int totalCount = 0;
int positiveNumbers = 0;
int negativeNumbers = 0;
int sum = 0;
cin >> number;
while (number != 0){
if (number > 0){
positiveNumbers++;
}
else{
negativeNumbers++;
}
sum += number;
totalCount++;
cout << "请继续输入: " ;
cin >> number;
}
cout << "正数个数为" << positiveNumbers << "个"<< endl;
cout << "负数个数为" << negativeNumbers << "个" << endl;
cout << "平均值为" <<getAvg(sum,totalCount)<< endl;
system("pause");
return 0;
}
4.3千克转换为磅(1千克=2.2磅):输出一个表从1到200的转换。
#include <iostream>
#include <iomanip>
using namespace std;
double kg2Pounds(double kg){
return kg*2.2;
}
void getTransformTableOfkgAndPounds(void){
cout << left<<setw(12) << "Kilograms" << setw(10) << "Pounds" << endl;
int count = 1;
while (count<200){
cout << left<<setw(12) << count << setw(10) << kg2Pounds(count) << endl;
count+=2;
}
}
int main(){
cout << "千克转换为磅(1千克=2.2磅)" << endl;
getTransformTableOfkgAndPounds();
system("pause");
return 0;
}
4.4 英里转千米(1英里=1.609千米)
#include <iostream>
#include <iomanip>
using namespace std;
double miles2Km(double miles){
return miles*1.609;
}
void getTransformTableOfmiles2Km(void){
cout << left << setw(6) << "Miles" << setw(10) << "Kilometers" << endl;
int count = 1;
while (count<=10){
cout << left << setw(6) << count << setw(10) << miles2Km(count) << endl;
count++;
}
}
int main(){
cout << "英里转千米(1英里=1.609千米)" << endl;
getTransformTableOfmiles2Km();
system("pause");
return 0;
}
4.5显示并排的表
#include <iostream>
#include <iomanip>
using namespace std;
double kg2Pounds(double kg){
return kg*2.2;
}
double pounds2Kg(double pounds){
return pounds / 2.2;
}
void getTransformTableOfkgAndPounds(void){
cout << left << setw(12) << "Kilograms" << setw(8) << "Pounds" << "| "<<setw(8)<<"Pounds"<<"Kilograms"<<endl;
int count = 1;
int poundsCount = 20;
while (count<200){
cout << left << setw(12) << count << fixed << setprecision(1)<< setw(8) << kg2Pounds(count) << "| " << setw(8) << poundsCount << fixed << setprecision(2) << pounds2Kg(poundsCount) << endl;
count+=2;
poundsCount += 5;
}
}
int main(){
cout << "千克转换为磅(1千克=2.2磅)并排" << endl;
getTransformTableOfkgAndPounds();
system("pause");
return 0;
}
4.6
并排的表:千里米
#include <iostream>
#include <iomanip>
using namespace std;
double km2Miles(double km){
return km / 1.609;
}
double miles2Km(double miles){
return miles*1.609;
}
void getTransformTableOfkgAndPounds(void){
//cout << left << setw(12) << "Kilograms" << setw(8) << "Pounds" << "| " << setw(8) << "Pounds" << "Kilograms" << endl;
cout << left << setw(6) << "Miles" << setw(12) << "Kilometers" << "| " << setw(12) << "Kilometers" << setw(6) << "Miles"<< endl;
int count = 1;
int kmCount = 20;
while (count <= 10){
cout << left << setw(6) << count << setw(12) << miles2Km(count) << "| " << setw(12) <<kmCount << setw(6) << km2Miles(kmCount) << endl;
count++;
kmCount += 5;
}
}
int main(){
cout << "并排的表 英里 米" << endl;
getTransformTableOfkgAndPounds();
system("pause");
return 0;
}
4.7 未来学费。第一年学费10000美元 学费每年比上一年多5%。求第十年的学费。
#include <iostream>
using namespace std;
#define rate 0.05//学费增长率
double getFee(int year){
double root = 10000.0;
double finalMoney = 0.0;
while (year--){
//root = 10000.0;
finalMoney = root*(1 + rate);
root = finalMoney;
}
return finalMoney;
}
int main(){
cout << "计算未来学费,一年学费10000美元 学费每年比上一年多5%。求第十年的学费。" << endl;
cout << getFee(10);
system("pause");
return 0;
}
4.8 用户输入一堆成绩,求最高成绩:
#include <iostream>
using namespace std;
int main(){
cout << "用户输入成绩求最高成绩:输入 0结束" << endl;
int mark = 0;
cin >> mark;
int maxMark = 0;
while (mark!=0){
if(mark > maxMark){
maxMark = mark;
}
cout << "请继续输入成绩:(0结束)" << endl;
cin >> mark;
}
cout << "最高成绩为:" << maxMark << endl;
system("pause");
return 0;
}
4.9 最高和次高成绩
#include <iostream>
using namespace std;
int main(){
cout << "用户输入成绩,求最高的两个成绩:输入 0结束" << endl;
int mark = 0;
cin >> mark;
int maxMark = 0;
int secondMark = 0;
int count = 0;
while (mark != 0){
if (mark > maxMark){
if (secondMark<maxMark)
secondMark = maxMark;
maxMark = mark;
//if (count = 0)continue;
}
else{
if (mark > secondMark)
secondMark = maxMark;
}
//if (mark > secondMark){
// secondMark = mark;
//}
cout << "请继续输入成绩:(0结束)" << endl;
cin >> mark;
count++;
}
cout << "最高成绩为:" << maxMark << endl;
cout << "次高成绩为:" << secondMark << endl;
system("pause");
return 0;
}
4.10同时被5和6整除
#include <iostream>
#include <iomanip>
using namespace std;
bool canBeDevided(int a){
if (a % 5 == 0 && a % 6 == 0){
return true;
}
return false;
}
void display(void){
int count = 0;
for (int i = 100; i < 1000; i++){
if (canBeDevided(i)){
cout << setw(4) << i;
count++;
if (count % 10 == 0){
cout << endl;
continue;
}
}
}
}
int main(){
cout << "同时被5和6整除的数" << endl;
display();
system("pause");
return 0;
}
4.11仅能被5和6之一整除
#include <iostream>
#include <iomanip>
using namespace std;
bool canBeDevided(int a){
if ((a % 5 == 0 && a % 6 != 0) || (a % 5!= 0 && a % 6 == 0)){
return true;
}
return false;
}
void display(void){
int count = 0;
for (int i = 100; i < 200; i++){
if (canBeDevided(i)){
cout << setw(4) << i;
count++;
if (count % 10 == 0){
cout << endl;
continue;
}
}
}
}
int main(){
cout << "同时被5和6整除的数" << endl;
display();
system("pause");
return 0;
}
4.12 求满足>12000的最小的n。
#include <iostream>
using namespace std;
int main(){
cout << "平方大于12000的最小整数" << endl;
int n = 100;
while (n*n<=12000){
n++;
}
cout << "平方大于12000的最小整数是" <<n<< endl;
system("pause");
return 0;
}
4.13 求立方小于12000的最大整数
#include <iostream>
using namespace std;
int main(){
cout << "立方小于12000的最大整数" << endl;
int n = 10;
while (n*n*n < 12000){
n++;
}
//单独写一个if判断?貌似也不错哎~~~ 一个解决off-by-one-bug的方式
if (n*n*n>12000)
n--;
cout << "立方小于12000的最大整数是" << n << endl;
cout << n*n*n << endl;
system("pause");
return 0;
}
4.14输出从 !到~中间的所有ASCII字符
#include <iostream>
using namespace std;
int main(){
cout << "asdfasdfasdf" << endl;
for (int i = '!'; i <= '~'; i++){
cout << static_cast<char>(i) << " ";
if (i % 10 == 0)cout << endl;
}
system("pause");
return 0;
}
什么时候使用continue什么时候 不使用,在i++连续的时候不使用,当要被输出的两个字符中间i的跨度增加了换行个单位【这里面就是10】的时候,中间就会打印多余的空行,记得在同一个函数内部增加回车,并且continue。
4.15公约数 做过了
4.16输出一个整数的所有因子,比如120就应该输出:2 2 2 3 5
#include <iostream>
using namespace std;
bool isPrime(int a){
for (int i = 2; i <= a / 2; i++){
if (a%i == 0)return false;
}
return true;
}
int main(){
cout << "输出一个整数的所有因子,比如120就应该输出:2 2 2 3 5" << endl;
int number = 0;
cout << "输入一个整数" << endl;
cin >> number;
//cout<<isPrime(number);
while (number>1){
bool flag = false;
int i = 2;
while (i<=number/2){
if (isPrime(number)){
cout << number << endl;
flag = true;
break;
}
if (number%i == 0){
cout << i << " ";
number /= i;
}
else
i++;
}
if (flag)break;
}
system("pause");
return 0;
}
4.17
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double commission = 0;
cout << "计算销售额:" << endl;
cout << "输入基本工资" << endl;
double baseSalary = 0;
cin >> baseSalary;
cout << "赚钱数" << endl;
double expect = 0;
cin >> expect;
double commisionSought = expect - baseSalary;
cout << "预估销售额:" << endl;
double initialSalesAmount = 0;
cin >> initialSalesAmount;
double salesAmount = initialSalesAmount;
do{
salesAmount += 0.01;
if (salesAmount < 5000){
commission = salesAmount*0.08;
}
else if (salesAmount < 10000){
commission = 5000 * 0.08 + (salesAmount - 5000)*0.1;
}
else {
commission = 5000 * 0.08 + (10000 - 5000)*0.1 + (salesAmount - 10000)*0.12;
}
} while (commission<commisionSought);
cout << fixed << setprecision(2) << salesAmount << endl;
system("pause");
return 0;
}
4.18
#include <iostream>
using namespace std;
void display1(void){
cout << "Pattern1" << endl;
for (int i = 1; i <=6; i++){
for (int j = 1; j <= i; j++){
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
void display2(void){
cout << "Pattern2" << endl;
for (int i = 1; i <= 6; i++){
for (int j = 1; j <= 6-i+1; j++){
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
void display3(void){
cout << "Pattern3" << endl;
for (int i = 1; i <= 6; i++){
for (int j = i; j<6;j++){
cout << " ";
}
for (int j = i; j>0;j--){
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
void display4(void){
cout << "Pattern4" << endl;
for (int i = 1; i <= 6; i++){
for (int j = 1; j < i; j++){
cout << " ";
}
for (int j = 1; j <= 6 - i + 1;j++){
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
int main(){
cout << "打印图案" << endl;
display4();
system("pause");
return 0;
}
4.19
#include <iostream>
#include <iomanip>
using namespace std;
void display(int n){
for (int i = 1; i <= n; i++){
for (int j = n - i; j>0; j--){
cout <<setw(4)<< "";
}
for (int j = 0; j < i;j++){
cout <<setw(4)<< pow(2, j);
}
for (int j = i-1; j >0;j--){
cout << setw(4) << pow(2, j-1);
}
cout << endl;
}
}
int main(){
cout << "数字金字塔" << endl;
display(8);
system("pause");
return 0;
}
4.20输出2-1000的素数
#include <iostream>
#include <iomanip>
using namespace std;
bool isPrime(int a){
for (int i = 2; i <= a / 2; i++){
if (a%i == 0)return false;
}
return true;
}
int main(){
cout << "输出2-1000的素数" << endl;
int count = 0;
for (int i = 2; i < 1000; i++){
if (isPrime(i)){
cout << setw(4)<<i ;
count++;
if (count % 8 == 0)cout << endl;
}
}
system("pause");
return 0;
}
4.21 还款金额
double getMonthlyPaymentBase(double loan,double monthlyRate,double year){
return loan*monthlyRate / (1 - 1 / pow((1 + monthlyRate), year * 12));
}
double getTotalPaymentBase(double year,double monthlyPayment){
return monthlyPayment*year * 12;
}
格式很要命:
到现在也没调成书上的那种格式:
#include <iostream>
#include <iomanip>
using namespace std;
double getMonthlyPaymentBase(double loan, double monthlyRate, double year){
return loan*monthlyRate / (1 - 1 / pow((1 + monthlyRate), year * 12));
}
double getTotalPaymentBase(double year, double monthlyPayment){
return monthlyPayment*year * 12;
}
void getLoanTable(double loan,double year){
cout <<left<< setw(15) << "Interest Rate" << setw(15) << "Monthly Payment " << setw(15)<<"Total Payment" << endl;
for (double annualRate = 5.0; annualRate < 8.1;annualRate+=0.125){
cout << left << fixed << setw(5) << setprecision(4) << annualRate << left << "%" << setw(9) << "" << setw(16) << getMonthlyPaymentBase(loan, annualRate / 1200, year) << fixed << setprecision(2) << getTotalPaymentBase(year, getMonthlyPaymentBase(loan, annualRate / 1200, year)) << endl;
}
}
int main(){
cout << "还款金额 表:" << endl;
double loanAmount = 0;
cout << "输入贷款额 ";
cin >> loanAmount;
cout << "输入贷款年限";
double year = 0;
cin >> year;
getLoanTable(loanAmount,year);
cout << 10000 * 5 / 1200.0;
system("pause");
return 0;
}
4.22
做一个这个表:
#include <iostream>
using namespace std;
double getMonthlyPaymentBase(double loan, double monthlyRate, double year){
return loan*monthlyRate / (1 - 1 / pow((1 + monthlyRate), year * 12));
}
double getTotalPaymentBase(double year, double monthlyPayment){
return monthlyPayment*year * 12;
}
double getMonthInterestBase(double balance,double monthlyRate){
return balance*monthlyRate;
}
double getPrincipal(double monthPay,double monthInterest){
return monthPay - monthInterest;
}
void displayPaymentTableBase(double loan, double monthlyRate, double year){
double monthPay = getMonthlyPaymentBase(loan,monthlyRate,year);
double totalPayment = getTotalPaymentBase(year, monthPay);
cout << "Payment#" << " " << "Interest" << " " << "Principal" << " " << "Balance" << " " << endl;
double balance = loan;
for (int i = 1; i <=12;i++){
double monthInterest = getMonthInterestBase(balance, monthlyRate);
double principal = getPrincipal(monthPay, monthInterest);
balance -= principal;
if (i!=12)
cout << i << " " << monthInterest << " " << principal << " " << balance << endl;
else
cout << i << " " << monthInterest << " " << principal << " " <<fixed<< balance << endl;
}
}
int main(){
cout << "分期贷款还款计划" << endl;
int loanAmount = 0;
cout << "输入借款额:";
cin >> loanAmount;
int year = 0;
cout << "输入借款年限:";
cin >> year;
double yearRate = 0;
cout << "输入年利率:";
cin >> yearRate;
double monthPay = getMonthlyPaymentBase(loanAmount, yearRate / 1200, year);
cout << "月还: " << monthPay << endl;
cout << "总共还: " << getTotalPaymentBase(year, monthPay) << endl;
displayPaymentTableBase(loanAmount, yearRate / 1200, year);
system("pause");
return 0;
}
4.23 级数和:
计算:
#include <iostream>
#include <iomanip>
using namespace std;
void getSum1(int number){
double sum = 0;
for (int i = number; i > 0; i--){
sum += 1.0 / i;
}
cout <<setw(20)<<fixed<<setprecision(20)<< sum << endl;
}
void getSum2(int number){
double sum = 0;
for (int i = 1; i<=number; i++){
sum += 1.0 / i;
}
cout << setw(20) << fixed << setprecision(20) << sum << endl;
}
int main(){
cout << "计算级数和" << endl;
getSum1(50000);
getSum2(50000);
cout << setw(20) << fixed << setprecision(20) << 1.0/50000 << endl;
system("pause");
return 0;
}
4.24
计算级数和:
#include <iostream>
#include <iomanip>
using namespace std;
void getSum1(int number){//由于知道,右边开始算比较精确,所以从右边开始计算:
double sum = 0;
for (double i = number; i > 2; i-=2){
sum += (i - 2) / i;
}
cout << setw(20) << fixed << setprecision(20) << sum << endl;
}
int main(){
cout << "计算级数和" << endl;
getSum1(99);
system("pause");
return 0;
}
4.25计算
上面两个i是一个东西。。。
#include <iostream>
#include <iomanip>
using namespace std;
double getSumOfPI(int number){
double sum = 0;
for (int i = number * 2 + 1; i >= 1; i -= 4){
sum += 1.0 / i;
}
return sum;
}
double getMinusOfPI(int number){
double sum = 0;
for (int i = number * 2 - 1; i >= 3; i -= 4){
sum += 1.0 / i;
}
return sum;
}
//传递参数不会减小精度。
double getPI(int number){//由于知道,右边开始算比较精确,所以从右边开始计算:
return 4*(getSumOfPI(number) - getMinusOfPI(number));
}
int main(){
cout << "计算级数和" << endl;
cout << setw(20) << fixed << setprecision(20) << getPI(10000) << endl;
cout << setw(20) << fixed << setprecision(20) << getPI(20000) << endl;
cout << setw(20) << fixed << setprecision(20) << getPI(100000) << endl;
cout << setw(20) << fixed << setprecision(20) << getPI(1000000) << endl;
//getPI(20000);
//getPI(100000);
system("pause");
return 0;
}
4.26 计算e
#include <iostream>
#include <iomanip>
using namespace std;
double getE(int number){//阶乘,,,貌似还是从小的来吧。。。
double e = 1;
double item = 1.0;
for (int i = 1; i <= number;i++){
item /= i;
e += item;
}
return e;
}
int main(){
cout << "计算级数和" << endl;
//没啥区别啊
cout << setw(20) << fixed << setprecision(20) << getE(10000) << endl;
cout << setw(20) << fixed << setprecision(20) << getE(20000) << endl;
cout << setw(20) << fixed << setprecision(20) << getE(100000) << endl;
system("pause");
return 0;
}
4.27输出 2001-2100之间的所有闰年。
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear(int year){//我从开始学 就搞不懂到底啥事闰年。。。并且也不知道为什么
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
int main(){
cout << "计算级数和" << endl;
int count = 0;
for (int i = 2001; i < 2100; i++){
if (isLeapYear(i)){
cout << i << " ";
count++;
if (count % 10 == 0)
cout << endl;
}
}
system("pause");
return 0;
}
4.28 程序输入2005 6 表示 2005年1月1日是星期六。要求输入这一年的每个月的第一天是星期几。
#include <iostream>
#include <iomanip>
using namespace std;
enum Weekday{
MONDAY=1,TUESDAY=2,WEDNESDAY=3,THRUSDAY=4,FRIDAY=5,STAURDAY=6,SUNDAY=7
};
/*
今天是星期today,days天以后是星期?
*/
int getWeekday(int today,int days){
//cout << "今天是星期" << today << "," << days << "天以后是星期 " << (today + days) % 7 ;
if (((today + days) % 7) == 0)
return 7;
return (today + days) % 7 ;
}
bool isLeapYear(int year){//
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
void display(int year,int weekday){
int days = 0;
for (int month = 1; month <= 12; month++){
//cout << year << "年 " <<setw(2)<< i << "月 1日 是星期" << weekday << endl;
cout << year << "年 " << setw(2) << month << "月 1日 是星期" << getWeekday(weekday, days) << endl;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10){//12不要了 那是下一年的
days += 31;
}
else if (month == 4 || month == 6 || month == 9 || month == 11){
days += 30;
}
else if (month==2){
if (isLeapYear(year)){
days += 29;
}
else{
days += 28;
}
}
}
}
int main(){
cout << "程序输入2005 6 表示 2005年1月1日是星期六。要求输入这一年的每个月的第一天是星期几。" << endl;
int year = 0;
cout << "输入年份" << endl;
cin >> year;
int weekday = 0;
cout << "输入星期" << endl;
cin >> weekday;
display(year, weekday);
//cout<<getWeekday(6, 31);
system("pause");
return 0;
}
4.29输出 日历:
#include <iostream>
#include <iomanip>
using namespace std;
/*
今天是星期today,days天以后是星期?
*/
int getWeekdayStartPosition(int today, int days){
//cout << "今天是星期" << today << "," << days << "天以后是星期 " << (today + days) % 7 ;
/*if (((today + days) % 7) == 0)
return 7;*/
return (today + days) % 7;
}
bool isLeapYear(int year){//
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
int getDayOfAMonth(int year, int month){
if (month == 2){
if (isLeapYear(year)){
return 29;
}
else return 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11){
return 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
return 31;
}
//这里会有月份异常
}
void display(int year, int weekday){
int days = 0;
int currentDayofThemonth = 0;
for (int month = 1; month <= 12; month++){
int forendl = 0;
currentDayofThemonth = getDayOfAMonth(year, month);
cout << year << "年 " << month << "月" << endl;
cout << "-----------------------------------------" << endl;
cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
for (int i = 0; i < getWeekdayStartPosition(weekday, days); i++){
cout << " ";
forendl++;
}
for (int i = 1; i <= currentDayofThemonth; i++){
cout << i<<" ";
forendl++;
if (forendl % 7 == 0)
cout << endl;
}
cout << endl << endl << endl;
days += getDayOfAMonth(year, month);
//if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10||month==12){//12不要了 那是下一年的
// days += 31;
//}
//else if (month == 4 || month == 6 || month == 9 || month == 11){
// days += 30;
//}
//else if (month == 2){
// if (isLeapYear(year)){
// days += 29;
// }
// else{
// days += 28;
// }
//}
}
}
int main(){
cout << "程序输入2005 6 表示 2005年1月1日是星期六。要求输入这一年的每个月的第一天是星期几。" << endl;
int year = 0;
cout << "输入年份" << endl;
cin >> year;
int weekday = 0;
cout << "输入星期" << endl;
cin >> weekday;
display(year, weekday);
//cout<<getWeekday(6, 31);
system("pause");
return 0;
}