无法获得C ++代码中的十进制输出。

时间:2018-08-26 08:13:20

标签: c++

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


int sum (int x);
int factorial (int y);
int greatest (int p, int q, int r);
int percentage (int m1, int m2, int m3, int m4, int m5);
int formula (int r, int h); 
int voter_age (int x);
int interest (int p, int r, int t);
void swap (int x, int y);
int tables (int i, int j, int k);
int distance (double x, double y, double z);
int speed (double x, double y, double z);
int power (double x, double y, double z);
int sqroot (double x, double y, double z);
int converter (double x, double y, double z);
int inr (double x, double y, double z);
int usd (double x, double y, double z);
int mtrs(double x, double y, double z);


int main () {
int a, b;
double x, y, p, q, m1, m2, m3, m4, m5, r, h, t, i, j, k, z, ans;
cout << "MAIN MENU";
cout << "\n 1. Sum of Natural nos.";
cout << "\n 2. Factorial";
cout << "\n 3. Greatest number among 3";
cout << "\n 4. Percentage(5 subjects)";
cout << "\n 5. Volume of cylinder";
cout << "\n 6. Vote age checker";
cout << "\n 7. interest_calculator";
cout << "\n 8. Swapping nos.";
cout << "\n 9. Table of a number.";
cout << "\n 10. Distance calculator";
cout << "\n 11. Speed calculator";
cout << "\n 12. Calculate the power of a number";
cout << "\n 13. Square root calculator";
cout << "\n 14. Converter";
cout <<
     "\n \n Select one of the above option typing the serial number of the 
same";
cin >> a;


switch (a) {
    case 1:
        cout << "Enter any number";
        cin >> x;
        ans = sum(x);
        cout << ans;
        break;

    case 2:
        cout << "Enter any number";
        cin >> y;
        ans = factorial(y);
        cout << ans;
        break;

    case 3:
        cout << "Enter 3 numbers";
        cin >> p >> q >> r;
        ans = greatest(p, q, r);
        cout << ans << " is the biggest number";
        break;

    case 4:
        cout << "Enter marks of 5 subjects ";
        cin >> m1 >> m2 >> m3 >> m4 >> m5;
        ans = percentage(m1, m2, m3, m4, m5);
        cout << ans << "%";
        break;

    case 5:
        cout << "enter value of radius" << "\n";
        cin >> r;
        cout << "Enter value of height" << "\n";
        cin >> h;
        ans = formula(r, h);
        cout << ans;
        break;

    case 6:
        cout << "Enter your age" << "\n";
        cin >> x;
        ans = voter_age(x);
        break;

    case 7:
        cout << "Enter principle amount " << "\n";
        cin >> p;
        cout << "Enter rate " << "\n";
        cin >> r;
        cout << "Enter time " << "\n";
        cin >> t;
        ans = interest(p, r, t);
        cout << ans;
        break;

    case 8:
        cout << "Enter 1st number.";
        cin >> x;
        cout << "Enter 2nd number";
        cin >> y;
        break;

    case 9:
        cout << "Enter a number to display its table";
        cin >> i;
        ans = tables(i, j, k);
        cout << ans;
        break;

    case 10:
        cout << "Enter speed in km/hr" << endl;
        cin >> x;
        cout << "Enter time in hours" << endl;
        cin >> y;
        ans = distance(x, y, z);
        cout << ans << "km";
        break;

    case 11:
        cout << "Enter distance in km" << endl;
        cin >> x;
        cout << "Enter time in hours" << endl;
        cin >> y;
        ans = speed(x, y, z);
        cout << ans << "km/hr.";
        break;


    case 12:
        cout << "Enter a number" << endl;
        cin >> x;
        cout << "Enter the power" << endl;
        cin >> y;
        ans = power(x, y, z);
        cout << ans;
        break;

    case 13:
        cout << "Enter a number" << endl;
        cin >> x;
        ans = sqroot(x, y, z);
        cout << ans;
        break;

    case 14:
        cout << "Select one of the following" << endl;
        cout << "\n a. Currency";
        cout << "\n b. Distance";
        cout << "\n c. mass";
        cout << "\n d. temperature" << endl;
        cin >> b;
        switch (b) {
            case 1:
                cout << "Select one of the following:" << endl;
                cout << "\t 1. For INR to USD type " << endl;
                cout << "\t 2. For USD to INR type " << endl;
                cin >> b;
                switch (b) {
                    case 1:
                        cout << "Enter amount in INR" << endl;
                        cin >> y;
                        ans = inr(x, y, z);
                        cout << ans << "$";
                        break;


                    case 2:
                        cout << "Enter amount in USD" << endl;
                        cin >> y;
                        ans = usd(x, y, z);
                        cout << ans << "Rs.";
                        break;
                }
                break;

            case 2:
                cout << "Slect one of the following" << endl;
                cout << "\t Mtrs to kms and cms" << endl;
                cout << "\t Kms to Mtrs and cms" << endl;
                cout << "\t Cms to Mtrs and Kms" << endl;
                break;

            case 3:
                cout << "Select one of the following" << endl;
                cout << "\t Kgs to grams and pounds" << endl;
                cout << "\t Grams to Kgs and Pounds" << endl;
                cout << "\t Pounds to kgs and grams" << endl;
                break;

            case 4:
                cout << "Select one of the following" << endl;
                cout << "\t Celcius to Farenhite and Kelvin" << endl;
                cout << "\t Farenhite to Celcius and Kelvin" << endl;
                cout << "\t Kelvin to Celcius and Farenhite" << endl;
                break;
        }
        break;

    default:
        cout << "please enter correct option";
}



}


