作者skyinmoon86 (阿咏~^^万岁)
看板C_and_CPP
标题[问题] C程式的问题..
时间Thu Oct 1 20:33:10 2009
题目如下:
Write a program to read a series of names, one per line, from standard input
,and write out those names spelled in reverse order to a file called file1.
Write another program to read the names in from file1, reverse them again,
sort them, and write them out to a file called file2.
第一题我已经做完了..
第一题的程式码如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file;
int i=0;
int j=0;
int k=0;
char filename[20];
char name[20];
char name2[20];
printf("Enter the name of the file: ");
gets(filename);
file=fopen(filename,"w");
if(file!=NULL){
printf("Enter several names (Enter EOF to end input):\n");
while(!feof(stdin)){
gets(name);
j=0;
for(i=(strlen(name)-1);i>=0;i--){
name2[j++]=name[i];
}
name2[j]='\0';
fprintf(file,"%s\n",name2);
for(k=0;k<19;k++){
name[k]=0;
}
}
}
fclose(file);
return 0;
}
第一题算是完成了..
但是第二题我不会排序..
程式码如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file;
FILE *file2;
char name[20]={0};
char name2[20]={0};
int i=0;
int j=0;
file2=fopen("file2","w");
if((file=fopen("file1","r"))!=NULL){
while(!feof(file)){
fread(&name,sizeof(name),1,file);
if(name!=0){
j=0;
for(i=(strlen(name)-1);i>=0;i--){
name2[j++]=name[i];
}
name2[j]='\0';
fwrite(&name2,sizeof(name2),1,file2);
}
}
}
fclose(file2);
fclose(file);
return 0;
}
请大家帮帮我= =
我的时间快到了..
谢谢大家
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.130.138.118
1F:推 tomore:这要用到二维的动态配置了 不然也不知道file里有几笔名字 10/01 20:57
2F:→ skyinmoon86:那我惨了= = 10/01 21:01
3F:→ MOONRAKER:你好像已经有两个同学了? 10/01 21:29
4F:→ MOONRAKER:抱歉…原来你就是那位同学 XD 10/01 21:29
5F:推 VictorTom:老实说, 连着三篇, 很想说请不要用没有意义的标题.... 10/01 21:33
6F:→ VictorTom:而且还三个都不一样串接都不能串, 实在莫名其妙.... 10/01 21:33
7F:→ VictorTom:二维动态配置或者Linked-List都可以处理不知道几笔资料 10/01 21:35
8F:→ VictorTom:的问题, 再不然就放大绝, 先问清楚测资范围, 再开一个 10/01 21:35
9F:→ VictorTom:够大的阵列放所有资料. 排序我在第一篇就推过了, 如果 10/01 21:36
10F:→ VictorTom:数字会排序, 那字串比大小用strcmp比, 字串copy用strcpy 10/01 21:36
11F:→ VictorTom:做; 如果排序不懂, 就先search找排序的演算法然後套进 10/01 21:37
12F:→ VictorTom:字串的比较和copy来排.... 10/01 21:38
13F:推 legnaleurc:用 linked list 可以直接用 insertion sort 10/01 21:43
14F:推 softwind:说真的 不方便allocate mem吗??? 10/01 23:43
15F:→ james732:先用个巨大阵列顶着用吧... 10/01 23:45
16F:→ softwind:你时间到了 就会有版友写给你 放心 OK的 10/02 01:15