您的位置:首页 > 其它

USTC机试—按照字典索引顺序的建立,输出行号和单词

2018-02-27 16:15 399 查看
如:i am      a student from

      china输出:i 1

          am 1          a 2

          student 2          from 2          china 3/*#include<stdio.h>//此算法是重新对每一个单词按照字典顺序排序
#include<algorithm>
using namespace std;
#define N 100
struct store{//设计一个结构体重载小于符号,比较字典序
char s
;
bool operator <(const store &a)const{
return s[0]<a.s[0];
}
}E
;
int main(){
int count=0;//累计行数
FILE *fp1,*fp2;
fp1=fopen("4.in","r");
fp2=fopen("4.out","w");
while(!feof(fp1)){
fscanf(fp1,"%s",E[count++].s);
}
sort(E,E+count);//结构体中已经重载了比较运算符,此处直接调用sort函数对字符串数组进行排序
for(int i=0;i<count;i++){
fprintf(fp2,"%d %s\n",i,E[i].s);
}
return 0;
}*/
//此算法是重新按照行的顺序顺序输出行号和单词,此处用到fgets函数
#include<stdio.h>
#include<string.h>
#define N 100
int main(){
char s
;
int count=0;
FILE *fp1,*fp2;
fp1=fopen("4.in","r");
fp2=fopen("4.out","w");
while(!feof(fp1)){
count++;
fgets(s,N,fp1);//此处注意fgets函数的参数格式以及其含义
int len=strlen(s);
fprintf(fp2,"%d ",count);//先提前输出每一行的行号
for(int i=0;i<len;i++){
if(s[i]!='\0'&&s[i]!=' '){
fprintf(fp2,"%c",s[i]);
}
else{
fprintf(fp2,"\n%d ",count);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