C语言简易计算器计算三角函数

这是一个简易计算器代码,但是在算三角函数的时候需要选择运算符号,选择运算内容和输入计算的数值,应该怎么实现直接输入算式呢,如:sin(5)或在continue 后输入 +sin(5)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

define pi 3.1415926

int fuhao(char c);
double jisuan(double a, double b, int p);
double again1(double digit, double n, int p);
double again2(double digit, int c, double result);
int panduan(int d);
int radian_angle(int e);
double sin_cos_tan(int f);
int main()
{
double a, b, digit = 0.0, n;
char c, tmp;
int d, e, h, c1;
printf("若普通四则运算,输入1;若三角函数计算输入2\n");
scanf("%d", &d);
if (panduan(d) == 1)
{
scanf("%lf%c%lf", &a, &c, &b);
digit = jisuan(a, b, fuhao(c));
}
else if (panduan(d) == 2)
{
printf("radian:1 angle:2\n");
scanf("%d", &e);
radian_angle(e);
digit = sin_cos_tan(e);
printf("%lf\n", digit);
}
printf("continue?");
scanf(" %c", &tmp);
for (; tmp == 'Y';)
{
printf("继续四则运算:1 继续三角函数:2\n");
scanf("%d", &h);
if (h == 1)
{
scanf(" %c%lf", &c, &n);
digit = again1(digit, n, fuhao(c));
printf("continue?");
scanf(" %c", &tmp);
}
else if (h == 2)
{
printf("+:1 -:2 :3 /:4 %%:5\n");
scanf(" %d", &c1);
printf("弧度:1 角度:2\n");
scanf("%d", &e);
radian_angle(e);
digit = again2(digit, c1, sin_cos_tan(e));
printf("%lf\n", digit);
printf("continue?");
scanf(" %c", &tmp);
}
else
{
printf("在继续中输入四则运算还是三角错误");
return 0;
}
}
return 0;
}
int panduan(int d)
{
if (d == 1)
return 1;
else if (d == 2)
return 2;
else
{
printf("第一次输入判断错误");
exit(0);
}
}
int fuhao(char c)
{
switch (c)
{
case '+':
return 1;
case'-':
return 2;
case'
':
return 3;
case '/':
return 4;
case'%':
return 5;
default:
printf("输入符号错误\n");
exit(0);
}
}
double jisuan(double a, double b, int p)
{
double d;
int d1;
switch (p)
{
case 1:
d = a + b;
printf("%lf\n", d);
return d;
case 2:
d = a - b;
printf("%lf\n", d);
return d;
case 3:
d = a * b;
printf("%lf\n", d);
return d;
case 4:
d = a / b;
printf("%lf\n", d);
return d;
case 5:
d1 = (int)a % (int)b;
d = (double)d1;
printf("%lf\n", d);
return d;
default:
printf("第一次四则运算错误\n");
exit(0);
}
}
int radian_angle(int e)
{
if (e == 1)
return 1;
else if (e == 2)
return 2;
else
{
printf("输入角度还是弧度错误");
exit(0);
}
}
double sin_cos_tan(int f)
{
int g;
double q;
double radian, result, angle;
if (f == 1)
{
printf("选择计算的内容:sin:1,cos:2,tan:3\n");
scanf("%d", &g);
printf("输入弧度:");
scanf("%lf", &radian);
switch (g)
{
case 1:
result = sin(radian);
return result;
case 2:
result = cos(radian);
return result;
case 3:
result = tan(radian);
return result;
default:
printf("选择sin cos tan弧度制错误");
exit(0);
}
}
else if (f == 2)
{
printf("选择计算的内容:sin:1,cos:2,tan:3\n");
scanf("%d", &g);
printf("输入角度:");
scanf("%lf", &angle);
q = (pi / 180) * angle;
switch (g)
{
case 1:
result = sin(q);
return result;
case 2:
result = cos(q);
return result;
case 3:
if (q == (pi / 2))
{
printf("tan不能计算90°\n");
exit(0);
}
else
{
result = tan(q);
return result;
}
default:
printf("选择sin cos tan角度制错误\n");
exit(0);
}
}
else
{
printf("错误\n");
exit(0);
}
}
double again1(double digit, double n, int p)
{
double d;
int d1;
switch (p)
{
case 1:
d = digit + n;
printf("%lf", d);
return d;
case 2:
d = digit - n;
printf("%lf", d);
return d;
case 3:
d = digit * n;
printf("%lf", d);
return d;
case 4:
d = digit / n;
printf("%lf", d);
return d;
case 5:
d1 = (int)digit % (int)n;
d = (double)d1;
printf("%lf", d);
return d;
default:
printf("输入再一次四则运算错误");
exit(0);
}
}
double again2(double digit, int c, double result)
{
double again_result;
int temp;
switch (c)
{
case 1:
again_result = digit + result;
return again_result;
case 2:
again_result = digit - result;
return again_result;
case 3:
again_result = digit * result;
return again_result;
case 4:
again_result = digit / result;
return again_result;
case 5:
temp = (int)digit % (int)result;
again_result = (double)temp;
return again_result;
default:
printf("输入再一次三角错误");
exit(0);
}
}

