开发者

How can I improve my C++ code?

开发者 https://www.devze.com 2023-02-02 05:57 出处:网络
This is my first program I really created besides hello world. The code is over 400 lines of code and wanted to know of new functions and what not that would help improve my code and my knowledge of C

This is my first program I really created besides hello world. The code is over 400 lines of code and wanted to know of new functions and what not that would help improve my code and my knowledge of C++. I figured I would just dive in and learn on my own instead of out of the book I have "Teach yourself C++ in one hour a day 6th edition".

What are some new functions I can learn that would improve my code?

// 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 &g开发者_运维百科t;> 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;
}

Edit: Sorry was not clear. I am trying to improve the code for the Runescape Skill Calculator I am working on. I am curious if I could have cut out some lines of code by using different functions. The Code provided is my project I am working on. Can you review it and give me pointers?


Your switch statement is unnecessary. Since

cout << " What is your current experience? ";
cin >> exp;

is exactly the same code in all cases, you can move it out of the switch (to right before it). Before those lines, add code which checks that lvl is in the range from 2 to 100 and exits. Then you'll see that all the cases remaining are of the form

result = k - exp;

where k is some integer. Create an array with 101 elements (so the indices go from 0 to 100) to hold all the possible values of k, and initialize it:

unsigned expToNextLevel[101] = {
    0, 0, 83, 174, 276, /* and so on */
};

Then your huge switch statement turns into:

if(lvl < 2 || lvl > 100)
    exit(0);

cout << " What is your current experience? ";
cin >> exp;
result = expToNextLevel[lvl] - exp;

You can use a similar technique of moving things into arrays to take your block of cout lines listing each material and ore requirements and turn it into a for loop.

One of the fundamental skills of programming is to recognize repetition and find a way to turn it into a loop or otherwise simplify it. What you've written is called "spaghetti code", and a red flag is that there's a lot of copying and pasting.

Whenever you find yourself copying and pasting code, you should think "this should be a loop or a function." The parts that change from version to version are the parts that need to be function parameters or array lookups.


When you see a lot of "magic numbers" in repeated code, you should always ask yourself: Is there some way I can calculate that number?

It turns out that you can calculate the amount of experience needed for a certain level:

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));
}

This removes the need for the monolithic switch statement.


the best two things you can do to learn to write good code are

  1. Write stuff, and get peer reviewed.
  2. Fix other people's broken stuff

you'll learn a lot with those two tasks.


There is hardly a way without reading anything.

Language reference: http://www.cplusplus.com/doc/tutorial/

Bible: http://www.amazon.com/C-Programming-Language-Special/dp/0201700735/ref=pd_sim_b_3

I like this one a lot but it's probably not the first step: http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889

This is the unbelievable book but its content borders with insanity at some point :) http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315/ref=pd_sim_b_6


I'm not sure I understand your question, but I've done my best to interpret it.

With that in mind, learn the stl, and learn boost.


I looked into open source code written by C++. There are a lots of well designed project in source forge and other open source project site. Download it and learn it from good source code.


No offence. But how about start from the 'function, array, loops' only if you don't know that stuff yet.


Here is a little conversion function that gets you close. It is a little off on the larger numbers. I don't know if its my function or a programming data leak. To use it just throw it in before the main function and after your library definitions and then you can use it!

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);
}

You will need to include the math library if you do end up using it. Here is a little example on how to use it:

int result = toExp(level) - currentExp;
cout << "Experience needed: " << result << endl
     << "Rune Essence: " << ceil(result/5.0) << endl
     << "Clay: " << ceil(result/5.0) << endl;

Again, the function does get of by 10 when it hits level 100 so its not the most accurate, but it is pretty short and simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消