如何将排序的结构返回到主函数?

时间:2021-01-11 07:33:18

Goal: Sort a Struct of 3 Dice and Return Sorted

目标:对3个骰子的结构进行排序并返回已排序

#include <stdio.h>
#include <string.h>
#include <time.h>  

Struct

结构

struct RolledDice
{
    int die1 ;
    int die2 ;
    int die3 ;
} dice;

Prototype

原型

void sort_dice(struct RolledDice dice );

Main

主要

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

sort_dice(dice);

return 0;
}

Sort Dice Function

排序骰子功能

void sort_dice(struct RolledDice dice) {

    printf("Die 1: %d \n", dice.die1);
    printf("Die 2: %d \n", dice.die2);
    printf("Die 3: %d \n\n", dice.die3);

    int tempDie = 0;

(There may be a better way to do this......but this is the best I could come up with)

(可能有更好的方法来做到这一点......但这是我能想到的最好的方法)

    while ( dice.die1 < dice.die2 || dice.die1 < dice.die3 || dice.die2 < dice.die3 )
    {
        if ( dice.die1 < dice.die2 )
         {
         tempDie = dice.die1 ;
         dice.die1 = dice.die2 ;
         dice.die2 = tempDie ;
         }
        if ( dice.die1 < dice.die3 )
         {
         tempDie = dice.die1 ;
         dice.die1 = dice.die3 ;
         dice.die3 = tempDie ;
         }
        if (dice.die2 < dice.die3 )
         {
         tempDie = dice.die2 ;
         dice.die2 = dice.die3 ;
         dice.die3 = tempDie ;
         }
    }

    printf( "Die 1: %d \n", dice.die1 );
    printf( "Die 2: %d \n", dice.die2 );
    printf( "Die 3: %d \n\n", dice.die3 );
}

I tried changing the void to int and struct but kept getting errors, or it wouldn't update the struct in the main.

我尝试将void更改为int和struct但不断出现错误,或者它不会更新main中的struct。

1 个解决方案

#1


2  

You're sorting a copy of your dice structure, which is lost as soon as it goes out of scope when the function returns.

你正在对你的骰子结构进行排序,当函数返回时,它会在超出范围时丢失。

change your function to:

将您的功能更改为:

struct RolledDice sort_dice(struct RolledDice dice) {

and in the end just return dice

最后只是返回骰子

usage:

用法:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

dice = sort_dice(dice);

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );  
return 0;
}

Or pass dice as a pointer and use dice-> instead of dice. in your function (heavier refactoring but less memory copy and thus more performant)

或者将骰子作为指针传递并使用骰子 - >而不是骰子。在你的函数中(更重的重构但更少的内存副本,因此更高性能)

void sort_dice(struct RolledDice *dice) {
...
 dice->die1 = dice->die3 ;
...

usage:

用法:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

sort_dice(&dice);  // pass as pointer so it can be modified in the function

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );
return 0;
}

note: your original code prints the bubble-sorted values correctly within your sort routine. I suppose that the problem is that you did not find a way to update it in the caller function (creating a function to just print sorted values is useless)

注意:您的原始代码会在排序例程中正确打印出气泡排序值。我想问题是你没有找到在调用函数中更新它的方法(创建一个只打印排序值的函数是没用的)

#1


2  

You're sorting a copy of your dice structure, which is lost as soon as it goes out of scope when the function returns.

你正在对你的骰子结构进行排序,当函数返回时,它会在超出范围时丢失。

change your function to:

将您的功能更改为:

struct RolledDice sort_dice(struct RolledDice dice) {

and in the end just return dice

最后只是返回骰子

usage:

用法:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

dice = sort_dice(dice);

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );  
return 0;
}

Or pass dice as a pointer and use dice-> instead of dice. in your function (heavier refactoring but less memory copy and thus more performant)

或者将骰子作为指针传递并使用骰子 - >而不是骰子。在你的函数中(更重的重构但更少的内存副本,因此更高性能)

void sort_dice(struct RolledDice *dice) {
...
 dice->die1 = dice->die3 ;
...

usage:

用法:

int main() {
srand ( time(NULL) );

dice.die1 = rand() % 6 + 1 ;
dice.die2 = rand() % 6 + 1 ;
dice.die3 = rand() % 6 + 1 ;

sort_dice(&dice);  // pass as pointer so it can be modified in the function

// print the sorted struct here
printf( "Die 1: %d \n", dice.die1 );
printf( "Die 2: %d \n", dice.die2 );
printf( "Die 3: %d \n\n", dice.die3 );
return 0;
}

note: your original code prints the bubble-sorted values correctly within your sort routine. I suppose that the problem is that you did not find a way to update it in the caller function (creating a function to just print sorted values is useless)

注意:您的原始代码会在排序例程中正确打印出气泡排序值。我想问题是你没有找到在调用函数中更新它的方法(创建一个只打印排序值的函数是没用的)