这个要用栈分析输入的字符串
参考下这个代码

img

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<stack>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;

stack< double > container;
double ans;

//规定优先级
int priority( char ch )
{
    int p = -3;
    if( int( ch ) >= 48 && int( ch ) <= 57 || ch == 'e' || ch == 'p' ) p = -1;
    else if( ch == '+' || ch == '-' ) p = 1;
    else if( ch == '*' || ch == '/' ) p = 2;
    else if(  ch == '^' ) p = 3;
    else if( ch == 's' || ch == 'c' || ch == 't' || ch == 'l' || ch == 'o' ) p = 5;
    return p;
}

string change( string st0 )
{
    string st;
    for( int i = 0; i < st0.size(); i++ )
    {
        if( priority( st0[ i ]) == -1 || priority( st0[ i ]) >=1 && priority( st0[ i ]) < 5 || st0[ i ] == '(' || st0[ i ] == ')' || st0[ i ] == '.' )
        {
            st.push_back( st0[ i ] );
        }
        else if( st0[ i ] == 's' && st0[ i + 1 ] == 'i' )
        {
            st.push_back( 's' );
        }
        else if( st0[ i ] == 'c' )
        {
            st.push_back( 'c' );
        }
        else if( st0[ i ] == 't' )
        {
            st.push_back( 't' );
        }
        else if( st0[ i ] == 'l' && st0[ i + 1 ] == 'g' )
        {
            st.push_back( 'l' );
        }
        else if( st0[ i ] == 'l' && st0[ i + 1 ] == 'o' )
        {
            st.push_back( 'o' );
        }
        else if( st0[ i ] == 'p' && st0[ i + 1 ] == 'i' )
        {
            st.push_back( 'p' );
        }
        else if( st0[ i ] == 'e' )
        {
            st.push_back( 'e' );
        }
        else if( st0[ i ] == 'a' && st0[ i + 1 ] == 'b' )
        {
            st.push_back( 'a' );
        }
        else;
    }
    return st;
}




//测试中缀式转化为后缀式
string infix_to_uffix( string s )
{
    string str0;
    stack< char > mystack;//建立一个符号栈

    int mark = 0;
    if( s[ 0 ] == '-' )
    {
        string s0 = "0";
        s = s0 + s;
    }
    int size = s.size();
    for( int i = 0; i < size; i++ )
    {
        if( s[ i ] == '(' && s[ i + 1 ] == '-' )
        {
            str0.push_back( ' ' );
            str0.push_back( '-' );
            mark ++;
        }
        else if( s[ i ] == '-' && s[ i - 1 ] == '(' );
        else
        {
            if( s[ i ] != ')' )
            {
                if( s[ i ] == '(' )
                {
                    str0.push_back( ' ' );
                    mystack.push( '(' );
                }
                else if( priority( s[ i ] ) == -1 || s[ i ] == '.'  )//数字和小数点
                {
                    str0.push_back( s[ i ] );
                }
                else//符号
                {
                    str0.push_back( ' ' );
                    if( mystack.empty() )
                        mystack.push( s[ i ] );
                    else
                    {
                        if( priority( s[ i ] ) > priority( mystack.top() ) )
                        {
                            str0.push_back( ' ' );
                            mystack.push( s[ i ] );
                        }
                        else if( priority( s[ i ] ) == priority( mystack.top() ) )
                        {
                            str0.push_back( mystack.top() );
                            mystack.pop();
                            str0.push_back( ' ' );
                            mystack.push( s[ i ] );
                        }
                        else//优先级小于栈顶元素
                        {
                            while( mystack.size() != 0 )
                            {
                                if( mystack.top() == '(' )   break;
                                str0.push_back( ' ' );
                                str0.push_back( mystack.top() );
                                mystack.pop();
                            }
                            str0.push_back( ' ' );
                            mystack.push( s[ i ] );
                        }
                    }
                }
            }

            else//当s[i]==')'
            {
                if( mark != 0 )//判断括号前为负数
                {
                    mark--;
                }
                else
                {
                    while( mystack.top() != '(' )
                    {
                        str0.push_back( ' ' );
                        str0.push_back( mystack.top() );
                        mystack.pop();
                    }
                    mystack.pop();
                }
            }
        }
    }
    while( mystack.size() != 0 )
    {
        str0.push_back( ' ' );
        str0.push_back( mystack.top() );
        mystack.pop();
    }
    return str0;
}

