一个简单的C++程序

计算输入的两个整数的乘积,计算乘积要单独用一个用户自定义有返回值的函数来实现。(函数这一章简直没弄好啊!!)

#include<iostream>
using namespace std;
int mult(int m,int n);//定义计算乘积函数
int main()
{
int a,b,p;
cout<<"please input the number a and b:";
cin>>a>>b;
p=mult(a,b); //将a,b的值传给m,n
cout<<"a*b="<<p<<endl;
return 0;
}
int mult(int m,int n) //实现两数的乘积
{
int q;
q=m*n;
return q;
}
写的比较细了,希望对你有所帮助...
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-22
求x1* x2 / x3:int MulDiv(int x1, int x2, int x3);

#include<stdio.h>
int MulDiv(int x1, int x2, int y)
{
unsigned op1 = x1 > 0 ? x1 : -x1; // multiplier
unsigned op2 = x2 > 0 ? x2 : -x2; // multiplicand
unsigned x = 0; // product
for (int i = 0; i < sizeof(int) * 8 - 1; i++) {
if (op1 & 0x01) x += op2;
op1 >>= 1, op2 <<= 1;
}

unsigned d = (y > 0 ? y : -y) << 16; // divisor
unsigned r = x; // remainder
unsigned q = 0; // quotient
for (int i = 0; i < sizeof(int) * 4 + 1; i++) {
r -= d;
if ((int)r >= 0) q <<= 1, q |= +1;
else r += d, q <<= 1, q &= -2;
d >>= 1;
}

const int sign = (((x1 > 0) + (x2 > 0) + (y > 0)) & 0x01) ? 1 : -1;
return sign * (int)q;
}

int main()
{
int x1, x2, y;
while (scanf("%d%d%d", &x1, &x2, &y) == 3 && y )
printf("result: %d\n", MulDiv(x1, x2, y));
return 0;
}
第2个回答  2009-05-22
#include <iostream.h>
int func(int x, int y);
int main(int argc, char *argv[])
{
int a,b,c;
cout << "请输入第一个整数: ";
cin >> a;
cout << "请输入第二个整数: ";
cin >>b;
c = func(a,b);
cout << "乘法结果是:" << c <<endl;
return 0;
}

int func(int x, int y)
{
return x*y;
}
第3个回答  2009-05-22
#include<iostream>
using namespace std;
int fun(int m,int n)
{
return m*n;
}
void main()
{
cout<<"input m and n:"<<endl;
int m,n;
cin>>m>>n;
cout<<"sum="<<fun(m,n)<<endl;
}
第4个回答  2009-05-22
#include<iostream>
void main(){
int x ,y;
cout<<"输入2个整数"<<endl;
plus(x,y);//自定义函数
}
int plus(a,b){//自定义函数
z=a*b;
return z;//返回值
}
希望对你有用

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网