诱人的老师中文字幕在线观看
遊客:  註冊 | 登錄 | 幫助





標題: [求助] Floating Point Error in C: 我的功課∼
Lynnmouse     Rank: 2Rank: 2
藍之初
性別 保密
UID 139

精華 0
帖子 490
積分 124   詳情

閱讀權限 30
註冊 2006-6-24
來自
狀態 離線

 
 
 
 
發表於 2007-2-21 02:11 PM  資料  個人空間  短訊  加為好友 
Floating Point Error in C: 我的功課∼

X-Seek
此 X-Seek 主題價值 100 XS。 (天藍出手)
X-Seek 出題者 Lynnmouse 希望能在 2007-3-7 02:11 PM 前尋找到解決方案。
已於 2007-2-23 04:22 AM 由 winniemouse 選出最合適的解決方案及關閉 X-Seek 主題。

(((救命

Write a program in C to input information of 8 computer suppliers who are participuting a tender to supply computers indentification number, supplier number, name, address and quotation price. The program reads the output file and finds the lowest quotation price and prints all the information of the supplier. Using the following structure supplier in the program.

struct Supplier
{
           int tenderID;
           int supplierNo;
           char name[20];
           char address[40];
           float quotationPrice;
};
typedef struct Supplier Sup;

Sup dealer[8];

Filename:Supplier.dat


/*Name: Lynn*/
/*Date: 16-05-2007*/

#include <conio.h>
#include <stdio.h>

struct Supplier
{
        int tenderID;
        int supplierNo;
        char name[20];
        char address[40];
        float quotationPrice;
};

typedef struct Supplier Sup;

main()
{
        /*Function Prototype*/
        void Info(Sup A[]);
        void LowestPrice(Sup B[]);

        /*Declaration*/
        Sup dealer[8];

        clrscr();
        /*Call Function*/
        Info(dealer);
        LowestPrice(dealer);

        gotoxy(30,24);
        printf("Press ENTER to continue");
        getch();
}
/**************************/
/*Function name        : Info    */
/*Input         : Sup A[] */
/*Output        : void    */
/**************************/
void Info(Sup A[])
{
        /*Declaration*/
        FILE *fin;
        int index;

        /*Open File*/
        fin=fopen("D:Supplier.dat","w");

        /*Error Message*/
        if(fin==NULL)
        {
                printf("CANNOT Create New File");
                exit(1);
        }

        /*Input Records*/
        for(index=0; index<8; index++)
        {
                clrscr();
                gotoxy(30,5);
                printf("Record No: %d",index+1);
                gotoxy(30,6);
                printf("Tender ID Number: ");
                gotoxy(30,8);
                printf("Supplier Number: ");
                gotoxy(30,10);
                printf("Name: ");
                gotoxy(30,12);
                printf("Address :");
                gotoxy(30,14);
                printf("Quotation Price: ");

                gotoxy(45,6);
                scanf("%d",&A[index].tenderID);
                gotoxy(45,8);
                scanf("%d",&A[index].supplierNo);
                fflush(stdin);
                gotoxy(45,10);
                gets(A[index].name);
                gotoxy(45,12);
                gets(A[index].address);
                gotoxy(45,14);
                scanf("%f",&A[index].quotationPrice);
        }

        /*Write to disk*/
        for(index=0; index<8; index++)
        {
                fprintf(fin,"%-6d",A[index].tenderID);
                fprintf(fin,"%-5d",A[index].suplierNo);
                fprintf(fin,"%-21s" A[index].name);
                fprintf(fin,"%-41s",A[index].address);
                fprintf(fin,"%-.2f\n",A[index].quotationPrice);
        }

        /*Close File*/
        fclose(fin);
}
/********************************/
/*Function Name        : LowestPrice   */
/*Input         : Sup B[]        */
/*Output        : void                */
/********************************/
void LowestPrice(Sup B[])
{
        /*Declaration*/
        FILE *flow;
        int index;
        int pass;
        Sup temp;

        /*Open File*/
        flow=fopen("D:Supplier.dat","r");

        /*Error Message*/
        if(flow==NULL)
        {
                printf("CANNOT read the FIle!!");
                exit(1);
        }

        /*Read from disk*/
        for(index=0; index<8; index++)
        {
                fscanf(flow,"%d",&B[index].tenderID);
                fscanf(flow,"%d",&B[index].supplierNo);
                fscanf(flow,"%s",&B[index].name);
                fscanf(flow,"%s",&B[index].address);
                fscanf(flow,"%f\n",&B[index].quotationPrice);
        }

        /*Print the List*/
        clrscr();
        gotoxy(30,5);
        printf("%-6s%-5s%-21s%-41s%-s\n","ID","No","Name","Address","Price");
        for(index=0; index<8; index++)
        {
                printf("%-6d",B[index].tenderID);
                printf("%-5s",B[index].supplierNo);
                printf("%-21s",B[index].name);
                printf("%-41s",B[index].address);
                printf("%.2f\n",B[index].quotationPrice);
        }
        /*Sorting*/
        for(pass=1; pass<8; pass++)
        {
                for(index=0; index<8-pass; index++)
                {
                        if(B[index+1].quotationPrice < B[index].quotationPrice)
                        {
                                /*Swap data*/
                                temp=B[index];
                                B[index]=B[index+1];
                                B[index+1]=temp;
                        }       
                        }
                }
        }

        /*print the lowest price*/
        printf("%-6d%-5d%-21d%-41s%.2f",B[0].tenderID,B[0].supplierNo,B[0].name,B[0].address,B[0].quotationPrice);

        /*Close File*/
        fclose(flow);
}

在screen出現了
Quotation Price: floating point formats not linked
Adnormal program termination




[ 觀看解決方案 ]


最後編輯: mickeyGoUp : 2007-2-22 12:44 AM
頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-2-23 12:12 AM  資料  個人空間  短訊  加為好友 
What IDE are you using to write the C programs?  Turbo C?  Borland C?  Or something else?  And what version is that?

頂部



Lynnmouse     Rank: 2Rank: 2
藍之初
性別 保密
UID 139

精華 0
帖子 490
積分 124   詳情

閱讀權限 30
註冊 2006-6-24
來自
狀態 離線

 
 
 
 
發表於 2007-2-23 01:27 AM  資料  個人空間  短訊  加為好友 
i m using Turbo C version 2.0

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-2-23 03:17 AM  資料  個人空間  短訊  加為好友 
Try to place the following function in your file, but do not call it.  And see if that errors go away.

static void forcefloat(float *p)
{
float f = *p;
forcefloat(&f);
}





X-Seek 解決方案 1/1   
頂部

Lynnmouse     Rank: 2Rank: 2
藍之初
性別 保密
UID 139

精華 0
帖子 490
積分 124   詳情

閱讀權限 30
註冊 2006-6-24
來自
狀態 離線

 
 
 
 
發表於 2007-2-23 03:40 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 mickeyGoUp 於 2007-2-23 03:17 AM 發表
Try to place the following function in your file, but do not call it.  And see if that errors go away.


static void forcefloat(float *p)
{
float f = *p;
forcefloat(&f);
}

放在哪裡﹖

不懂
因為﹐只學C一小份

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-2-23 04:01 AM  資料  個人空間  短訊  加為好友 
試下放響下面 --- here --- 既位置?  

...
typedef struct Supplier Sup;

--- here ---

main()
...


頂部

Lynnmouse     Rank: 2Rank: 2
藍之初
性別 保密
UID 139

精華 0
帖子 490
積分 124   詳情

閱讀權限 30
註冊 2006-6-24
來自
狀態 離線

 
 
 
 
發表於 2007-2-23 04:17 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 mickeyGoUp 於 2007-2-23 04:01 AM 發表
試下放響下面 --- here --- 既位置?  


...
typedef struct Supplier Sup;

--- here ---

main()
...

真的行了

謝謝

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-2-23 06:45 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 winniemouse 於 2007-2-22 03:17 PM 發表

真的行了

謝謝

不用客氣!  

行就要說說原由了﹐不要盲目地把問題解決而沒有知道問題的起因呢!    我已很久沒有寫 C﹐所以我也是在網上做了些 research 才找到這個方法。  話說 Turbo C 和 Borland C 的 compiler 都有些問題﹐當您使用 %f 時﹐它不會自動 link 到負責 floating point 的 library。 所以就要寫入那 function 'forcefloat'﹐當 compile 時﹐Turbo C 讀到這個 function 時﹐便會 load 那個 floating point library﹐到您 scanf %f 或 printf %f 時﹐便不會再有問題了。  但記著不要真的 call 那 'forcefloat' function 啊!  

頂部

Lynnmouse     Rank: 2Rank: 2
藍之初
性別 保密
UID 139

精華 0
帖子 490
積分 124   詳情

閱讀權限 30
註冊 2006-6-24
來自
狀態 離線

 
 
 
 
發表於 2007-2-23 02:21 PM  資料  個人空間  短訊  加為好友 
謝謝~~~

但,又有問題了(

Floting point error: Domain

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-2-23 10:11 PM  資料  個人空間  短訊  加為好友 
請先 comment out 所有 function calls﹐剩下 Info(dealer)﹐run 一 run 看看有沒有同一 error。

main()
{
        /*Function Prototype*/
        void Info(Sup A[]);
        void LowestPrice(Sup B[]);

        /*Declaration*/
        Sup dealer[8];

        clrscr();
        /*Call Function*/
        Info(dealer);
/*        LowestPrice(dealer);*/

/*        gotoxy(30,24);*/
/*        printf("Press ENTER to continue");*/
/*        getch();*/
}

如果沒有﹐便 uncomment LowestPrice(dealer)﹐又再 run run。 都沒有的話﹐便 uncomment gotoxy(30,24)﹐... 如此類推﹐直到找到是那一個 function 引起問題。  然後我們再研究那一個 function 有甚麼出錯。  

我猜是某 floating point 數值上 "出了位" 之類。

頂部

快速美言
           


當前時區 GMT+8, 現在時間是 2024-4-17 12:20 AM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.024060 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver