当前位置: 首页 > news >正文

C++Primerplus编程练习 第五章

第五章

1

在这里插入图片描述

#include <iostream>int main()
{using namespace std;int low;int high;long result=0;cout << "请输入两个整数"<<endl;cout << "第一个较小的整数: ";cin >> low;cout << "第二个较大的整数: ";cin >> high;cout << low <<" 到 " << high<<" 之间所有整数和为 ";for(;low<=high;low++){                                                 result +=low;}   cout << result <<endl;return 0;
}

2

在这里插入图片描述

#include <iostream>
#include <array>
int main()
{using namespace std;const int AR_SIZE = 101;                                               array<long double, AR_SIZE> factorials;factorials[0]=factorials[1]=1;for(int i=2;i<AR_SIZE;i++){factorials[i]=factorials[i-1]*i;}   for(int i=0;i<AR_SIZE;i++){cout<<i << "!= " << factorials[i]<<endl;}   return 0;
}

3

在这里插入图片描述

#include <iostream>
int main()
{using namespace std;long long result=0;cout << "请输入数字,输入0时停止" <<endl;int temp;cin >> temp;while(temp !=0){result += temp;if(cin.get()=='\n'){cout << "目前的总和为 "<< result<<endl;                        }   cin >> temp;}   return 0;
}

4

在这里插入图片描述

#include <iostream>int main()
{using namespace std;                                                   const double DEPOSIT=100;const double singleMargin=0.1;const double compoundMargin=0.05;double DaphneProfit=0;double CleoProfit=0;int year=0;while(CleoProfit<=DaphneProfit){CleoProfit += (DEPOSIT+CleoProfit)*compoundMargin;DaphneProfit += DEPOSIT*singleMargin;year++;}   cout << year << " 年后Cleo的利润超过Daphne \n"<< "Cleo 的利润为 " << CleoProfit<<endl<< "Daphne 的利润为 " << DaphneProfit << endl;return 0;
}

5

在这里插入图片描述

#include <iostream>
#include <string>
int main()
{using namespace std;const int MONTHNUM=12;const string MONTH[MONTHNUM] {"一月", "二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};  int sales[MONTHNUM];int all =0; for(int i=0;i<MONTHNUM;i++){cout << "请输入"<<MONTH[i] << "的销售额 ";                         cin >> sales[i];all += sales[i];}   cout << "总销售额为 " << all <<" 元\n";return 0;
}

6

在这里插入图片描述

#include <iostream>
#include <string>
int main()
{using namespace std;const int MONTHNUM=12;const string MONTH[MONTHNUM] {"一月", "二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};  int sales[3][MONTHNUM];int all =0;for(int j=0;j<3;j++){cout << "请输入第 " << j+1<< " 年的销售记录"<<endl;for(int i=0;i<MONTHNUM;i++){                                       cout << "请输入"<<MONTH[i] << "的销售额 ";cin >> sales[j][i];all += sales[j][i];}   }   cout << "总销售额为 " << all <<" 元\n";return 0;
}

7

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
struct car{string manufacturer;int year;
};
int main()
{int carNum;car* carArray;cout << "How many cats do you wish to catalog? ";cin >> carNum;cin.get();carArray = new car [2];for(int i=0;i<carNum;i++){cout << "Car #"<<i<<" :"<<endl<<"Please enter the make: ";getline(cin,carArray[i].manufacturer);cout << "Please enter the year made: ";cin >> carArray[i].year;cin.get();}                                                                      cout << "Here is your collection:"<<endl;for(int i=0;i<carNum;i++){cout<<carArray[i].year<<" "<<carArray[i].manufacturer<<endl;}   delete [] carArray;return 0;
}

8

在这里插入图片描述

#include <iostream>
#include <cstring>
int main()
{using namespace std;char words[50];int wordNum=0;cout << "Enter words (to stop, type the word done): "<<endl;cin >> words;while(strcmp(words, "done")){wordNum++;cin>>words;}   cout << "You entered a total of " << wordNum << " words."<<endl;       return 0;
}

9

在这里插入图片描述

#include <iostream>
#include <string>
int main()
{using namespace std;string words;int wordNum=0;cout << "Enter words (to stop, type the word done): "<<endl;cin >> words;while(words!="done"){                                                  wordNum++;cin>>words;}   cout << "You entered a total of " << wordNum << " words."<<endl;return 0;
}

10

在这里插入图片描述

#include <iostream>int main()
{using namespace std;int rows;cout << "Enter number of rows: ";cin >> rows;for(int i=0;i<rows;i++){for(int j=rows-i-1;j>0;j--){cout<<'.';}for(int j=i+1;j>0;j--){cout<<'*';}cout <<endl;}                                                                      return 0;
}
http://www.xdnf.cn/news/271369.html

相关文章:

  • 继V1.5之后,幻方又发布了 DeepSeek-Prover-V2-671B,参数提升100倍
  • 【AI平台】n8n入门6:调用MCP服务(非社区节点)
  • 构建灵活的配置管理系统:YAML 与 TOML 的应用与热更新实践
  • 生成树、Prime、Kruskal
  • 第40课 常用快捷操作——按“Tab键”即时更改属性
  • 为什么需要启动探针(StartupProb)?
  • neatchat轻量级丝滑的ai模型web客户端
  • python进阶(2)二进制
  • 文件操作-
  • 【今日三题】游游的重组偶数(模拟) / 体操队形(回溯) / 二叉树中的最大路径和(树形dp)
  • 注入内部Bean
  • C与指针5——字符串合集
  • 高频数据冲击数据库的技术解析与应对方案
  • 基于构件的软件开发方法及其应用
  • Linux系统如何完成系统周期化任务
  • 什么是 Redis?
  • 定长滑动窗口(基础)
  • 【Mytais系列】核心工作流程
  • C++类_移动构造函数
  • <init-param>和<load-on-startup>的作用
  • 重新构想E-E-A-T:提升销售与搜索可见性的SEO策略
  • 如何优化MySQL主从复制的性能?
  • 【电路笔记】-自耦变压器
  • c++ 函数参数传递
  • 推理能力:五一模型大放送
  • 硬件零基础入门(尚硅谷)
  • JavaScript中的AES加密与解密:原理、代码与实战
  • Day04 新增套餐
  • 双指针算法详解(含力扣和蓝桥杯例题)
  • 王道考研数据结构课后题代码题(2026版)——排序部分