码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Primer章课后编程问题

时间:2015-10-18 09:56:14      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
技术分享
1、
代码
#include<iostream>
int main()
{
  using namespace std;
  int num1;
  int num2;
  int total=0;
  cout << "请输入開始数字\n";
  cin >> num1;
  cout << "请输入结束数字\n";
  cin >> num2;
  for (num1; num1<=num2; num1++)
    total = num1 + total;
  cout << num1 << " 和 " << num2 << "之间的整数和为 " << total <<endl;
  return 0;
}
执行结果
技术分享

2、
代码
#include<iostream>
int main()
{
  using namespace std;
  double total = 0.0;
  double in;
  cout << "请输入数字:";
  cin >> in;
  while (in != 0)
  {
    total += in;
    cout << "全部输入数的和为:" << total << "\n";
    cout << "请输入下一个数字:";
    cin >> in;
  }
  cout << "执行结束";
  return 0;
}
执行结果
技术分享

3、
代码
#include<iostream>
int main()
{
  using namespace std;
  double daphne=100;
  double cleo=100;
  int year=1;
  while (daphne >= cleo)
  {
    daphne += 100*0.1;
    cleo += cleo*0.05;
    cout << "第" << year << "年,daphne投资价值为 " << daphne << "cleo投资价值为 " << cleo <<endl;
    year++;
  }
  cout << "第 " << year << "年,时cleo超过daphne的投资价值"<< endl;
  return 0;
}
执行结果
技术分享

4、
代码
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{
    using namespace std;
    int sales[12];
    int total;
    for (int i=1; i<=12; i++)
    {
        cout << "请输入" << months[i-1] << "销售数量:";
        cin >> sales[i-1];
    }
    for (int j=0; j<12; j++)
    {
        total = total+sales[j];
    }
    cout << "总销售为:" << total <<endl;
    return 0;
}
执行结果
技术分享

5、
代码
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{
    using namespace std;
    int sales[3][12];
    int total[3] = {0};   //一定要初始化。不然初始值不为0
    int sum;
    for (int a=1;a<=3;a++)
    {
            for (int i=1; i<=12; i++)
            {
                cout << "请输入第"<< a << "年" << months[i-1] << "销售数量:";
                cin >> sales[a-1][i-1];
            }
    }
    for (int b=0; b<3; b++)
    {
            for (int j=0; j<12; j++)
            {
                total[b] = total[b]+sales[b][j];
            }
            sum = sum + total[b];
            cout << "第" << b+1 << "年总销量为" << total[b] <<endl;
    }
    cout << "总销售为:" << sum <<endl;
    return 0;
}
执行结果
技术分享

6、
代码
<pre name="code" class="cpp">//一定要加while (cin.get() != '\n'); 
#include<iostream>
using namespace std;
const int LEN = 60;
struct Car
{
  char brand[LEN];
  int year;
};
int main()
{
  int num;
  cout << "How many cars do you wish to catalog?";
  cin >> num;
  while (cin.get() != '\n');
  Car *ps = new Car[num];
  for (int i=0;i<num;i++)
  {
    cout << "Car #" << (i+1) << ":\n";
    cout << "Please enter the make:";
    cin.getline(ps[i].brand, LEN);
    cout << "Please enter the year made:";
    cin >> ps[i].year;
    while(cin.get() != '\n');
  }
  cout << "Here is your collection:\n";
  for (int i=0; i<num; i++)
  {
    cout << ps[i].year << " " << ps[i].brand <<endl;
  }
  delete [] ps;
  return 0;
}

执行结果
技术分享

7、
代码
#include<iostream>
#include<cstring>
const int STR_LEN=60;
int main()
{
  using namespace std;
  char words[STR_LEN];
  int count=0;
  cout << "Enter words(to stop,type the word done):\n";
  while (cin >> words && strcmp("done", words))
    ++count;
  cout << "You entered a total of " << count << " words .\n" <<endl;
  return 0;
}
执行结果
技术分享

8、
代码
#include<iostream>
#include<string>
int main()
{
  using namespace std;
  string words;
  int count=0;
  cout << "Enter words {to stop,type the word done}:\n";
  while (cin >> words && words != "done")
    ++count;
  cout << "You entered a total of " << count << " words .\n";
  return 0;
}
执行结果
技术分享

9、
代码
#include<iostream>
int main()
{
  using namespace std;
  int row;
  cout << "Enter number of row:";
  cin >> row;
  for (int i=0; i<row; i++)
  {
    for (int j=row-i; j>1; j--)
    {
      cout << ".";
    }
    for (int k=0; k<=i; k++)
    {
      cout << "*";
    }
    cout << "\n";
  }
  return 0;
}
执行结果
技术分享


版权声明:本文博主原创文章。博客,未经同意不得转载。

C++ Primer章课后编程问题

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4888896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!