Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 28.c #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions 经典示例/028.从键盘读入实数/28.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@

int status;
double result,sig,scale;
int sign(int c)/*�������ķ��ź���*/
int sign(int c)/*处理数的符号函数*/
{
if(c=='-')/*��Ϊ���ţ��ø������*/
sig=-sig;
if(c=='-')/*若为负号,置负数标记*/
sig=-sig;
// .
}

int integer(int c)/*ת���������֣�ת��һλ����λ*/
int integer(int c)/*转换整数部分,转换一位整数位*/
{
result=result*10.0+c-'0';
}

int decimal(int c)/*ת��С�����֣�ת��һλС��λ*/
int decimal(int c)/*转换小数部分,转换一位小数位*/
{
result+=(c-'0')*scale;
scale/=10.0;
}
/*״̬��*/
/*状态表*/
int statbl[ ][4]={{ 1,2,3,ERR},/*0*/
{ERR,2,3,ERR},/*1*/
{OK,2,4,OK},/*2*/
{ERR,4,ERR,ERR},/*3*/
{OK,4,OK,OK}};/*4*/
/*ת��������*/
/*转换函数表*/
int(*funtbl[ ][4])( )={{sign,integer,NULL,NULL},
{NULL,integer,NULL,NULL},
{NULL,integer,NULL,NULL},
Expand All @@ -40,29 +41,29 @@ int readreal(double *dp)
result=0.0;
scale=0.1;

while((c=getchar( ))==' '||c=='\n'||c=='\t');/*����ǰ���հ׷�*/
status=0;/*�ó�ʼ״̬*/
while((c=getchar( ))==' '||c=='\n'||c=='\t');/*跳过前导空白符*/
status=0;/*置初始状态*/
for(;;)
{
/*���൱ǰ�ַ�*/
if(c=='+'||c=='-') ckind=0;/*���ķ����ַ�*/
else if(c>='0'&&c<='9') ckind=1;/*���ַ�*/
else if(c=='.') ckind=2;/*����*/
else ckind=3;/* �����ַ� */
/*分类当前字符*/
if(c=='+'||c=='-') ckind=0;/*数的符号字符*/
else if(c>='0'&&c<='9') ckind=1;/*数字符*/
else if(c=='.') ckind=2;/*小数点*/
else ckind=3;/* 其它字符 */

if(funtbl[status][ckind])/* ����ת������ */
(*funtbl[status][ckind])(c);/* ִ����Ӧ�ĺ��� */
status=statbl[status][ckind];/*�����µ�״̬*/
if(status==ERR||status==OK)break;/* ������������ɹ� */
if(funtbl[status][ckind])/* 如有转换函数 */
(*funtbl[status][ckind])(c);/* 执行相应的函数 */
status=statbl[status][ckind];/*设置新的状态*/
if(status==ERR||status==OK)break;/* 结束:出错或成功 */
c=getchar();
}
ungetc(c,stdin); /* �黹���½����� */
ungetc(c,stdin); /* 归还数德结束符 */
if(status==OK)
{
*dp=result *sig;/* ��������ָ�����������Ӧ���� */
*dp=result *sig;/* 读入数按指针参数赋给相应变量 */
return 1;
}
return -1; /* �������� */
return -1; /* 出错返回 */
}
main()
{
Expand All @@ -73,4 +74,4 @@ main()
printf("The real number you input is: %f\n",x);
printf("\nYou have inputted nonreal char.\n Press any key to quit...\n");
getch();
}
}