c语言 关于if与else if的问题!求大佬
{:10_256:}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[],int n,int p[]){
/**********Program**********/
int i,j,num,t;
for(i=0; i<n; i++)
{
num=0;
for(j=0; w; j++)
{
t=w;
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=num;
}
/**********End**********/
}
void sort(int p[],int n,char w[]){
/**********Program**********/
int i,j,t;
char ch;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(p<p)
{
t=p;
p=p;
p=t;
strcpy(ch,w);
strcpy(w,w);
strcpy(w,ch);
}
}
}
/**********End**********/
}
int main(){
char words={{"JiangSu"},{"Teachers"},{"University"},{"of"},{"Technology"}};
int value={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,value);
printf("%-20s%4d\n",words,value);
}
printf("\n");
fprintf(fp,"\n");
sort(value,5,words);
for(i=0;i<5;i++){
fprintf(fp,"%-20s%4d\n",words,value);
printf("%-20s%4d\n",words,value);
}
fclose(fp);
return 0;
}
首先能看出的是
'A'<=t<='E' c没有这种写法
'A'<=t && t<='E' 应该是这样 ba21 发表于 2019-2-24 14:02
首先能看出的是
'A'
{:10_266:}
我忘记了<尴尬>
页:
[1]