求一个简单的c++ 程序!!

来源:百度知道 编辑:UC知道 时间:2024/06/25 12:23:12
输入两个数m和k ,及另一正整数k,计算m/n,结果精确到小数点后k位
怎么写啊 高手求助啊

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double m,n;
double i;
int k;
cout<<"please enter three number : ";
cin>>m>>n>>k;
cout<<endl;
if(n==0)
{
cout<<"0 不能做除数!!!";
}
else
{
i=m/n;
cout<< setprecision(2)<<fixed<<i<<endl;
}
return 0;

}

如果你真想简单的做的话,
就将K和1~6用switch/case语句对应起来

这个很容易
#include <iostream>
#include <cstdlib>
#include <cstdio>
void test(int m, int n, int k)
{
if (n == 0)
{
cerr<<"Error! divided by zero"<<endl;
return;
}
double d = m/n;
char * fmt = new char(200);
sprintf(fmt, "%12.%df", k); // 格式化指示串,12位数字保留K位小数。
printf(fmt, d);
}