我有一个类的以下简单代码,并从c = c ...if语句开始每行得到一个解析错误.另外我收到以下错误:
警告:类型与先前的隐式声明不匹配.函数isUpperCase中的isUpperCase的先前隐式声明:在'='标记之前解析错误.而且isLowerCase也有类似的错误.
有没有人有任何见解?
#include<stdio.h>
#include<string.h>
#define LOWERCASE_START = 97
#define LOWERCASE_END = 122
#define UPPERCASE_START = 65
#define UPPERCASE_END = 90
#define ALPHABET_LENGTH = 26
void simpleEncryption(char s[]){
int i;
for (i=0; i < strlen(s); i++){
char c = s[i];
if (isUpperCase(c) == 1){
c = c - UPPERCASE_START + 1;
c = c % ALPHABET_LENGTH;
c = c + UPPERCASE_START;
} else if (isLowerCase(c) == 1){
c = c - LOWERCASE_START + 1;
c = c % ALPHABET_LENGTH;
c = c + LOWERCASE_START;
}
s[i]=c;
}
}
int isUpperCase(char c) {
if (c >= UPPERCASE_START && c <= UPPERCASE_END) {
return 1;
} else {
return 0;
}
}
int isLowerCase(char c) {
if (c >= LOWERCASE_START && c <= LOWERCASE_END) {
return 1;
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
你的宏定义不应该包含等号,你只需要这样:
#define LOWERCASE_START 97
#define LOWERCASE_END 122
#define UPPERCASE_START 65
#define UPPERCASE_END 90
#define ALPHABET_LENGTH 26
Run Code Online (Sandbox Code Playgroud)
预处理器相当简单,会盲目地替换= 97你的C来产生这样的破碎的东西:
c = c - = 97 + 1;
Run Code Online (Sandbox Code Playgroud)
如果你有=你的#defines.
isUpperCase()并且isLowerCase()在使用它们之前不会声明:要么为每个声明添加一个声明,要么在之前移动它们的定义simpleEncryption().
由于mu首先说明太短,宏定义是不正确的.修复宏或使用const int变量:
static const int ALPHABET_LENGTH = 26;
Run Code Online (Sandbox Code Playgroud)