[問題] 排序
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...)
Dev-C++
餵入的資料(Input):
Input.dat
內容為 名字,ID,薪水
名字,ID,薪水
|
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
無法compiler
程式碼(Code): (請善用置底文標色功能)
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct client
{
char name[20];
char id[20];
int sal[20];
char temp[20];
};
int main()
{
struct client who[100];
int i,k,m,n,num;
int j[100];
FILE *input;
input=fopen("input.dat","r");
i=0;
while(fscanf(input,"%s",who[i].temp)!=-1)
{i++;}
num=i;
for(k=0;k<num;k++)
{
sscanf(who[k].temp, "%[^','],%[^','],%s",who[k].name,who[k].id,who[k].sal);
j[k]=atoi(who[k].sal);
}
printf("原始數值\n");
for(i=0;i<num;i++)
{
printf("%s,%s,%d\n",who[i].name,who[i].id,j[i]);
}
system("PAUSE");
return 0;
}
//以上main的部分是對的,執行時跑出來的是正確的結果
//以下是改過後的mergesort碼
希望從ID的排序使整體名字,ID,薪水一起排序
main裡的who[].temp為整體
切出來後的who[].name 名字
id ID
sal 薪水
void mergesort(char who[],int num)//這個地方不知道該怎麼寫
{
if(num<=1) return ;
int index,r,l;
char left[num/2],right[num-num/2];
index=r=l=0;
while(index<num/2)
left[l++]=who[index++].temp;//
while(index<num)
right[r++]=who[index++].temp;
//錯誤訊息 request for member 'temp' in something not a structure or union
mergesort(left,l);
mergesort(right,r);
merge(l,r,left,right,who.temp);
}
void merge(int l_max,int r_max,int left[],int right[],char who[])
{
int index,l,r;
index=l=r=0;
while(l<l_max&&r<r_max)
if(strcmp(who[l].id,who[r].id)<0)
who[index++].temp=left[l++];
else
who[index++].temp=right[r++];
//錯誤訊息 request for member 'id' in something not a structure or union
while(l<l_max)
who[index++].temp=left[l++];
while(r<r_max)
who[index++].temp=right[r++];
}
//錯誤訊息 request for member 'temp' in something not a structure or union
另請教:要把who[0].temp也就是整體存在left[0] 時該如何宣告left[]和right[]
who[num].temp存在right[num-num/2]
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.181
→
12/20 20:03, , 1F
12/20 20:03, 1F
→
12/20 20:15, , 2F
12/20 20:15, 2F
→
12/20 20:20, , 3F
12/20 20:20, 3F
→
12/20 20:30, , 4F
12/20 20:30, 4F
→
12/20 20:40, , 5F
12/20 20:40, 5F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章