int sum (int x)
{
int i, sum = 0;
for (i = 1; i <= x; i++)
    sum = sum + i;
return (sum);

}

int factorial (int y)
{
int i, fact = 1;
for (i = 1; i <= y; i++)
    fact = fact * i;
return (fact);
}

int greatest (int p, int q, int r)
{
int s;
if ((p > q) && (p > r))
    s = p;
else if ((q > p) && (q > r))
    s = q;
else if ((r > p) && (r > q))
    s = r;
return (s);
}

int percentage (int m1, int m2, int m3, int m4, int m5)
{
int s, q;
s = m1 + m2 + m3 + m4 + m5;
q = s / 5;
return (q);
}


int formula (int r, int h)
{

return (r * r * h * 3.14);
} 

int voter_age (int x)
{
if (x >= 18)
    cout << "eligible to vote";
else if (x < 18)
    cout << "Not eligible to vote, wait for " << 18 - x << " years";
return (x);
}

int interest (int p, int r, int t)
{
return (p * r * t) / 100;
}

void swap (int x, int y)
{
x = x + y;
y = x - y;
x = x - y;
cout << "Value of x is " << x << "Value of y is " << y;
}

int tables (int i, int j, int k)
{
for (j = 1; j <= 10; j++)
{
    k = i * j;
    cout << i << "*" << j << "=" << k << "\n";
}
return (k);

}

int distance (double x, double y, double z)
{
z = x * y;
return (z);
}

int speed (double x, double y, double z)
{
z = x / y;
return (z);
}

int power (double x, double y, double z)
{
z = pow (x, y);
return (z);
}

int sqroot (double x, double y, double z)
{
z = sqrt (x);
return (z);
}


int inr (double x, double y, double z)
{
z = y * 69.70;
return (z);
}

int usd (double x, double y, double z)
{
z = (1 / 69.70) * y;
return (z);
}


int mtrs(double x, double y, double z)
{
z = (1/1000)*y;
return(z);
}

这段代码是我的学校项目,要求我们使用switch创建函数。除了转换器的输出(在第一个开关的情况下)或必须提供十进制输出的任何其他程序的输出之外,其他所有东西都工作正常。 从菜单中选择转换器时,所有的操作都被编程为以小数点形式输出,但是四舍五入。

2 个答案:

答案 0 :(得分:2)

请注意,整数类型(charunsigned int(u)int<n>_tsize_t)只能容纳整数值。因此,如果为它们分配某种浮点类型的值,则始终会丢失小数。

让我们以distance为例:

int distance (double x, double y, double z)
{
   z = x * y;   // distance calculated as double!
   return (z);  // double is cast to int -> you lose the decimals
}

如果要保留小数,请返回浮点类型:

double distance (double x, double y, double z);
// ^^

但是,还有其他一些问题:

首先,不要在返回值上使用括号!!!它们具有特殊含义(创建 reference ),并且可能会给您带来意想不到的结果:

auto distance (double x, double y, double z)
// ^ (!)
{
   return (z);
}

在这里,推导返回类型,它将获得对局部变量z的引用,因此您最终将获得未定义的行为!

那为什么要完全将z作为参数传递呢?您永远不会使用它,因此请改为使用局部变量:

double distance (double x, double y)
{
   double z = x * y;
   return z;
}

或更短,根本不使用中间变量并直接返回(在简短计算中首选此样式):

   return x * y;

有时,您希望具有其他输出参数,然后可以将它们作为参数传递-但是要能够在函数外部接收任何值,则需要将它们作为引用或指针传递。如果必须始终提供值 ,则首选引用,仅当nullptr也被视为有效输入时才使用指针。

int distance (double x, double y, double& z)
//                                      ^ (!)
{
   z = x * y;
   return z;
}

// use:
double distance;
int rounded = distance(10.12, 12.10, distance);

在此示例中,您有两个结果值,在double变量中计算的距离(带小数)和在int变量中切除的带小数的距离。请注意,将double转换为int时可能会溢出!

以上是一个非常糟糕的示例,由于输出是多余的,如果其中一个输出具有不同/独立的含义,您将执行以下操作:

int distance (double x, double y, double& z)
{
   // check input variables x and y
   if(...)
   {
       return INVALID_PARAMETERS; // assuming you have an enum or a #define for
   }
   // calculations and other checks, different return values for different errors

   z = x * y;
   return SUCCESS;
}

这有点C编程风格,在C ++中,考虑是否可能抛出一些异常更为合适。具有输出参数的另一种方法是返回结构或类–考虑在结构“点”中返回2D或3D坐标或在结构中具有实部和虚部的复杂结果–好吧,猜想–“复杂”(请注意,不过已经有std::complex

答案 1 :(得分:0)

除了将返回类型从int更改为double ...

尝试与fixedsetprecision一起运行: fixed不会删除多余的0,而setprecision会在小数点后的一些小数位数后减少。

例如:

#include <iomanip> //add this include

int main()
{
    cout<< fixed;
    double x = sum(3);
    cout<<setprecision(5)<<x;

    return 0;
}
  

/ 未指定 / setprecision(int n);

     

设置小数精度

     

设置十进制精度,用于格式化输出上的浮点值   操作。

     

表现为成员精度以n作为参数   作为操纵器插入/提取的流(可以是   在输入流或输出流上插入/提取)。

     

此操纵器在标头<iomanip>中声明。