| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 821 人关注过本帖
标题:关于程序说明的一些问题
只看楼主 加入收藏
damnc
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
关于程序说明的一些问题
#include
#include
#include
#include
#include
#include
#define e 2.718281828459
#define pi 3.1415926535898
typedef struct gtype{
    char str[100]; char str1[100];
    double num[100]; int sym[100];
    int topc,topn,tops;
    void clear(){
        topc = topn = tops = -1;
        for ( int i = 0 ; i <= 99 ; i ++ ){
            str[i] = str1[i] = 0;
            num[i] = sym[i] = 0 ;
        }
    }
}expression;
char cach[10], st[5] = {"e"}, st1[5] = {"pi"}, st2[5] = {"x"};
char sg[100][10] = { "", "sin", "cos", "tan", "ln", "asin", "acos", "atan", "sinh", "cosh",
                    "tanh", "ceil", "floor", "fabs" , "c" , "a" , "sqrt" , "sqrtII" , "sqrtIII" , "lg" , "log" , "sum", "mul" };
double a ; bool t;
double  s ,  m ;
int l ;
int level[240];

double calc( double x , char str1[]);
double jiech( double d){
    double n;
    if ( d == 0 )
        return 1 ;
    n = d;
    while( n > 1 ){
        n = n - 1;
        d = d * n;
    }
    return d;
}

double aa( double x , double y ){
    return jiech(x) / jiech( x - y ) ;
}

double cc( double x , double y ){
    return jiech(x) / jiech( y ) / jiech( x - y ) ;
}

