在 C 中排序和分析数组
Sorting and Analyzing Arrays In C
我做了一个小骰子游戏,如果我掷出 5 次相同的数字,我应该获得 Grand,如果我掷出 4 次相同的数字,我应该获得 Poker。
我的问题是我的代码只适用于“1”和“2”,如果我尝试用 4 获得扑克牌,它不算
#include <stdio.h>
void abfrage(int wurf[], int size){
int i;
for (i=0; i<size; i++){
printf("Würfel %i: ", i+1);
scanf("%i", &wurf[i]);
}
}
// switch int*
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Sort the array
void bubbleSort(int wurf[], int size)
{
int i, j;
for (i = 0; i < size-1; i++){
for (j = 0; j < size-i-1; j++){
if (wurf[j] > wurf[j+1])
{
swap(&wurf[j], &wurf[j+1]);
}
}
}
}
void arrayausgabe(int wurf[], int size){
int u = 0;
for (u=0;u<size;u++) {
printf("array nummer %i: ", u); // err with "&"
printf("%i \n", wurf[u]);
}
}
void bewertung(int wurf[]){
int *x;
x = wurf;
if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Grand");
}else if (*x==*(x+1)==*(x+2)==*(x+3) || *(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Poker");
}else if ((*x==*(x+1)==*(x+2) && *(x+3)==*(x+4) )||(*(x+2)==*(x+3)==*(x+4) && *x==*(x+1))) {
printf("Full House");
}else {
printf("HAAA Verloren");
}
}
int main() {
int wurf[5];
printf("Programm Würfelspiel\nGrand\tgleiche Augenzahl auf allen 5 Würfeln\nPoker\tgleiche Augenzahl auf 4 Würfeln\nFull House\tgleiche und 2 gleiche Augenzahlen\n\nBitte gibt deine gewürfelten zahlen ein\n");
abfrage(wurf, 5);
bubbleSort(wurf, 5);
arrayausgabe(wurf, 5);
bewertung(wurf);
}
我是大一新生,如果代码看起来有点垃圾,请见谅
这个逻辑:
if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
在 C 中没有达到您的期望(尽管在 Python 中做到了):根据值是否不同,与 ==
returns 的比较为 0 或 1或相等。链接它们并不会检查它们是否彼此相等;相反,它检查前两个是否相等,然后将相等的结果(可以是 0 或 1)与第三个值的值进行比较,依此类推。
您需要分别检查每个比较:
if (wurf[0] == wurf[1] && wurf[0] == wurf[2] && wurf[0] == wurf[3] && wurf[0] == wurf[4]) {
我做了一个小骰子游戏,如果我掷出 5 次相同的数字,我应该获得 Grand,如果我掷出 4 次相同的数字,我应该获得 Poker。
我的问题是我的代码只适用于“1”和“2”,如果我尝试用 4 获得扑克牌,它不算
#include <stdio.h>
void abfrage(int wurf[], int size){
int i;
for (i=0; i<size; i++){
printf("Würfel %i: ", i+1);
scanf("%i", &wurf[i]);
}
}
// switch int*
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Sort the array
void bubbleSort(int wurf[], int size)
{
int i, j;
for (i = 0; i < size-1; i++){
for (j = 0; j < size-i-1; j++){
if (wurf[j] > wurf[j+1])
{
swap(&wurf[j], &wurf[j+1]);
}
}
}
}
void arrayausgabe(int wurf[], int size){
int u = 0;
for (u=0;u<size;u++) {
printf("array nummer %i: ", u); // err with "&"
printf("%i \n", wurf[u]);
}
}
void bewertung(int wurf[]){
int *x;
x = wurf;
if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Grand");
}else if (*x==*(x+1)==*(x+2)==*(x+3) || *(x+1)==*(x+2)==*(x+3)==*(x+4)) {
printf("Poker");
}else if ((*x==*(x+1)==*(x+2) && *(x+3)==*(x+4) )||(*(x+2)==*(x+3)==*(x+4) && *x==*(x+1))) {
printf("Full House");
}else {
printf("HAAA Verloren");
}
}
int main() {
int wurf[5];
printf("Programm Würfelspiel\nGrand\tgleiche Augenzahl auf allen 5 Würfeln\nPoker\tgleiche Augenzahl auf 4 Würfeln\nFull House\tgleiche und 2 gleiche Augenzahlen\n\nBitte gibt deine gewürfelten zahlen ein\n");
abfrage(wurf, 5);
bubbleSort(wurf, 5);
arrayausgabe(wurf, 5);
bewertung(wurf);
}
我是大一新生,如果代码看起来有点垃圾,请见谅
这个逻辑:
if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
在 C 中没有达到您的期望(尽管在 Python 中做到了):根据值是否不同,与 ==
returns 的比较为 0 或 1或相等。链接它们并不会检查它们是否彼此相等;相反,它检查前两个是否相等,然后将相等的结果(可以是 0 或 1)与第三个值的值进行比较,依此类推。
您需要分别检查每个比较:
if (wurf[0] == wurf[1] && wurf[0] == wurf[2] && wurf[0] == wurf[3] && wurf[0] == wurf[4]) {