在C中调用函数
这是我的代码
#include orders_char(int c1, int c2, int c3); void orders_char(int c1, int c2, int c3) { if(c1 < c2) if(c2 < c3) printf("Ordered characters are: %c %c %c", c1, c2, c3); else if(c3 < c2 && c1 < c3) printf("Ordered characters are: %c %c %c", c1, c3, c2); else if(c2 < c1 if(c1 < c3) printf("Ordered characters are: %c %c %c", c2, c1, c3); else if(c3 < c1 && c3 c3) if (c3 c1) printf("Ordered characters are: %c %c %c", c3, c1, c2); else if(c3 > c2 && c2 < c1) printf("Ordered characters are: %c %c %c", c2, c3, c1); return; } int main(void) { char c1, c2 ,c3; int i = 65; printf("Please enter 3 capital letters with no spaces: \n"); scanf("%c%c%c", &c1, &c2, &c3); orders_char(c1, c2, c3); return 0; }
但是我得到了错误:
8.7.c:3: warning: data definition has no type or storage class 8.7.c:6: error: conflicting types for 'orders_char' 8.7.c:3: error: previous declaration of 'orders_char' was here 8.7.c:6: error: conflicting types for 'orders_char' 8.7.c:3: error: previous declaration of 'orders_char' was here 8.7.c: In function `orders_char': 8.7.c:14: error: syntax error before "if"
你真的应该使用{ }
作为你的if
表达式。 我通过格式化程序运行你的代码,它清楚地表明if
和else if
不按你希望它们工作的方式工作。
编辑:
- 你有很多情况 – 这三个变量只有六种可能的排序顺序
- 你使用int作为char,虽然这样做并不好 – 更好地使用
char
作为字符。
void orders_char(int c1, int c2, int c3);
因为没有任何东西,它默认为int。
见到你,Beco
编辑:
顺便说一句,你需要一个支撑
else if(c2 < c1
此外,你不匹配if-else's。 else将始终与near if匹配。 所以:
if(a>b) if(b>c) printf("a>b>c"); else printf("a<=b"); // <<== wrong! This else matches with if(b>c)
你需要放一些括号:
if(a>b) { if(b>c) printf("a>b>c"); } else printf("a<=b"); // <<== right! This else matches with if(a>b)
照顾自己。
我能看到的两个错误:
一:
orders_char(int c1, int c2, int c3);
应该
void orders_char(int c1, int c2, int c3);
如果未指定函数的返回类型,则编译器假定为int
。 因此,当它看到函数返回void
而你无法下定决心时,它会抱怨。
二:
else if(c2 < c1
缺少关闭paren。
一个else总是绑定到最接近的前一个不匹配的if。 缩进不会显示此处实际发生的情况。
if (condition) if (condition2) Something; else if (condition3) SomethingElse; else if (condition4) DoMore;
是代码中实际发生的事情。 要像你想要的那样工作,你必须像这样写:
if (condition) { if (condition2) Something; else if (condition3) SomethingElse; } else if (condition4) DoMore;
在顶部:
orders_char(int c1, int c2, int c3); void orders_char(int c1, int c2, int c3) { //...
首先声明然后定义一个函数。
但是你的声明缺少一个返回类型(第一个警告),编译器将默认返回int
,因此不同于定义(第一个错误)。
通常,c编译器会发出一连串错误消息,因此在读取错误消息时,您应该只查看第一个(或两个)。
当然,它不会返回任何东西。
你没有满足第一个if条件:
if (c1 < c2) { //all your code is here }
C在比较字符时使用ASCII值。 一个体面的ASCII图表在这里:
http://www.elec-intro.com/logic-ascii
SAR的值是83,65,82。所以你的第一个基本上是:
if (83 < 65)
这显然是错误的。 所以 - 你的printfs都没有被击中而且没有任何输出。 对数字进行排序的最快方法是进行一系列交换:
char temp = c1; if(c1 > c2) { temp = c1; c1 = c2; c2 = temp; } if(c2 > c3) { temp = c2; c2 = c3; c3 = temp; } if(c1 > c3) { temp = c1; c1 = c3; c3 = temp; }
编辑:我应该说这可能是最直接的方式,不使用内置插件或第三方工具。 显然比这更快的排序算法。
您不需要第三行的orders_char。 它不是一个合适的原型(因为返回类型被推断为int)并且你在main()之前有完整的函数定义。
没有冒犯,但这是基本的调试技巧。 首先检查字符是否按预期进入,可能在调用orders_char()之前使用printf()。 其次,你应该使用std :: sort()来做你想做的事情。
std :: vector vC; vC.push_back(C1); vC.push_back(C2); vC.push_back(C3); 的std ::排序(vC.begin(),vc.end());
或类似的东西。 您可以在自己喜欢的浏览器中搜索std :: sort()。
祝好运。
更简单的实现:
#include #include void orders_char (int c1, int c2, int c3); int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } void orders_char (int c1, int c2, int c3) { int a[] = {c1, c2, c3}; qsort(a, 3, sizeof(int), compare); printf("Ordered characters are: %c %c %c\n", a[0], a[1], a[2]); } int main (void) { char c1, c2, c3; int i = 65; printf("Please enter 3 capital letters with no spaces: \n"); scanf("%c%c%c", &c1, &c2, &c3); orders_char(c1, c2, c3); return 0; }
通常,尝试重建现有算法是没有意义的。 更好地了解哪些可以立即使用。
当然,有更好的方法来实现上述,但这是超级简单和快速。
当您可以简单地展开冒泡排序时,这似乎是一个相当复杂的代码,用于查找序列(并且,在任何人攻击使用冒泡排序之前,您应该意识到在某些情况下它是完全可以接受的,例如数据在哪里已经大部分已经排序,或者数据集很小 – 在这些情况下,它通常可以胜过其他排序):
#include void orders_char(int c1, int c2, int c3) { int tmp; // Unrolled bubble sort for three elements. if (c1 > c2) { tmp = c1; c1 = c2; c2 = tmp; } if (c2 > c3) { tmp = c2; c2 = c3; c3 = tmp; } if (c1 > c2) { tmp = c1; c1 = c2; c2 = tmp; } printf("Ordered characters are: %c %c %c\n", c1, c2, c3); } int main (void) { char c1, c2 ,c3; printf("Please enter 3 capital letters with no spaces: \n"); scanf("%c%c%c", &c1, &c2, &c3); orders_char(c1, c2, c3); return 0; }
上面的函数只是简单地将元素移动到正确的顺序,明智地使用三个比较和零到三个交换,然后将它们打印出来。 你可以在函数内交换它们,因为它们只是那里的局部变量,不会影响从调用者传入的内容。
如果注释掉提示行( main()
函数中的printf
),并执行以下脚本,则可以看到它适用于所有输入:
pax> for i in abc acb bac bca cab cba ; do ...> echo $i | ./sort3 ...> done Ordered characters are: abc Ordered characters are: abc Ordered characters are: abc Ordered characters are: abc Ordered characters are: abc Ordered characters are: abc