#include
#include
#define M 10
#define N 5
/* 将16进制字符串转换成10进制数,若为素数则返回改数字,若为合数则返回-1 */
int primevalue(char *num)
{
int n, /* 数字 */
i, /* 循环变量 */
x; /* 每次遇到的一位16进制数位 */
char *p = num, /* 字符指针 */
c /* 每一位字符 */;
/* 递增信高指针弯睁,直到\0结束 */
n = 0;
while(c = *p++)
{
if('0' <= c && '9' >=c) { /* 0-9 */
x = (int)(c - '0');
} else if('A' <= c && 'F' > c) { /* ABCDEF */
x = (int)(c - 'A' + 10);
} else if('a' <= c && 'f' > c) { /* abcdef */
x = (int)(c - 'a' + 10);
} else {
/* 非法字符 */
x = -1;
break;
}
if(x != -1) {
n *= 16; /*或者改成 n = n<<4*/
n += x;
}
}
if(n == 2) return n;
for (i = 2; i <= sqrt(n); ++i) {
if (n % i == 0)
return -1; /* 这是一个合数 */
}
return n;
}
/* 主函数 */
int main(void) {
char numbers[M][N] = {"B3","A1"埋坦岁,"11","14","1D","13","21","D1","A4","CD"}, *str;
int i, prime;
FILE *fp = fopen("myf2.out","w");
for(i=0; i
if(primevalue(str) != -1)
fprintf(fp, "%s %d\n", str, primevalue(str));
}
fprintf(fp, "%s", "学号?");
fclose(fp);
return 0;
}