栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1958 Friends

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1958 Friends

#include <stdio.h>#include <string.h>char stack_op[256];unsigned int stack_num[256];int top_op;int top_num;int GetPrecedence(char op, int bInStack);char* GetNumber(char *str, unsigned int *pNum);unsigned int Compute(char* line);int main(int argc, char* argv[]);int GetPrecedence(char op, int bInStack){    switch(op)    {    case 0: return 1;    case '(': return bInStack? 1 : 100;    case ')': return 1;    case '+': return 2;    case '-': return 2;    case '*': return 3;    }    return 0;}char* GetNumber(char *str, unsigned int *pNum){    char *pCh = str + 1;    *pNum = 0;    while(*pCh != '}')    {        if(*pCh >= 'A' && *pCh <= 'Z') *pNum = *pNum | (1 << (*pCh - 'A'));        ++pCh;    }    return pCh;}unsigned int Compute(char* line){    char *pCh;    unsigned int n1, n2, result;    int prec1, prec2;    top_op = 0;    stack_op[top_op] = '(';    top_num = -1;    pCh = line;    while(1)    {        if(*pCh == '{')        { pCh = GetNumber(pCh, &n1); ++top_num; stack_num[top_num] = n1;        }        else        { prec2 = GetPrecedence(*pCh, 0); prec1 = GetPrecedence(stack_op[top_op], 1); if(prec2 > prec1) {     ++top_op;     stack_op[top_op] = *pCh; } else {     while(prec2 <= prec1 && strchr("*+-", stack_op[top_op]) != NULL)     {         n1 = stack_num[top_num - 1];         n2 = stack_num[top_num];         switch(stack_op[top_op])         {         case '+': result = (n1 | n2); break;         case '-': result = (n1 & (~n2)); break;         case '*': result = (n1 & n2); break;         }         --top_num;         stack_num[top_num] = result;         --top_op;         prec1 = GetPrecedence(stack_op[top_op], 1);     }     if(*pCh == ')')     {         while(stack_op[top_op] != '(')         {  --top_op;         }         --top_op;     }     else if(*pCh == 0)         break;     else     {         ++top_op;         stack_op[top_op] = *pCh;     } }        }        ++pCh;    }     if(top_num == 0)        result = stack_num[0];    else        result = 0;    return result;}int main(int argc, char* argv[]){    unsigned int result, x;    char line[256];    while(gets(line) != NULL)    {        result = Compute(line);        printf("{");        for(x = 0; x < 26; x++)        { if(result & (1 << x))     printf("%c", x + 'A');        }        printf("}n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号