简单加减乘除

Posted cokefentas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单加减乘除相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <string.h>

char get_choice(void);
char get_first(void);
float get_float(void);
void add(void);
void subtract(void);
void multiply(void);
void divide(void);

int main(void)
{
   int choice;

   while((choice=get_choice())!=q)
   {
      switch (choice)
      {
      case a:
         add();
         break;
      case b:
         subtract();
         break;
      case c:
         multiply();
         break;
      case d:
         divide();
         break;
      case q:
         break;
      }
   }
   return 0;
}

char get_choice(void)
{
   int ch;

   printf("input the letter of your choice:
");
   printf("a. add
b. subtract
c. multiply
d. divide
q. quit
");
   ch = get_first();
   while((ch<a || ch>d) && ch!=q)
   {
      printf("please respond with a,b,c,d,q:");
      ch = get_first();
   }

   return ch;
}

char get_first(void)
{
   int ch;

   ch = getchar();
   while(getchar()!=
)
      continue;

   return ch;
}

float get_float(void)
{
   float input;
   char ch;

   while(scanf("%f",&input) != 1)
   {
      while((ch=getchar()) != 
)
         putchar(ch);
      printf("is not a float number.
");
      printf("please input a float number,such as 1.1, 2.5:");
   }
   return input;
}

void add(void)
{
   float f_num,s_num;

    printf("input first number:");
    f_num = get_float();
    printf("input second number:");
    s_num = get_float();
    printf("%f + %f = %f
",f_num,s_num,f_num+s_num);
}

void subtract(void)
{
   float f_num,s_num;

    printf("input first number:");
    f_num = get_float();
    printf("input second number:");
    s_num = get_float();
    printf("%f - %f = %f
",f_num,s_num,f_num-s_num);
}

void multiply(void)
{
   float f_num,s_num;

    printf("input first number:");
    f_num = get_float();
    printf("input second number:");
    s_num = get_float();
    printf("%f * %f = %f
",f_num,s_num,f_num*s_num);
}

void divide(void)
{
   float f_num,s_num;

    printf("input first number:");
    f_num = get_float();
    printf("input second number:");
    s_num = get_float();
   if(s_num == 0)
   {
      printf("input a number other than 0:");
      s_num = get_float();
   }
    printf("%f / %f = %f
",f_num,s_num,f_num/s_num);
}

 

以上是关于简单加减乘除的主要内容,如果未能解决你的问题,请参考以下文章

求嵌入式开发,简单的加减乘除计算机代码。。利用ARM9进行的。。

java简单加减乘除

[C++]利用逆波兰式,简单实现下加减乘除的混合运算

如何用VB做一个简单计算器 要求实现加减乘除乘方开方功能

为啥这段代码会泄露? (简单的代码片段)

怎么用Bootstrap 写出加减的符号