码迷,mamicode.com
首页 > 编程语言 > 详细

用c语言写的简单计算器

时间:2016-05-27 21:34:51      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

最初版本:

#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);

}

 

用c语言写的简单计算器

标签:

原文地址:http://www.cnblogs.com/yjh123/p/5535971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!