原创 C++ Primer Plus 第五章 课后编程练习题1-10

//1题
#include
int main()
{
using namespace std;
int large, small,total = 0;
cout <<"请输入一个较小的数: ";
cin >> small;
cout <<“请输入一个较大的数: “;
cin >> large;
for (int i = small; i <= large; ++i)
total+= i;
cout<<small <<” — " <<large <<” 之间所有整数的和是: " <<total;
cout<< endl;

return 0;

}

//2题
#include
#include
const int ArSize = 100;
int main()
{

std::array<long double, ArSize> factorials;
factorials[0] = factorials[1] = 1.0L;
for (int i = 2; i<=ArSize; i++)
    factorials[i] = i * factorials[i-1];
for (int i = 0; i <= ArSize; i++)
    std::cout << i <<"! = " <<factorials[i] << std::endl;

return 0;

}

//3题
#include
int main()
{
using namespace std;
double num, total = 0;
cout <<"请输入一个数字 (0 退出): ";
cin >>num;
while(num)
{
total+=num;
cout <<"目前输入数字的总和 = " << total << endl;
cout <<"请输入下一个数字(0 退出): ";
cin >>num;
}

return 0;

}

//4题
#include
const float Fll = 0.05; //固定的利率
const float Zll = 0.1; //固定的利率
int main()
{
double cleo = 100, daphne = 100;
int years = 0;
while(cleo <= daphne)
{
daphne +=100 * Zll;
cleo += cleo * Fll;
++years;
}
std::cout <<years <<" 年后Cleo才能超过Daphne的投资."<< std::endl;
std::cout <<"Cleo = “<<cleo <<”\tDaphne = "<< daphne << std::endl;

return 0;

}

//5题
#include
#include
const int Month = 12;
int main()
{
using namespace std;
int totla = 0;
//const char *month[Month] =
array<array<char , 20>, Month> month =
{
“January”,
“February”,
“March”,
“April”,
“may”,
“June”,
“July”,
“August”,
“September”,
“October”,
“November”,
“December”
};
int books[Month];
for (int i = 0; i < Month; ++i)
{
cout <<"请输入 “;
int j = 0;
while(month[i][j] != ‘\0’)
{
cout <<month[i][j];
++j;
}
cout<<” 书籍卖出数量: ";
cin >> books[i];
}
for (int i = 0; i < Month; ++i)
{
int j = 0;
while(month[i][j] != ‘\0’)
{
cout <<month[i][j];
++j;
}
cout << “\t卖出数量:\t” <<books[i] << endl;
}
for (int i = 0; i < Month; ++i)
totla += books[i];
cout <<"一年中总共卖出《C++ For Fools》一书 = " << totla << endl;

return 0;

}

//6题
#include
#include
const int Month = 12;
int main()
{
using namespace std;
int totla = 0;
//const char *month[Month] =
array<array<char , 20>, Month> month =
{
“January”,
“February”,
“March”,
“April”,
“may”,
“June”,
“July”,
“August”,
“September”,
“October”,
“November”,
“December”
};
int books[3][Month];
for (int n = 0; n < 3; ++n)
{
for (int i = 0; i < Month; ++i)
{
cout <<“请输入第” << n+1 <<“年”;
int j = 0;
while(month[i][j] != ‘\0’)
{
cout <<month[i][j];
++j;
}
cout<<"书籍卖出数量: ";
cin >> books[n][i];
}
}
for (int n = 0; n < 3; ++n)
{
for (int i = 0; i < Month; ++i)
{
cout <<“第” << n+1 <<“年”;
int j = 0;
while(month[i][j] != ‘\0’)
{
cout <<month[i][j];
++j;
}
cout << “\t卖出数量:\t” <<books[n][i] << endl;
}
}
for (int n = 0; n < 3; ++n)
{
for (int i = 0; i < Month; ++i)
totla += books[n][i];
cout <<“第” << n+1 <<"年中总共卖出《C++ For Fools》一书: " << totla << endl;
}

return 0;

}

//7题
#include
#include
struct Cars * cars(); //new创建动态内存,接受数据并存储,返回内存地址。
struct Cars {
char brand[20];
int year;
};
using namespace std;
int main()
{
int size; //结构数组的大小
cout <<"How many cars do you wish to catalog? ";
cin >> size;
cin.get(); //处理留存在缓冲区中的回车符
Cars *pn[size];
for (int i = 0; i < size; ++i)
{
cout <<“car #” << i + 1 << endl;
pn[i] = cars();
}
cout <<“Here is your collection:\n”;

//打印结构内容,并释放内存空间
for(int i = 0; i < size; ++i)
{
    cout <<pn[i]->year <<" " <<pn[i]->brand << endl;
    delete pn[i];
}
    
return 0;

}
struct Cars * cars()
{
Cars *pf = new Cars;
cout <<"Please enter the make: ";
cin.getline(pf->brand, 20);
cout <<"Please enter the year made: ";
cin >>pf->year;
cin.get();

return pf;

}

//8题
#include
#include
int main()
{
char ch[20] = {’\0’};
int number = 0;

std::cout <<"Enter words (to stop, type the word done):\n";
std:: cin >>ch;
//cin 接受了多个单词,把第一个单词存储在数组ch中,其他的全部留存在缓冲区,等待输入
//第一个cin 在接受回车符时,会停止输入,所以循环体内需要一个cin
//来引导输入缓冲区中留存的单词,最后通过循环来释放留存在缓冲区中的单词,并存储在数组中。
//这里缓冲区没有释放完待输入的单词时,是不需要人为输入单词的。
//每循环一次对比数组的字符串 单词数+1;和done字符串一样将退出循环,如果在释放缓冲区中
//所有单词都没有done,将需要继续输入知道有一个单词时done为止结束循环。 
while (strcmp(ch, "done"))
{
    std:: cin >>ch;
    ++number;
}
std::cout <<"You entered a total of " << number <<" words.\n";
return 0;

}

//9题
#include
#include
int main()
{
std::string ch;
int number = 0;

std::cout <<"Enter words (to stop, type the word done):\n";
std:: cin >>ch;
//cin 接受了多个单词,把第一个单词存储在数组ch中,其他的全部留存在缓冲区,等待输入
//第一个cin 在接受回车符时,会停止输入,所以循环体内需要一个cin
//来引导输入缓冲区中留存的单词,最后通过循环来释放留存在缓冲区中的单词,并存储在数组中。
//这里缓冲区没有释放完待输入的单词时,是不需要人为输入单词的。
//每循环一次对比数组的字符串 单词数+1;和done字符串一样将退出循环,如果在释放缓冲区中
//所有单词都没有done,将需要继续输入知道有一个单词时done为止结束循环。
while (ch != "done")
{
    std:: cin >>ch;
    ++number;
}
std::cout <<"You entered a total of " << number <<" words.\n";
return 0;

}

//10题
#include
int main()
{
using namespace std;
int num,i,j;
cout <<“Enter number of rows: “;
cin >>num;
for(i = 0; i < num; ++i)
{
for (j = 0; j < (num -(i+1)); ++j)
cout <<”.”;
for (int k = 0; k< (num - j); ++k)
cout <<"*";
cout << endl;
}

return 0;

}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/103728185
今日推荐