//转化string到int
double exchange( string a )
{
    istringstream ss( a );
    double num;
    ss >> num;
    return num;
}

void aadd()
{
    ans = container.top();
    container.pop();
    ans += container.top();
    container.pop();
    container.push( ans );
}

void mminus()
{
    ans = container.top();
    container.pop();
    ans = container.top() - ans;
    container.pop();
    container.push( ans );
}

void mmul()
{
    ans = container.top();
    container.pop();
    ans *= container.top();
    container.pop();
    container.push( ans );
}

void ddiv()
{
    ans = container.top();
    container.pop();
    ans = container.top() / ans;
    container.pop();
    container.push( ans );
}

void ppow()
{
    ans = container.top();
    container.pop();
    ans = pow( container.top(), ans );
    container.pop();
    container.push( ans );
}

void ssin()
{
    ans = sin( container.top() );
    container.pop();
    container.push( ans );
}

void ccos()
{
    ans = cos( container.top() );
    container.pop();
    container.push( ans );
}

void ttan()
{
    ans = tan( container.top() );
    container.pop();
    container.push( ans );
}

void llog()
{
    ans = log( container.top() );
    container.pop();
    container.push( ans );
}

void llg()
{
    ans = log10( container.top() );
    container.pop();
    container.push( ans );
}

void aabs()
{
    ans = abs( container.top() );
    container.pop();
    container.push( ans );
}

int main()
{
    stack< double > s;
    string str;
    cout<<endl;
    cout<<"———————— This is a culculator ————————"<<endl;

    while( cout<<">> " && cin >> str )
    {

        //cout<<change( str )<<endl;;
        //cout << infix_to_uffix( change( str ) ) << endl;

        stringstream ss( infix_to_uffix( change( str ) ) );
        string sp;
        while( ss >> sp )
        {
            if( int( sp[ 0 ] ) > 47 && int( sp[ 0 ] ) < 58 )
            {
            container.push( exchange( sp ) );
            }
            else if( sp[ 0 ] == '-' && sp.size() != 1 )
            {
                if( int( sp[ 1 ] ) > 47 && int( sp[ 1 ] ) < 58 )
                    container.push( exchange( sp ) );
                else if( sp[ 1 ] == 'p' )
                {
                    double pi = 3.14159265358979323846264;
                    container.push( -pi );
                }
                else if( sp[ 1 ] == 'e' )
                {
                    container.push( -2.71828182846 );
                }
            }
            else if( sp[ 0 ] == 'p' )
            {
                double pi = 3.14159265358979323846264;
                container.push( pi );
            }
            else if( sp[ 0 ] == 'e' )
                container.push( 2.71828182846 );
            else if( sp[ 0 ] == '+' )
                aadd();
            else if( sp[ 0 ] == '-' )
                mminus();
            else if( sp[ 0 ] == '*' )
                mmul();
            else if( sp[ 0 ] == '/' )
                ddiv();
            else if( sp[ 0 ] == '^' )
                ppow();
            else if( sp[ 0 ] == 's' )
                ssin();
            else if( sp[ 0 ] == 'c' )
                ccos();
            else if( sp[ 0 ] == 't' )
                ttan();
            else if( sp[ 0 ] == 'l' )
                llg();
            else if( sp[ 0 ] == 'o' )
                llog();
            else if( sp[ 0 ] == 'a' )
                aabs();
            else;
        }
        if( container.top() > 0 && container.top() < pow( 10.0,-8 ) || container.top() < 0 && container.top() > 0-pow( 10.0,-8 ) )
            cout << "result:"  << 0 << endl;
        else
            printf("result:%.8g\n",container.top());
        cout<<endl;

    }
}



如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632