程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用c語言寫的簡單計算器,c語言計算器

用c語言寫的簡單計算器,c語言計算器

編輯:關於C語言

用c語言寫的簡單計算器,c語言計算器


最初版本:

#include<stdio.h>

int checkPassword();

int add(int x,int y);

int sub(int x,int y);

int multi(int x,int y);

float quo(float x1,float y1);

int com(int x,int y);

int x,y;

int result;

int select;

float result1;

float x1,y1;

int success,psw;

int a;

int i;

int t;

 

 

void main()

{

         printf("請輸入密碼:\n");

         i=0;

         do{

        

                   scanf("%d",&a);

 

                   if( a!=1234  && i<2)

                            printf("請重新輸入密碼:\n");

                   else

                            i=3;

                   i++;

}while( i<=2);

         if(a==1234)

         {

                            for(t=1;t!=6;t=select)

         {

        

        

 

         printf("+=============================+\n");

         printf("+======計=算=器=主=菜=單======+\n");

         printf("+=============================+\n");

         printf("+==========1、  加法==========+\n");

         printf("+==========2、  減法==========+\n");

         printf("+==========3、  乘法==========+\n");

         printf("+==========4、  除法==========+\n");

         printf("+==========5、  求余==========+\n");

         printf("+==========6、  退出==========+\n");

         printf("+=============================+\n");

 

 

         printf("請輸入你的選擇:\n");

         scanf("%d",&select);

 

         switch(select)

         {

        

         case 1:

                   printf("請輸入兩個數:\n");

                   scanf("%d%d",&x,&y);

                   result=add(x,y);

                   printf("%d+%d=%d\n",x,y,result);

                   break;

         case 2:

                   printf("請輸入兩個數:\n");

                   scanf("%d%d",&x,&y);

                   result=sub(x,y);

                   printf("%d-%d=%d\n",x,y,result);

                   break;

         case 3:

                   printf("請輸入兩個數:\n");

                   scanf("%d%d",&x,&y);

                   result=multi(x,y);

                   printf("%d*%d=%d\n",x,y,result);

                   break;

         case 4:

                   printf("請輸入兩個數:\n");

                   scanf("%f%f",&x1,&y1);

                   result1=quo(x1,y1);

                   printf("%f/%f=%f\n",x1,y1,result1);

                   break;

         case 5:

                   printf("請輸入兩個數:\n");

                   scanf("%d%d",&x,&y);

                   result=com(x,y);

                   printf("%d/%d=%d\n",x,y,result);

                   break;

         case 6:

                   break;

         default:

                   printf("請輸入1~5之間的數\n");

                   break;

 

 

         }

         }

         }

}

int add(int x,int y)

{

         int sum;

         sum=x+y;

         return sum;

}

int sub(int x,int y)

{

         int sub;

         sub=x-y;

         return sub;

}

int multi(int x,int y)

{

         int multi;

         multi=x*y;

         return multi;

}

float quo(float x,float y)

{

         float quo;

         quo=x/y;

         return quo;

}

int com(int x,int y)

{

         int com;

         com=x%y;

         return com;

}

int checkPassword( int psw)

{

         if(psw==1234)

                   success=1;

         else

                   success=-1;

         return success;

}

 

 

 

 

 

改版後:

 

 

#include<stdio.h>

int yanzheng(int );

void caidan();

void jiafa(int,int);

void jianfa(int,int);

void chengfa(float,float);

void chufa(float,float);

void mo(int,int);

 

void main(){

         int x,m;

         int y;

         float a,b;

         printf("請輸入密碼:\n");

         scanf("%d",&x);

         m=yanzheng(x);

         if(m==1){

                   caidan();

                   //int y;

                   //float a,b;

                   for(;y!=6;){

                   printf("請輸入選項:\n");

                   scanf("%d",&y);

                   if(y>=1 && y<6){

                   printf("請輸入要運算的兩個數:\n");

                   scanf("%f%f",&a,&b);

                   }

 

                   switch(y){

                   case 1 :jiafa(a,b);break;

                   case 2 :jianfa(a,b);break;

                   case 3 :chengfa(a,b);break;

                   case 4 :chufa(a,b);break;

                   case 5 :mo(a,b);break;

                   case 6 :break;

                   default : printf("選項輸入錯誤!請重試\n");break;

                   }

                   }

         }

}

 

 

int yanzheng(int x){

         int i,m;

         for(i=1,m=0;i<3;i++){

                   if(x==1234){

                            m=1;

                            break;

                   }

                   printf("請重新出入密碼:");

                   scanf("%d",&x);

                  

         }

         return m;

}

 

void caidan(){

         printf("+=============================+\n");

         printf("+======計=算=器=主=菜=單======+\n");

         printf("+=============================+\n");

         printf("+==========1、  加法==========+\n");

         printf("+==========2、  減法==========+\n");

         printf("+==========3、  乘法==========+\n");

         printf("+==========4、  除法==========+\n");

         printf("+==========5、  求余==========+\n");

         printf("+==========6、  退出==========+\n");

         printf("+=============================+\n");

}

 

void jiafa(int x,int y){

         int sum;

         sum=x+y;

         printf("%d\n", sum);

}

 

void jianfa(int x,int y){

         int cha;

         cha=x-y;

         printf("%d\n", cha);

}

 

void chengfa(float x,float y){

         float ji;

         ji=x*y;

         printf("%f\n", ji);

}

 

void chufa(float x,float y){

         float shang;

         shang=x/y;

         printf("%f\n", shang);

}

 

void mo(int x,int y){

         int mo;

         mo=x%y;

         printf("%d\n", mo);

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved