How to fine tune code

Page 1 of 1

3 Replies - 298 Views - Last Post: 13 May 2013 - 08:15 PM Rate Topic: -----

#1 hangloose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-May 13

How to fine tune code

Posted 13 May 2013 - 04:55 PM

I am a beginner at C++ programing and struggling with simplifying code. For example, the code bellow converts units and uses pass by reference but I am not sure how to simplify the code while keeping the functions that I have. Can anyone show me how to!? :bigsmile:

#include <iostream>
#include <math.h>
using namespace std;

enum ConversionType {LENGTH_TO_METRIC, LENGTH_TO_US, WEIGHT_TO_METRIC, WEIGHT_TO_US};
void convert_lengths();
void convert_weights();
void length_to_metric(ConversionType conversion);
void length_to_us(ConversionType conversion);
void weight_to_metric(ConversionType conversion);
void weight_to_us(ConversionType conversion);
void prompt_user(int& large_unit, double& small_unit, ConversionType conversion);
void convert(int large_unit, double small_unit, int& new_large, double& new_small, ConversionType conversion);
void output_results(int large_before, double small_before, int large_after, double small_after, ConversionType conversion);

int main()
{
       int value;
       do {
              do {
                     cout <<"Would you like to convert the weight or length:\n"
                              "Press 1 for Lengths\n"
                              "Press 2 for Weight or\n"
                              "Press 0 to terminate program\n";
                     cin >> value;
                     if (value != 0 && value != 1 && value != 2)
                           cout << "Invalid entry." << endl;
                     } while (value != 0 && value != 1 && value != 2); 
              switch (value)
              {
                     case 0: 
                           break;
                     case 1: convert_lengths(); 
                           break;
                     case 2: convert_weights();
                           break;
              } 
       } while(value != 0);

       return 0;
}

void convert_lengths()
{
       cout << "We are going to convert lengths for you." << endl;
       int value;
       do 
       {
              cout << "Would you like to convert:\n"
                           "Feet/Inches to Meters/Centimeters? If so press 1.\n"
                           "Meters/Centimeters to Feet/Inches? If so press 2. \n"
                           "Press 0 to quit.\n";
              cin >> value;
              if (value !=0 &&value !=1 && value !=2)
                     cout << "Invalad entry." << endl;
       } while (value !=0 &&value !=1 && value !=2);
       switch (value)
       {
              case 0: break;
              case 1: length_to_metric(LENGTH_TO_METRIC);     break;
              case 2: length_to_us(LENGTH_TO_US);      break;
       }
}

void convert_weights()
{
       cout << "We are going to convert weights for you." << endl;
       int value;
       do 
       {
              cout << "Would you like to convert:\n"
                           "Pounds/ounces to Kilograms/grams? If so press 1.\n"
                           "Kilograms/Grams to pounds/ounces? If so press 2.\n"
                           "Press 0 to quit.\n";
              cin >> value;
              if (value !=0 &&value !=1 && value !=2)
                     cout << "Invalad entry." << endl;
       } while (value !=0 &&value !=1 && value !=2);
       switch (value)
       {
              case 0: break;
              case 1: weight_to_metric(WEIGHT_TO_METRIC);     break;
              case 2: weight_to_us(WEIGHT_TO_US);      break;
       }
}

