Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个用C编写的小程序,用if,else查找四个数字中最大的一个_C - Fatal编程技术网

一个用C编写的小程序,用if,else查找四个数字中最大的一个

一个用C编写的小程序,用if,else查找四个数字中最大的一个,c,C,我不熟悉C编程,也不熟悉“编程”。到目前为止还不到一个月。我制作了一个小程序,可以打印四个输入数字中最大和最小的整数。有没有使用基本c编程方法的最短方法 #include <stdio.h> main() { int a, b, c, d, y; printf("Enter four integers (separate them with spaces): "); scanf("%d %d %d %d", &a, &b, &

我不熟悉C编程,也不熟悉“编程”。到目前为止还不到一个月。我制作了一个小程序,可以打印四个输入数字中最大和最小的整数。有没有使用基本c编程方法的最短方法

#include <stdio.h>

main()
{   
    int a, b, c, d, y;

    printf("Enter four integers (separate them with spaces): ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    if (a>b && a>c && a>d){
      if (b<c && b<d){
        y = b;
      }
      else if (c<b && c<d){
        y = c;
      }
      else if (d<b && d<c){
        y = d;
      }

      printf("Largest: %d\n", a);
      printf("Smallest: %d", y);
    }
    else if (b>a && b>c && b>d) {
      if (a<c && a<d){
        y = a;
      }
      else if(c<a && c<d){
        y = c;
      }
      else if(d<a && d<c){
        y = d;
      }

      printf("Largest: %d\n", b);
      printf("Smallest: %d", y);
    }
    else if (c>a && c>b && c>d)
    {
      if (a<b && a<d){
        y = a;
      }
      else if(b<a && b<d){
        y = b;
      }
      else if(d<a && d<b){
        y = d;
      }

      printf("Largest: %d\n", c);
      printf("Smallest: %d", y);
    }
    else if (d>a && d>b && d>c) {
      if (a<b && a<c){
        y = a;
      }
    else if(b<a && b<c){
      y = b;
    }
    else if(c<a && c<b){
      y = c;
    }

    printf("Largest: %d\n", d);
    printf("Smallest: %d", y);
  }

  return 0; 
}
#包括
main()
{   
INTA,b,c,d,y;
printf(“输入四个整数(用空格分隔):”;
scanf(“%d%d%d%d”、&a、&b、&c、&d);
如果(a>b&&a>c&&a>d){
如果(b)
有没有使用基本c编程方法的最短方法

#include <stdio.h>

main()
{   
    int a, b, c, d, y;

    printf("Enter four integers (separate them with spaces): ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    if (a>b && a>c && a>d){
      if (b<c && b<d){
        y = b;
      }
      else if (c<b && c<d){
        y = c;
      }
      else if (d<b && d<c){
        y = d;
      }

      printf("Largest: %d\n", a);
      printf("Smallest: %d", y);
    }
    else if (b>a && b>c && b>d) {
      if (a<c && a<d){
        y = a;
      }
      else if(c<a && c<d){
        y = c;
      }
      else if(d<a && d<c){
        y = d;
      }

      printf("Largest: %d\n", b);
      printf("Smallest: %d", y);
    }
    else if (c>a && c>b && c>d)
    {
      if (a<b && a<d){
        y = a;
      }
      else if(b<a && b<d){
        y = b;
      }
      else if(d<a && d<b){
        y = d;
      }

      printf("Largest: %d\n", c);
      printf("Smallest: %d", y);
    }
    else if (d>a && d>b && d>c) {
      if (a<b && a<c){
        y = a;
      }
    else if(b<a && b<c){
      y = b;
    }
    else if(c<a && c<b){
      y = c;
    }

    printf("Largest: %d\n", d);
    printf("Smallest: %d", y);
  }

  return 0; 
}
根据您的问题,循环的
将完成以下工作:

#include<stdio.h>

int main (void){
    int array[] = {1,6,15,9};
    int length = sizeof array / sizeof array[0];
    int big, small;

    big=small=array[0];
    for (int i = 0; i < length; i++){
        if(array[i]>big){
            big=array[i];
        }

        if(array[i]<small){
            small=array[i];
        }
    }

    printf("The biggest Number is:\t%d\n",big);
    printf("The smallest Number is:\t%d\n",small);

    return 0;
}

无论如何,我认为这是一个问题,但事实并非如此。

下面是一个简单的方法

#include <stdio.h>

int main( void )
{   
    int a, b, c, d;
    int largest, smallest;

    printf( "Enter four integers (separate them with spaces): " );
    scanf( "%d %d %d %d", &a, &b, &c, &d );


    largest = smallest = a;

    if ( largest < b )
    {
        largest = b;
    }
    else if ( b < smallest )
    {
        smallest = b;
    }
    if ( largest < c )
    {
        largest = c;
    }
    else if ( c < smallest )
    {
        smallest = c;
    }
    if ( largest < d )
    {
        largest = d;
    }
    else if ( d < smallest )
    {
        smallest = d;
    }

    printf( "\nLargest: %d\n", largest );
    printf( "Smallest: %d", smallest );

    return 0;
}
#include <stdio.h>

int main( void )
{   
    int a, b, c, d;

    printf( "Enter four integers (separate them with spaces): " );
    scanf( "%d %d %d %d", &a, &b, &c, &d );

    if ( !( a < b || a < c || a < d ) )
    {        
        printf( "\nLargest: %d\n", a );
    }        
    else if ( !( b < c || b < d ) )
    {        
        printf( "\nLargest: %d\n", b );
    }        
    else if ( !( c < d ) )
    {        
        printf( "\nLargest: %d\n", c );
    }        
    else 
    {        
        printf( "\nLargest: %d\n", d );
    }        

    if ( !( b < a || c < a || d < a ) )
    {        
        printf( "Smallest: %d", a );
    }        
    else if ( !( c < b || d < b ) )
    {        
        printf( "Smallest: %d", b );
    }        
    else if ( !( d < c ) )
    {        
        printf( "Smallest: %d", c );
    }        
    else 
    {        
        printf( "Smallest: %d", d );
    }        

    return 0;
}
然后输出将是

Enter four integers (separate them with spaces): 3 2 1 4

Largest: 4
Smallest: 1
另一种方法如下

#include <stdio.h>

int main( void )
{   
    int a, b, c, d;
    int largest, smallest;

    printf( "Enter four integers (separate them with spaces): " );
    scanf( "%d %d %d %d", &a, &b, &c, &d );


    largest = smallest = a;

    if ( largest < b )
    {
        largest = b;
    }
    else if ( b < smallest )
    {
        smallest = b;
    }
    if ( largest < c )
    {
        largest = c;
    }
    else if ( c < smallest )
    {
        smallest = c;
    }
    if ( largest < d )
    {
        largest = d;
    }
    else if ( d < smallest )
    {
        smallest = d;
    }

    printf( "\nLargest: %d\n", largest );
    printf( "Smallest: %d", smallest );

    return 0;
}
#include <stdio.h>

int main( void )
{   
    int a, b, c, d;

    printf( "Enter four integers (separate them with spaces): " );
    scanf( "%d %d %d %d", &a, &b, &c, &d );

    if ( !( a < b || a < c || a < d ) )
    {        
        printf( "\nLargest: %d\n", a );
    }        
    else if ( !( b < c || b < d ) )
    {        
        printf( "\nLargest: %d\n", b );
    }        
    else if ( !( c < d ) )
    {        
        printf( "\nLargest: %d\n", c );
    }        
    else 
    {        
        printf( "\nLargest: %d\n", d );
    }        

    if ( !( b < a || c < a || d < a ) )
    {        
        printf( "Smallest: %d", a );
    }        
    else if ( !( c < b || d < b ) )
    {        
        printf( "Smallest: %d", b );
    }        
    else if ( !( d < c ) )
    {        
        printf( "Smallest: %d", c );
    }        
    else 
    {        
        printf( "Smallest: %d", d );
    }        

    return 0;
}

这种带有函数的方法更具动态性、可重用性和可读性:

int max(int* arr ,int n) {

    if (n < 0)
        return 0;

    int ans = arr[0];
    for (int i=1; i<n; i++) {
        if (arr[i]>ans) {
            ans = arr[i];
        }
    }

    return ans;
}

int min(int* arr ,int n) {

    if (n < 0)
        return 0;

    int ans = arr[0];
    for (int i=1; i<n; i++) {
        if (arr[i]<ans) {
            ans = arr[i];
        }
    }

    return ans;
}

int main()
{
    int a, b, c, d;

    printf("Enter four integers (separate them with spaces): ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    int arr[] = {a, b, c, d};
    printf("Largest: %d\n", max(arr, 4));
    printf("Smallest: %d", min(arr, 4));

    return 0; 
}
int max(int*arr,int n){
if(n<0)
返回0;
int ans=arr[0];
for(int i=1;ians){
ans=arr[i];
}
}
返回ans;
}
最小整数(整数*arr,整数n){
if(n<0)
返回0;
int ans=arr[0];

for(inti=1;i欢迎来到编程世界。你会喜欢这里的:)

在逻辑思维中,您应该始终尝试想出一个易于扩展的通用解决方案

最大值问题的一种更通用的方法是任意设置最大值,然后使用循环比较所有其他值

将数字放入一个数组中

int array[] = {a,b,c,d};
将最大值和最小值任意设置为数组的第一个元素

int max = array[0];
int min = array[0];
将循环运行到数组的长度,并将每个元素与最大值进行比较。如果其大于最大值,则更新最大值。同样,将每个元素与最小值进行比较,如果其小于最小值,则更新最小值

int length = sizeof array / sizeof array[0];
for (int n = 0; n < length; n++)
    {
       if(array[n]>max)
       {
        max = array[i];
       }
       if(array[n]<min)
       {
        min = array[i];
       }
    }
printf("Maximum Number:\t%d\n",max);
printf("Minimum Number:\t%d\n",min);
int length=sizeof数组/sizeof数组[0];
对于(int n=0;n最大值)
{
max=数组[i];
}

如果(array[n]以下代码包含错误检查,并且是功能的一个简短实现,而不涉及新程序员难以理解的代码

#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <limits.h> // INT_MAX, INT_MIN

#define MAX_INPUTS (4)

int main( void )
{
    // declare array of MAX_INPUTS integers
    int myInputs[ MAX_INPUTS];

    // for flexibility, the printf() and scanf() could be in a loop
    // so most any number (defined in MAX_INPUTS) could be used
    // with out changing anything but the #define statement
    printf("Enter four integers (separate them with spaces): ");
    if( 4 != scanf("%d %d %d %d", &myInputs[0], &myInputs[1], &myInputs[2], &myInputs[3]) )
    { // then scanf failed
        // output error message, including the system error message to stderr
        perror( "scanf for 4 inputs failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    int minValue = INT_MAX;
    int maxValue = INT_MIN;

    for( size_t i=0; i< MAX_INPUTS; i++ )
    {
        if( myInputs[i] > maxValue) maxValue = myInputs[i];
        if( myInputs[i] < minValue) minValue = myInputs[i];
    }

      printf("Largest: %d\n", maxValue);
      printf("Smallest: %d\n", minValue);

    return 0;
} // end function: main
#包括
#包括//退出(),退出失败
#包括//INT\u MAX,INT\u MIN
#定义最大输入(4)
内部主(空)
{
//声明最大输入整数的数组
int myInputs[MAX_INPUTS];
//为了灵活性,printf()和scanf()可以在一个循环中
//因此,可以使用大多数任意数字(在MAX_输入中定义)
//除了#define语句之外,不做任何更改
printf(“输入四个整数(用空格分隔):”;
如果(4!=scanf(“%d%d%d%d”、&myInputs[0]、&myInputs[1]、&myInputs[2]、&myInputs[3]))
{//然后scanf失败了
//向stderr输出错误消息,包括系统错误消息
perror(“4个输入的扫描失败”);
退出(退出失败);
}
//否则,scanf将成功
int minValue=int_MAX;
int maxValue=int_MIN;
对于(大小i=0;imaxValue)maxValue=myInputs[i];
如果(myInputs[i]
查看数组和循环。尝试用20个元素而不是4个元素来实现这一点,您会发现您的解决方案无法扩展。如果此代码正常工作,那么问题可能属于逻辑思维良好的问题,重要的第一步是:)代码没有干净地编译。使用操作系统时,来自main的返回类型()始终是
int
。编译时,始终启用所有警告,然后修复这些警告。(对于gcc,至少使用:
-Wall-Wextra-pedantic
(除了这些参数外,我还使用:
-Wconversion-std=c99
)我投票结束这个问题,因为关于工作代码、寻求改进建议的问题更适合解决。在涉及指针的情况下,您只需使用一个函数就可以完成所有这些。
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <limits.h> // INT_MAX, INT_MIN

#define MAX_INPUTS (4)

int main( void )
{
    // declare array of MAX_INPUTS integers
    int myInputs[ MAX_INPUTS];

    // for flexibility, the printf() and scanf() could be in a loop
    // so most any number (defined in MAX_INPUTS) could be used
    // with out changing anything but the #define statement
    printf("Enter four integers (separate them with spaces): ");
    if( 4 != scanf("%d %d %d %d", &myInputs[0], &myInputs[1], &myInputs[2], &myInputs[3]) )
    { // then scanf failed
        // output error message, including the system error message to stderr
        perror( "scanf for 4 inputs failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    int minValue = INT_MAX;
    int maxValue = INT_MIN;

    for( size_t i=0; i< MAX_INPUTS; i++ )
    {
        if( myInputs[i] > maxValue) maxValue = myInputs[i];
        if( myInputs[i] < minValue) minValue = myInputs[i];
    }

      printf("Largest: %d\n", maxValue);
      printf("Smallest: %d\n", minValue);

    return 0;
} // end function: main