Từ 1 tới 8 trên tổng số 8 kết quả

Đề tài: Mã nguồn C | Chương trình Calculator viết bằng C

  1. #1
    Ngày gia nhập
    06 2007
    Bài viết
    14

    Mặc định Mã nguồn C | Chương trình Calculator viết bằng C

    Chương trình này chưa đầy đủ chức năng nhưng hy vọng là có ích cho các bạn mới và đang tiếp xúc với C,C++.
    Mong nhận được các góp ý của các tiền bối hihi....

    C Code:
    1. #include<conio.h>
    2. #include<iostream.h>
    3. #include<stdio.h>
    4. #include<string.h>
    5. #include<math.h>
    6. #include<dos.h>
    7. #include<stdlib.h>
    8. // ___define
    9.  
    10. #define NumOperator 11
    11. #define Left 10
    12. #define Top 10
    13. #define Row 8
    14. #define Column 4
    15. #define ColorFrame 10
    16. #define ColorText 12
    17. #define BackGround 9
    18. #define WIDTH 5
    19. #define HEIGHT 1
    20. //// const
    21. char* btnControl[8][4]={
    22. {" Cos "," Tag ","Cotag","Log"},
    23. {" Sin ","  -> "," <-  ","ESC"},
    24. {"  !  ","  (  ","  )  ","DEL"},
    25. {"Mod %"," a/b ","^ x^y","CLR"},
    26. {"  7  ","  8  ","  9  "," . "},
    27. {"  4  ","  5  ","  6  "," / "},
    28. {"  1  ","  2  ","  3  "," * "},
    29. {"  0  ","  -  ","  +  "," = "},
    30. };
    31. char* listOperator[]={ "+", "-", "*", "/","^","%","!", "sin", "cos", "tag", "cotag","log" };
    32. char* Error="";
    33. //// enum
    34. enum Boolean
    35. {
    36. false,true
    37. };
    38.  
    39. /// methods
    40.  
    41. void DrawRect(int left,int top,int width,int height,int color)
    42. {
    43. textcolor(color);
    44. gotoxy(left,top);
    45. putch(218);
    46. gotoxy(left,top+height+1);
    47. putch(192);
    48. gotoxy(left+width+1,top);
    49. putch(191);
    50. gotoxy(left+width+1,top+height+1);
    51. putch(217);
    52. for(int i=1;i<=width;i++)
    53. {
    54. gotoxy(left+i,top);
    55. putch(196);
    56. gotoxy(left+i,top+height+1);
    57. putch(196);
    58. }
    59. for(int j=1;j<=height;j++)
    60. {
    61. gotoxy(left,top+j);
    62. putch(179);
    63. gotoxy(left+width+1,top+j);
    64. putch(179);
    65. }
    66. }
    67. void DrawString(char *str,int x,int y,int forecolor)
    68. {
    69.   textcolor(forecolor);
    70.   gotoxy(x,y);
    71.   cprintf(str);
    72. }
    73. void ShowHelp()
    74. {
    75. int left=Left+28,top=Top-5,color=BLUE;
    76.  DrawRect(left,top,20,20,CYAN);
    77.  DrawString("Press key !",left+5,top+1,YELLOW);
    78.  DrawString("Esc :ESC -> Exit",left+1,top+4,color);
    79.  DrawString("S,s :Sin",left+1,top+6,color);
    80.  DrawString("C,c :Cos",left+1,top+8,color);
    81.  DrawString("T,t :Tag",left+1,top+10,color);
    82.  DrawString("O,o :Cotag",left+1,top+12,color);
    83.  DrawString("L,l :Log",left+1,top+14,color);
    84.  DrawString("D,d :DEL",left+1,top+16,color);
    85.  DrawString("Space :CLR",left+1,top+18,color);
    86.  
    87. }
    88. void DrawFrame()
    89. {
    90. // textbackground(4);
    91.  ShowHelp();
    92.  //textbackground(0);
    93.  DrawRect(Left-1,Top-8,Column*6+2,Row*4-1,RED);
    94.  DrawString("My Calculator !",Left+6,Top-7,14);
    95.  DrawRect(Left,Top-6,Column*6,1,ColorFrame);
    96.  DrawRect(Left,Top-3,Column*6,1,ColorFrame);
    97.  textbackground(BackGround);
    98.  int width=5,height=1;
    99.  for(int i=0;i<Row;i++)
    100.  for(int j=0;j<Column;j++)
    101.   {
    102.    if(j==Column-1)
    103.     DrawRect(Left+j*(width+2),Top+i*(height+2),width-2,height,ColorFrame);
    104.    else
    105.     DrawRect(Left+j*(width+2),Top+i*(height+2),width,height,ColorFrame);
    106.     DrawString(btnControl[i][j],Left+j*(width+2)+1,Top+i*(height+2)+1,ColorText);
    107.  }
    108. gotoxy(Left+1,Top-5);
    109. //DrawString(btnControl[0][0],Left+1,Top+1,YELLOW);
    110. }
    111. Boolean IsOperator(char *str)
    112. {
    113.         for (int i = 0; i < NumOperator; i++)
    114.         {
    115.          if(strcmpi(str,listOperator[i])==0)
    116.         return true;
    117.         }
    118. return false;
    119. }
    120. int LevelOfOperator(char* str)
    121. {
    122.     if(strcmpi(str,"+")==0||strcmpi(str,"-")==0)
    123.         return 1;
    124.     if(strcmpi(str,"*")==0||strcmpi(str,"/")==0||strcmpi(str,"%")==0)
    125.         return 2;
    126.     if(strcmpi(str,"^")==0)
    127.         return 3;
    128.     if(strcmpi(str,"cos")==0||strcmpi(str,"sin")==0||strcmpi(str,"tag")==0||strcmpi(str,"cotag")==0||strcmpi(str,"log")==0)
    129.         return 4;
    130.     if(strcmpi(str,"!")==0)
    131.         return 5;
    132.     return 0;
    133. }
    134. Boolean IsNumber(char* str)
    135. {
    136.  Boolean has=false;
    137.    for(int i=0;i<strlen(str);i++)
    138.    {
    139.     if((str[i]<'0'||str[i]>'9')&&str[i]!='.')
    140.      {
    141.         return false;
    142.      }
    143.     if(str[i]=='.')
    144.     {
    145.     if(has==false)has=true;
    146.     else
    147.     if(has==true)return false;
    148.     }
    149.    }
    150. return true;
    151. }
    152.  
    153. //// Class
    154.  template<class Entry>
    155.   class Queue  // xay dung hang doi
    156.   {
    157.    private:
    158.    struct Node
    159.    {
    160.     Entry data;
    161.     Node *next;
    162.     Node(Entry value)
    163.     {
    164.      data=value;
    165.      next=NULL;
    166.     }
    167.    };
    168.    Node *first,*last;
    169.    int count;
    170.   public:
    171.   Queue()
    172.   {
    173.    first=last=NULL;
    174.    count=0;
    175.   }
    176.   Entry DeQueue()
    177.   {
    178.    if(first==NULL)
    179.     return NULL;
    180.    Node *temp=first;
    181.    first=first->next;
    182.    count--;
    183.   return temp->data;
    184.   }
    185.   Boolean EnQueue(Entry item)
    186.   {
    187.     Node *temp=new Node(item);
    188.     if(temp)
    189.     {if(first==NULL)
    190.     {
    191.      first=last=temp;
    192.     }
    193.     else {
    194.      last->next=temp;
    195.      last=temp;
    196.     }
    197.     count++;
    198.     return true;
    199.     }
    200.    return false;
    201.   }
    202.   int Length()
    203.   {
    204.    return count;
    205.   }
    206. };
    207.  
    208.   class Node
    209.   {
    210.   private:
    211.       char* data;
    212.       Node *left,*right,*parent;
    213.       int level;
    214.   public:
    215.       Boolean Isoperator,Isnumber;
    216.       Node(char* value)
    217.       {
    218.           data=value;
    219.           left=right=parent=NULL;
    220.           Isoperator=IsOperator(value);
    221.           Isnumber=Isoperator==true?false:true;
    222.           if(Isoperator)
    223.               level=LevelOfOperator(data);
    224.       }
    225.       void setLeft(Node *value)
    226.       {
    227.         left=value;
    228.         value->setParent(this);
    229.       }
    230.       void setRight(Node *value)
    231.       {
    232.           right=value;
    233.           value->setParent(this);
    234.       }
    235.       void setParent(Node *value)
    236.       {
    237.        parent=value;
    238.       }
    239.       int Level()
    240.       {
    241.         return level;
    242.       }
    243.       Node* getLeft()
    244.       {
    245.           return left;
    246.       }
    247.       Node* getRight()
    248.       {
    249.           return right;
    250.       }
    251.       Node* getParent()
    252.       {
    253.           return parent;
    254.       }
    255.       char* getData()
    256.       {
    257.           return data;
    258.       }
    259.   };
    260.  
    261.   class TreeExpression
    262.   {
    263.   private:
    264.      char* data;
    265.      Node *root;
    266.      Queue<char*>* getQueue(char* str)
    267.   {  Queue<char*> *lst=new Queue<char*>();
    268.      char *strtemp;
    269.      int count=0;
    270.      strtemp=NULL;
    271.      int threadNum=0,threadOpe=0;
    272.      for(int i=0;i<strlen(str);i++)
    273.      {
    274.        if(str[i]=='('||str[i]==')')
    275.     {
    276.      if(threadOpe)
    277.      strtemp[threadOpe]='\0';
    278.        else if(threadNum)
    279.       strtemp[threadNum]='\0';
    280.  
    281.      if(strlen(strtemp)>0)
    282.       {
    283.       if(IsNumber(strtemp)||IsOperator(strtemp))
    284.       lst->EnQueue(strtemp);
    285.       else {Error=strcat("No support ",strtemp);}
    286.       strtemp=NULL;
    287.       }
    288.       lst->EnQueue(str[i]==')'?")":"(");
    289.       threadOpe=threadNum=0;
    290.     }
    291.        else
    292.        {
    293.     if((str[i]>='0'&&str[i]<='9')||str[i]=='.')
    294.       {
    295.         if(threadNum)
    296.         {
    297.          strtemp[threadNum]=str[i];
    298.          threadNum++;
    299.         }
    300.        else {
    301.          if(threadOpe)
    302.          {
    303.           strtemp[threadOpe]='\0';
    304.     //        cout<<endl<<strtemp;
    305.           if(IsOperator(strtemp)==true)
    306.            lst->EnQueue(strtemp);
    307.           else {
    308.           Error=strcat("No support ",strtemp);
    309.           }
    310.           threadOpe=0;
    311.          }
    312.          strtemp=new char[10];
    313.          strtemp[threadNum]=str[i];
    314.          threadNum++;
    315.        }
    316.       }
    317.        else
    318.        { if(threadOpe)
    319.      {
    320.       strtemp[threadOpe]='\0';
    321.       if(IsOperator(strtemp))
    322.         {
    323.          lst->EnQueue(strtemp);
    324.          threadOpe=0;
    325.          strtemp=new char[10];
    326.         }
    327.       strtemp[threadOpe]=str[i];
    328.       threadOpe++;
    329.      }
    330.      else {
    331.           if(threadNum)
    332.           {
    333.            strtemp[threadNum]='\0';
    334.        //    cout<<endl<<strtemp;
    335.            lst->EnQueue(strtemp);
    336.            threadNum=0;
    337.           }
    338.          strtemp=new char[10];
    339.          strtemp[threadOpe]=str[i];
    340.          threadOpe++;
    341.      }
    342.     }
    343.        }
    344.     }
    345.    if(threadOpe)
    346.     {
    347.     strtemp[threadOpe]='\0';
    348.     lst->EnQueue(strtemp);
    349.     }
    350.    else if(threadNum)
    351.    {
    352.    strtemp[threadNum]='\0';
    353.    lst->EnQueue(strtemp);
    354.    }
    355.    return lst;
    356.   }
    357.      Node* CreateTree(Queue<char*> *lst)
    358.       {
    359.           Node *root=NULL,*current=NULL;
    360.         while(lst->Length()>0)
    361.         {
    362.            char *str=lst->DeQueue();
    363.            if(strcmpi(str,"(")==0)
    364.            {
    365.             Node *getNode = CreateTree(lst);
    366.             Node *dn = new Node("()");
    367.             dn->setRight(getNode);
    368.             if(root==NULL)
    369.             root = dn;
    370.             else current->setRight(dn);
    371.             current = dn;
    372.            }
    373.            else if(strcmpi(str,")")==0)
    374.            {
    375.             return root;
    376.            }
    377.            else {
    378.             Node* temp=new Node(str);
    379.             if(root==NULL)
    380.                 root=temp;
    381.             else if(IsNumber(str)||strcmpi(str,"()")==0)
    382.                  current->setRight(temp);
    383.             //else if(current->Isoperator==true&&current->Level()<temp->Level())
    384.             //   current->setRight(temp);
    385.             else {
    386.             //  cout<<"parent cua "<<current->getData()<<" la :"<<current->getParent()->getData();
    387.                 do{
    388.                     if(current->Isoperator==true)
    389.                     {
    390.                         if(current->Level()<temp->Level())
    391.                         {
    392.                             temp->setLeft(current->getRight());
    393.                             current->setRight(temp);
    394.                             break;
    395.                         }
    396.                     }
    397.                     if(current->getParent()==NULL)
    398.                     {
    399.                         temp->setLeft(current);
    400.                         root=temp;
    401.                         break;
    402.                     }
    403.                     current=current->getParent();
    404.                 }while(current!=NULL);
    405.             }
    406.                current=temp;
    407.             }
    408.         }
    409.         return root;
    410.       }
    411.      long double ExpressionValue(Node *root)
    412.      {
    413.         if(root->getRight()==NULL&&root->getLeft()==NULL)
    414.                {
    415.             if(IsNumber(root->getData())==false)
    416.             {
    417.             Error="Error Syntax !";
    418.             return 0;
    419.             }
    420.             return _atold(root->getData());
    421.                }
    422.          long double a=NULL,b=NULL;
    423.             Boolean anull=false,bnull=false;
    424.             if(root->getLeft()!=NULL)
    425.                 a=ExpressionValue(root->getLeft());
    426.                 else anull=true;
    427.             if(root->getRight()!=NULL)
    428.                 b=ExpressionValue(root->getRight());
    429.                 else bnull=true;
    430.             char *str=root->getData();
    431. //          cout<<"a :"<<a<<str<<" b :"<<b;
    432.             if(strlen(Error))
    433.             return 0;
    434.             if(strcmpi(str,"()")==0)
    435.                 return b;
    436.             if(strcmpi(str,"+")==0)
    437.                {
    438.                 if(bnull)
    439.                 Error="Syntax Error !";
    440.                 return a+b;
    441.                }
    442.             if(strcmpi(str,"-")==0)
    443.                    {
    444.                 if(bnull)
    445.                 Error="Syntax Error !";
    446.                 return a-b;
    447.                    }
    448.             if(strcmpi(str,"*")==0)
    449.                   {
    450.                 if(anull||bnull)
    451.                 Error="Syntax Error !";
    452.                 return a*b;
    453.                   }
    454.             if(strcmpi(str,"/")==0)
    455.             {
    456.                 if(anull||bnull)
    457.                 {Error="Error Syntax !";
    458.                  return 0;
    459.                 }
    460.                 if(b==0)
    461.                 {Error="Khong the chia cho 0";
    462.                  return 0;
    463.                 }
    464.                 return a/b;
    465.             }
    466.             if(strcmpi(str,"%")==0)
    467.                    {
    468.                 if(anull||bnull)
    469.                 {Error="Syntax Error !";
    470.                 return 0;
    471.                 }
    472.                 return (long)a%(long)b;
    473.                    }
    474.             if(strcmpi(str,"^")==0)
    475.                    {
    476.                 if(anull||bnull)
    477.                 {
    478.                 Error="Syntax Error !";
    479.                 return 0;
    480.                 }
    481.                 return pow(a,b);
    482.                    }
    483.             if(strcmpi(str,"cos")==0)
    484.                 {
    485.                 if(bnull)
    486.                 Error="Syntax Error !";
    487.                 return cos(b);
    488.                 }
    489.             if(strcmpi(str,"sin")==0)
    490.                    {
    491.                 if(bnull)
    492.                 Error="Syntax Error !";
    493.                 return sin(b);
    494.                    }
    495.             if(strcmpi(str,"tag")==0)
    496.              {
    497.                 if(bnull)
    498.                    {Error="Syntax Error !";
    499.                    return 0;
    500.                    }
    501.                 if(cos(b)==0)
    502.                  {
    503.                  Error="Khong the chia cho 0 !";
    504.                  return 0;
    505.                  }
    506.                 return tan(b);
    507.              }
    508.             if(strcmpi(str,"cotag")==0)
    509.             {
    510.                 if(bnull)
    511.                    {Error="Syntax Error !";
    512.                 return 0;
    513.                    }
    514.               if(sin(b)==0)
    515.               { Error="Khong the chia cho 0 !";
    516.                return 0;
    517.               }
    518.               return (long double)cos(b)/sin(b);
    519.             }
    520.             if(strcmpi(str,"!")==0)
    521.             {
    522.                 if(anull)
    523.                    {
    524.                 Error="Syntax Error !";
    525.                 return 0;
    526.                    }
    527.              return (long double) CalFactorial((long)a);
    528.             }
    529.             if(strcmpi(str,"log")==0)
    530.             {
    531.                 if(bnull)
    532.                 {
    533.                 Error="Error Syntax !";
    534.                 return 0;
    535.                 }
    536.             return (long double)log(b);
    537.             }
    538.             else Error=strcat("No Support ",str);
    539.             return 0;
    540.          }
    541.      int CalFactorial(int t)
    542.      {
    543.         int result = 1;
    544.         for (int i = 2; i <= t; i++)
    545.         {
    546.         result *= i;
    547.         }
    548.         return result;
    549.      }
    550.   public:
    551.       TreeExpression(char* expression)
    552.       {
    553.        data=expression;
    554.       }
    555.       long double Calculator()
    556.       {
    557.         Queue<char*> *lst=getQueue(data);
    558.         Node *root=CreateTree(lst);
    559.         return ExpressionValue(root);
    560.       }
    561.   };
    562. char* InsertString(char* str,char* sub,int Index)
    563. {
    564.     int l1=strlen(str);
    565.     int l2=strlen(sub);
    566.     char *temp=new char[500];
    567.     for(int i=0;i<Index;i++)
    568.         temp[i]=str[i];
    569.     for(int j=0;j<l2;j++)
    570.         temp[j+Index]=sub[j];
    571.     for(int k=Index;k<l1;k++)
    572.         temp[k+l2]=str[k];
    573.     temp[l1+l2]='\0';
    574.     return temp;
    575. }
    576. void ShowKeyPress(int row,int column)
    577. {
    578.  int x=wherex(),y=wherey();
    579.  textbackground(BackGround);
    580.  DrawString(btnControl[row][column],Left+column*(WIDTH+2)+1,Top+row*(HEIGHT+2)+1,YELLOW);
    581.  gotoxy(x,y);
    582.  delay(80);
    583.  DrawString(btnControl[row][column],Left+column*(WIDTH+2)+1,Top+row*(HEIGHT+2)+1,ColorText);
    584.  textbackground(0);
    585. }
    586. Boolean CompareTo(char *str,char *sub)
    587. {
    588.    int j=0;
    589.  for(int i=0;i<strlen(str);i++)
    590.   {
    591.    if(str[i]!=32)
    592.    {
    593.     if(str[i]!=sub[j])
    594.      return false;
    595.     j++;
    596.    }
    597.   }
    598.  if(j==strlen(sub))
    599.  return true;
    600.  return false;
    601. }
    602. void SearchKey(char* str)
    603. {
    604.  for(int i=0;i<Row;i++)
    605.  for(int j=0;j<Column;j++)
    606.   {
    607.    if(CompareTo(btnControl[i][j],str))
    608.    { ShowKeyPress(i,j);
    609.    break;
    610.    }
    611.   }
    612. }
    613. char* DeleteChar(char *str,int index)
    614. {
    615.     int l=strlen(str);
    616.     char *temp=new char[500];
    617.     for(int i=0;i<index;i++)
    618.     temp[i]=str[i];
    619.     for(int j=index+1;j<=l;j++)
    620.     temp[j-1]=str[j];
    621. return temp;
    622. }
    623. void KeyInput()
    624. {
    625.  int TopInput=Top-5,TopOutput=Top-2,maxlen=23;
    626.  int first=0,last=0,current=0;
    627.  textbackground(0);
    628.  int color=CYAN;
    629.  int row=0,column=0;
    630.  char* str=new char[500];
    631.  char* value;
    632.  str="";
    633.  char* show=new char[maxlen];
    634.  int key;
    635.  do{
    636.  key=getch();
    637.  switch(key)
    638.  {
    639.  case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
    640.  case '+':case '-':case '*':case '/':case '^':case '!':case '(':case ')':case '%':case '.':
    641.      {
    642.          value=new char[1];
    643.          value[0]=key;
    644.          value[1]='\0';
    645.          str=InsertString(str,value,first+current);
    646.          if(current<maxlen)
    647.             {  current++;
    648.                if(strlen(str)<maxlen)
    649.                last++;
    650.             }
    651.          else {
    652.              current=maxlen;
    653.              first++;last++;
    654.          }
    655.          SearchKey(value);
    656.          break;
    657.      }
    658.  case 'c':case 'C':case 's':case 'S':case 't':case 'T':case 'o':case 'O':case 'l':case 'L':
    659.      {
    660.          if(key=='c'||key=='C')
    661.          value="Cos";
    662.          else if(key=='s'||key=='S')
    663.          value="Sin";
    664.          else if(key=='t'||key=='T')
    665.          value="Tag";
    666.          else if(key=='o'||key=='O')
    667.          value="Cotag";
    668.          else if(key=='l'||key=='L')
    669.          value="Log";
    670.          str=InsertString(str,value,first+current);
    671.          int l=strlen(value);
    672.          if(current+l<maxlen)
    673.          {
    674.              current+=l;
    675.              if(last+l<maxlen)
    676.                  last+=l;
    677.          }
    678.          else {
    679.              int t=current+l-maxlen;
    680.              last=maxlen;
    681.              current=maxlen;
    682.              first+=(l-t);last+=(l-t);
    683.                }
    684.                SearchKey(value);
    685.          break;
    686.      }
    687.  case 75:{
    688.      if(current>0)
    689.        current--;
    690.      else if(first>0)
    691.           {
    692.          first--;last--;
    693.           }
    694.      SearchKey("<-");
    695.      break;
    696.      }
    697.  case 77:{
    698.      if(current<maxlen&&current<last)
    699.       current++;
    700.       else
    701.        if(last<strlen(str))
    702.      { first++;last++;}
    703.        SearchKey("->");
    704.      break;
    705.      }
    706.   case 'd':case 'D':{
    707.       if(first+current<strlen(str))
    708.       {
    709.       strcpy(str+first+current,str+first+current+1);
    710.       if(last<maxlen)
    711.       last--;
    712.       }
    713.       SearchKey("DEL");
    714.       break;
    715.       }
    716.   case 13:{
    717.         DrawString("                        ",Left+1,TopOutput,RED);
    718.         if(strlen(str)>0)
    719.         {
    720.         Error="";
    721.         TreeExpression *tree=new TreeExpression(str);
    722.         long double t=tree->Calculator();
    723.         if(strlen(Error)>0)
    724.         DrawString(Error,Left+1,TopOutput,RED);
    725.            else
    726.         { char result[23];
    727.         ltoa(t,result,10);
    728.         DrawString(result,Left+1,TopOutput,RED);
    729.         }
    730.         }
    731.         else DrawString("Expression = null",Left+1,TopOutput,RED);
    732.         break;
    733.       }
    734.  case 32:{
    735.            current=0;
    736.            first=0;
    737.            last=0;
    738.            str[0]='\0';
    739.            SearchKey("CLR");
    740.  break;
    741.  }
    742.  case 27:{SearchKey("ESC");break;}
    743.  default :break;
    744.  }
    745.          int temp;
    746.          for(temp=first;temp<=last&&temp<strlen(str);temp++)
    747.           show[temp-first]=str[temp];
    748.           show[temp]='\0';
    749.          DrawString("                        ",Left+1,TopInput,color);
    750.          DrawString(show,Left+1,TopInput,color);
    751.          gotoxy(Left+1+current,wherey());
    752.  }while(key!=27);
    753. }
    754. void main()
    755. {
    756.  textbackground(0);
    757.  clrscr();
    758. DrawFrame();
    759. KeyInput();
    760. }

  2. #2
    Ngày gia nhập
    06 2007
    Bài viết
    14

    Gửi bạn: giacmobuonlc

    chương trình này sử dụng ngôn ngữ C++ mới OK
    bạn thử cài Borlandc 3.1 xem mình đang dùng nó và chương trình vẫn chạy bình thường.

    Gửi bạn: PoPoPoPo

    cái này đã được mình sửa lại
    C Code:
    1. #include<conio.h>
    2. #include<iostream.h>
    3. #include<stdio.h>
    4. #include<string.h>
    5. #include<math.h>
    6. #include<dos.h>
    7. #include<stdlib.h>
    8. // ___define
    9.  
    10. #define NumOperator 12
    11. #define Left 10
    12. #define Top 10
    13. #define Row 8
    14. #define Column 4
    15. #define ColorFrame 10
    16. #define ColorText 12
    17. #define BackGround 9
    18. #define WIDTH 5
    19. #define HEIGHT 1
    20. //// const
    21. char* btnControl[8][4]={
    22. {" Cos "," Tag ","Cotag","Log"},
    23. {" Sin ","  -> "," <-  ","ESC"},
    24. {"  !  ","  (  ","  )  ","DEL"},
    25. {"Mod %"," a/b ","^ x^y","CLR"},
    26. {"  7  ","  8  ","  9  "," . "},
    27. {"  4  ","  5  ","  6  "," / "},
    28. {"  1  ","  2  ","  3  "," * "},
    29. {"  0  ","  -  ","  +  "," = "},
    30. };
    31. char* listOperator[]={ "+", "-", "*", "/","^","%","!", "sin", "cos", "tag", "cotag","log" };
    32. char* Error="";
    33. //// enum
    34. enum Boolean
    35. {
    36. false,true
    37. };
    38.  
    39. /// methods
    40.  
    41. void DrawRect(int left,int top,int width,int height,int color)
    42. {
    43. textcolor(color);
    44. gotoxy(left,top);
    45. putch(218);
    46. gotoxy(left,top+height+1);
    47. putch(192);
    48. gotoxy(left+width+1,top);
    49. putch(191);
    50. gotoxy(left+width+1,top+height+1);
    51. putch(217);
    52. for(int i=1;i<=width;i++)
    53. {
    54. gotoxy(left+i,top);
    55. putch(196);
    56. gotoxy(left+i,top+height+1);
    57. putch(196);
    58. }
    59. for(int j=1;j<=height;j++)
    60. {
    61. gotoxy(left,top+j);
    62. putch(179);
    63. gotoxy(left+width+1,top+j);
    64. putch(179);
    65. }
    66. }
    67. void DrawString(char *str,int x,int y,int forecolor)
    68. {
    69.   textcolor(forecolor);
    70.   gotoxy(x,y);
    71.   cprintf(str);
    72. }
    73. void DrawFloat(long float value,int x,int y,int forecolor)
    74. {
    75.   textcolor(forecolor);
    76.   gotoxy(x,y);
    77.   cprintf("%lf",value);
    78. }
    79. void ShowHelp()
    80. {
    81. int left=Left+28,top=Top-5,color=BLUE;
    82.  DrawRect(left,top,20,20,CYAN);
    83.  DrawString("Press key !",left+5,top+1,YELLOW);
    84.  DrawString("Esc :ESC -> Exit",left+1,top+4,color);
    85.  DrawString("S,s :Sin",left+1,top+6,color);
    86.  DrawString("C,c :Cos",left+1,top+8,color);
    87.  DrawString("T,t :Tag",left+1,top+10,color);
    88.  DrawString("O,o :Cotag",left+1,top+12,color);
    89.  DrawString("L,l :Log",left+1,top+14,color);
    90.  DrawString("D,d :DEL",left+1,top+16,color);
    91.  DrawString("Space :CLR",left+1,top+18,color);
    92.  
    93. }
    94. void DrawFrame()
    95. {
    96. // textbackground(4);
    97.  ShowHelp();
    98.  //textbackground(0);
    99.  DrawRect(Left-1,Top-8,Column*6+2,Row*4-1,RED);
    100.  DrawString("My Calculator !",Left+6,Top-7,14);
    101.  DrawRect(Left,Top-6,Column*6,1,ColorFrame);
    102.  DrawRect(Left,Top-3,Column*6,1,ColorFrame);
    103.  textbackground(BackGround);
    104.  int width=5,height=1;
    105.  for(int i=0;i<Row;i++)
    106.  for(int j=0;j<Column;j++)
    107.   {
    108.    if(j==Column-1)
    109.     DrawRect(Left+j*(width+2),Top+i*(height+2),width-2,height,ColorFrame);
    110.    else
    111.     DrawRect(Left+j*(width+2),Top+i*(height+2),width,height,ColorFrame);
    112.     DrawString(btnControl[i][j],Left+j*(width+2)+1,Top+i*(height+2)+1,ColorText);
    113.  }
    114. gotoxy(Left+1,Top-5);
    115. //DrawString(btnControl[0][0],Left+1,Top+1,YELLOW);
    116. }
    117. Boolean IsOperator(char *str)
    118. {
    119.         for (int i = 0; i < NumOperator; i++)
    120.         {
    121.          if(strcmpi(str,listOperator[i])==0)
    122.         return true;
    123.         }
    124. return false;
    125. }
    126. int LevelOfOperator(char* str)
    127. {
    128.     if(strcmpi(str,"+")==0||strcmpi(str,"-")==0)
    129.         return 1;
    130.     if(strcmpi(str,"*")==0||strcmpi(str,"/")==0||strcmpi(str,"%")==0)
    131.         return 2;
    132.     if(strcmpi(str,"^")==0)
    133.         return 3;
    134.     if(strcmpi(str,"cos")==0||strcmpi(str,"sin")==0||strcmpi(str,"tag")==0||strcmpi(str,"cotag")==0||strcmpi(str,"log")==0)
    135.         return 4;
    136.     if(strcmpi(str,"!")==0)
    137.         return 5;
    138.     return 0;
    139. }
    140. Boolean IsNumber(char* str)
    141. {
    142.  Boolean has=false;
    143.    for(int i=0;i<strlen(str);i++)
    144.    {
    145.     if((str[i]<'0'||str[i]>'9')&&str[i]!='.')
    146.      {
    147.         return false;
    148.      }
    149.     if(str[i]=='.')
    150.     {
    151.     if(has==false)has=true;
    152.     else
    153.     if(has==true)return false;
    154.     }
    155.    }
    156. return true;
    157. }
    158.  
    159. //// Class
    160.  template<class Entry>
    161.   class Queue  // xay dung hang doi
    162.   {
    163.    private:
    164.    struct Node
    165.    {
    166.     Entry data;
    167.     Node *next;
    168.     Node(Entry value)
    169.     {
    170.      data=value;
    171.      next=NULL;
    172.     }
    173.    };
    174.    Node *first,*last;
    175.    int count;
    176.   public:
    177.   Queue()
    178.   {
    179.    first=last=NULL;
    180.    count=0;
    181.   }
    182.   Entry DeQueue()
    183.   {
    184.    if(first==NULL)
    185.     return NULL;
    186.    Node *temp=first;
    187.    first=first->next;
    188.    count--;
    189.   return temp->data;
    190.   }
    191.   Boolean EnQueue(Entry item)
    192.   {
    193.     Node *temp=new Node(item);
    194.     if(temp)
    195.     {if(first==NULL)
    196.     {
    197.      first=last=temp;
    198.     }
    199.     else {
    200.      last->next=temp;
    201.      last=temp;
    202.     }
    203.     count++;
    204.     return true;
    205.     }
    206.    return false;
    207.   }
    208.   int Length()
    209.   {
    210.    return count;
    211.   }
    212. };
    213.  
    214.   class Node
    215.   {
    216.   private:
    217.       char* data;
    218.       Node *left,*right,*parent;
    219.       int level;
    220.   public:
    221.       Boolean Isoperator,Isnumber;
    222.       Node(char* value)
    223.       {
    224.           data=value;
    225.           left=right=parent=NULL;
    226.           Isoperator=IsOperator(value);
    227.           Isnumber=Isoperator==true?false:true;
    228.           if(Isoperator)
    229.               level=LevelOfOperator(data);
    230.       }
    231.       void setLeft(Node *value)
    232.       {
    233.         left=value;
    234.         value->setParent(this);
    235.       }
    236.       void setRight(Node *value)
    237.       {
    238.           right=value;
    239.           value->setParent(this);
    240.       }
    241.       void setParent(Node *value)
    242.       {
    243.        parent=value;
    244.       }
    245.       int Level()
    246.       {
    247.         return level;
    248.       }
    249.       Node* getLeft()
    250.       {
    251.           return left;
    252.       }
    253.       Node* getRight()
    254.       {
    255.           return right;
    256.       }
    257.       Node* getParent()
    258.       {
    259.           return parent;
    260.       }
    261.       char* getData()
    262.       {
    263.           return data;
    264.       }
    265.   };
    266.  
    267.   class TreeExpression
    268.   {
    269.   private:
    270.      char* data;
    271.      Node *root;
    272.      Queue<char*>* getQueue(char* str)
    273.   {  Queue<char*> *lst=new Queue<char*>();
    274.      char *strtemp;
    275.      int count=0;
    276.      strtemp=NULL;
    277.      int threadNum=0,threadOpe=0;
    278.      for(int i=0;i<strlen(str);i++)
    279.      {
    280.        if(str[i]=='('||str[i]==')')
    281.     {
    282.      if(threadOpe)
    283.      strtemp[threadOpe]='\0';
    284.        else if(threadNum)
    285.       strtemp[threadNum]='\0';
    286.  
    287.      if(strlen(strtemp)>0)
    288.       {
    289.       if(IsNumber(strtemp)||IsOperator(strtemp))
    290.       lst->EnQueue(strtemp);
    291.       else {Error=strcat("No support ",strtemp);}
    292.       strtemp=NULL;
    293.       }
    294.       lst->EnQueue(str[i]==')'?")":"(");
    295.       threadOpe=threadNum=0;
    296.     }
    297.        else
    298.        {
    299.     if((str[i]>='0'&&str[i]<='9')||str[i]=='.')
    300.       {
    301.         if(threadNum)
    302.         {
    303.          strtemp[threadNum]=str[i];
    304.          threadNum++;
    305.         }
    306.        else {
    307.          if(threadOpe)
    308.          {
    309.           strtemp[threadOpe]='\0';
    310.     //        cout<<endl<<strtemp;
    311.           if(IsOperator(strtemp)==true)
    312.            lst->EnQueue(strtemp);
    313.           else {
    314.           Error=strcat("No support ",strtemp);
    315.           }
    316.           threadOpe=0;
    317.          }
    318.          strtemp=new char[10];
    319.          strtemp[threadNum]=str[i];
    320.          threadNum++;
    321.        }
    322.       }
    323.        else
    324.        { if(threadOpe)
    325.      {
    326.       strtemp[threadOpe]='\0';
    327.       if(IsOperator(strtemp))
    328.         {
    329.          lst->EnQueue(strtemp);
    330.          threadOpe=0;
    331.          strtemp=new char[10];
    332.         }
    333.       strtemp[threadOpe]=str[i];
    334.       threadOpe++;
    335.      }
    336.      else {
    337.           if(threadNum)
    338.           {
    339.            strtemp[threadNum]='\0';
    340.        //    cout<<endl<<strtemp;
    341.            lst->EnQueue(strtemp);
    342.            threadNum=0;
    343.           }
    344.          strtemp=new char[10];
    345.          strtemp[threadOpe]=str[i];
    346.          threadOpe++;
    347.      }
    348.     }
    349.        }
    350.     }
    351.    if(threadOpe)
    352.     {
    353.     strtemp[threadOpe]='\0';
    354.     lst->EnQueue(strtemp);
    355.     }
    356.    else if(threadNum)
    357.    {
    358.    strtemp[threadNum]='\0';
    359.    lst->EnQueue(strtemp);
    360.    }
    361.    return lst;
    362.   }
    363.      Node* CreateTree(Queue<char*> *lst)
    364.       {
    365.           Node *root=NULL,*current=NULL;
    366.         while(lst->Length()>0)
    367.         {
    368.            char *str=lst->DeQueue();
    369.            if(strcmpi(str,"(")==0)
    370.            {
    371.             Node *getNode = CreateTree(lst);
    372.             Node *dn = new Node("()");
    373.             dn->setRight(getNode);
    374.             if(root==NULL)
    375.             root = dn;
    376.             else current->setRight(dn);
    377.             current = dn;
    378.            }
    379.            else if(strcmpi(str,")")==0)
    380.            {
    381.             return root;
    382.            }
    383.            else {
    384.             Node* temp=new Node(str);
    385.             if(root==NULL)
    386.                 root=temp;
    387.             else if(IsNumber(str)||strcmpi(str,"()")==0)
    388.                  current->setRight(temp);
    389.             //else if(current->Isoperator==true&&current->Level()<temp->Level())
    390.             //   current->setRight(temp);
    391.             else {
    392.             //  cout<<"parent cua "<<current->getData()<<" la :"<<current->getParent()->getData();
    393.                 do{
    394.                     if(current->Isoperator==true)
    395.                     {
    396.                         if(current->Level()<temp->Level())
    397.                         {
    398.                             temp->setLeft(current->getRight());
    399.                             current->setRight(temp);
    400.                             break;
    401.                         }
    402.                     }
    403.                     if(current->getParent()==NULL)
    404.                     {
    405.                         temp->setLeft(current);
    406.                         root=temp;
    407.                         break;
    408.                     }
    409.                     current=current->getParent();
    410.                 }while(current!=NULL);
    411.             }
    412.                current=temp;
    413.             }
    414.         }
    415.         return root;
    416.       }
    417.        /*   long double ExpressionValue(Node *root)
    418.      {
    419.         if(root->getRight()==NULL&&root->getLeft()==NULL)
    420.                {
    421.             if(IsNumber(root->getData())==false)
    422.             {
    423.             Error="Error Syntax !";
    424.             return 0;
    425.             }
    426.             return _atold(root->getData());
    427.                }
    428.          long double a=NULL,b=NULL;
    429.             if(root->getLeft()!=NULL)
    430.                 a=ExpressionValue(root->getLeft());
    431.  
    432.             if(root->getRight()!=NULL)
    433.                 b=ExpressionValue(root->getRight());
    434.  
    435.             char *str=root->getData();
    436.  
    437.             if(strcmpi(str,"sin")==0)
    438.             {
    439.              cout<<"\nco vao :"<<sin(b);
    440.              return (long double)sin(b);
    441.             }
    442.             return 20;
    443.        }               */
    444.      long double ExpressionValue(Node *root)
    445.      {
    446.         if(root->getRight()==NULL&&root->getLeft()==NULL)
    447.                {
    448.             if(IsNumber(root->getData())==false)
    449.             {
    450.             Error="Error Syntax !";
    451.             return 0;
    452.             }
    453.             return _atold(root->getData());
    454.                }
    455.          long double a=NULL,b=NULL;
    456.             Boolean anull=false,bnull=false;
    457.             if(root->getLeft()!=NULL)
    458.                 a=ExpressionValue(root->getLeft());
    459.                 else anull=true;
    460.             if(root->getRight()!=NULL)
    461.                 b=ExpressionValue(root->getRight());
    462.                 else bnull=true;
    463.             char *str=root->getData();
    464.             if(strlen(Error))
    465.             return 0;
    466.             if(strcmpi(str,"()")==0)
    467.                 return b;
    468.             if(strcmpi(str,"+")==0)
    469.                {
    470.                 if(bnull)
    471.                 Error="Syntax Error !";
    472.                 return a+b;
    473.                }
    474.             if(strcmpi(str,"-")==0)
    475.                    {
    476.                 if(bnull)
    477.                 Error="Syntax Error !";
    478.                 return a-b;
    479.                    }
    480.             if(strcmpi(str,"*")==0)
    481.                   {
    482.                 if(anull||bnull)
    483.                 Error="Syntax Error !";
    484.                 return a*b;
    485.                   }
    486.             if(strcmpi(str,"/")==0)
    487.             {
    488.                 if(anull||bnull)
    489.                 {Error="Error Syntax !";
    490.                  return 0;
    491.                 }
    492.                 if(b==0)
    493.                 {Error="Khong the chia cho 0";
    494.                  return 0;
    495.                 }
    496.                 return a/b;
    497.             }
    498.             if(strcmpi(str,"%")==0)
    499.                    {
    500.                 if(anull||bnull)
    501.                 {Error="Syntax Error !";
    502.                 return 0;
    503.                 }
    504.                 return (long)a%(long)b;
    505.                    }
    506.             if(strcmpi(str,"^")==0)
    507.                    {
    508.                 if(anull||bnull)
    509.                 {
    510.                 Error="Syntax Error !";
    511.                 return 0;
    512.                 }
    513.                 return pow(a,b);
    514.                    }
    515.             if(strcmpi(str,"cos")==0)
    516.                 {
    517.                 if(bnull)
    518.                 Error="Syntax Error !";
    519.                 return cos(b);
    520.                 }
    521.             if(strcmpi(str,"sin")==0)
    522.                    {
    523.                 if(bnull)
    524.                 Error="Syntax Error !";
    525.                 return sin(b);
    526.                    }
    527.             if(strcmpi(str,"tag")==0)
    528.              {
    529.                 if(bnull)
    530.                    {Error="Syntax Error !";
    531.                    return 0;
    532.                    }
    533.                 if(cos(b)==0)
    534.                  {
    535.                  Error="Khong the chia cho 0 !";
    536.                  return 0;
    537.                  }
    538.                 return tan(b);
    539.              }
    540.             if(strcmpi(str,"cotag")==0)
    541.             {
    542.                 if(bnull)
    543.                    {Error="Syntax Error !";
    544.                 return 0;
    545.                    }
    546.               if(sin(b)==0)
    547.               { Error="Khong the chia cho 0 !";
    548.                return 0;
    549.               }
    550.               return (long double)cos(b)/sin(b);
    551.             }
    552.             if(strcmpi(str,"!")==0)
    553.             {
    554.                 if(anull)
    555.                    {
    556.                 Error="Syntax Error !";
    557.                 return 0;
    558.                    }
    559.              return (long double) CalFactorial((long)a);
    560.             }
    561.             if(strcmpi(str,"log")==0)
    562.             {
    563.                 if(bnull)
    564.                 {
    565.                 Error="Error Syntax !";
    566.                 return 0;
    567.                 }
    568.             return (long double)log(b);
    569.             }
    570.             else Error=strcat("No Support ",str);
    571.             return 0;
    572.          }
    573.      int CalFactorial(int t)
    574.      {
    575.         int result = 1;
    576.         for (int i = 2; i <= t; i++)
    577.         {
    578.         result *= i;
    579.         }
    580.         return result;
    581.      }
    582.   public:
    583.       TreeExpression(char* expression)
    584.       {
    585.        data=expression;
    586.       }
    587.       long double Calculator()
    588.       {
    589.         Queue<char*> *lst=getQueue(data);
    590.         Node *root=CreateTree(lst);
    591.         return ExpressionValue(root);
    592.        //while(lst->Length())
    593.        //cout<<"\n"<<lst->DeQueue();
    594.        //cout<<"\n Nguyn thuy :"<<data;
    595.        //return 0;
    596.       }
    597.   };
    598. char* InsertString(char* str,char* sub,int Index)
    599. {
    600.     int l1=strlen(str);
    601.     int l2=strlen(sub);
    602.     char *temp=new char[500];
    603.     for(int i=0;i<Index;i++)
    604.         temp[i]=str[i];
    605.     for(int j=0;j<l2;j++)
    606.         temp[j+Index]=sub[j];
    607.     for(int k=Index;k<l1;k++)
    608.         temp[k+l2]=str[k];
    609.     temp[l1+l2]='\0';
    610.     return temp;
    611. }
    612. void ShowKeyPress(int row,int column)
    613. {
    614.  int x=wherex(),y=wherey();
    615.  textbackground(BackGround);
    616.  DrawString(btnControl[row][column],Left+column*(WIDTH+2)+1,Top+row*(HEIGHT+2)+1,YELLOW);
    617.  gotoxy(x,y);
    618.  delay(80);
    619.  DrawString(btnControl[row][column],Left+column*(WIDTH+2)+1,Top+row*(HEIGHT+2)+1,ColorText);
    620.  textbackground(0);
    621. }
    622. Boolean CompareTo(char *str,char *sub)
    623. {
    624.    int j=0;
    625.  for(int i=0;i<strlen(str);i++)
    626.   {
    627.    if(str[i]!=32)
    628.    {
    629.     if(str[i]!=sub[j])
    630.      return false;
    631.     j++;
    632.    }
    633.   }
    634.  if(j==strlen(sub))
    635.  return true;
    636.  return false;
    637. }
    638. void SearchKey(char* str)
    639. {
    640.  for(int i=0;i<Row;i++)
    641.  for(int j=0;j<Column;j++)
    642.   {
    643.    if(CompareTo(btnControl[i][j],str))
    644.    { ShowKeyPress(i,j);
    645.    break;
    646.    }
    647.   }
    648. }
    649. char* DeleteChar(char *str,int index)
    650. {
    651.     int l=strlen(str);
    652.     char *temp=new char[500];
    653.     for(int i=0;i<index;i++)
    654.     temp[i]=str[i];
    655.     for(int j=index+1;j<=l;j++)
    656.     temp[j-1]=str[j];
    657. return temp;
    658. }
    659. void KeyInput()
    660. {
    661.  int TopInput=Top-5,TopOutput=Top-2,maxlen=23;
    662.  int first=0,last=0,current=0;
    663.  textbackground(0);
    664.  int color=CYAN;
    665.  int row=0,column=0;
    666.  char* str=new char[500];
    667.  char* value;
    668.  str="";
    669.  char* show=new char[maxlen];
    670.  int key;
    671.  do{
    672.  key=getch();
    673.  switch(key)
    674.  {
    675.  case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
    676.  case '+':case '-':case '*':case '/':case '^':case '!':case '(':case ')':case '%':case '.':
    677.      {
    678.          value=new char[1];
    679.          value[0]=key;
    680.          value[1]='\0';
    681.          str=InsertString(str,value,first+current);
    682.          if(current<maxlen)
    683.             {  current++;
    684.                if(strlen(str)<maxlen)
    685.                last++;
    686.             }
    687.          else {
    688.              current=maxlen;
    689.              first++;last++;
    690.          }
    691.          SearchKey(value);
    692.          break;
    693.      }
    694.  case 'c':case 'C':case 's':case 'S':case 't':case 'T':case 'o':case 'O':case 'l':case 'L':
    695.      {
    696.          if(key=='c'||key=='C')
    697.          value="Cos";
    698.          else if(key=='s'||key=='S')
    699.          value="Sin";
    700.          else if(key=='t'||key=='T')
    701.          value="Tag";
    702.          else if(key=='o'||key=='O')
    703.          value="Cotag";
    704.          else if(key=='l'||key=='L')
    705.          value="Log";
    706.          str=InsertString(str,value,first+current);
    707.          int l=strlen(value);
    708.          if(current+l<maxlen)
    709.          {
    710.              current+=l;
    711.              if(last+l<maxlen)
    712.                  last+=l;
    713.          }
    714.          else {
    715.              int t=current+l-maxlen;
    716.              last=maxlen;
    717.              current=maxlen;
    718.              first+=(l-t);last+=(l-t);
    719.                }
    720.                SearchKey(value);
    721.          break;
    722.      }
    723.  case 75:{
    724.      if(current>0)
    725.        current--;
    726.      else if(first>0)
    727.           {
    728.          first--;last--;
    729.           }
    730.      SearchKey("<-");
    731.      break;
    732.      }
    733.  case 77:{
    734.      if(current<maxlen&&current<last)
    735.       current++;
    736.       else
    737.        if(last<strlen(str))
    738.      { first++;last++;}
    739.        SearchKey("->");
    740.      break;
    741.      }
    742.   case 'd':case 'D':{
    743.       if(first+current<strlen(str))
    744.       {
    745.       strcpy(str+first+current,str+first+current+1);
    746.       if(last<maxlen)
    747.       last--;
    748.       }
    749.       SearchKey("DEL");
    750.       break;
    751.       }
    752.   case 13:{
    753.         DrawString("                        ",Left+1,TopOutput,RED);
    754.         if(strlen(str)>0)
    755.         {
    756.         Error="";
    757.         TreeExpression *tree=new TreeExpression(str);
    758.         long double t=tree->Calculator();
    759.         if(strlen(Error)>0)
    760.         DrawString(Error,Left+1,TopOutput,RED);
    761.            else
    762.         {
    763.          //char result[23];
    764.          //ltoa(t,result,10);
    765.         //DrawString(result,Left+1,TopOutput,RED);
    766.           DrawFloat(t,Left+1,TopOutput,RED);
    767.         }
    768.         }
    769.         else DrawString("Expression = null",Left+1,TopOutput,RED);
    770.         break;
    771.       }
    772.  case 32:{
    773.            current=0;
    774.            first=0;
    775.            last=0;
    776.            str[0]='\0';
    777.            SearchKey("CLR");
    778.  break;
    779.  }
    780.  case 27:{SearchKey("ESC");break;}
    781.  default :break;
    782.  }
    783.          int temp;
    784.          for(temp=first;temp<=last&&temp<strlen(str);temp++)
    785.           show[temp-first]=str[temp];
    786.           show[temp]='\0';
    787.          DrawString("                        ",Left+1,TopInput,color);
    788.          DrawString(show,Left+1,TopInput,color);
    789.          gotoxy(Left+1+current,wherey());
    790.  }while(key!=27);
    791. }
    792. void main()
    793. {
    794. textbackground(0);
    795. clrscr();
    796. DrawFrame();
    797. KeyInput();
    798.  
    799. }

    ->Cảm ơn các bạn đã xem và đóng góp ý kiến nhé

  3. #3
    Ngày gia nhập
    10 2006
    Nơi ở
    Hà Nội
    Bài viết
    146

    uhm. Đáng nể.

    Cảm ơn bạn đã chia sẻ những bài tập hay.

  4. #4
    Ngày gia nhập
    07 2007
    Bài viết
    1

    OK test ổn, nhưng đơn vị góc khi tính Sin Cos là gì vậy độ hay radian, nếu là độ thì tính Sin có vấn đề , sin90 khác 1

  5. #5
    Ngày gia nhập
    06 2007
    Bài viết
    14

    đơn vị tính góc là radian vì mình chỉ mới viết song với lại nhiều chức năng còn thiếu nữa.
    lẻ ra phải có chế độ cho người dùng chọn là radian hay degree nữa, với lại giờ mình không thể tiếp tục làm nó nữa nên gửi lên đây để ai đó có thể tiếp tục cho nó hoàn chỉnh hơn.

  6. #6
    Ngày gia nhập
    03 2008
    Bài viết
    1

    Mặc định Mã nguồn C | Chương trình Calculator viết bằng C

    tớ sài tc 3.1 ngon mà
    cho tớ hỏi là đoạn chương trình này lamg gì nhỉ?
    C++ Code:
    1.    struct Node
    2.    {
    3.     Entry data;
    4.     Node *next;
    5.     Node(Entry value)
    6.     {
    7.      data=value;
    8.      next=NULL;
    9.     }
    10.    };
    Đã được chỉnh sửa lần cuối bởi Xcross87 : 07-04-2008 lúc 03:30 PM. Lý do: Lần sau code thì cho vào tag nha !

  7. #7
    Ngày gia nhập
    09 2006
    Nơi ở
    /usr/share/.hack@
    Bài viết
    1,433

    Trích dẫn Nguyên bản được gửi bởi thuongbk Xem bài viết
    tớ sài tc 3.1 ngon mà
    cho tớ hỏi là đoạn chương trình này lamg gì nhỉ?
    C Code:
    1. struct Node
    2.    {
    3.     Entry data;
    4.     Node *next;
    5.     Node(Entry value)
    6.     {
    7.      data=value;
    8.      next=NULL;
    9.     }
    10.    };
    Cấu trúc Liên Kết Đơn (Linear List)
    Học rồi sẽ biết đừng hỏi tại sao khi chưa có nền tảng
    None!

  8. #8
    Ngày gia nhập
    11 2008
    Bài viết
    10

    tại sao tôi dùng c-free 4.0 thì không chạy được nhỉ?????

Các đề tài tương tự

  1. Chương Trình Tử Vi viết bằng C# [Đầy đủ mã nguồn]
    Gửi bởi phanquoctoan trong diễn đàn Dự án & Source code C#, ASP.NET
    Trả lời: 21
    Bài viết cuối: 12-08-2017, 01:54 PM
  2. Algorithm Thuật toán viết scientific calculator như thế nào?
    Gửi bởi phuongham trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 5
    Bài viết cuối: 23-02-2012, 09:43 AM
  3. Calculator (Standard and Scientific) viết bằng C#
    Gửi bởi tiensigiay trong diễn đàn Dự án & Source code C#, ASP.NET
    Trả lời: 3
    Bài viết cuối: 24-03-2011, 01:18 PM
  4. Calculator viết bằng MFC của vc++
    Gửi bởi langtudatinh_2509 trong diễn đàn Dự án & Source code VC++
    Trả lời: 5
    Bài viết cuối: 13-10-2010, 09:31 PM
  5. Mã nguồn C | Lập trình 3D viết bằng C
    Gửi bởi phanvankhai trong diễn đàn Thủ thuật, Tutorials và Mã nguồn C/C++/C++0x
    Trả lời: 5
    Bài viết cuối: 25-03-2010, 09:07 PM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn