C语言三个数求最大值与最小值
#include<stdio.h>/*Getthemaxvaluefrom3numbers*/intGetMax(intn1,intn2,intn3){/********...
#include <stdio.h>
/*Get the max value from 3 numbers*/
int GetMax(int n1, int n2, int n3)
{
/*************Coding here****************/
}
/*Get the min value from 3 numbers*/
int GetMin(int n1, int n2, int n3)
{
/*************Coding here***************/
}
int main()
{
FILE *fpIn;
FILE *fpOut;
int n1, n2, n3;
int max, min;
fpIn = fopen("in2.dat", "rb");
if(fpIn == NULL)
{
printf("Error: Can not open in2.dat file.\n");
}
fread(&n1, sizeof(n1), 1, fpIn);
fread(&n2, sizeof(n2), 1, fpIn);
fread(&n3, sizeof(n3), 1, fpIn);
max = GetMax(n1, n2, n3);
min = GetMin(n1, n2, n3);
fpOut = fopen("out2.dat", "w");
if(fpOut == NULL)
{
printf("Error: Can not open out2.dat file.\n");
}
fprintf(fpOut, "%d\n", max);
fprintf(fpOut, "%d\n", min);
fclose(fpIn);
fclose(fpOut);
return 0;
} 展开
/*Get the max value from 3 numbers*/
int GetMax(int n1, int n2, int n3)
{
/*************Coding here****************/
}
/*Get the min value from 3 numbers*/
int GetMin(int n1, int n2, int n3)
{
/*************Coding here***************/
}
int main()
{
FILE *fpIn;
FILE *fpOut;
int n1, n2, n3;
int max, min;
fpIn = fopen("in2.dat", "rb");
if(fpIn == NULL)
{
printf("Error: Can not open in2.dat file.\n");
}
fread(&n1, sizeof(n1), 1, fpIn);
fread(&n2, sizeof(n2), 1, fpIn);
fread(&n3, sizeof(n3), 1, fpIn);
max = GetMax(n1, n2, n3);
min = GetMin(n1, n2, n3);
fpOut = fopen("out2.dat", "w");
if(fpOut == NULL)
{
printf("Error: Can not open out2.dat file.\n");
}
fprintf(fpOut, "%d\n", max);
fprintf(fpOut, "%d\n", min);
fclose(fpIn);
fclose(fpOut);
return 0;
} 展开
3个回答
展开全部
#include <stdio.h>
int main()
{
int a = 2, b = 100, c = 3, t;
if(a > b) {t = a; a = b; b = t;}
if(a > c) {t = a; a = c; c = t;}
if(b > c) {t = b; b = c; c = t;}
printf("最小值:%d 最大值:%d\n", a , c);
return 0;
}
依次比较,让a保存最小值,让b保存最大值。
展开全部
int GetMax(int n1, int n2, int n3)
{
int maxv = n1;
if( n1 < n2 ) maxv = n2;
if( maxv < n3 ) maxv = n3;
return maxv;
}
int GetMin(int n1, int n2, int n3)
{
int minv = n1;
if( n1 > n2 ) minv = n2;
if( minv > n3 ) minv = n3;
return minv;
}
{
int maxv = n1;
if( n1 < n2 ) maxv = n2;
if( maxv < n3 ) maxv = n3;
return maxv;
}
int GetMin(int n1, int n2, int n3)
{
int minv = n1;
if( n1 > n2 ) minv = n2;
if( minv > n3 ) minv = n3;
return minv;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*Get the max value from 3 numbers*/
int GetMax(int n1, int n2, int n3)
{
return n1 > n2 ? (n1 > n3 ? n1 : (n2 > n3 ? n2 : n3)) : (n2 > n3 ? n2 : (n1 > n3 ? n1 : n3));
}
/*Get the min value from 3 numbers*/
int GetMin(int n1, int n2, int n3)
{
return n1 < n2 ? (n1 < n3 ? n1 : (n2 < n3 ? n2 : n3)) : (n2 < n3 ? n2 : (n1 < n3 ? n1 : n3));
}
int GetMax(int n1, int n2, int n3)
{
return n1 > n2 ? (n1 > n3 ? n1 : (n2 > n3 ? n2 : n3)) : (n2 > n3 ? n2 : (n1 > n3 ? n1 : n3));
}
/*Get the min value from 3 numbers*/
int GetMin(int n1, int n2, int n3)
{
return n1 < n2 ? (n1 < n3 ? n1 : (n2 < n3 ? n2 : n3)) : (n2 < n3 ? n2 : (n1 < n3 ? n1 : n3));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询