简单计算器代码(含加减乘除取余5个操作)

2021/12/28 23:41:56

本文主要是介绍简单计算器代码(含加减乘除取余5个操作),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

简单计算器代码(含加减乘除取余5个操作)

#include"stdio.h"
#include"math.h"
#include"ctype.h"
#include<stdlib.h>
#define ZERO 0.0000001
float GetDataFromKeyBoard();
int GetContinueYesOrNo();
char Displaymenu();
void  add();
void  sub();
void mul();
void did();
void sur();
void TheComputer();

char Displaymenu()
{
 char choice;
 printf("本程序实现一个简单的计算器,请选择下面的需要执行的计算\n");
 printf("a.加法  b.减法\n");
 printf("c.乘法  d.除法\n");
 printf("e.取余  q.结束\n");
 printf("输入字母a,b,c,d,e选择运算方法,输入q结束\n");
 printf("请输入:");
 do
 {
  choice = tolower(getch());
  if (choice == 'q' || choice == 'Q')
   return'q';
  else
  {
   if (choice >= 'a' && choice <= 'e')
   {
    putchar(choice);
    return choice;
   }
   else
   {
    putchar('\a');
   }
  }
 } while (1);
}




void  add()
{
 float firstData, secondData;
 printf("\n请输入第一个操作数:");
 firstData = GetDataFromKeyBoard();
 printf("\n请输入第二个操作数:");
 secondData = GetDataFromKeyBoard();
 printf("\n%f +%f = %f\n", firstData, secondData, firstData + secondData);

}
void  sub()
{
 float firstData, secondData;
 printf("\n请输入第一个操作数:");
 firstData = GetDataFromKeyBoard();
 printf("\n请输入第二个操作数:");
 secondData = GetDataFromKeyBoard();
 printf("\n%f -%f = %f\n", firstData, secondData, firstData - secondData);
}
void mul()
{
 float firstData, secondData;
 printf("\n请输入第一个操作数:");
 firstData = GetDataFromKeyBoard();
 printf("\n请输入第二个操作数:");
 secondData = GetDataFromKeyBoard();
 printf("\n%f ×%f = %f\n", firstData, secondData, firstData * secondData);
}
void did()
{
 float firstData, secondData;
 printf("\n请输入第一个操作数:");
 firstData = GetDataFromKeyBoard();
 printf("\n请输入第二个操作数:");
 secondData = GetDataFromKeyBoard();
 if (fabs(secondData) > ZERO)
  printf("\n%f ÷%f = %f\n", firstData, secondData, firstData / secondData);
 else
  printf("除数为零,不能完成除法运算!\n");
}
void sur()
{
 float firstData, secondData;
 printf("\n请输入第一个操作数(整数):");
 firstData = GetDataFromKeyBoard();
 printf("\n请输入第二个操作数(整数):");
 secondData = GetDataFromKeyBoard();
 if ((int)firstData == firstData && (int)secondData == secondData)
  if (secondData > ZERO)
   printf("\n%d %% %d = %d\n", (int)firstData, (int)secondData, (int)firstData%(int)secondData);
  else
   printf("除数为零,不能完成模的运算\n");
 else
  printf("输入的预算数不是整数,不能进行模的运算\n");
}

float GetDataFromKeyBoard()
{
    float i;
    scanf("%f",&i);
    return i;
}

void TheComputer()

{
 char choice;
 while (1)
 {
  choice = Displaymenu();
  switch (choice)
  {
  case'a':
   add();
   break;
  case'b':
   sub();
   break;
  case'c':
   mul();
   break;
  case'd':
   did();
   break;
  case'e':
   sur();
   break;
  case'q':
   return;
  }
  if (GetContinueYesOrNo())
   system("cls");
  else
   break;
 }
}

int GetContinueYesOrNo()
{
    char c;
    printf("还要再继续吗Y/N: ");
    c=getche();
    if(c=='Y')
        return 1;
    else  return 0;

}

main()
{
 TheComputer();
}



这篇关于简单计算器代码(含加减乘除取余5个操作)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程