- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我除了hello world之外真正创建的第一个程序。该代码超过 400 行代码,想了解新功能以及有助于改进我的代码和我的 C++ 知识的内容。我想我应该投入其中并自学,而不是从我拥有的“每天一小时自学 C++ 第 6 版”这本书中学习。
我可以学习哪些新函数来改进我的代码?
// Runescape Mining Calculator
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int lvl;
int exp;
int result;
cout << " \t\t\tRunescape Skill Calculator" << endl;
cout << " Enter Target level: ";
cin>>lvl;
switch(lvl)
{
case 2: cout << " What is your current experience? ";;
cin >> exp;
result=83 - exp;
break;
case 3: cout << " What is your current experience? ";
cin >> exp;
result=174 - exp;
break;
case 4: cout << " What is your current experience? ";
cin >> exp;
result=276 - exp;
break;
case 5: cout << " What is your current experience? ";
cin >> exp;
result=388 - exp;
break;
case 6: cout << " What is your current experience? ";
cin >> exp;
result=512 - exp;
break;
case 7: cout << " What is your current experience? ";
cin >> exp;
result=650 - exp;
break;
case 8: cout << " What is your current experience? ";
cin >> exp;
result=801 - exp;
break;
case 9: cout << " What is your current experience? ";
cin >> exp;
result=969 - exp;
break;
case 10: cout << " What is your current experience? ";
cin >> exp;
result=1154 - exp;
break;
case 11: cout << " What is your current experience? ";
cin >> exp;
result=1358 - exp;
break;
case 12: cout << " What is your current experience? ";
cin >> exp;
result=1584 - exp;
break;
case 13: cout << " What is your current experience? ";
cin >> exp;
result=1833 - exp;
break;
case 14: cout << " What is your current experience? ";
cin >> exp;
result=2107 - exp;
break;
case 15: cout << " What is your current experience? ";
cin >> exp;
result=2411 - exp;
break;
case 16: cout << " What is your current experience? ";
cin >> exp;
result=2746 - exp;
break;
case 17: cout << " What is your current experience? ";
cin >> exp;
result=3115 - exp;
break;
case 18: cout << " What is your current experience? ";
cin >> exp;
result=3523 - exp;
break;
case 19: cout << " What is your current experience? ";
cin >> exp;
result=3973 - exp;
break;
case 20: cout << " What is your current experience? ";
cin >> exp;
result=4470 - exp;
break;
case 21: cout << " What is your current experience? ";
cin >> exp;
result=5018 - exp;
break;
case 22: cout << " What is your current experience? ";
cin >> exp;
result=5624 - exp;
break;
case 23: cout << " What is your current experience? ";
cin >> exp;
result=6291 - exp;
break;
case 24: cout << " What is your current experience? ";
cin >> exp;
result=7028 - exp;
break;
case 25: cout << " What is your current experience? ";
cin >> exp;
result=7842 - exp;
break;
case 26: cout << " What is your current experience? ";
cin >> exp;
result=8740 - exp;
break;
case 27: cout << " What is your current experience? ";
cin >> exp;
result=9730 - exp;
break;
case 28: cout << " What is your current experience? ";
cin >> exp;
result=10824 - exp;
break;
case 29: cout << " What is your current experience? ";
cin >> exp;
result=12031 - exp;
break;
case 30: cout << " What is your current experience? ";
cin >> exp;
result=13363 - exp;
break;
case 31: cout << " What is your current experience? ";
cin >> exp;
result=14833 - exp;
break;
case 32: cout << " What is your current experience? ";
cin >> exp;
result=16456 - exp;
break;
case 33: cout << " What is your current experience? ";
cin >> exp;
result=18247 - exp;
break;
case 34: cout << " What is your current experience? ";
cin >> exp;
result=20224 - exp;
break;
case 35: cout << " What is your current experience? ";
cin >> exp;
result=22406 - exp;
break;
case 36: cout << " What is your current experience? ";
cin >> exp;
result=24815 - exp;
break;
case 37: cout << " What is your current experience? ";
cin >> exp;
result=27473 - exp;
break;
case 38: cout << " What is your current experience? ";
cin >> exp;
result=30408 - exp;
break;
case 39: cout << " What is your current experience? ";
cin >> exp;
result=33648 - exp;
break;
case 40: cout << " What is your current experience? ";
cin >> exp;
result=37224 - exp;
break;
case 41: cout << " What is your current experience? ";
cin >> exp;
result=41171 - exp;
break;
case 42: cout << " What is your current experience? ";
cin >> exp;
result=45529 - exp;
break;
case 43: cout << " What is your current experience? ";
cin >> exp;
result=50339- exp;
break;
case 44: cout << " What is your current experience? ";
cin >> exp;
result=55649 - exp;
break;
case 45: cout << " What is your current experience? ";
cin >> exp;
result=61512 - exp;
break;
case 46: cout << " What is your current experience? ";
cin >> exp;
result=67983 - exp;
break;
case 47: cout << " What is your current experience? ";
cin >> exp;
result=75127 - exp;
break;
case 48: cout << " What is your current experience? ";
cin >> exp;
result=83014 - exp;
break;
case 49: cout << " What is your current experience? ";
cin >> exp;
result=91721 - exp;
break;
case 50: cout << " What is your current experience? ";
cin >> exp;
result=101333 - exp;
break;
case 51: cout << " What is your current experience? ";
cin >> exp;
result=111945 - exp;
break;
case 52: cout << " What is your current experience? ";
cin >> exp;
result=123660 - exp;
break;
case 53: cout << " What is your current experience? ";
cin >> exp;
result=136594 - exp;
break;
case 54: cout << " What is your current experience? ";
cin >> exp;
result=150872 - exp;
break;
case 55: cout << " What is your current experience? ";
cin >> exp;
result=166636 - exp;
break;
case 56: cout << " What is your current experience? ";
cin >> exp;
result=184040 - exp;
break;
case 57: cout << " What is your current experience? ";
cin >> exp;
result=203254 - exp;
break;
case 58: cout << " What is your current experience? ";
cin >> exp;
result=224466 - exp;
break;
case 59: cout << " What is your current experience? ";
cin >> exp;
result=247886 - exp;
break;
case 60: cout << " What is your current experience? ";
cin >> exp;
result=273742 - exp;
break;
case 61: cout << " What is your current experience? ";
cin >> exp;
result=302288 - exp;
break;
case 62: cout << " What is your current experience? ";
cin >> exp;
result=333804 - exp;
break;
case 63: cout << " What is your current experience? ";
cin >> exp;
result=368599 - exp;
break;
case 64: cout << " What is your current experience? ";
cin >> exp;
result=407015 - exp;
break;
case 65: cout << " What is your current experience? ";
cin >> exp;
result=449428 - exp;
break;
case 66: cout << " What is your current experience? ";
cin >> exp;
result=496254 - exp;
break;
case 67: cout << " What is your current experience? ";
cin >> exp;
result=547953 - exp;
break;
case 68: cout << " What is your current experience? ";
cin >> exp;
result=605032 - exp;
break;
case 69: cout << " What is your current experience? ";
cin >> exp;
result=668051 - exp;
break;
case 70: cout << " What is your current experience? ";
cin >> exp;
result=737627 - exp;
break;
case 71: cout << " What is your current experience? ";
cin >> exp;
result=814445 - exp;
break;
case 72: cout << " What is your current experience? ";
cin >> exp;
result=899257 - exp;
break;
case 73: cout << " What is your current experience? ";
cin >> exp;
result=992895 - exp;
break;
case 74: cout << " What is your current experience? ";
cin >> exp;
result=1096278 - exp;
break;
case 75: cout << " What is your current experience? ";
cin >> exp;
result=1210421 - exp;
break;
case 76: cout << " What is your current experience? ";
cin >> exp;
result=1336443 - exp;
break;
case 77: cout << " What is your current experience? ";
cin >> exp;
result=1475581 - exp;
break;
case 78: cout << " What is your current experience? ";
cin >> exp;
result=1629200 - exp;
break;
case 79: cout << " What is your current experience? ";
cin >> exp;
result=1798808 - exp;
break;
case 80: cout << " What is your current experience? ";
cin >> exp;
result=1986068 - exp;
break;
case 81: cout << " What is your current experience? ";
cin >> exp;
result=2192818 - exp;
break;
case 82: cout << " What is your current experience? ";
cin >> exp;
result=2421087 - exp;
break;
case 83: cout << " What is your current experience? ";
cin >> exp;
result=2673114 - exp;
break;
case 84: cout << " What is your current experience? ";
cin >> exp;
result=2951373 - exp;
break;
case 85: cout << " What is your current experience? ";
cin >> exp;
result=3258594 - exp;
break;
case 86: cout << " What is your current experience? ";
cin >> exp;
result=3597792 - exp;
break;
case 87: cout << " What is your current experience? ";
cin >> exp;
result=3972294 - exp;
break;
case 88: cout << " What is your current experience? ";
cin >> exp;
result=4385776 - exp;
break;
case 89: cout << " What is your current experience? ";
cin >> exp;
result=4842295 - exp;
break;
case 90: cout << " What is your current experience? ";
cin >> exp;
result=5346332 - exp;
break;
case 91: cout << " What is your current experience? ";
cin >> exp;
result=5902831 - exp;
break;
case 92: cout << " What is your current experience? ";
cin >> exp;
result=6517253 - exp;
break;
case 93: cout << " What is your current experience? ";
cin >> exp;
result=7195629 - exp;
break;
case 94: cout << " What is your current experience? ";
cin >> exp;
result=7944614 - exp;
break;
case 95: cout << " What is your current experience? ";
cin >> exp;
result=8771558 - exp;
break;
case 96: cout << " What is your current experience? ";
cin >> exp;
result=9684577 - exp;
break;
case 97: cout << " What is your current experience? ";
cin >> exp;
result=10692629 - exp;
break;
case 98: cout << " What is your current experience? ";
cin >> exp;
result=11805606 - exp;
break;
case 99: cout << " What is your current experience? ";
cin >> exp;
result=13034431 - exp;
case 100: cout << " What is your current experience? " <<endl;
cin >> exp;
result=14391160 - exp;
break;
default: exit(0);
}
cout << " Experience Needed: " << result << endl;
// Ores needed to be mined to acquire the level up.
cout << " Rune Essence: " << ceil(result/5.0) << endl;
cout << " Clay: " << ceil(result/5.0) << endl;
cout << " Copper: " << ceil(result/17.5) << endl;
cout << " Tin: " << ceil(result/17.5) << endl;
cout << " Bluerite: " << ceil(result/17.5) << endl;
cout << " Iron: " << ceil(result/35.0) << endl;
cout << " Silver: " << ceil(result/40.0) << endl;
cout << " Coal: " << ceil(result/50.0) << endl;
cout << " Gold: " << ceil(result/65.0) << endl;
cout << " Mithril: " << ceil(result/80.0) << endl;
cout << " Adamant: " << ceil(result/ 95.0) << endl;
cout << " Runite: " << ceil(result/125.0) << endl;
cout << endl << endl << endl << "\t\t\tCreated by USDblades" << endl;
return 0;
}
编辑:抱歉不清楚。我正在尝试改进我正在使用的 Runescape 技能计算器的代码。我很好奇我是否可以通过使用不同的函数来删除一些代码行。提供的代码是我正在从事的项目。能否帮我看看并指点一下?
最佳答案
您的 switch
语句是不必要的。由于
cout << " What is your current experience? ";
cin >> exp;
在所有情况下都是完全相同的代码,您可以将它移出 switch
(移到它之前)。在这些行之前,添加检查 lvl
是否在 2 到 100 范围内并退出的代码。然后你会看到剩下的所有情况都是
result = k - exp;
其中 k
是某个整数。创建一个包含 101 个元素的数组(因此索引从 0 到 100)以保存 k
的所有可能值,并对其进行初始化:
unsigned expToNextLevel[101] = {
0, 0, 83, 174, 276, /* and so on */
};
然后你巨大的switch
语句变成:
if(lvl < 2 || lvl > 100)
exit(0);
cout << " What is your current experience? ";
cin >> exp;
result = expToNextLevel[lvl] - exp;
您可以使用类似的技术将事物移动到数组中,以获取列出每种 Material 和矿石要求的 cout
行 block ,并将其转换为 for
循环。
编程的一项基本技能是识别重复并找到将其变成循环或以其他方式简化它的方法。您编写的内容称为“意大利面条代码”,一个危险信号是存在大量复制和粘贴。
每当您发现自己正在复制和粘贴代码时,您应该思考“这应该是一个循环或一个函数”。版本不同的部分就是需要做函数参数或者数组查找的部分。
关于c++ - 如何改进我的 C++ 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4600798/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!