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;
}
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;
}
相关文章推荐
- POJ, 2418 Hardwood Species(按照字典顺序输出单词并且输出此单词出现的百分比)
- kwic--Java统计单词个数并按照顺序输出
- 设计相应的数据结构和算法,尽量高效的统计一片英文文章(总单词数目)里出现的所有英文单词, * 按照在文章中首次出现的顺序打印输出该单词和它的出现次数。
- Python按照书写顺序输出字典中的元素
- Python按照书写顺序输出字典中的元素
- java中实现HashMap中的按照key的字典顺序排序输出
- 将英文句子的单词拆分,并按按字典顺序排序逐个输出单词
- java中实现HashMap中的按照key的字典顺序排序输出
- c语言冒泡实现将一个字符串按照字典顺序输出
- 按照字典的顺序输出字符串,并不允许重复
- java中实现HashMap中的按照key的字典顺序排序输出
- 2015蘑菇街笔试题——给定一个英文单词的字符串,按照英文单词的顺序反转输出
- 牛客网三道编程题(四)华为机试在线训练(三)将数字逆序输出、将n个字符串按照字典顺序排序
- 北航研究生复试2015上机第三题:记录单词并按字典顺序输出
- POJ2388map的应用统计树的百分比,并按照字母顺序输出
- java中找出奇数和偶数并按照从小到大的顺序输出
- 将字符串"BASIC"按照相反顺序输出
- 尾插法建立链表--顺序输出
- mysql实现按照指定顺序输出
- 使用jQuery建立一个按字母顺序排列的友好的页面索引