马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
A-E,a-e 1;
F-J,f-j 2;
K-O,k-o 3;
P-T,p-t 4;
U-Y,u-y 5;
Z,z 6
计算单词有权重。(英文单词的权重为该单词各字母权值之和)
如:World (5+3+4+3+1=16)
并将单词按权重降序排序;
但是我这写的if,else if 好像有问题,num的值不对。
of (3+2=5),而我的只有2。
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void calculate(char w[][20],int n,int p[]){
/**********Program**********/
int i,j,num,t;
for(i=0; i<n; i++)
{
num=0;
for(j=0; w[i][j]; j++)
{
t=w[i][j];
if('A'<=t<='E' || 'a'<=t<='e')
num+=1;
else if ('F'<=t<='J' || 'f'<=t<='j')
num+=2;
else if('K'<=t<='O' || 'k'<=t<='o')
num+=3;
else if('P'<=t<='T' || 'p'<=t<='t')
num+=4;
else if('U'<=t<='Y' || 'u'<=t<='y')
num+=5;
else if(t=='Z' || t=='z')
num+=6;
// printf("%d ",num);
}
// printf("\n");
p[i]=num;
}
/********** End **********/
}
void sort(int p[],int n,char w[][20]){
/**********Program**********/
int i,j,t;
char ch[20];
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(p[i]<p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
strcpy(ch,w[i]);
strcpy(w[i],w[j]);
strcpy(w[j],ch);
}
}
}
/********** End **********/
}
int main(){
char words[6][20]={{"JiangSu"},{"Teachers"},{"University"},{"of"},{"Technology"}};
int value[5]={0};
int i;
FILE *fp;
if((fp=fopen("DATA.TXT","w"))==NULL){
printf("File open error\n");
exit(0);
}
calculate(words,5,value);
for(i=0;i<5;i++){
fprintf(fp,"%-20s%4d\n",words[i],value[i]);
printf("%-20s%4d\n",words[i],value[i]);
}
printf("\n");
fprintf(fp,"\n");
sort(value,5,words);
for(i=0;i<5;i++){
fprintf(fp,"%-20s%4d\n",words[i],value[i]);
printf("%-20s%4d\n",words[i],value[i]);
}
fclose(fp);
return 0;
}
|