云计算实验
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int main()
{
clock_t start, stop;
start = clock();//记录程序开始时间
FILE *fp;
int isfirst = 1;//文件第一个单词
int isfound = 0;//是否发现新单词,表示未发现
myword *head;
myword *p1, *p2, *p3,*p4;
p1 = (myword*)malloc(sizeof(myword));
p1->occnum=0;
p1->next=NULL;p2 = p1;p3 = p1;head = NULL;
fp = fopen("//home//huchaowei//Desktop//data(1).dat", "r");//打开数据文件
while (!feof(fp))//当文件未结束
{if (isfirst == 1)
{fscanf(fp, "%s", p1->name);p1->occnum = 1;head = p1;isfirst = 0;}
else{char temp[MAX]={0};
fscanf(fp, "%s", temp);//fscanf函数遇空格或回车当成\0处理。
change(temp);
p3 = head;isfound = 0;
while (p3 != NULL)
{if (strcmp(p3->name, temp) != 0){p3 = p3->next;}
else{p3->occnum += 1;isfound = 1;break;}
}
if (isfound == 0)//若发现新单词,则新建结点插入链表
{
p4 = (myword*)malloc(sizeof(myword);
p4->occnum=0;
p4->next=NULL;
p2=head;
while(p2->next!=NULL)
p2=p2->next;
strcpy(p4->name,temp);
p4->occnum = 1;
p2->next = p4;
}
}
}
select(head);//选出前三
fclose(fp);
stop = clock(); //记录程序结束时间
printf("Time:");
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);//打印程序运行时间
system("pause");
return 0;
}
typedef struct myword
{
char name[MAX];
int occnum;
struct myword *next;
}myword;
void select(myword *head)
{
FILE *fp2;
myword *p3 = head;
int na,nb,nc;
char* a=(char*)malloc(20*sizeof(char));char* b=(char*)malloc(20*sizeof(char));
char* c=(char*)malloc(20*sizeof(char));na=nb=nc=0;
while (p3 != NULL)
{printf("name=%s,count=%d\n", p3->name, p3->occnum);
if(na
{nc=nb;c=b;nb=na;b=a;na=p3->occnum;a=p3->name;}
else if(nb
{nc=nb; c=b;nb=p3->occnum;b=p3->name;}
else if(nc
{nc=p3->occnum;c=p3->name;}
p3 = p3->next;}
printf("\nTop three:%s:%d %s:%d %s:%d\n",a,na,b,nb,c,nc);
fp2 = fopen("//home//huchaowei//Desktop//data.dat", "w");
fprintf(fp2,"\nTop three:%s:%d %s:%d %s:%d\n",a,na,b,nb,c,nc);//统计结果写入文件中
fclose(fp2);}
void change(char *str)//将从文件中读出的字符串中含有的标点符号去掉,并将大写转为小写
{char ch;char temp[MAX];
memset(temp,'\0',sizeof(temp)/sizeof(char));int i=0,j=0;
while((ch=str[i])!='\0')
{if(ch>96&&ch<123){temp[j] = ch;j++;}
else if(ch>64&&ch<91){temp[j] = ch + 32;j++;}i++;}
memset(str,'\0',sizeof(str)/sizeof(char));strcpy(str,temp);}
public class Topcount {
public static class TokenizerMapper extends
Mapper
{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException
{
String line = value.toString().toLowerCase(); // 全部转换为小写字母
StringTokenizer itr = new StringTokenizer(line, " \t\n\f\" . , : ; ? ! [ ] ' - ");
//常见的标点符号识别
while (itr.hasMoreTokens())
{
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class TokenizerMapper2
extends Mapper
{
int c=0;
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException
{
StringTokenizer itr = new StringTokenizer(value.toString());
IntWritable a=new IntWritable(Integer.parseInt(itr.nextToken()));
Text b=new Text(itr.nextToken());
if(c<3)//取前三
{
context.write(a, b);
c++;
}
}
}
public static class IntSumReducer extends
Reducer
{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable
Context context) throws IOException, InterruptedException {
int sum = 0;