void length_to_metric(ConversionType conversion)
{
       int feet, meters;
       double inches, centimeters;
       cout << "You have chosen to convert Feet/Inches to Meters/Centimeters.\n";
       prompt_user(feet, inches, LENGTH_TO_METRIC);
       convert(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
       output_results(feet, inches, meters, centimeters, LENGTH_TO_METRIC);

}

void length_to_us(ConversionType conversion)
{
       int meters, feet;
       double centimeters, inches;
       cout << "You have chosen to convert Meters/Centimeters to Feet/Inches.\n";
       prompt_user(meters, centimeters, LENGTH_TO_US);
       convert(meters, centimeters, feet, inches, LENGTH_TO_US);
       output_results(meters, centimeters, feet, inches, LENGTH_TO_US);
}

void weight_to_metric(ConversionType conversion)
{
       int pounds, kilograms;
       double ounces, grams;
       cout << "You have chosen to convert Pounds/Ounces to Kilograms/Grams.\n";
       prompt_user(pounds, ounces, WEIGHT_TO_METRIC);
       convert(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
       output_results(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
}

void weight_to_us(ConversionType conversion) 
{
       int kilograms, pounds;
       double grams, ounces;
       cout << "You have chosen to convert Kilograms/Grams to Pounds/Ounces.\n";
       prompt_user(kilograms, grams, WEIGHT_TO_US);
       convert(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
       output_results(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
}

void prompt_user(int& bigger_unit, double& smaller_unit, ConversionType conversion) 
{

       switch (conversion) {
       case LENGTH_TO_METRIC:
              cout << "Please enter the length:\n"
                           "Feet(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Inches(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case LENGTH_TO_US:
              cout << "Please enter the length:\n"
                           "Meters(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Centimeters(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case WEIGHT_TO_METRIC:
              cout << "Please enter the weight:\n"
                           "Pounds(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Ounces(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case WEIGHT_TO_US:
              cout << "Please enter the weight:\n"
                           "Kilograms(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Grams(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       }
}

void convert(int bigger_unit, double smaller_unit, int& new_bigger, double& new_smaller, ConversionType conversion)
{
       const double LENGTH_CONV = .3048,  SM_L_CONV = 2.54,
                           WEIGHT_CONV = 2.2046, SM_W_CONV = 28.35;
       switch (conversion) {
       case LENGTH_TO_METRIC:
              new_bigger = bigger_unit * LENGTH_CONV;
              new_smaller = smaller_unit * SM_L_CONV;
              new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
              while (new_smaller >= 100.0) {
                     new_bigger++;
                     new_smaller -= 100;
              }
              break;
       case LENGTH_TO_US:
              new_bigger = bigger_unit / LENGTH_CONV;
              new_smaller = smaller_unit / SM_L_CONV;
              new_smaller += ((bigger_unit / LENGTH_CONV) - floor(bigger_unit / LENGTH_CONV)) * 12;
              while (new_smaller >= 12.0) {
                     new_bigger++;
                     new_smaller -= 12.0;
              }
              break;
       case WEIGHT_TO_METRIC:
              new_bigger = bigger_unit / WEIGHT_CONV;
              new_smaller = smaller_unit * SM_W_CONV;
              new_smaller += ((bigger_unit / WEIGHT_CONV) - floor(bigger_unit / WEIGHT_CONV)) * 1000;
              if (new_smaller >= 1000.0) {
                     new_bigger++;
                     new_smaller -= 1000;
              }
              break;
       case WEIGHT_TO_US:
              new_bigger = bigger_unit * WEIGHT_CONV;
              new_smaller = smaller_unit / SM_W_CONV;
              new_smaller += ((bigger_unit * WEIGHT_CONV) - floor(bigger_unit * WEIGHT_CONV)) * 16;
              if (new_smaller >= 16.0) {
                     new_bigger++;
                     new_smaller -= 16;
              }
              break;
       }
}

void output_results(int bigger_before, double smaller_before, int bigger_after, double smaller_after, ConversionType conversion) 
{
       switch (conversion) {
       case LENGTH_TO_METRIC:
              cout << bigger_before << " feet " << smaller_before << " inches = " << bigger_after << " meters " << smaller_after << " centimeters." << endl;
              break;
       case LENGTH_TO_US:
              cout << bigger_before << " meters " << smaller_before << " centimeters = " << bigger_after << " feet " << smaller_after << " inches." << endl;
              break;
       case WEIGHT_TO_METRIC:
              cout << bigger_before << " pounds " << smaller_before << " ounces = " << bigger_after << " kilograms " << smaller_after << " grams." << endl;
              break;
       case WEIGHT_TO_US:
              cout << bigger_before << " kilograms " << smaller_before << " grams = " << bigger_after << " pounds " << smaller_after << " ounces." << endl;
              break;
       }
}







Is This A Good Question/Topic? 0
  • +

Replies To: How to fine tune code

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: How to fine tune code

Posted 13 May 2013 - 05:07 PM

Closing until I get more explanation via PM regarding the recent spamming.
Was This Post Helpful? 0
  • +
  • -

#3 hangloose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-May 13

Re: How to fine tune code

Posted 13 May 2013 - 07:35 PM

This program converts units and uses pass by reference. I am new to C++ program and am still trying to learn of to simplify code. I am a physics major and it is important I understand how to do this.

#include <iostream>
#include <math.h>
using namespace std;

enum ConversionType {LENGTH_TO_METRIC, LENGTH_TO_US, WEIGHT_TO_METRIC, WEIGHT_TO_US};
void convert_lengths();
void convert_weights();
void length_to_metric(ConversionType conversion);
void length_to_us(ConversionType conversion);
void weight_to_metric(ConversionType conversion);
void weight_to_us(ConversionType conversion);
void prompt_user(int& large_unit, double& small_unit, ConversionType conversion);
void convert(int large_unit, double small_unit, int& new_large, double& new_small, ConversionType conversion);
void output_results(int large_before, double small_before, int large_after, double small_after, ConversionType conversion);

int main()
{
       int value;
       do {
              do {
                     cout <<"Would you like to convert the weight or length:\n"
                              "Press 1 for Lengths\n"
                              "Press 2 for Weight or\n"
                              "Press 0 to terminate program\n";
                     cin >> value;
                     if (value != 0 && value != 1 && value != 2)
                           cout << "Invalid entry." << endl;
                     } while (value != 0 && value != 1 && value != 2); 
              switch (value)
              {
                     case 0: 
                           break;
                     case 1: convert_lengths(); 
                           break;
                     case 2: convert_weights();
                           break;
              } 
       } while(value != 0);

       return 0;
}

void convert_lengths()
{
       cout << "We are going to convert lengths for you." << endl;
       int value;
       do 
       {
              cout << "Would you like to convert:\n"
                           "Feet/Inches to Meters/Centimeters? If so press 1.\n"
                           "Meters/Centimeters to Feet/Inches? If so press 2. \n"
                           "Press 0 to quit.\n";
              cin >> value;
              if (value !=0 &&value !=1 && value !=2)
                     cout << "Invalad entry." << endl;
       } while (value !=0 &&value !=1 && value !=2);
       switch (value)
       {
              case 0: break;
              case 1: length_to_metric(LENGTH_TO_METRIC);     break;
              case 2: length_to_us(LENGTH_TO_US);      break;
       }
}

void convert_weights()
{
       cout << "We are going to convert weights for you." << endl;
       int value;
       do 
       {
              cout << "Would you like to convert:\n"
                           "Pounds/ounces to Kilograms/grams? If so press 1.\n"
                           "Kilograms/Grams to pounds/ounces? If so press 2.\n"
                           "Press 0 to quit.\n";
              cin >> value;
              if (value !=0 &&value !=1 && value !=2)
                     cout << "Invalad entry." << endl;
       } while (value !=0 &&value !=1 && value !=2);
       switch (value)
       {
              case 0: break;
              case 1: weight_to_metric(WEIGHT_TO_METRIC);     break;
              case 2: weight_to_us(WEIGHT_TO_US);      break;
       }
}

void length_to_metric(ConversionType conversion)
{
       int feet, meters;
       double inches, centimeters;
       cout << "You have chosen to convert Feet/Inches to Meters/Centimeters.\n";
       prompt_user(feet, inches, LENGTH_TO_METRIC);
       convert(feet, inches, meters, centimeters, LENGTH_TO_METRIC);
       output_results(feet, inches, meters, centimeters, LENGTH_TO_METRIC);

}

void length_to_us(ConversionType conversion)
{
       int meters, feet;
       double centimeters, inches;
       cout << "You have chosen to convert Meters/Centimeters to Feet/Inches.\n";
       prompt_user(meters, centimeters, LENGTH_TO_US);
       convert(meters, centimeters, feet, inches, LENGTH_TO_US);
       output_results(meters, centimeters, feet, inches, LENGTH_TO_US);
}

void weight_to_metric(ConversionType conversion)
{
       int pounds, kilograms;
       double ounces, grams;
       cout << "You have chosen to convert Pounds/Ounces to Kilograms/Grams.\n";
       prompt_user(pounds, ounces, WEIGHT_TO_METRIC);
       convert(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
       output_results(pounds, ounces, kilograms, grams, WEIGHT_TO_METRIC);
}

void weight_to_us(ConversionType conversion) 
{
       int kilograms, pounds;
       double grams, ounces;
       cout << "You have chosen to convert Kilograms/Grams to Pounds/Ounces.\n";
       prompt_user(kilograms, grams, WEIGHT_TO_US);
       convert(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
       output_results(kilograms, grams, pounds, ounces, WEIGHT_TO_US);
}

void prompt_user(int& bigger_unit, double& smaller_unit, ConversionType conversion) 
{

       switch (conversion) {
       case LENGTH_TO_METRIC:
              cout << "Please enter the length:\n"
                           "Feet(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Inches(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case LENGTH_TO_US:
              cout << "Please enter the length:\n"
                           "Meters(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Centimeters(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case WEIGHT_TO_METRIC:
              cout << "Please enter the weight:\n"
                           "Pounds(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Ounces(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       case WEIGHT_TO_US:
              cout << "Please enter the weight:\n"
                           "Kilograms(integer): ";
              cin >> static_cast<int>(bigger_unit);
              cin.sync();
              cout << "Grams(decimal): ";
              cin >> static_cast<double>(smaller_unit);
              cin.sync();
              break;
       }
}

void convert(int bigger_unit, double smaller_unit, int& new_bigger, double& new_smaller, ConversionType conversion)
{
       const double LENGTH_CONV = .3048,  SM_L_CONV = 2.54,
                           WEIGHT_CONV = 2.2046, SM_W_CONV = 28.35;
       switch (conversion) {
       case LENGTH_TO_METRIC:
              new_bigger = bigger_unit * LENGTH_CONV;
              new_smaller = smaller_unit * SM_L_CONV;
              new_smaller += ((bigger_unit * LENGTH_CONV) - floor(bigger_unit * LENGTH_CONV)) * 100;
              while (new_smaller >= 100.0) {
                     new_bigger++;
                     new_smaller -= 100;
              }
              break;
       case LENGTH_TO_US:
              new_bigger = bigger_unit / LENGTH_CONV;
              new_smaller = smaller_unit / SM_L_CONV;
              new_smaller += ((bigger_unit / LENGTH_CONV) - floor(bigger_unit / LENGTH_CONV)) * 12;
              while (new_smaller >= 12.0) {
                     new_bigger++;
                     new_smaller -= 12.0;
              }
              break;
       case WEIGHT_TO_METRIC:
              new_bigger = bigger_unit / WEIGHT_CONV;
              new_smaller = smaller_unit * SM_W_CONV;
              new_smaller += ((bigger_unit / WEIGHT_CONV) - floor(bigger_unit / WEIGHT_CONV)) * 1000;
              if (new_smaller >= 1000.0) {
                     new_bigger++;
                     new_smaller -= 1000;
              }
              break;
       case WEIGHT_TO_US:
              new_bigger = bigger_unit * WEIGHT_CONV;
              new_smaller = smaller_unit / SM_W_CONV;
              new_smaller += ((bigger_unit * WEIGHT_CONV) - floor(bigger_unit * WEIGHT_CONV)) * 16;
              if (new_smaller >= 16.0) {
                     new_bigger++;
                     new_smaller -= 16;
              }
              break;
       }
}

void output_results(int bigger_before, double smaller_before, int bigger_after, double smaller_after, ConversionType conversion) 
{
       switch (conversion) {
       case LENGTH_TO_METRIC:
              cout << bigger_before << " feet " << smaller_before << " inches = " << bigger_after << " meters " << smaller_after << " centimeters." << endl;
              break;
       case LENGTH_TO_US:
              cout << bigger_before << " meters " << smaller_before << " centimeters = " << bigger_after << " feet " << smaller_after << " inches." << endl;
              break;
       case WEIGHT_TO_METRIC:
              cout << bigger_before << " pounds " << smaller_before << " ounces = " << bigger_after << " kilograms " << smaller_after << " grams." << endl;
              break;
       case WEIGHT_TO_US:
              cout << bigger_before << " kilograms " << smaller_before << " grams = " << bigger_after << " pounds " << smaller_after << " ounces." << endl;
              break;
       }
}






Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: How to fine tune code

Posted 13 May 2013 - 08:15 PM

In prompt_user for example you have :

131	       switch (conversion) {
132	       case LENGTH_TO_METRIC:
133	              cout << "Please enter the length:\n"
134	                           "Feet(integer): ";
135	              cin >> static_cast<int>(bigger_unit);
136	              cin.sync();
137	              cout << "Inches(decimal): ";
138	              cin >> static_cast<double>(smaller_unit);
139	              cin.sync();
140	              break;




Instead of getting the values in the case statement, you can get them after the switch statement, and use the case statement to specify what the output will be.

   string type_str, bigger_str, smaller_str;

   switch (conversion) {
     case LENGTH_TO_METRIC:
       type_str    = "length";
       bigger_str  = "Feet";
       smaller_Str = "Inches";
       break;




I don't see any need for the casts :

static_cast<>


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1