大致明白是怎么回事了。
按照楼主您现在的意思,这么改就行了:
if(*operatorp!='c' && *operatorp!='q')
scanf("%lf%c",operandp,&junk);
改为:
if(*operatorp!='c' && *operatorp!='q')
scanf("%lf%c",operandp,&junk);
else
scanf( "%c", &junk );
这是键盘缓冲区在输入的时候,有回车符产生的,会影响到下一次的读取一个字符(scanf)
另外char junk;用处不大,可以去除。
每次读取完成后,用fflush(stdin);清一下键盘缓冲区。
void scan_data(char *operatorp, double *operandp)
{
scanf("%c",operatorp);
if(*operatorp!='c' && *operatorp!='q')
scanf("%lf",operandp);
fflush(stdin);
}
/*
* =================================================================
*
* Filename: test.c
*
* Description:
*
* Version: 1.0
* Created: 03/27/2012 08:21:47 AM
* Compile FAQ: gcc -o x x.c `pkg-config --cflags --libs gtk+-2.0`
* Compiler: gcc
*
* Author: zjhxmjl (), zjhxmjl@gmail.com
* MY_SITE: http://www.92linux.tk
*
* =================================================================
*/
#include
#include
void instructions(void);
void scan_data(char *operatorp, double *operandp);
void do_next_op(char *Operator, double *Operandp, double *r);
int main(void)
{
double result=0;
char OPERATOR;
double OPERAND;
instructions();
do
{
printf("Result:%14.6f Input> ",result);
scan_data(&OPERATOR,&OPERAND);
do_next_op(&OPERATOR,&OPERAND,&result);
} while(OPERATOR!='q');
printf("Final: %.6f ",result);
return(0);
}
void instructions(void)
{
printf("This simple calculator program has 5 operations:\n");
printf(" +, -, *, /, and ^\n");
printf("Type an operator, space, then a number, e.g., + 5.0\n");
printf("Type c to clear the result.\n");
printf("Type q to quit the program.\n\n\n");
}
void scan_data(char *operatorp, double *operandp)
{
char junk;
scanf("%c",operatorp);
if(*operatorp!='c' && *operatorp!='q')//逻辑与问题,当前者为假时后者不执行(短路),但仍占用下次输入的内存空间
scanf("%lf%c",operandp,&junk);
else
scanf ( "%c", &junk );
}
void do_next_op(char *Operator, double *Operandp, double *r)
{
switch(*Operator)
{
case '+':
*r=*r+*Operandp;
break;
case '-':
*r=*r-*Operandp;
break;
case '*':
*r=*r**Operandp;
break;
case '/':
*r=*r/(*Operandp);
break;
case '^':
*r=pow(*r,*Operandp);
break;
case 'c':
*r=0;
break;
}
}
#include
#include
void instructions(void);
void scan_data(char *operatorp, double *operandp);
void do_next_op(char *Operator, double *Operandp, double *r);
int main(void)
{
double result=0;
char OPERATOR = 'a'; //操作符
double OPERAND; //操作数据
instructions(); //说明
while(OPERATOR!='q')
{
printf("\nInput> ");
scan_data(&OPERATOR,&OPERAND);
do_next_op(&OPERATOR,&OPERAND,&result);
if(OPERATOR == 'c')
{
result = 0;
continue;
}
if(OPERATOR == 'q')
break;
printf("Result:%14.6f\n", result);
}
printf("Final: %.6f \n",result);
return(0);
}
void instructions(void)
{
printf("This simple calculator program has 5 operations:\n");
printf(" +, -, *, /, and ^\n");
printf("Type an operator, space, then a number, e.g., + 5.0\n");
printf("Type c to clear the result.\n");
printf("Type q to quit the program.\n\n\n");
}
void scan_data(char *operatorp, double *operandp)
{
scanf("%c",operatorp);
if(*operatorp!='c' && *operatorp!='q') //操作符不是c也不是q
scanf("%lf",operandp);
while((getchar())!='\n') //这里将所有多余的输入都接收完毕,防止被下次循环接收
continue;
}
void do_next_op(char *Operator, double *Operandp, double *r)
{
switch(*Operator)
{
case '+':
*r=*r+*Operandp;
break;
case '-':
*r=*r-*Operandp;
break;
case '*':
*r=*r**Operandp;
break;
case '/':
*r=*r/(*Operandp);
break;
case '^':
*r=pow(*r,*Operandp);
break;
case 'c':
*r=0;
break;
}
}
/*
IDE:VC6.0
运行结果如下
This simple calculator program has 5 operations:
+, -, *, /, and ^
Type an operator, space, then a number, e.g., + 5.0
Type c to clear the result.
Type q to quit the program.
Input> +5.6
Result: 5.600000
Input> +5.4
Result: 11.000000
Input> c 0
Input> +4.5
Result: 4.500000
Input> +3.9
Result: 8.400000
Input> q 0
Final: 8.400000
Press any key to continue
*/
是在获取操作符和操作数据的时候,如果输入的是c或者q的时候没有处理好
void scan_data(char *operatorp, double *operandp)
{
。。。 。。。
fflush(stdin);
}