gpt4 book ai didi

c - 如何正确使用宏函数?

转载 作者:行者123 更新时间:2023-11-30 18:10:52 25 4
gpt4 key购买 nike

我有一个包含数字的数组。三种数字(floatdouble 等),当我搜索特定数字时,它应该返回/显示该数字在数组中的位置。

所以我的想法是,我可以将 -3、1.25 这样的数字放入数组中,而无需专门定义数组类型,因为我需要接收任何类型的数字。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define finder(find_num, array,type,format)\
void find_num(type array[], type ,int size)\
{\
int location = 0, i;\
for (i = 0; i < size; i++)\
{\
location++;\
if (array[i] == num)\
break;\
}\
if (location != arry[0]\
printf("Location of the number is at %d place\n", result);\
else\
printf("Location of number not found\n");\
}

finder(funcfind, int array[], int %d\t, int %d\t);
finder(funcfind, int array[] ,double %f, int %d\t)

int main()
{
int array[] = { 5,6,7,88,12,3,20 }, result = 0, num = 0, size = 0;
size = sizeof(array);
scanf("%d", &num);
funcfind(array, num, size);
}

我需要在这个练习中使用宏,但我陷入了 finder 函数和 funcfind 的困境,这是我大部分错误的来源。

**更新:

#include <stdio.h>
#include <stdlib.h>
#define finder(find_num, array ,type ,format)\
void find_num(type array[], type num,int size)\
{\
int location = 0, i;\
for (i = 0; i < size; i++)\
{\
location++;\
if (array[i] == num)\
break;\
}\
if (location != array[0])\
printf("Location of the number is at %d place\n", location);\
else\
printf("Location of number not found\n");\
}

finder(funcfind, array, double, int %d\t);
finder(funcfind, array, int, int %d\t);
finder(funcfind, array, float, int %d\t);

int main()
{
int array[] = { 5,6,7,-3,1.5,3,20 }, num = 0, size = 0;
size = sizeof(array) / sizeof(array[0]);
scanf("%d", &num);
funcfind(array, num, size);
}

它可以工作,但某些数字(例如 -3 和 1.5)不起作用

最佳答案

如果我通过预处理(gcc -E),结果是:

void funcfind(int %d\t int array[][], int %d\t ,int size){ int location = 0, i; for (i = 0; i < size; i++) { location++; if (int array[][i] == num) break; } if (location != arry[0] printf("Location of the number is at %d place\n", result); else printf("Location of number not found\n");};
void funcfind(double %f int array[][], double %f ,int size){ int location = 0, i; for (i = 0; i < size; i++) { location++; if (int array[][i] == num) break; } if (location != arry[0] printf("Location of the number is at %d place\n", result); else printf("Location of number not found\n");}

类型int %d\t所以第一个参数是错误的,为什么int %d而不是int v例如?

因为很难猜测你想要什么,所以我让你继续解决问题

无论如何在宏中-void find_num(type array[], type ,int size)很奇怪,也许一定是void find_num(type array[], type num,int size)和电话void find_num(type array[], type ,int size);finder(funcfind, array ,double, int %d\t)-if (location != arry[0]\必须是if (location != array[0])\

给出:

void funcfind(int array[], int num ,int size){ int location = 0, i; for (i = 0; i < size; i++) { location++; if (array[i] == num) break; } if (location != array[0]) printf("Location of the number is at %d place\n", result); else printf("Location of number not found\n");};
void funcfind(double array[], double num ,int size){ int location = 0, i; for (i = 0; i < size; i++) { location++; if (array[i] == num) break; } if (location != array[0]) printf("Location of the number is at %d place\n", result); else printf("Location of number not found\n");}

如果我缩进第一个函数:

void funcfind(int array[], int num ,int size){
int location = 0, i;
for (i = 0; i < size; i++) {
location++;
if (array[i] == num)
break;
}
if (location != array[0])
printf("Location of the number is at %d place\n", result);
else
printf("Location of number not found\n");
};

所以有一些错误,例如结果未知等,但我认为你可以轻松解决

关于c - 如何正确使用宏函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com