我制作了一个程序,所以当用户选择选项1时,他们可以输入分子然后输入分母,如果他们选择选项2,它将打印该分数。但是我希望它以混合形式打印分数。例如,如果我做了20/3,它将打印为6 2/3。关于我如何解决这个问题的任何帮助?
这是我的代码(希望很容易阅读(:)
#include <stdio.h>
#include <stdlib.h>
//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;
int main()
{
//Array of 100 fractions
fraction arrFraction[100];
int i = 0, j, num = 1;
//Loop till user want to stop
while (num == 1)
{
int choice;
printf("\nPress 1 to enter a fraction\n");
printf("Press 2 to view stored fractions\n");
scanf("%d", &choice);
if(choice == 1)
{
//Prompting user
printf("\nEnter your fraction, numerator followed by denominator\n ");
//Reading values from user
scanf("%d %d", &arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
}
if (choice == 2) {
for (j = 0; j < i; j++)
{
//Printing fractions
printf("\n %d / %d \n", arrFraction[j].numerator, arrFraction[j].denom);
}
}
}//end of while loop
return(0);
}
答案 0 :(得分:0)
您可以使用除法(/)和模数(%)运算符。
printf("%d %d/%d",
arrFraction[j].numerator / arrFraction[j].denom ,
arrFraction[j].numerator / arrFraction[j].denom ,
arrFraction[j].denom
);