gpt4 book ai didi

c++ - 如何改进我的 C++ 代码?

转载 作者:太空狗 更新时间:2023-10-29 23:29:02 27 4
gpt4 key购买 nike

这是我除了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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com