用两条scanf()读取整形变量x和字符形变量c的顺序问题~!

2974786048 2009-06-02 06:00:55
做书后的练习题,自己动手写的程序,遇到一个小问题:


/****************************************************************************************/
/*题目:请编写一个程序,读取一个double类型的x值和字符类型的变量 T,其中x表示的是以弧度 */
/* 为单位的角,T表示的是三角函数的类型,然后显示如下值: */
/* */
/* (a) sin(x), 如果把s或S赋给T; */
/* (b) cos(x), 如果把c或C赋给T; */
/* (c) tan(x), 如果把t或T赋给T; */
/* 请分别使用以下语句来实现: */
/* (i) if...else语句; */
/* (ii) switch语句; */
/* */
/*时间:2009.06.02 */
/****************************************************************************************/

#include <stdio.h>
#include <math.h>

int main(void)
{
double x;
char T;

printf("\nplease input T and x (eg:x=3.141593 T=s ):\n\n");

printf("x=");
scanf("%lf",&x);

printf("T="); // 1)
scanf("%c",&T); // 2)


if (T=='s'||T=='S')
printf("\nx=%lf\tsin(x)=%lf\n\n",x,sin(x));
else if (T=='c'||T=='C')
printf("\nx=%lf\tcos(x)=%lf\n\n",x,cos(x));
else if (T=='t'||T=='T')
printf("\nx=%lf\ttan(x)=%lf\n\n",x,tan(x));
else
printf("\ndata error,invalid T!\n\n");

return 0;
}


问题:先输入数字,后输入字符。在我运行的时候,比如输入完 3.1419526 + 回车后,并没有提示“T=”程序就过去了,为什么不是如下顺序
1.提示x=
2.输入3.1415926并回车
3.提示T=
4.输入s并回车

事实上是在第二步就把“回车”当成字符T了,除非我输入3.1415926s,怎样写才能按上边的顺序运行呢?
...全文
116 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
2974786048 2009-06-02
  • 打赏
  • 举报
回复

/****************************************************************************************/
/*题目:请编写一个程序,读取一个double类型的x值和字符类型的变量 T,其中x表示的是以弧度 */
/* 为单位的角,T表示的是三角函数的类型,然后显示如下值: */
/* */
/* (a) sin(x), 如果把s或S赋给T; */
/* (b) cos(x), 如果把c或C赋给T; */
/* (c) tan(x), 如果把t或T赋给T; */
/* 请分别使用以下语句来实现: */
/* (i) if...else语句; */
/* (ii) switch语句; */
/* */
/*时间:2009.06.02 */
/****************************************************************************************/

/**解法1*********************************************************************************/
/*
#include <stdio.h>
#include <math.h>

int main(void)
{
double x;
char T;

printf("\nplease input T and x (eg:T=s x=3.141593):\n\n");

printf("x=");
scanf("%lf",&x);

fflush(stdin);

// getchar(); //用fflush(stdin)吧!

printf("T=");
scanf("%c",&T);



if (T=='s'||T=='S')
printf("\nx=%lf\tsin(x)=%lf\n\n",x,sin(x));
else if (T=='c'||T=='C')
printf("\nx=%lf\tcos(x)=%lf\n\n",x,cos(x));
else if (T=='t'||T=='T')
printf("\nx=%lf\ttan(x)=%lf\n\n",x,tan(x));
else
printf("\ndata error,invalid T!\n\n");

return 0;
}
*/


/**解法2*********************************************************************************/

#include <stdio.h>
#include <math.h>

int main(void)
{
double x;
char T;

printf("\nplease input T and x (eg:T=s x=3.141593):\n\n");

printf("x=");
scanf("%lf",&x);

fflush(stdin);

// getchar(); //用fflush(stdin)吧!

printf("T=");
scanf("%c",&T);



switch (T)
{
case 's':
case 'S':
printf("\nx=%lf\tsin(x)=%lf\n\n",x,sin(x));
break;
case 'c':
case 'C':
printf("\nx=%lf\tcos(x)=%lf\n\n",x,cos(x));
break;
case 't':
case 'T':
printf("\nx=%lf\ttan(x)=%lf\n\n",x,tan(x));
break;
default:
printf("\ndata error,invalid T!\n\n");
}

return 0;
}
2974786048 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liao05050075 的回复:]
getchar();

printf("T=");
[/Quote]

也是一个好方法。。。

2974786048 2009-06-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lingyin55 的回复:]
printf("x=");
scanf("%lf",&x);
fflush(stdin);/////////

printf("T=");            //  1)
scanf("%c",&T);
[/Quote]

搞定。。。

结贴给分。。。。
liao05050075 2009-06-02
  • 打赏
  • 举报
回复
getchar();

printf("T=");
lingyin55 2009-06-02
  • 打赏
  • 举报
回复
printf("x=");
scanf("%lf",&x);
fflush(stdin);/////////

printf("T="); // 1)
scanf("%c",&T);

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