august 发表于 2007-6-22 21:40:57

以前问过的一个题

这也算是链表吧
#include<stdio.h>
#include<stdlib.h>
struct list
{
    int num;
    float cash;
    struct list *next;
}*head;
void new_record()
{
    struct list *newer,*th1;
    newer=(struct list *)malloc(sizeof(struct list));
    if(head==NULL)
    head=newer;
    else
    {
   th1=head;
   while(th1->next!=NULL)
       th1=th1->next;
   th1->next=newer;
    }
    th1=newer;
    printf("please enter the score and number:");
    scanf("%d %f",&th1->num,&th1->cash);
    th1->next=NULL;
}
void display()
{
    struct list *th1;
    if(head==NULL)
   {
       printf("\nthis is empty.\n");
       exit(1);
   }
    else
    {
    th1=head;
    do
    {
    printf("num:%d\t",th1->num);
    printf("cash:%f\n",th1->cash);th1=th1->next;
    }while(th1!=NULL);
    }
}
void main()
{
    char ch;
    int flag=1;
    head=NULL;
    while(flag)
    {
    printf("'e' if enter nubmer,'l'is list the number:");

    ch=getchar();
    switch(ch)
    {
    case 'e':new_record();break;
    case 'l':display();break;
    default:flag=0;printf("will exit the programming!\n");
    }
    }
}


执行一次,然后就提示错为
“scanf:floating pointformatsnotlinkedAbnormalprogramtermination”
然后有人解答过说可能是while重复分配内存空间,所以导致,但是在VC++里的话是可以运行的,那如果在TC里运行应该怎么改才可以呢?

Zelsazgh 发表于 2007-6-22 23:41:24

这个错误信息的意思是:scanf的浮点格式转换程序没有连接。

TC开发时(80年代)DOS下的存储资源紧缺,因此TC在编译时尽量不加入无关部分。在没发现需要做浮点转换时,就不将这个部分安装到可执行程序里。但有时TC不能正确识别实际确实需要浮点转换,因此就会出现上面错误。

解决方法:设法告诉TC需要做浮点数输入转换。

大程序里由于变量很多,只要有了线索,TC就会把浮点转换连上,因此反而不常遇到这个问题。

解决方法很简单
在主程序中设定一个变量 float x;
在scanf中调用,再将值传给.....结构即可
void new_record()
{
    struct list *newer,*th1;
    float x;
    newer=(struct list *)malloc(sizeof(struct list));
    if(head==NULL)
    head=newer;
    else
    {
   th1=head;
   while(th1->next!=NULL)
       th1=th1->next;
   th1->next=newer;
    }
    th1=newer;
    printf("please enter the score and number:");
    scanf("%d %f",&th1->num,&x);
    th1->crash=x;
    th1->next=NULL;
}

august 发表于 2007-6-23 04:28:39

浮点格式转换怎么理解?

Zelsazgh 发表于 2007-6-23 22:48:13

由于在汇编中浮点数值要另外进行处理....要调用专门的FPU(浮点运算处理单元)进行处理...但同时由于TC的DOS年代....内存的紧缺限制....为保证程序的高效率运行所以编译过程通常不进行加载浮点运算的处理项...

coolpay64 发表于 2007-6-23 23:11:26

LZ真的很勤力呢,每天也發問

august 发表于 2007-6-24 09:09:09

引用第4楼coolpay64于2007-06-23 23:11发表的:
LZ真的很勤力呢,每天也發問

与其说我勤力,不与说我笨好了
页: [1]
查看完整版本: 以前问过的一个题