沐丶心 发表于 2020-4-25 22:56:08

加减乘除

本帖最后由 沐丶心 于 2020-4-25 23:46 编辑

写了半天{:10_285:}
结果一输入字母(a,s,m,d,)就选择default的选项
有没有大佬帮忙找找错{:10_266:}
#include <stdio.h>
void pr();
float add();//加
float subtract();//减
float multiply();//乘法
float divide();//除
char wdnmd();
float first();
float last();
int main()
{
    pr();
    char ch;
    while((ch = wdnmd()) != 'q')
    {
      switch (ch)
      {
      case 'a':add();break;
      case 's':subtract();break;
      case 'm':multiply();break;
      case 'd':divide();break;
      default:printf("非法输入,请重新输入:");continue;
      }

    }
    printf("game over");
    return 0;
}
void pr()
{
    printf("Enter the operation of your choice:\n");
    printf("a. add(+)         s. subtract(-)\n");
    printf("m. mulit(*)       d. divide(/)\n");
    printf("q. quit\n");
}
char wdnmd()
{
      char ch;
      ch = getchar();
      while(getchar() != '\n')
      continue;
      return ch;
}
float first()
{
    float lbw;
    char ch;
    printf("Enter first number:");
    while(scanf("%f", &lbw) == 0)
    {
      while((ch=getchar()) != '\n')
      putchar(ch);
      printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
    }
    while(getchar()!= '\n')
      continue;

    return lbw;
}
float last()
{
    float lbw;
    char ch;
    printf("Enter first number:");
    while(scanf("%f", &lbw) == 0)
    {
      while((ch=getchar()) != '\n')
      putchar(ch);
      printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
    }
    while(getchar()!= '\n')
      continue;

    return lbw;
}
float add()
{
    float a = first();
    float b = last();
    printf("%.2f + %.2f = %.2f\n", a, b, a+b);
}
float subtract()
{
    float a = first();
    float b = last();
    printf("%.2f - %.2f = %.2f\n", a, b, a-b);
}
float multiply()
{
    float a = first();
    float b = last();
    printf("%.2f * %.2f = %.2f\n", a, b, a*b);
}
float divide()
{
   
    float a = first();
    float b = last();
    if(b == 0)
    {
      printf("除数不能为0,请重新输入:");
      scanf("%f", &b);
    }
    printf("%.2f / %.2f = %.2f\n", a, b, a/b);
}

   

人造人 发表于 2020-4-25 23:06:13

#include <stdio.h>
void pr();
float add();//加
float subtract();//减
float multiply();//乘法
float divide();//除
char wdnmd();
float first();
float last();
int main()
{
    pr();
    char ch;
    //while(ch = wdnmd() != 'q')
    while((ch = wdnmd()) != 'q')
    {
      switch (ch)
      {
      case 'a':add();break;
      case 's':subtract();break;
      case 'm':multiply();break;
      case 'd':divide();break;
      default:printf("非法输入,请重新输入:");continue;
      }

    }
    printf("game over");
    return 0;
}
void pr()
{
    printf("Enter the operation of your choice:\n");
    printf("a. add(+)         s. subtract(-)\n");
    printf("m. mulit(*)       d. divide(/)\n");
    printf("q. quit\n");
}
char wdnmd()
{
      char ch;
      ch = getchar();
      while(getchar() != '\n')
      continue;
      return ch;
}
float first()
{
    float ch;
    printf("Enter first number:");
    while((ch = getchar()) == 0)
    {
      while(getchar() != '\n')
      putchar(ch);
      printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
    }
    while(getchar()!= '\n')
      continue;

    return ch;
}
float last()
{
    float ch;
    printf("Enter first number:");
    while((ch = getchar()) == 0)
    {
      while(getchar() != '\n')
      putchar(ch);
      printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
    }
    while(getchar()!= '\n')
      continue;

    return ch;
}
float add()
{
    float a = first();
    float b = last();
    printf("%.2f + %.2f = %.2f\n", a, b, a+b);
}
float subtract()
{
    float a = first();
    float b = last();
    printf("%.2f + %.2f = %.2f\n", a, b, a-b);
}
float multiply()
{
    float a = first();
    float b = last();
    printf("%.2f + %.2f = %.2f\n", a, b, a*b);
}
float divide()
{
   
    float a = first();
    float b = last();
    if(b == 0)
    {
      printf("除数不能为0,请重新输入:");
      scanf("%f", &b);
    }
    printf("%.2f + %.2f = %.2f\n", a, b, a/b);
}

沐丶心 发表于 2020-4-25 23:10:53

人造人 发表于 2020-4-25 23:06


谢谢人造人老师{:10_264:}
小细节太重要了{:10_297:}
页: [1]
查看完整版本: 加减乘除