C++实现分数的四则运算

代码如下,需要的自取,给个赞就好。

#include
#include
#include
using namespace std;
struct Fraction{
     
    int up, down;
};
int gongYinshu(int data1, int data2){
      //找到最大公约数
    int k=2;
    int gcd=1;
    while (k<=data1 && k<=data2)
    {
     
        if(data1%k==0 && data2%k==0){
     
            gcd = k;
        }
        k++;
    }
    return gcd;
}

Fraction reduction(Fraction data){
       //分数的简化
    if(data.down<0){
            //如果分母小于零,则将其转化在分子上
        data.up = -data.up;
        data.down = - data.down;
    }
    if(data.up == 0){
          //因为分母不能为零,所以将其转化在分子上
        data.down = 1;
    }else{
                     //找出分子和分母的最大公约数,分子分母同时除以这个数,就可以简化分数
        int YinShu = gongYinshu(abs(data.up), abs(data.down));
        data.up = data.up / YinShu;
        data.down = data.down / YinShu;
    }
    return data;
}


void showIt(Fraction data){
          //展示分数
    Fraction result = reduction(data);
    cout<<result.up<<"/"<<result.down<<endl;
}

void showAllData(Fraction *dataList,int N){
      //展示所有分数
    cout<<"已有分数如下:"<<endl;
    for(int i=0;i<N;i++){
     
        cout<<"\t"<<i+1<<". ";
        showIt(dataList[i]);
    }    
}

Fraction addOfFraduction(Fraction data1, Fraction data2){
       //分数的加法
    Fraction use, result;
    use.up = data1.up * data2.down + data2.up * data1.down;  //计算分子
    use.down = data1.down * data2.down;                      //计算分母
    result = reduction(use);
    return result; 
}

Fraction minu(Fraction f1, Fraction f2){
                      //分数的减法
    Fraction result;
    result.up = f1.up*f2.down - f2.up * f1.down;        
    result.down = f1.down * f2.down; 
    return reduction(result);     //返回简化以后的分数
}


Fraction multi(Fraction data1, Fraction data2){
       //分数的乘法
    Fraction result;
    result.up = reduction(data1).up * reduction(data2).up;
    result.down = reduction(data1).down * reduction(data2).down;
    return result;
}

Fraction divide(Fraction data1, Fraction data2){
        //分数的除法
    Fraction result;
    result.up = reduction(data1).up * reduction(data2).down;
    result.down = reduction(data1).down * reduction(data2).up;
    return result;
}


void inputData(Fraction *dataList, int N){
     
    for(int i=0;i<N;i++){
     
        cout<<endl<<"请输入第"<<i+1<<"个分数的分子:";
        cin>>dataList[i].up;
        cout<<"请输入第"<<i<<"个分数的分母:";
        cin>>dataList[i].down;
    }
    showAllData(dataList,N);
}


int main(){
     
    int b=1,choose;
    int N;
    cout<<"你想计算几个分数:";
    cin>>N;
    Fraction *dataList = new Fraction[N];
    Fraction show;
    int p1,p2;
    while(b){
     
        system("cls");
        cout<<"\t\t——————Cpp实现分数的四则运算——————"<<endl
            <<"\t\t|1.输入想要计算的分数\t|"<<endl
            <<"\t\t|2.分数的加法\t|"<<endl
            <<"\t\t|3.分数的减法\t|"<<endl
            <<"\t\t|4.分数的乘法\t|"<<endl
            <<"\t\t|5.分数的除法\t|"<<endl
            <<"\t\t|0.退出程序\t\t|"<<endl;
        cout<<"\t请选择以上功能:";
        cin>>choose;
        switch (choose)
        {
     
        case 1: inputData(dataList, N);                                 break;
        case 2: showAllData(dataList, N);                       
                cout<<endl<<"请选择数据序号相加:";                 
                cin>>p1;
                cin>>p2;                                     
                show = addOfFraduction(dataList[p1-1], dataList[p2-1]);
                cout<<"相加后得到:";
                showIt(show);                                           break;
        case 3: showAllData(dataList, N);
                cout<<endl<<"请选择数据序号做差:";                 
                cin>>p1;
                cin>>p2;
                show = minu(dataList[p1-1], dataList[p2-1]);             
                cout<<"做差后得到:";
                showIt(show);                                           break;
        case 4: showAllData(dataList, N);
                cout<<endl<<"请选择数据序号做乘法:";                 
                cin>>p1;
                cin>>p2;
                show = multi(dataList[p1-1], dataList[p2-1]);           
                cout<<"两分数相乘得到:";
                showIt(show);                                           break;
        case 5: showAllData(dataList, N);
                cout<<endl<<"请选择数据序号做除法:";                 
                cin>>p1;
                cin>>p2;
                show = divide(dataList[p1-1], dataList[p2-1]);          
                cout<<"两分数相除得:";
                showIt(show);                                           break;

        case 0: b=0;                                                    break;

        default:
            break;
        }
        if(choose != 0)
        {
     
            cout<<endl<<"点击任意键继续!"<<endl;
            getchar();
            getchar();
        }
    }
    system("cls");
    free(dataList);
}   

你可能感兴趣的:(c++,c++,小程序)