温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++怎么实现分数计算器

发布时间:2022-04-14 17:11:51 来源:亿速云 阅读:209 作者:zzz 栏目:编程语言

这篇文章主要介绍“C++怎么实现分数计算器”,在日常操作中,相信很多人在C++怎么实现分数计算器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么实现分数计算器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、问题描述及功能要求

1、分数计算器程序的每种功能都可以用菜单选项列出,用户可以根据需要选择相应的菜单项,从而执行不同的子程序以完成相应的功能
2、增加运算符重载功能,使所设计的分数计算器可以进行四则运算&幂运算&逻辑运算。四则运算&幂运算可以用菜单选项列出,用户可以根据需要选择相应的运算。
3、程序具有判断功能,当有非法的输入时(如分母等于零等),能给出提示信息并退出运算
4、可将分数化为十进制小数和带分数
5、设计逻辑功能的函数使其可以进行逻辑比较,比如大于、小于的逻辑比较。
6、设计数字选择功能程序,用户可以输入数字键选择进行四则运算&幂运算或逻辑比较,运算结果为TRUE或FLASE。
7、要求界面提示清楚、便于操作

二、代码实现 带有注释

废话不说,直接代码,欢迎指正。

大家CV可能有不兼容的情况,可以跟我滴滴,尽可能解决问题地回复。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
using namespace std;
void jiafa1()
{
    system("cls");
    while(1)
    {
        long long int a,c,d,f;
        char b,e;
        cin>>a>>b>>c>>d>>e>>f;
        a=a*f+d*c;
        c=c*f;
        int flag=1;
        if((a<0&&c<0)||(a>0&&c>0)) flag=0;
        a=abs(a);c=abs(c);
        if(a%c==0)
        {
            if(flag==0){cout<<a/c<<endl;}
            if(flag==1){cout<<"-"<<a/c<<endl;}

        }
        else if(a>c)
        {
            int x,y;
            x=a;y=c;
            int z=c;
            while(a%c!=0)
            {
                z=a%c;
                a=c;
                c=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
       else if(c>a)
        {
            int x,y;
            x=a;y=c;
            int z=a;
            while(c%a!=0)
            {
                z=c%a;
                c=a;
                a=z;
            }
            c=y/z;a=x/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }
}
void jiafa2()
{
    system("cls");
    while(1)
    {
        long long int a,c,d;
        char b;
        cin>>a>>b>>c>>d;
        a=a+d*c;
        cout<<a<<"/"<<c<<endl;
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }

}
void jiafa()
{
    while(1)
    {
        system("cls");
        cout<<"(1) 两个分数"<<endl;
        cout<<"(2) 分数和整数"<<endl;
        cout<<"(3) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1){jiafa1();}
        if(a==2){jiafa2();}
        if(a==3){break;}
    }
}
void jianfa1()
{
    system("cls");
    while(1)
    {
        int a,c,d,f;
        char b,e;
        cin>>a>>b>>c>>d>>e>>f;
        a=a*f-d*c;
        c=c*f;
        int flag=1;
        if((a<0&&c<0)||(a>0&&c>0)) flag=0;
        a=abs(a);c=abs(c);
        if(a%c==0)
        {
            if(flag==0){cout<<a/c<<endl;}
            if(flag==1){cout<<"-"<<a/c<<endl;}
        }
        else if(a>c)
        {
            int x,y;
            x=a;y=c;
            int z=c;
            while(a%c!=0)
            {
                z=a%c;
                a=c;
                c=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        else if(c>a)
        {
            int z=a;
            int x,y;
            x=a;y=c;
            while(c%a!=0)
            {
                z=c%a;
                c=a;
                a=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }
}
void jianfa2()
{
    system("cls");
    while(1)
    {
        long long int a,c,d;
        char b;
        cin>>a>>b>>c>>d;
        a=a-d*c;
        cout<<a<<"/"<<c<<endl;
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }
}
void jianfa()
{
   while(1)
    {
        system("cls");
        cout<<"(1) 两个分数"<<endl;
        cout<<"(2) 分数和整数"<<endl;
        cout<<"(3) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1){jianfa1();}
        if(a==2){jianfa2();}
        if(a==3){break;}
    }
}
void chengfa1()
{
    system("cls");
    while(1)
    {
        long long int a,c,d,f;
        char b,e;
        cin>>a>>b>>c>>d>>e>>f;
        a=a*d;c=c*f;
        int flag=1;
        if((a<0&&c<0)||(a>0&&c>0)) flag=0;
        a=abs(a);c=abs(c);
        if(a%c==0)
        {
            if(flag==0){cout<<a/c<<endl;}
            if(flag==1){cout<<"-"<<a/c<<endl;}
        }
        else if(a>c)
        {
            int z=c;
            int x,y;x=a,y=c;
            while(a%c!=0)
            {
                z=a%c;
                a=c;
                c=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        else if(c>a)
        {
            int z=a;
            int x,y;x=a;y=c;
            while(c%a!=0)
            {
                z=c%a;
                c=a;
                a=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }
}
void chengfa2()
{
    system("cls");
    while(1)
    {
        long long int a,c,d;
        char b;
        cin>>a>>b>>c>>d;
        int flag=1;
        if((a<0&&d<0)||(a>0&&d>0)) flag=0;
        a=abs(a);d=abs(d);
        if(d%c==0) {a=a*(d/c);
        if(flag==0) {cout<<a<<endl;}
        if(flag==1) {cout<<"-"<<a<<endl;}
        }
        else if(c%d==0)
        {
            if(flag==0) {cout<<a<<"/"<<c/d<<endl;}
            if(flag==1) {cout<<"-"<<a<<"/"<<c/d<<endl;}
        }
        else
        {
            if(flag==0){cout<<a*d<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a*d<<"/"<<c<<endl;}
        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }

}
void chengfa()
{
    while(1)
    {
        system("cls");
        cout<<"(1) 两个分数"<<endl;
        cout<<"(2) 分数和整数"<<endl;
        cout<<"(3) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1){chengfa1();}
        if(a==2){chengfa2();}
        if(a==3){break;}
    }
}
void chufa1()
{
    system("cls");
    while(1)
    {
        int a,c,d,f;
        char b,e;
        cin>>a>>b>>c>>d>>e>>f;
        int flag=1;
        a=a*f;c=c*d;if((a<0&&c<0)||(a>0&&c>0)) flag=0;
        a=abs(a);c=abs(c);
        if(a%c==0)
        {
            if(flag==0) cout<<a/c<<endl;
            if(flag==1) cout<<"-"<<a/c<<endl;
        }
        else if(a>c)
        {
            int z=c;
            int x,y;x=a;y=c;
            while(a%c!=0)
            {
                z=a%c;
                a=c;
                c=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        else if(c>a)
        {
            int z=a;
            int x,y;x=a;y=c;
            while(c%a!=0)
            {
                z=c%a;
                c=a;
                a=z;
            }
            a=x/z;c=y/z;
            if(flag==0){cout<<a<<"/"<<c<<endl;}
            if(flag==1){cout<<"-"<<a<<"/"<<c<<endl;}
        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }

}
void chufa2()
{
    system("cls");
    while(1)
    {
        long long int a,c,d;
        char b;
        cin>>a>>b>>c>>d;
        int flag=1;
        if((a<0&&d<0)||(a>0&&d>0)) flag=0;
        a=abs(a);d=abs(d);
        if(d%c==0) {a=a*(d/c);if(flag==0){cout<<a<<endl;}if(flag==1){cout<<"-"<<a<<endl;}}
        else if(c%d==0)
        {
            if(flag==0) {cout<<a<<"/"<<c/d<<endl;}
            if(flag==1) {cout<<"-"<<a<<"/"<<c/d<<endl;}
        }
        else
        {
            if(flag==0) {cout<<a*d<<"/"<<c<<endl;}
            if(flag==1) {cout<<"-"<<a*d<<"/"<<c<<endl;}

        }
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }

}
void chufa()
{
    while(1)
    {
        system("cls");
        cout<<"(1) 两个分数"<<endl;
        cout<<"(2) 分数和整数"<<endl;
        cout<<"(3) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1){chufa1();}
        if(a==2){chufa2();}
        if(a==3){break;}
    }
}
void chengfang1()
{
    system("cls");
    while(1)
    {
        long long int a,c,d;
        char b;
        cin>>a>>b>>c>>d;
        int flag=1;
        if(a<0&&d%2==0) flag=0;
        if(a>0) flag=0;
        a=abs(a);
        long int sum1=1;
        long int sum2=1;
        while(d--)
        {
            sum1*=a;
            sum2*=c;
        }
        if(flag==0){cout<<sum1<<"/"<<sum2<<endl;}
        if(flag==1){cout<<"-"<<sum1<<"/"<<sum2<<endl;}
        cout<<"1继续使用,0返回上一级"<<endl;
        int g;cin>>g;
        if(g==1) continue;
        if(g==0) break;
    }


}
void chengfang()
{
    while(1)
    {
        system("cls");
        cout<<"(1) 分数和整数"<<endl;
        cout<<"(2) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1){chengfang1();}
        if(a==2){break;}
    }
}
void sizeyunsuan()
{
    while(1)
   {
        system("cls");
        cout<<"(1) 加法运算"<<endl;
        cout<<"(2) 减法运算"<<endl;
        cout<<"(3) 乘法运算"<<endl;
        cout<<"(4) 除法运算"<<endl;
        cout<<"(5) 乘方运算"<<endl;
        cout<<"(6) 返回上一级"<<endl;
        int a;cin>>a;
        if(a==1) {jiafa();}
        if(a==2){jianfa();}
        if(a==3){chengfa();}
        if(a==4){chufa();}
        if(a==5){chengfang();}
        if(a==6) break;
   }

}
void jiemian()
{
    cout<<"(1) 四则运算及幂运算"<<endl;
    cout<<"(2) 退出程序"<<endl;
    int xxx;cin>>xxx;
    if(xxx==1){sizeyunsuan();}
    if(xxx==2){system("cls");cout<<"蟹蟹你的使用,欢迎下次使用哦"<<endl;exit(0);}
}
int main()
{
    while(1)
    {
        system("cls");
        jiemian();
    }

    return 0;
}

到此,关于“C++怎么实现分数计算器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI