code.club

 找回密碼
 立即註冊
搜索
查看: 7227|回復: 0
打印 上一主題 下一主題

輸入加減乘除的文字並計算出來

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2015-8-9 21:53:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
先很初步的做二個數字的算式,例如輸入 123*90會自動算出答案。很粗糙,不完備。

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

int main(void){
       
        int a[10],i,j=0,k=0,x,y=0;
        char str[30],num[5][20],ch;
        printf("enter it:\n");
       
        gets(str);
       
        for(i=0;str[i]!='\0';i++)
        {
         if(str[i]!='+'&&str[i]!='-'&&str[i]!='*'&&str[i]!='/')
         {
         num[y][k]=str[i];
         k++;
     }
         if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='\0')
         {
         ch=str[i];       
         num[y][k]='\0';
         
         y++;
         k=0;
     }
    }
    for(j=0,y=0;j<2,y<2;j++,y++)
        sscanf(num[y],"%d",&a[j]);
       
   // printf("%c\n%d\n%d\n",ch,a[0],a[1]);
   

         switch(ch){
                 case '+':
                         printf("%d",a[0]+a[1]);
                         break;
                 case '-':
                   printf("%d",a[0]-a[1]);
                   break;
                 case '*':
                   printf("%d",a[0]*a[1]);
                   break;
                  case '/':
                   printf("%d",a[0]/a[1]);
                    }        

       
        return 0;
}
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-4-26 14:13 , Processed in 0.086417 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表