这是我在hello world之外创建的第一个程序。代码超过400行代码,想要了解新功能,这有助于改善我的代码和我对C ++的了解。我想我会潜入并自己学习,而不是出自“我每天一小时第六版自学C ++”这本书。
我可以学习哪些新功能可以改善我的代码?
// 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技能计算器的代码。我很好奇我是否可以通过使用不同的函数来删除一些代码行。提供的守则是我正在进行的项目。你能复习一下并给我指点吗?
答案 0 :(得分:8)
您的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;
您可以使用类似的方法将事物移动到数组中,以便列出列出每个材料和矿石要求的cout
行,并将其转换为for
循环。
编程的基本技能之一是识别重复并找到将其转换为循环或以其他方式简化它的方法。你所写的内容被称为“意大利面条代码”,而红旗则表示有很多复制和粘贴。
每当你发现自己复制和粘贴代码时,你应该认为“这应该是一个循环或一个函数。”从版本更改为版本的部分是需要作为函数参数或数组查找的部分。
答案 1 :(得分:3)
当你在重复的代码中看到很多“神奇的数字”时,你应该总是问自己:我有什么方法可以计算出这个数字吗?
事实证明,您可以计算某个级别所需的经验数量:
unsigned int experienceNeeded(unsigned int level) {
float needed = 0;
for(unsigned int x = 1; x < level; ++x) {
needed += ::floor(static_cast<float>(x) + 300 * ::pow(2, static_cast<float>(x)/7));
}
return static_cast<unsigned int>(::floor(needed / 4));
}
这消除了对单片开关语句的需要。
答案 2 :(得分:2)
你可以做的最好的两件事就是学习编写好的代码
你将从这两项任务中学到很多东西。
答案 3 :(得分:1)
没有阅读任何东西几乎没有办法。
语言参考:http://www.cplusplus.com/doc/tutorial/
圣经:http://www.amazon.com/C-Programming-Language-Special/dp/0201700735/ref=pd_sim_b_3
我非常喜欢这个,但可能不是第一步:http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889
这是一本令人难以置信的书,但它的内容在某些时候与精神错乱接近:) http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315/ref=pd_sim_b_6
答案 4 :(得分:0)
我不确定我理解你的问题,但我已尽力解释它。
考虑到这一点,学习stl,并学习提升。
答案 5 :(得分:0)
我查看了C ++编写的开源代码。 源码伪造和其他开源项目网站有很多精心设计的项目。 下载并从良好的源代码中学习。
答案 6 :(得分:0)
没有冒犯。但是,如果你还不知道那些东西,那么从'函数,数组,循环'开始吧。
答案 7 :(得分:0)
这是一个让你感动的小转换功能。对于较大的数字,它有点偏差。我不知道它的功能或编程数据是否泄漏。要使用它,只需将它放在主函数之前和库定义之后,然后就可以使用它了!
int toExp(int level)
{
double experience = 0;
for (int i = 1; i < level; i++)
{
experience += (i / 4.0) + 75.0 * pow(2.0, (i / 7.0));
}
return floor(experience);
}
如果最终使用数学库,则需要包含数学库。 以下是如何使用它的一个小例子:
int result = toExp(level) - currentExp;
cout << "Experience needed: " << result << endl
<< "Rune Essence: " << ceil(result/5.0) << endl
<< "Clay: " << ceil(result/5.0) << endl;
同样,当它达到100级时,该函数确实得到10,因此它不是最准确的,但它非常简短。