C++ 编程 计算机
1、 整数、实数的加、减、乘、除2、分数的计算。将分数定义为两个整数之比,如:1/2,3/4,24/48,64/2等;分数也可以是负数,如-1/2,15/-24;分数运算结果要是最简化的,如:4/-8,应表示成等价的-1/2。
3、复数的计算
1、 整数、实数的加、减、乘、除2、分数的计算。将分数定义为两个整数之比,如:1/2,3/4,24/48,64/2等;分数也可以是负数,如-1/2,15/-24;分数运算结果要是最简化的,如:4/-8,应表示成等价的-1/2。
3、复数的计算
答案:#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;class Complex//复数
{
public:
Complex(double ,double );//有参数构造
Complex():real(0),image(0){}//无参数构造
~Complex(){}//析构void print();//显示
Complex operator + (Complex );//重载 + 号
Complex operator - (Complex );//重载 - 号
Complex operator * (Complex );//重载 * 号
Complex operator / (Complex );//重载 / 号private:
double real;
double image;
};struct Fraction//分数
{
int denominator;//分母
int molecular;//分子
};double add_integer(double a,double b,char c)//实数计算
{
switch(c)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
default:return 0;break;
}
}int change(int a,int b)//求最大公约数,用以简化分数
{
int n;
int r=1;
if(a>b) n=b;
else n=a;
for(int i=1;i<=n;i++)
{
if((a%i==0)&&(b%i==0)) r=i;
}
return r;
}Fraction get_fraction(string str)//从一个字符串中提取分子和分母
{
Fraction F;
string str_temp="";
int n=str.find('/');//找到 / 号的位置
int i=0;
for(;i<n;i++)
{
str_temp+=str[i];
}
F.molecular=atoi(str_temp.c_str());//获取分子
str_temp="";
for(i=n+1;i<str.length();i++)
{
str_temp+=str[i];
}
F.denominator=atoi(str_temp.c_str());//获取分母
return F;
}Fraction add_fraction(Fraction a,Fraction b,char c)//分数运算
{
Fraction F;
switch(c)
{
case '+':
{
F.molecular=a.molecular+b.molecular;
break;
}
case '-':
{
F.molecular=a.molecular-b.molecular;
break;
}
case '*':
{
F.denominator=a.denominator*b.denominator;
F.molecular=a.molecular*b.molecular;
break;
}
case '/':
{
F.denominator=a.denominator*b.molecular;
F.molecular=a.molecular*b.denominator;
break;
}
}F.denominator=F.denominator/change(F.denominator,F.molecular);//简化分母
F.molecular=F.molecular/change(F.denominator,F.molecular);//简化分子return F;
}Complex::Complex(double real,double image)//构造
{
this->real=real;
this->image=image;
}void Complex::print()//显示
{
if(image==0) cout<<real;
else cout<<"("<<real<<","<<image<<"i)";
}Complex Complex::operator + (Complex a)//重载 + 号
{
Complex c;
c.real=a.real+this->real;
c.image=a.image+this->image;
return c;
}Complex Complex::operator - (Complex a)//重载 - 号
{
Complex c;
c.real=this->real-a.real;
c.image=this->image-a.image;
return c;
}Complex Complex::operator * (Complex i)//重载 * 号
{
Complex r;
double a,b,c,d;a=this->real;
b=this->image;
c=i.real;
d=i.image;r.real=a*c-b*d;
r.image=b*c+a*d;return r;
}Complex Complex::operator / (Complex i)//重载 / 号
{
Complex r;
double a,b,c,d;a=this->real;
b=this->image;
c=i.real;
d=i.image;r.real=(a*c+b*d)/(c*c+d*d);
r.image=(b*c-a*d)/(c*c+d*d);return r;
}Complex add_complex(Complex c1,Complex c2,char c)
{
switch(c)
{
case '+':return c1+c2;break;
case '-':return c1-c2;break;
case '*':return c1*c2;break;
case '/':return c1/c2;break;
default:return c1;break;
}
}int main()
{
string str_choose;
while(true)
{
cout<<endl;
cout<<endl;
cout<<"******************"<<endl;
cout<<"计算实数,请输入1"<<endl;
cout<<"计算分数,请输入2"<<endl;
cout<<"计算复数,请输入3"<<endl;
cout<<"退出,请输入4"<<endl;
cout<<"******************"<<endl;
cout<<endl;
cout<<endl;cin>>str_choose;
if(str_choose=="1")
{
double a,b;
char c;cout<<"请输入第一个数:";
cin>>a;
cout<<"请输入第二个数:";
cin>>b;
cout<<"请输入要执行的运算(+、-、*、/):";
cin>>c;cin.clear();
cin.sync();cout<<a<<c<<b<<"="<<add_integer(a,b,c)<<endl;
}
else if(str_choose=="2")
{
Fraction F1,F2,F;
string str_fraction1,str_fraction2;
char c;cout<<"请输入第一个分数:";
cin>>str_fraction1;
cout<<"请输入第二个分数:";
cin>>str_fraction2;
cout<<"请输入要执行的运算(+、-、*、/):";
cin>>c;cin.clear();
cin.sync();F1=get_fraction(str_fraction1);
F2=get_fraction(str_fraction2);F=add_fraction(F1,F2,c);
cout<<str_fraction1<<" "<<c<<" "<<str_fraction2<<" = "<<F.molecular<<"/"<<F.denominator<<endl;
}
else if(str_choose=="3")
{
double a,b,c,d;
char ch;cout<<"请输入第一个复数的实部:"<<endl;
cin>>a;
cout<<"请输入第一个复数的虚部:"<<endl;
cin>>b;
cout<<"请输入第二个复数的实部:"<<endl;
cin>>c;
cout<<"请输入第二个复数的虚部:"<<endl;
cin>>d;
cout<<"请输入要执行的运算(+、-、*、/):";
cin>>ch;cin.clear();
cin.sync();Complex c1(a,b);
Complex c2(c,d);Complex c3;
c3=add_complex(c1,c2,ch);c1.print();
cout<<" "<<ch<<" ";
c2.print();
cout<<" = ";
c3.print();
cout<<endl;
}
else if(str_choose=="4")
{
cout<<"谢谢使用,再见!!"<<endl;
break;
}
else cout<<"您的输入有误!!"<<endl;
}
return 0;
}