printf 与 #define 配合使用(乱数产生器)
此为一乱数产生器,我希望让用户自订义乱数范围,但不能建置!(若人手预定义范围则正常运作!)
printf("%i ", rand()%终止数+起始数;
printf("%i ", rand()%(string(range[1]))+(string(range[0])));
錯誤訊息:
main.m:32:37: Invalid operands to binary expression ('int' and 'char *')
#import <Foundation/Foundation.h> // 这一行不用理,因为我是在 OS X 环境下开发
#define string(x) # x
int main(int argc, const char * argv[])
{
@autoreleasepool { // 不用理
int i, j, k, l, range[2];
printf("欢迎使用乱数产生器\n产生多少乱数:");
scanf("%i", &i);
printf("产生多少次:");
scanf("%i", &j);
printf("乱数范围:");
scanf("%i%*[^0-9]%i", &range[0], &range[1]);
for (l = 0; l < j; l++) {
srand((unsigned)(time(NULL)+l));
printf("第 %02i 次:", (l + 1));
for (k = 0; k < i; k++) {
srand((unsigned)(time(NULL)+l+k+rand()));
printf("%i ", rand()%(string(range[1]))+(string(range[0]))); //问题所在
}
printf("\n");
}
}
return 0;
}
#define string(x) # x
int main(int argc, const char * argv[])
{
@autoreleasepool { // 不用理
int i, j, k, l, range[2];
printf("欢迎使用乱数产生器\n产生多少乱数:");
scanf("%i", &i);
printf("产生多少次:");
scanf("%i", &j);
printf("乱数范围:");
scanf("%i%*[^0-9]%i", &range[0], &range[1]);
for (l = 0; l < j; l++) {
srand((unsigned)(time(NULL)+l));
printf("第 %02i 次:", (l + 1));
for (k = 0; k < i; k++) {
srand((unsigned)(time(NULL)+l+k+rand()));
printf("%i ", rand()%(string(range[1]))+(string(range[0]))); //问题所在
}
printf("\n");
}
}
return 0;
}
[ 本帖最后由 PlusA2T 于 2012-10-13 18:36 编辑 ]