全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:825
推到 Plurk!
推到 Facebook!

菜鳥請教各位老手

尚未結案
maiching33325
一般會員


發表:1
回覆:1
積分:0
註冊:2003-03-26

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-03-26 01:51:39 IP:61.227.xxx.xxx 未訂閱
最近再寫一個程式..目的是讀取檔案裡的數值然後由小到大排列 但我無法做到由小到大排列....請問哪裡寫的不對..懇請各位指教    String str=""; char fstr[20]; FILE *fp ; fp = fopen(".\\file.txt","r"); str="編號\t數值A\t數值B\t數值C\t數值D\n"; while(!feof(fp)){ fscanf(fp,"%s",fstr); str=str+fstr+"\t"; fscanf(fp,"%s",fstr); str=str+fstr+"\t"; fscanf(fp,"%s",fstr); str=str+fstr+"\t"; fscanf(fp,"%s",fstr); str=str+fstr+"\n"; int i,j,x; for(i=0;i<3;i ){ for(j=i 1;j<4;j ){ if(fstr[i]>fstr[j]){ x=fstr[i]; fstr[i]=fstr[j]; fstr[j]=x; } } } } fclose(fp); ShowMessage(str);
okiss
一般會員


發表:11
回覆:55
積分:13
註冊:2002-11-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-03-26 08:23:20 IP:140.127.xxx.xxx 未訂閱
引言: int i,j,x; for(i=0;i<3;i ){ for(j=i 1;j<4;j ){ //你這裡是string compare..@@..不對吧 if(fstr[i]>fstr[j]){ //應該先把char string轉成int x=fstr[i]; fstr[i]=fstr[j]; fstr[j]=x; } } } } fclose(fp); ShowMessage(str);
把char string轉成int請用atoi這個函數... 不過,如果你可以用C 的話,建議你用fstream來open and close file 且array宣告成integer比較好...^^
maiching33325
一般會員


發表:1
回覆:1
積分:0
註冊:2003-03-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-03-26 12:10:48 IP:61.227.xxx.xxx 未訂閱
謝謝大大的指教.... 小弟是看書學Builder c 的 我買的書中沒找到如何把char string轉成int 更找不到atoi這個函數 >.< 還有更好的方法或怎麼使用這函數嗎 請大大們再不吝賜教....感激
okiss
一般會員


發表:11
回覆:55
積分:13
註冊:2002-11-27

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-03-26 13:06:15 IP:140.127.xxx.xxx 未訂閱
atoi ----------------------------------------------------------- Header File stdlib.h     Category Conversion Routines, Math Routines    Syntax #include  int atoi(const char *s); int _wtoi(const wchar_t *s); Description Converts a string to an integer. atoi converts a string pointed to by s to int; atoi recognizes (in the following order) An optional string of tabs and spaces An optional sign A string of digits The characters must match this generic format: [ws] [sn] [ddd] In this function, the first unrecognized character ends the conversion. There are no provisions for overflow in atoi (results are undefined). Return Value atoi returns the converted value of the input string. If the string cannot be converted to a number of the corresponding type (int), atoi returns 0. /* atoi example */ #include #include int main(void) { int n; char *str = "12345.67"; n = atoi(str); printf("string = %s integer = %d\n", str, n); return 0; } --------------------------------------------------------------------- 這是 c 的standard library裡的函式...@@
sunex
一般會員


發表:9
回覆:7
積分:3
註冊:2003-03-18

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-03-27 10:27:15 IP:61.216.xxx.xxx 未訂閱
強迫轉形也可以ㄚ char o='3'; int a; a=int(o)-48; //因為數字的'0'的ASCII是從48開始 ListBox1->Items->Add(IntToStr(a));
系統時間:2024-05-05 2:20:29
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!