无聊得从写了个,写的不好,
修正了函数中的对除数的判断。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char str[] = "请输入:数字、运算符(+-*/)和数字的组合,并且在一行内!";
int fun(double a, double b, char ch, double * z);
int main(void)
{
double a, b, c;
int ch;
int k;
puts(str);
do
{
k = 0;
k += scanf("%lf", &a);
while( (ch = getchar()) == ' ')
continue;
ch = tolower(ch);
if(ch != 'q')
{
if(ch != '\n')
{
k += scanf("%lf", &b);
while(getchar() != '\n')
continue;
}
(k == 2 && fun(a, b, ch, &c) ) ? printf(" = %g\n\n", c)
: puts("无效输入!\n");
}
else
while(getchar() != '\n')
continue;
}while(ch != 'q');
puts("Bye!");
system("pause");
return 0;
}
int fun(double a, double b, char ch, double * z)
{
switch(ch)
{
case '+':
*z = a + b;
break;
case '-':
*z = a - b;
break;
case '*':
*z = a * b;
break;
case '/':
if(b) // 如果b不等于0
*z = a / b;
else
{
puts("除数不能为0!");
return 0;
}
default :
return 0;
}
return 1;
}
修正了函数中的对除数的判断。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char str[] = "请输入:数字、运算符(+-*/)和数字的组合,并且在一行内!";
int fun(double a, double b, char ch, double * z);
int main(void)
{
double a, b, c;
int ch;
int k;
puts(str);
do
{
k = 0;
k += scanf("%lf", &a);
while( (ch = getchar()) == ' ')
continue;
ch = tolower(ch);
if(ch != 'q')
{
if(ch != '\n')
{
k += scanf("%lf", &b);
while(getchar() != '\n')
continue;
}
(k == 2 && fun(a, b, ch, &c) ) ? printf(" = %g\n\n", c)
: puts("无效输入!\n");
}
else
while(getchar() != '\n')
continue;
}while(ch != 'q');
puts("Bye!");
system("pause");
return 0;
}
int fun(double a, double b, char ch, double * z)
{
switch(ch)
{
case '+':
*z = a + b;
break;
case '-':
*z = a - b;
break;
case '*':
*z = a * b;
break;
case '/':
if(b) // 如果b不等于0
*z = a / b;
else
{
puts("除数不能为0!");
return 0;
}
default :
return 0;
}
return 1;
}
—>〉Sun〈<—