当前位置: 代码迷 >> C语言 >> [讨论]重载练习,大家做做
  详细解决方案

[讨论]重载练习,大家做做

热度:240   发布时间:2006-11-30 23:17:17.0
[讨论]重载练习,大家做做

写二个名为mod的重载函数,要求一个用来求两个整数的余数,一个用来求两个实数之间的余数,写出完整的程序,特别要注意容错功能!
OK,不要小看这个题目,写出来再说``````
实数的求模就是将实数先转换成int型(采用四舍五入法):-9.5变成-10,2.2变成2

环境:VC++/VS/C 不限
输入:提示用户输入m和n
输出:输出m%n=?

[此贴子已经被作者于2006-12-1 12:59:46编辑过]

搜索更多相关的解决方案: 重载  练习  

----------------解决方案--------------------------------------------------------
实数的%具体操作结果是什么样子的,举个例子吧.
----------------解决方案--------------------------------------------------------

恩,实数的求模就是将实数先转换成int型(采用四舍五入法):-9.5变成-10,2.2变成2


----------------解决方案--------------------------------------------------------
long mod(double a,double b)
{
long c,d;
c=(long)a;
d=(long)b;
if(c<0)
{
c--;
}
if(d<0)
{
d--;
}
return c%d;
}


----------------解决方案--------------------------------------------------------

#include<math.h>
...

int mod(double m,double n)
{
int a,b;
a=(m-0.5)>=int(m)?ceil(m):floor(m);
if(n==0) exit(0);
b=(n-0.5)>=int(n)?ceil(n):floor(n);
return a%b;
}


----------------解决方案--------------------------------------------------------
以下是引用nuciewth在2006-12-1 11:10:33的发言:
long mod(double a,double b)
{
long c,d;
c=(long)a;
d=(long)b;
if(c<0)
{
c--;
}
if(d<0)
{
d--;
}
return c%d;
}

如果传进来的a= -9.2,b= -3.4,依题意,KEY=-9%-3=0;而上面的KEY=-10%-4= -2!
所以不对!
而且要求是用函数重载,而上面未用到``````


----------------解决方案--------------------------------------------------------
以下是引用zzymoon在2006-12-1 12:37:20的发言:

如果传进来的a= -9.2,b= -3.4,依题意,KEY=-9%-3=0;而上面的KEY=-10%-4= -2!
所以不对!
而且要求是用函数重载,而上面未用到``````

我看错了,是用四舍五入.改一下吧,我只写了一个,整数的应该可以直接写吧.

long mod(double a,double b)
{
long c,d;
if(a<0)
{
c=(long)(a-0.5);
}
else
{
c=(long)(a+0.5);
}
if(b<0)
{
d=(long)(b-0.5);
}
else
{
d=(long)(b+0.5);
}
return c%d;
}
----------------解决方案--------------------------------------------------------

以下是引用unicorn在2006-12-1 11:46:08的发言:

#include<math.h>
...

int mod(double m,double n)
{
int a,b;
a=(m-0.5)>=int(m)?ceil(m):floor(m);
if(n==0) exit(0);
b=(n-0.5)>=int(n)?ceil(n):floor(n);
return a%b;
}

也不对,要求是对实数四舍五入,如果传进来的a= -1.2,
那经过这一句:a=(m-0.5)>=int(m)?ceil(m):floor(m);
即:-1.7>= -1为假,a=floor(-1.2)= -2,而我们的要求是a= -1!


----------------解决方案--------------------------------------------------------

long mod(double a, double b)
{
long m, n;

if (a > 0)
m = (long) (a + 0.5);
else
m = (long) (a - 0.5);
if (b > 0)
n = (long) (b + 0.5);
else
n = (long) (b - 0.5);
if (n == 0)
exit(1);
return m % n;
}


----------------解决方案--------------------------------------------------------

// 实验一:实数求余.cpp : 定义控制台应用程序的入口点。
//利用函数重载求二个数的余数(包括实数)

#include <stdafx.h>
#include <iostream>
using namespace std;
int mod(float m,float n)
{
//cout<<"int mod(float m,float n) is transfer.\n";use to test which function is transtered
int tmp_m,tmp_n;
tmp_m=int(m>0?m+0.5:m-0.5);//if m>0,m add 0.5 else m reduce 0.5 then get its integer
tmp_n=int(n>0?n+0.5:n-0.5);//the same as preceding
return tmp_m%tmp_n;
}
int mod(int m,int n)
{
//cout<<"int mod(int m,int n) is transfer.\n";use to test which function is transtered
return m%n;
}
int _tmain(int argc, _TCHAR* argv[])
{
float m,n=1;//initialize n and y to 1
int x,y=1;
int value,flag;
cout<<"[0]:整数求余\t[1]:实数求余\n";
handle:cout<<"CHOOSE>>";
cin>>flag;
if(flag!=0&&flag!=1)//avoid the wrong number input
{
cout<<"CHOICE IS WRONG!\n";
goto handle;
}
cout<<"Input m:";
if(flag)
cin>>m;
else
cin>>x;
here:cout<<"Input n:";
if(flag)
cin>>n;
else
cin>>y;
if(!n||!y)//if user input 0,this will do
{
cout<<"Input error,n can't be 0!\n";
goto here;
}

if(flag)
value=mod(m,n);
else
value=mod(x,y);
cout<<"m%n="<<value<<endl;
return 0;
}

我写出完整的程序如上,但是其中还有一个bug,谁能找出??


----------------解决方案--------------------------------------------------------
  相关解决方案