void hsji( expression *now ){
    if( now->sym[now->tops] == 1){
        now->num[now->topn] = sin( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 2 ){
        now->num[now->topn] = cos( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 3 ){
        now->num[now->topn] = tan( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 4){
        now->num[now->topn] = log( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 5 ){
        now->num[now->topn] = asin( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 6 ){
        now->num[now->topn] = acos( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 7 ){
        now->num[now->topn] = atan( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 8 ){
        now->num[now->topn] = sinh( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 9 ){
        now->num[now->topn] = cosh( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 10 ){
        now->num[now->topn] = tanh( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 11 ){
        now->num[now->topn] = ceil( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 12 ){
        now->num[now->topn] = floor( now->num[now->topn] );
    }
    else if( now->sym[now->tops] == 13 ){
        now->num[now->topn] = fabs( now->num[now->topn] );
    }
    else if ( now->sym[now->tops] == 14 ){
        now->topn -- ;
        now->num[now->topn] = cc( now->num[now->topn] , now->num[now->topn + 1 ] ) ;
    }
    else if ( now->sym[now->tops] == 15 ){
        now->topn -- ;
        now->num[now->topn] = aa( now->num[now->topn] , now->num[now->topn + 1 ]) ;
    }
    else if ( now->sym[now->tops] == 16 ){
        now->topn -- ;
        now->num[now->topn] = pow( now->num[now->topn] , 1 / now->num[now->topn+1] ) ;
    }
    else if ( now->sym[now->tops] == 17 ){
        now->num[now->topn] = sqrt( now->num[now->topn] ) ;
    }
    else if ( now->sym[now->tops] == 18 ){
        now->num[now->topn] = pow( now->num[now->topn] , 1.0 / 3 ) ;
    }
    else if ( now->sym[now->tops] == 19 ){
        now->num[now->topn] = log10(now->num[now->topn]);
    }
    else if ( now->sym[now->tops] == 20 ){
        now->topn -- ;
        now->num[now->topn] = log(now->num[now->topn+1])/log(now->num[now->topn]);
    }
    now->tops--;
}

double chstr( char p[] ){
    double sl = 0, d = 1;
    int sg = 0;
    for( int i = 0 ; i < strlen( p ) ; i++ ){
        if( p[i] <= '9' && p[i] >= '0' ){
            if( sg == 0 )
                sl = sl * 10 + p[i] - 48;
            else{
                d = d * 10 ;
                sl = sl + ( p[i] - 48 ) / d;
            }
        }
        else if( p[i] = '.' ){
            sg = 1;
        }
    }
    return sl;
}
void didnow( double x , expression *now ){
    if( t == true )
        now ->num[++now->topn] = a;
    else if( strcmp( cach, st ) == 0 )
        now->num[++now->topn] = e;
    else if( strcmp(cach, st1 ) == 0 )
        now->num[++now->topn] = pi;
    else if( strcmp( cach, st2 ) == 0 )
        now->num[++now->topn] = x;
    a = 0;
    t = false;
    memset( cach , 0 , sizeof(cach) ) ;
    l = 0 ; s = 1 ; m = 0 ;
}

void popstr( expression *now ){
    if ( now->str[now->topc] == '+' ){
        now->topn--;
        now->num[now->topn] += now->num[now->topn+1];
    }
    else if ( now->str[now->topc] == '-' ){
        now->topn--;
        now->num[now->topn] = now->num[now->topn] - now->num[now->topn + 1];
    }
    else if( now->str[now->topc] == '*' ){
        now->topn--;
        now->num[now->topn] = now->num[now->topn] * now->num[now->topn + 1];
    }
    else if( now->str[now->topc] == '/' ){
        now->topn--;
        now->num[now->topn] = now->num[now->topn] / now->num[now->topn + 1];
    }
    else if( now->str[now->topc] == '^' ){
        now->topn--;
        now->num[now->topn] = pow( now->num[now->topn], now->num[now->topn + 1] );
    }
    now->topc -- ;
}
double total( char str1[], int x){
    char ss[1000] ;
    int top = -1 , i ;
    double ans;
    memset(ss,0,sizeof(ss));
    for ( i = 0 ; str1[i] != ',' ; i ++ )
        ss[++top] = str1[i] ;
    double a = calc( 0 , ss ) ;
    i ++ ;
    top = -1 ;
    memset(ss,0,sizeof(ss));
    for ( ; str1[i] != ',' ; i ++ )
        ss[++top] = str1[i] ;
    double b = calc( 0 , ss );
    if ( a > b ){
        double temp = a ; a = b ; b = temp ;
    }
    top = -1 ;
    memset( ss , 0 ,sizeof( ss ) );
    i ++ ;
    int have = 1 ;
    while ( have ){
        if ( str1[i] == '(' )
            have ++ ;
        if ( str1[i] == ')' )
            have -- ;
        ss[++top] = str1[i] ;
        i ++ ;
    }
    if( x == 1){
        ans = 0;
        for ( double j = a ; j <= b ; j ++ )
            ans += calc( j ,  ss ) ;
    }
    else if( x == 0 ){
        ans = 1;
        for( double j = a ; j <= b ; j++ )
            ans = ans * calc( j, ss );
    }
    return ans ;
}



void fenli( double x , expression * now ){
        int j = 0, k = 0, b ;
        double s = 1, m = 0;a = 0 ; l = 0 ;
        for( int i = 0  ; i < strlen( now->str1 ) ; i++ ){
                if( now->str1[i] <= '9' && now->str1[i] >= '0' ){
                    s = s * 10;
                    if( m == 1 ){
                        a = a + ( double( now->str1[i] - 48 ) ) / s;
                        t = true;
                     }
                    else{
                        a = a * 10 + now->str1[i] - 48;
                        t = true;
                    }

                }
                else if( now->str1[i] == '!' ){
                    didnow( x, now );
                    now->num[now->topn] = jiech( now->num[now->topn] );
                }
                else if ( now->str1[i] == '+' || now->str1[i] == '-' || now->str1[i] == '*' || now->str1[i] == '/') {
                    didnow( x, now );
                    while ( now->topc != -1 && level[now->str1[i]] >= level[now->str[now->topc]] && now->str[now->topc] != '(' )
                        popstr(now);
                    now->str[++now->topc] = now->str1[i];
                }
               
                else if( now->str1[i] == '(' ){
                    now->str[++now->topc] = now->str1[i];
                    for( l = 0; l < 100; l++ ){
                        b = strcmp( cach, sg[l] );
                        if( b == 0 ){
                            now->sym[++now->tops] = l;
                            if ( l == 21 ){
                                int have = 1 , l = 0, v = 1 ;
                                char temp[1000];
                                memset(temp,0,sizeof(temp));
                                now->topc -- ;
                                i ++ ;
                                while ( have ){
                                    if ( now->str1[i] == '(' )
                                        have ++ ;
                                    if ( now->str1[i] == ')' )
                                        have --;
                                    temp[l++] = now->str1[i] ;
                                    i ++ ;
                                }
                                i -- ;
                                a = total( temp, v );
                                t = true ;
                                didnow(x,now);
                            }
                            else if( l == 22 ){
                                int have = 1 , l = 0, v = 0 ;
                                char temp[1000];
                                memset(temp,0,sizeof(temp));
                                now->topc -- ;
                                i ++ ;
                                while ( have ){
                                    if ( now->str1[i] == '(' )
                                        have ++ ;
                                    if ( now->str1[i] == ')' )
                                        have --;
                                    temp[l++] = now->str1[i] ;
                                    i ++ ;
                                }
                                i -- ;
                                a = total( temp, v );
                                t = true ;
                                didnow(x,now);
                            }
                            break;
                        }
                    }
                    a = 0 ; t = false ;
                    memset(cach,0,sizeof(cach));
                    l = 0;
                }
                else if( now->str1[i] == ')' ){
                    didnow( x, now );
                    while( now->str[now->topc] != '(')
                        popstr( now );
                    hsji(now);
                    now->topc--;
                }
                else if( (now->str1[i] <= 'z' && now->str1[i] >= 'a') || ( now->str1[i] >= 'A' && now->str1[i] <= 'Z' && now->str1[i] != 'R') ){
                    cach[l] = now->str1[i];
                    l++;
                }
                else if( now->str1[i] == '^' ){
                    didnow( x, now );
                    now->str[++now->topc] = now->str1[i];
                }
                else if ( now->str1[i] == 'R' ) {
                    didnow( x, now );
                }
                else if ( now->str1[i] == ',' ){
                    didnow( x, now );
                    while( now->topc != -1 && level[now->str1[i]] > level[now->str[now->topc]] && now->str[now->topc] != '(')
                        popstr( now );
                    now->str[++now->topc] = now->str1[i];
                }
                else if( now->str1[i] == '.' ){
                    s = 1;
                    m = 1;
                }
        }
}
double solve( double x , expression now ){
        while( now.topc > -1 )
            popstr( &now ) ;
        return now.num[0];
}
double calc( double x , char str1[] ){
    expression now ;
    now.clear();
     str1[strlen(str1)] = 'R';
     for ( int i = 0 ; i < strlen(str1) ; i ++ )
        now.str1[i] = str1[i] ;
    int i ; double ans;
    level['+'] = level['-'] = 20;
    level['('] = 0;level[')'] = 100;
    level['*'] = level['/'] = 10;
    level['^'] = 5;level[','] = 50;
    fenli( x , &now );
    ans = solve( x , now );
    return ans ;
}

int main(){
    double x;
    char p[100];
    char str1[100];
    while( true ){
        system("cls");
        printf("   |---------------------------------------------------------|\n");
        printf("   |                                                         |\n");
        printf("   |                          计算器                         |\n");
        printf("   |                                                         |\n");
        printf("   |---------------------------------------------------------|\n");
        printf("   |                                                         |\n");
        printf("   |                   1 -- calculate                        |\n");
        printf("   |                   2 -- function calculate               |\n");
        printf("   |                   E -- exit                        |\n");
        printf("   |                                                         |\n");
        printf("   |---------------------------------------------------------|\n");
        while ( !kbhit()) ;
        char ch = getch();
        if ( ch == 'q')
            break ;
        if ( ch == '1' ){
            while ( true){
                system("cls");
                memset(str1,0,sizeof(str1));
                printf("pleace input a Expression\n");
                scanf( "%s" , str1) ;
                if ( str1[0] == 'E' )
                    break;
                printf("answer = %lf\n" , calc(0,str1) ) ;
                system("pause");
            }
        }
        else if ( ch == '2') {
            while ( true){
                system("cls");
                memset(str1,0,sizeof(str1));
                printf("pleace input a function\n");
                scanf( "%s" , str1) ;
                if ( str1[0] == 'E' )
                    break;
                while( scanf( "%s", p ) != EOF ){
                    if( p[0] == 'E' )
                        break;
                    if( strcmp( p, st ) == 0 )
                        x = e;
                    else if( strcmp( p, st1 ) == 0 )
                        x = pi;
                    else
                        x = chstr( p );
                    printf("answer = %lf\n" , calc( x , str1 ) ) ;
                }
            }
        }
        else if( ch == 'E' )
            break;
    }
}
搜索更多相关主题的帖子: double void expression include 
2012-07-06 20:22
damnc
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-6
收藏
得分:0 
求写说明!!
2012-07-06 20:22
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:20 
好长

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2012-07-12 14:27
快速回复:关于程序说明的一些问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021597 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved