规定26个英文字符的能量值分别为1-26,键盘输入一个英文单词,输出其能量值,字符不区分大小写
直接减ascll码差值就行,加一个判断大小字母
#include <stdio.h>
using namespace std;
int main()
{
printf("英文单词能量计算");
printf("请输入一个英文单词,以回车结束\n");
string str = "";
int litterNum = 0;
char ch = getchar();
while (ch !='\n')
{
if (isalpha(ch))//判断输入的是否为字母
{
ch = toupper(ch);
litterNum += (ch - 0x40); //ASCII中 A对应0x41 转换后A-Z 对应1-26
}
ch = getchar();
}
printf("输入的单词的能量为: %d", litterNum);
return 0;
#include <stdio.h>
int main()
{
int flg=0;
char tem;
while(1)
{
printf("请输入一个字母,输入1结束:");
scanf("%c",&tem);
getchar();
if(65<=tem&&tem<=90)
{
printf("%d\n",tem-64); flg=1;
}
if(97<=tem&&tem<=122)
{
printf("%d\n",tem-96); flg=1;
}
if(tem=='1')
{
flg=1;
break;
}
if(flg==0)
{
printf("输入的不是字母。\n");
}
}
return 0;
}