OverIQ 中文系列教程(九)

原文:OverIQ Tutorials

协议:CC BY-NC-SA 4.0

C 程序:将十进制数转换成八进制数

原文:https://overiq.com/c-examples/c-program-to-convert-a-decimal-number-to-an-octal-number/

最后更新于 2020 年 9 月 24 日


下面是一个将十进制数转换成八进制数的 C 程序:

/*******************************************************
 Program to convert a decimal number to an octal number
 ******************************************************/

#include<stdio.h> // include stdio.h library
#include<math.h> // include math.h library

int main(void)
{
    
       
    long long num, oct = 0;    
    int i = 0, rem;

    printf("Enter a decimal number: ");
    scanf("%lld", &num);      

    while(num != 0)
    {
    
    
        rem = num % 8;  // get the last digit
        oct = rem * (long long)pow(10, i++) + oct;  
        num /= 8;  // get the quotient
    }

    printf("0o");

    printf("%lld", oct);        

    return 0; // return 0 to operating system
}

现在试试

**预期输出:**第一次运行:

Enter a decimal number: 74
0o112

第二次运行:

Enter a decimal number: 2545
0o4761

它是如何工作的

要将十进制数转换为八进制数,我们遵循以下步骤:

第一步:十进制数连续除以 8,余数写在被除数的右边。我们重复这个过程,直到得到商 0。

第二步:从下往上写剩余部分。

让我们举一些例子:

示例 1 :将十进制125转换为八进制数:

第一步:

剩余物
125/8 15 5
15/8 1 7
1/2 0 1

第二步:

(\ matht { 125 _ { 10 } = 175 _ { 8 } } )

示例 2 :将十进制500转换为八进制数:

第一步:

剩余物
500/8 62 4
62/8 7 6
1/2 0 7

第二步:

(\mathtt{500_{10} = 764_{8}})

下表显示了循环每次迭代时发生的情况(假设num = 74):

循环 雷姆 容器 数字
第一次迭代后 rem=74%8=2 oct=2*(10^0)+0=2 num=74/8=9 i=2
第二次迭代后 rem=9%8=1 oct=1*(10^1)+2=12 num=9/8=1 i=3
第三次迭代后 rem=1%8=1 oct=1*(10^2)+12=112 num=1/8=0 i=4

推荐阅读



C 程序:将二进制数转换成十进制数

原文:https://overiq.com/c-examples/c-program-to-convert-a-binary-number-to-a-decimal-number/

最后更新于 2020 年 9 月 24 日


下面是一个将二进制数转换成十进制数的 C 程序。

/*******************************************************
 * Program to convert a binary number to decimal number
********************************************************/

#include<stdio.h> // include stdio.h
#include<math.h> // include stdio.h

int main()
{
    
    
    long int bin, remainder, decimal = 0, i = 0;       

    printf("Enter a binary number: ");
    scanf("%d", &bin);

    while(bin != 0)
    {
    
    
        remainder = bin % 10;
        decimal += remainder * (int)pow(2, i++);
        bin = bin / 10;        
    }

    printf("Decimal: %d", decimal);    

    return 0;
}

现在试试

**预期输出:**第一次运行:

Enter a binary number: 100
Decimal: 4

第二次运行:

Enter a binary number: 1000011
Decimal: 67

它是如何工作的

以下是将二进制数转换为十进制数的步骤:

例 1 :将二进制数100转换为十进制等效值。

=> ( 1 * 2^2 ) + ( 0 * 2^1 ) + ( 0 * 2^0 )
=> 4 + 0 + 0
=> 4

例 2 :将二进制数1001转换为其十进制等效值。

=> ( 1 * 2^3 ) + ( 0 * 2^2 ) + ( 0 * 2^1 ) + ( 1 * 2^0 )
=> 8 + 0 + 0 + 1
=> 9

下表显示了循环每次迭代时发生的情况(假设bin = 10101):

循环 剩余物 小数 容器
第一次迭代后 remainder = 10101 % 10 = 1 decimal = 0 + 1 * (2^0) = 1 bin = 10101 / 10 = 1010
第二次迭代后 remainder = 1010 % 10 = 0 decimal = 1 + 0 * (2^1) = 1 bin = 1010 / 10 = 101
第三次迭代后 remainder = 101 % 10 = 1 decimal = 1 + 1 * (2^2) = 5 bin = 101 / 10 = 10
第四次迭代后 remainder = 10 % 10 = 0 decimal = 5 + 0 * 2^3 = 5 bin = 10 / 10 = 1
第五次迭代后 remainder = 1 % 10 = 1 decimal = 5 + 1 * (2^4) = 21 bin = 1 / 10 = 0


C 程序:将华氏温度转换为摄氏温度

原文:https://overiq.com/c-examples/c-program-to-convert-the-temperature-in-fahrenheit-to-celsius/

最后更新于 2020 年 9 月 24 日


要将华氏温度转换为摄氏温度,我们使用以下公式:

[
cel = \ frac { 5 } { 9 }+[fah-32]t1]]

以下是将华氏温度转换为摄氏温度的 C 程序:

/*****************************************************************
 * C Program to convert the temperature in Fahrenheit to Celsius
 * 
 * Formula used: c = (5/9) * (f - 32)
******************************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    float fah, cel;

    printf("Enter a temp in fah: ");
    scanf("%f", &fah);

    cel = (5.0/9) * (fah - 32);

    printf("%.2f°F is same as %.2f°C", fah, cel);

    return 0;
}

现在试试

**预期输出:**第一次运行:

Enter a temp in fah: 455
455.00°F is same as 235.00°C

第二次运行:

Enter a temp in fah: 32
32.00°F is same as 0.00°C



C 程序:将十进制数转换成罗马数字

原文:https://overiq.com/c-examples/c-program-to-convert-a-decimal-number-to-roman-numerals/

最后更新于 2020 年 9 月 24 日


下面是一个将十进制数转换成罗马数字的 C 程序:

/******************************************************
 Program to convert a decimal number to roman numerals
 * 
 * Enter a number: 1996
 * Roman numerals: mmxii
 * 
 ******************************************************/

#include <stdio.h> 

int main(void) 
{
    
       
    int num, rem;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("Roman numerals: ");        

    while(num != 0)
    {
    
    

        if (num >= 1000)       // 1000 - m
        {
    
    
           printf("m");
           num -= 1000;
        }

        else if (num >= 900)   // 900 -  cm
        {
    
    
           printf("cm");
           num -= 900;
        }        

        else if (num >= 500)   // 500 - d
        {
    
               
           printf("d");
           num -= 500;
        }

        else if (num >= 400)   // 400 -  cd
        {
    
    
           printf("cd");
           num -= 400;
        }

        else if (num >= 100)   // 100 - c
        {
    
    
           printf("c");
           num -= 100;                       
        }

        else if (num >= 90)    // 90 - xc
        {
    
    
           printf("xc");
           num -= 90;                                              
        }

        else if (num >= 50)    // 50 - l
        {
    
    
           printf("l");
           num -= 50;                                                                     
        }

        else if (num >= 40)    // 40 - xl
        {
    
    
           printf("xl");           
           num -= 40;
        }

        else if (num >= 10)    // 10 - x
        {
    
    
           printf("x");
           num -= 10;           
        }

        else if (num >= 9)     // 9 - ix
        {
    
    
           printf("ix");
           num -= 9;                         
        }

        else if (num >= 5)     // 5 - v
        {
    
    
           printf("v");
           num -= 5;                                     
        }

        else if (num >= 4)     // 4 - iv
        {
    
    
           printf("iv");
           num -= 4;                                                            
        }

        else if (num >= 1)     // 1 - i
        {
    
    
           printf("i");
           num -= 1;                                                                                   
        }

    }

    return 0;
}

现在试试

**预期输出:**第一次运行:

Enter a number: 99
Roman numerals: xcix

第二次运行:

Enter a number: 2020
Roman numerals: mmxx

它是如何工作的

下表列出了一些十进制数字及其对应的罗马数字:

小数 罗马数字
one
four 静脉的
five V
nine 离子交换
Ten X
Forty 特大号
Fifty L
Ninety 容抗
One hundred C
four hundred 激光唱片
Five hundred D
Nine hundred 厘米
One thousand M

要将十进制数字num转换为罗马数字,我们执行以下操作:

  1. 在上表中找出小于或等于小数num的最大小数r
  2. 写出十进制数字r对应的罗马数字。
  3. num中减去r,并将其分配回num,即num = num - r
  4. 重复步骤 1、2、3,直到num降为0

举个例子吧。

示例:将十进制 123 转换为罗马数字

循环 数字 r 罗马数字 数字的新值
第一次迭代后 123 100 c num=123-100=23
第二次迭代后 23 10 x num=23-10=13
第三次迭代后 13 10 x num=13-10=3
第四次迭代后 3 1 i num=3-1=2
第五次迭代后 2 1 i num=2-1=1
第 6 次迭代后 1 1 i num=1-1=0

于是,123 = cxiii



C 程序:检查一年是否是闰年

原文:https://overiq.com/c-examples/c-program-to-check-whether-a-year-is-a-leap-year/

最后更新于 2020 年 9 月 24 日


下面是一个 C 程序,它检查输入的年份是否是闰年。

/******************************************************************
 * Program to check whether the entered year is a leap year or not
 ******************************************************************/

#include<stdio.h> // include stdio.h

int main() {
    
    
    int year;    

    printf("Enter a year: ");
    scanf("%d", &year);

    if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
    {
    
    
        printf("%d is a leap year", year);
    }

    else
    {
    
    
        printf("%d is not a leap year", year);
    }

    return 0;
}

现在试试

**预期输出:**第一次运行:

Enter a year: 2000
2000 is a leap year

第二次运行:

Enter a year: 1900
1900 is not a leap year

它是如何工作的

为了确定一年是否是闰年,我们使用以下算法:

  1. 检查年份是否能被 4 整除。如果是,请转到步骤 2,否则,请转到步骤 3。
  2. 检查年份是否能被 100 整除。如果是,请转到第 3 步,否则,这一年是闰年
  3. 检查年份是否能被 400 整除。如果是,那么这一年就是闰年,否则,这一年就不是闰年。


C 程序:打印两个日期中较早的一个

原文:https://overiq.com/c-examples/c-program-to-print-the-earlier-of-the-two-dates/

最后更新于 2020 年 9 月 24 日


下面的 C 程序要求用户输入两个日期,并打印两个日期中较早的一个。

/**********************************************
 Program to print the earlier of the two dates 
 * 
 * Enter first date (MM/DD/YYYY): 20/10/2020
 * Enter second date (MM/DD/YYYY): 02/29/2001
 * 
 * 02/29/2001 comes earlier than 20/10/2020
 **********************************************/

#include<stdio.h> // include stdio.h library
int check_date(int date, int mon, int year);

int main(void)
{
    
    
    int day1, mon1, year1,
        day2, mon2, year2;

    int is_leap = 0, is_valid = 1;

    printf("Enter first date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon1, &day1, &year1);

    printf("Enter second date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon2, &day2, &year2);

    if(!check_date(day1, mon1, year1))
    {
    
    
        printf("First date is invalid.\n");        
    }

    if(!check_date(day2, mon2, year2))
    {
    
    
        printf("Second date is invalid.\n");
        exit(0);
    }

    if(year1 > year2)
    {
    
    
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
    }

    else if (year1 < year2)
    {
    
    
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
    }

    // year1 ==  year2
    else
    {
    
    
        if (mon1 ==  mon2)
        {
    
    
            if (day1 ==  day2)
            {
    
    
                printf("Both dates are the same.");
            }

            else if(day1 > day2)
            {
    
    
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
            }

            else
            {
    
    
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
            }
        }

        else if (mon1 > mon2)
        {
    
    
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
        }

        else 
        {
    
    
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
        }        
    }

    return 0; // return 0 to operating system
}

// function to check whether a date is valid or not

int check_date(int day, int mon, int year)    
{
    
    
    int is_valid = 1, is_leap = 0;

    if (year >= 1800 && year <= 9999) 
    {
    
    

        //  check whether year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
        {
    
    
            is_leap = 1;
        }

        // check whether mon is between 1 and 12
        if(mon >= 1 && mon <= 12)
        {
    
    
            // check for days in feb
            if (mon == 2)
            {
    
    
                if (is_leap && day == 29) 
                {
    
    
                    is_valid = 1;
                }
                else if(day > 28) 
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in April, June, September and November
            else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
            {
    
    
                if (day > 30)
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in rest of the months 
            // i.e Jan, Mar, May, July, Aug, Oct, Dec
            else if(day > 31)
            {
    
                
                is_valid = 0;
            }        
        }

        else
        {
    
    
            is_valid = 0;
        }

    }
    else
    {
    
    
        is_valid = 0;
    }

    return is_valid;

}

现在试试

**预期输出:**第一次运行:

Enter the first date (MM/DD/YYYY): 05/10/2016
Enter the second date (MM/DD/YYYY): 04/09/2016
04/09/2016 comes earlier than 05/10/2016

第二次运行:

Enter the first date (MM/DD/YYYY): 02/01/2008 
Enter the second date (MM/DD/YYYY): 02/01/2008
Both dates are the same.

它是如何工作的

程序首先要求用户输入两个日期。

在第 27 行和第 32 行,我们使用用户定义的check_date()函数检查输入的日期是否有效。要了解更多关于check_date()功能如何工作的信息,请访问此链接

如果其中一个日期无效,程序将终止,并显示相应的错误消息。

在第 39-85 行,我们有一个 if-else 语句来比较这两个日期。比较日期的算法如下:

  • 如果year1 > year2,第二次约会比第一次来得早。
  • 如果year1 < year2,第一次约会比第二次来得早。
  • 如果year1 == year2。以下情况是可能的:
    • 如果mon1 == mon2,可能出现以下情况:
      • 如果day1 == day2,两个日期相同。
      • 如果day1 > day2,那么第二次约会比第一次来得早。
      • 如果day1 < day2,那么第一次约会比第二次来得早。
    • 如果mon1 > mon2,第二次约会比第一次来得早。
    • 否则,mon1 < mon2和第一个日期比第二个来得早。


C 程序:打印两个日期中较早的一个

原文:https://overiq.com/c-examples/c-program-to-print-the-earlier-of-the-two-dates/

最后更新于 2020 年 9 月 24 日


下面的 C 程序要求用户输入两个日期,并打印两个日期中较早的一个。

/**********************************************
 Program to print the earlier of the two dates 
 * 
 * Enter first date (MM/DD/YYYY): 20/10/2020
 * Enter second date (MM/DD/YYYY): 02/29/2001
 * 
 * 02/29/2001 comes earlier than 20/10/2020
 **********************************************/

#include<stdio.h> // include stdio.h library
int check_date(int date, int mon, int year);

int main(void)
{
    
    
    int day1, mon1, year1,
        day2, mon2, year2;

    int is_leap = 0, is_valid = 1;

    printf("Enter first date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon1, &day1, &year1);

    printf("Enter second date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon2, &day2, &year2);

    if(!check_date(day1, mon1, year1))
    {
    
    
        printf("First date is invalid.\n");        
    }

    if(!check_date(day2, mon2, year2))
    {
    
    
        printf("Second date is invalid.\n");
        exit(0);
    }

    if(year1 > year2)
    {
    
    
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
    }

    else if (year1 < year2)
    {
    
    
        printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
    }

    // year1 ==  year2
    else
    {
    
    
        if (mon1 ==  mon2)
        {
    
    
            if (day1 ==  day2)
            {
    
    
                printf("Both dates are the same.");
            }

            else if(day1 > day2)
            {
    
    
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
            }

            else
            {
    
    
                printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
            }
        }

        else if (mon1 > mon2)
        {
    
    
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon2, day2, year2, mon1, day1, year1);
        }

        else 
        {
    
    
            printf("%02d/%02d/%d comes earlier than %02d/%02d/%d", 
                        mon1, day1, year1, mon2, day2, year2);
        }        
    }

    return 0; // return 0 to operating system
}

// function to check whether a date is valid or not

int check_date(int day, int mon, int year)    
{
    
    
    int is_valid = 1, is_leap = 0;

    if (year >= 1800 && year <= 9999) 
    {
    
    

        //  check whether year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
        {
    
    
            is_leap = 1;
        }

        // check whether mon is between 1 and 12
        if(mon >= 1 && mon <= 12)
        {
    
    
            // check for days in feb
            if (mon == 2)
            {
    
    
                if (is_leap && day == 29) 
                {
    
    
                    is_valid = 1;
                }
                else if(day > 28) 
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in April, June, September and November
            else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
            {
    
    
                if (day > 30)
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in rest of the months 
            // i.e Jan, Mar, May, July, Aug, Oct, Dec
            else if(day > 31)
            {
    
                
                is_valid = 0;
            }        
        }

        else
        {
    
    
            is_valid = 0;
        }

    }
    else
    {
    
    
        is_valid = 0;
    }

    return is_valid;

}

现在试试

**预期输出:**第一次运行:

Enter the first date (MM/DD/YYYY): 05/10/2016
Enter the second date (MM/DD/YYYY): 04/09/2016
04/09/2016 comes earlier than 05/10/2016

第二次运行:

Enter the first date (MM/DD/YYYY): 02/01/2008 
Enter the second date (MM/DD/YYYY): 02/01/2008
Both dates are the same.

它是如何工作的

程序首先要求用户输入两个日期。

在第 27 行和第 32 行,我们使用用户定义的check_date()函数检查输入的日期是否有效。要了解更多关于check_date()功能如何工作的信息,请访问此链接

如果其中一个日期无效,程序将终止,并显示相应的错误消息。

在第 39-85 行,我们有一个 if-else 语句来比较这两个日期。比较日期的算法如下:

  • 如果year1 > year2,第二次约会比第一次来得早。
  • 如果year1 < year2,第一次约会比第二次来得早。
  • 如果year1 == year2。以下情况是可能的:
    • 如果mon1 == mon2,可能出现以下情况:
      • 如果day1 == day2,两个日期相同。
      • 如果day1 > day2,那么第二次约会比第一次来得早。
      • 如果day1 < day2,那么第一次约会比第二次来得早。
    • 如果mon1 > mon2,第二次约会比第一次来得早。
    • 否则,mon1 < mon2和第一个日期比第二个来得早。


C 程序:计算两个年月日的日期之差

原文:https://overiq.com/c-examples/c-program-to-calculate-the-difference-of-two-dates-in-years-months-and-days/

最后更新于 2020 年 9 月 24 日


下面是一个计算年、月、日两个日期差的 C 程序。确保开始日期早于结束日期。

/*******************************************************************
 Program to calculate the number of days, months and years
 between two dates dates
 * 
 * Enter first date (MM/DD/YYYY): 05/20/2004
 * Enter second date (MM/DD/YYYY): 06/05/2008
 * 
 * Difference: 4 years 00 months and 15 days.
 *******************************************************************/

#include<stdio.h> // include stdio.h library
#include<stdlib.h> // include stdlib.h library
int valid_date(int date, int mon, int year);

int main(void)
{
    
    
    int day1, mon1, year1,
        day2, mon2, year2;

    int day_diff, mon_diff, year_diff;         

    printf("Enter start date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon1, &day1, &year1);

    printf("Enter end date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon2, &day2, &year2);

    if(!valid_date(day1, mon1, year1))
    {
    
    
        printf("First date is invalid.\n");        
    }

    if(!valid_date(day2, mon2, year2))
    {
    
    
        printf("Second date is invalid.\n");
        exit(0);
    }       

    if(day2 < day1)
    {
    
          
        // borrow days from february
        if (mon2 == 3)
        {
    
    
            //  check whether year is a leap year
            if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0)) 
            {
    
    
                day2 += 29;
            }

            else
            {
    
    
                day2 += 28;
            }                        
        }

        // borrow days from April or June or September or November
        else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12) 
        {
    
    
           day2 += 30; 
        }

        // borrow days from Jan or Mar or May or July or Aug or Oct or Dec
        else
        {
    
    
           day2 += 31;
        }

        mon2 = mon2 - 1;
    }

    if (mon2 < mon1)
    {
    
    
        mon2 += 12;
        year2 -= 1;
    }       

    day_diff = day2 - day1;
    mon_diff = mon2 - mon1;
    year_diff = year2 - year1;

    printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);

    return 0; // return 0 to operating system
}

// function to check whether a date is valid or not

int valid_date(int day, int mon, int year)    
{
    
    
    int is_valid = 1, is_leap = 0;

    if (year >= 1800 && year <= 9999) 
    {
    
    

        //  check whether year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
        {
    
    
            is_leap = 1;
        }

        // check whether mon is between 1 and 12
        if(mon >= 1 && mon <= 12)
        {
    
    
            // check for days in feb
            if (mon == 2)
            {
    
    
                if (is_leap && day == 29) 
                {
    
    
                    is_valid = 1;
                }
                else if(day > 28) 
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in April, June, September and November
            else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
            {
    
    
                if (day > 30)
                {
    
    
                    is_valid = 0;
                }
            }

            // check for days in rest of the months 
            // i.e Jan, Mar, May, July, Aug, Oct, Dec
            else if(day > 31)
            {
    
                
                is_valid = 0;
            }        
        }

        else
        {
    
    
            is_valid = 0;
        }

    }
    else
    {
    
    
        is_valid = 0;
    }

    return is_valid;

}

现在试试

预期输出:

第一次运行:

Enter start date (MM/DD/YYYY): 08/05/2001
Enter end date (MM/DD/YYYY): 08/20/2001
Difference: 0 years 00 months and 15 days.

第二次运行:

Enter start date (MM/DD/YYYY): 10/11/2005
Enter end date (MM/DD/YYYY): 05/20/2016
Difference: 10 years 07 months and 09 days.

它是如何工作的

用年、月、日来计算两个日期之差的过程很简单。我们需要做的就是分别从结束日期的日、月、年中减去开始日期的日、月、年。

date_diff = day2 - day1
mon_diff = mon2 - mon1
year_diff = year2 - year1

请注意,这里我们假设开始日期小于结束日期,并且天数、月数和年数的差值将为正值。

然而,有一个问题。

如果天数和月数的差异不是正值呢?

例如,考虑以下两个日期:

08/25/2001 (25 Aug 2001)
05/05/2005 (05 May 2005)

在这种情况下,第一个日期小于第二个日期,但天数和月数的差异不是正的。

date_diff = day2 - day1
date_diff = 5 - 25
date_diff = -20

mon_diff = mon2 - mon1
mon_diff = 5 - 8
mon_diff = -3

为了处理这种情况,我们执行以下操作:

如果day2 < day1,我们借用mon2之前的一个月,将该月的天数加到day2。比如mon2 == 09即 9 月,那么我们借月 8 月,把 8 月的天数加到day2上。

day2 = day2 + 30。(8 月有 30 天)

此外,由于我们借了一个月,我们必须从mon2中减去1

mon2 = m2 - 1

同样的,如果mon2 < mon1,那么我们借一个 1 年,也就是 12 个月,再加上这个多月到mon2

mon2 = mon + 12

同样,就像月份一样,我们必须从year中减去1

year2 = year2 - 1



C 程序:计算从日期开始的一年中的某一天

原文:https://overiq.com/c-examples/c-program-to-calculate-the-day-of-year-from-the-date/

最后更新于 2020 年 9 月 24 日


下面是一个从日期开始计算一年中某一天的 C 程序:

/*************************************************
 Program to calculate day of year from the date
 * 
 * Enter date (MM/DD/YYYY): 12/30/2006
 * Day of year: 364
 *  
 ************************************************/

#include<stdio.h> // include stdio.h library

int main(void)
{
    
    
    int day, mon, year, days_in_feb = 28, 
            doy;    // day of year

    printf("Enter date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon, &day, &year);

    doy = day;

    // check for leap year
    if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
    {
    
    
        days_in_feb = 29;
    }

    switch(mon)
    {
    
    
        case 2:
            doy += 31;
            break;
        case 3:
            doy += 31+days_in_feb;
            break;
        case 4:
            doy += 31+days_in_feb+31;
            break;
        case 5:
            doy += 31+days_in_feb+31+30;
            break;
        case 6:
            doy += 31+days_in_feb+31+30+31;
            break;
        case 7:
            doy += 31+days_in_feb+31+30+31+30;
            break;            
        case 8:
            doy += 31+days_in_feb+31+30+31+30+31;
            break;
        case 9:
            doy += 31+days_in_feb+31+30+31+30+31+31;
            break;
        case 10:
            doy += 31+days_in_feb+31+30+31+30+31+31+30;            
            break;            
        case 11:
            doy += 31+days_in_feb+31+30+31+30+31+31+30+31;            
            break;                        
        case 12:
            doy += 31+days_in_feb+31+30+31+30+31+31+30+31+30;            
            break;                                    
    }

    printf("Day of year: %d", doy);

    return 0; // return 0 to operating system
}

现在试试

**预期输出:**第一次运行:

Enter date (MM/DD/YYYY): 03/05/2000
Day of year: 65

第二次运行:

Enter date (MM/DD/YYYY): 12/25/2018
Day of year: 359

它是如何工作的

一年中的某一天是一个介于 1 和 365 之间的数字(如果是闰年,则为 366)。例如,1 月 1 日是第 1 天,2 月 5 日是第 36 天,以此类推。

为了计算一年中的某一天,我们只需将给定月份中的天数与前几个月中的天数相加。


推荐阅读:



C 程序:以有效形式打印日期

原文:https://overiq.com/c-examples/c-program-to-print-the-date-in-legal-form/

最后更新于 2020 年 9 月 24 日


下面是一个用有效格式打印日期的 C 程序:

/******************************************
 Program to print the date in legal form
 * 
 * Enter date(MM/DD/YYY): 05/25/2018
 * 25th May, 2018
 ******************************************/

#include<stdio.h> // include stdio.h library

int main(void)
{
    
           

    int day, mon, year;

    char *months[] = {
    
    
                        "January", "February", "March", "April",
                        "May", "June", "July", "August", 
                        "September", "October", "November", "December",
                      };

    printf("Enter date(MM/DD/YYY): ");
    scanf("%d/%d/%d", &mon, &day, &year);

    printf("%d", day);

    // if day is 1 or 21 or 31, add the suffix "st"
    if(day == 1 || day == 21 || day == 31)
    {
    
    
        printf("st ");
    }

    // if day is 2 or 22, add the suffix "nd"
    else if(day == 2 || day == 22)
    {
    
    
        printf("nd ");
    }

    // if day is 3 or 23, add the suffix "rd"
    else if(day == 3 || day == 23)
    {
    
    
        printf("rd ");
    }

    // for everything else add the suffix "th"
    else
    {
    
    
        printf("th ");
    }    

    printf("%s, %d", months[mon - 1], year);

    return 0;
}

现在试试

**预期输出:**第一次运行:

Enter date(MM/DD/YYY): 10/21/2005
21st October, 2005

第二次运行:

Enter date(MM/DD/YYY): 04/23/2012
23rd April, 2012

它是如何工作的

在非正式写作中,日期中的数字通常用/-隔开。例如:

05/11/15
05-11-2015

然而,在法律和正式文件中,日期是完整的。例如:

(21^{st})2012 年 12 月
(21^{st}),2012 年

以下是上述程序的工作原理:

  • 程序主体首先定义三个变量:daymonyear
  • 在第 16-20 行中,我们定义了一个字符指针数组,其中包含我们想要显示的月份名称。
  • 接下来,我们要求用户输入日期。
  • 第 27-49 行的 if-else 语句打印日后缀(即“st”、“nd”、“rd”或“th”)
  • 最后,第 51 行的打印语句打印月份和年份。

推荐阅读:



C 程序:打印各种三角形图案

原文:https://overiq.com/c-examples/c-program-to-print-various-triangular-patterns/

最后更新于 2020 年 9 月 24 日


图案 1:使用*的半金字塔图案

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * *

下面是一个使用*打印半金字塔图案的 C 程序:

/**************************************************
 * C Program to print Half Pyramid pattern using *
 **************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = 1; j <= i; j++)
        {
    
    
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 5

* 
* * 
* * * 
* * * * 
* * * * *

模式 2:使用数字的半金字塔模式

1     
1     2     
1     2     3     
1     2     3     4     
1     2     3     4     5     
1     2     3     4     5     6     
1     2     3     4     5     6     7     
1     2     3     4     5     6     7     8     
1     2     3     4     5     6     7     8     9     
1     2     3     4     5     6     7     8     9     10

下面是一个用数字打印半金字塔图案的 C 程序:

/********************************************************
 * C Program to print Half Pyramid pattern using numbers
 ********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = 1; j <= i; j++)
        {
    
    
            printf("%-5d ", j);
        }
        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 5

1     
1     2     
1     2     3     
1     2     3     4     
1     2     3     4     5

模式 3:使用字母的半金字塔模式

A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G

下面是一个用字母打印半金字塔图案的 C 程序:

/**********************************************************
 * C Program to print Half Pyramid pattern using alphabets
 **********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n, ch = 'A';

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {
    
            
        // loop to print alphabets
        for(int j = 1; j <= i; j++)
        {
    
    
            printf(" %c", ch);
        }

        ch++;

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 5

A
B B
C C C
D D D D
E E E E E

图案 4:使用*的倒直角三角形图案

* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*

下面是一个使用*打印倒直角三角形的 C 程序:

/*****************************************************
 * C Program to print inverted right triangle pattern
******************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = n; i >= 1; i--)
    {
    
            
        // loop to print *
        for(int j = i; j >= 1; j--)
        {
    
    
            printf("* ");
        }               

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 5

* * * * * 
* * * * 
* * * 
* * 
*

模式 5:全金字塔模式使用*

.
                               * 
                            *  *  * 
                         *  *  *  *  * 
                      *  *  *  *  *  *  * 
                   *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

下面是一个使用*打印全金字塔图案的 C 程序:

/*******************************************
 * C Program to print full pyramid using * 
********************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {
    
       
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
    
    
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {
    
    
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 8

                         * 
                      *  *  * 
                   *  *  *  *  * 
                *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

图案 6:使用*的全倒金字塔图案

*  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  * 
                *  *  *  *  * 
                   *  *  * 
                      *

下面是一个使用*打印全倒金字塔图案的 C 程序:

/***************************************************
 * C Program to print full inverted pyramid using * 
****************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = n; i >= 1; i--)
    {
    
       
        // loop to print leading spaces in each line
        for(int space = n-i; space >= 1; space--)
        {
    
    
            printf("   ");
        }

        // loop to print *
        for(int j = i * 2 - 1; j >= 1; j--)
        {
    
    
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 10

 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  *  *  * 
                   *  *  *  *  *  *  * 
                      *  *  *  *  * 
                         *  *  * 
                            *

图案 7:使用*的空心直角三角形

* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
* * * * * * * *

下面是一个使用*打印空心直角三角形的 C 程序:

/**********************************************************
 * C Program to print hollow right angled triangle using *
***********************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for number of lines
    for(int i = 1; i <= n; i++)
    {
    
    
        for(int j = 1; j <= i; j++)
        {
    
    
            //  print * only on the first line, last column of every line and on the last line
            if(j == 1 || j == i || i == n)
            {
    
    
                printf("* ");
            }

            else
            {
    
    
                printf("  ");    
            }            
        }
        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 10

* 
* * 
*   * 
*     * 
*       * 
*         * 
*           * 
*             * 
*               * 
* * * * * * * * * *

图案 8:使用*的倒置空心直角三角形

* * * * * * * * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
*

下面是一个使用*打印倒空心直角三角形的 C 程序:

/*******************************************************************
 * C Program to print inverted hollow right angled triangle using *
********************************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for number of lines
    for(int i = n; i >= 1; i--)
    {
    
    
        for(int j = i; j >= 1; j--)
        {
    
    
            //  print * only on the first line, last line and last column of every line and on the 
            if(j == 1 || j == i || i == n)
            {
    
    
                printf("* ");
            }

            else
            {
    
    
                printf("  ");    
            }            
        }
        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 10

* * * * * * * * * * 
*               * 
*             * 
*           * 
*         * 
*       * 
*     * 
*   * 
* * 
*

图案 9:全空心金字塔使用*

.      
                         * 
                      *     * 
                   *           * 
                *                 * 
             *                       * 
          *                             * 
       *                                   * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

下面是一个使用*打印全空心金字塔的 C 程序:

/*************************************************
 * C Program to print full hollow pyramid using * 
*************************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for(int i = 1; i <= n; i++)
    {
    
       
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
    
    
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {
    
    

            if (j == 1 || (j == i * 2 - 1) || i == n )
            {
    
    
                printf(" * ");
            }
            else
            {
    
    
                printf("   ");            
            }            
        }               

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 10

                               * 
                            *     * 
                         *           * 
                      *                 * 
                   *                       * 
                *                             * 
             *                                   * 
          *                                         * 
       *                                               * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

图案 10:全倒置空心金字塔使用*

*  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *                                   * 
       *                             * 
          *                       * 
             *                 * 
                *           * 
                   *     * 
                      *

下面是一个使用*打印全空心倒金字塔的 C 程序:

/**********************************************************
 * C Program to print full inverted hollow pyramid using * 
**********************************************************/

#include<stdio.h> // include stdio.h

int main() {
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop for line number of lines
    for (int i = n; i >= 1; i--) {
    
    
        // loop to print leading spaces in each line
        for (int space = n - i; space >= 1; space--) {
    
    
            printf("   ");
        }

        // loop to print *
        for (int j = i * 2 - 1; j >= 1; j--) 
        {
    
    
            if (j == 1 || (j == i * 2 - 1) || i == n) 
            {
    
    
                printf(" * ");
            }             
            else 
            {
    
    
                printf("   ");
            }
        }

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 10

 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *                                               * 
       *                                         * 
          *                                   * 
             *                             * 
                *                       * 
                   *                 * 
                      *           * 
                         *     * 
                            *

图案 11:菱形图案使用*

.
                * 
             *  *  * 
          *  *  *  *  * 
       *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  * 
          *  *  *  *  * 
             *  *  * 
                *

下面是一个使用*打印钻石图案的 C 程序:

/*********************************************
 * C Program to print Diamond pattern using * 
**********************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");

    // loop to print upper pyramid
    for(int i = 1; i <= n; i++)
    {
    
       
        // loop to print leading spaces in each line
        for(int space = 0; space <= n - i; space++)
        {
    
    
            printf("   ");
        }

        // loop to print *
        for(int j = 1; j <= i * 2 - 1; j++)
        {
    
    
            printf(" * ");
        }                              

        printf("\n");
    }

    // loop to print lower pyramid 
    for(int i = n+1; i >= 1; i--)
    {
    
       
        // loop to print leading spaces in each line
        for(int space = n-i; space >= 0; space--)
        {
    
    
            printf("   ");
        }

        // loop to print *
        for(int j = i * 2 - 1; j >= 1; j--)
        {
    
    
            printf(" * ");
        }               

        printf("\n");
    }

    return 0;
}

现在试试

预期输出:

Enter number of lines: 8 

                         * 
                      *  *  * 
                   *  *  *  *  * 
                *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
    *  *  *  *  *  *  *  *  *  *  *  *  *  *  * 
       *  *  *  *  *  *  *  *  *  *  *  *  * 
          *  *  *  *  *  *  *  *  *  *  * 
             *  *  *  *  *  *  *  *  * 
                *  *  *  *  *  *  * 
                   *  *  *  *  * 
                      *  *  * 
                         *


推荐节目:



C 程序:打印帕斯卡三角形

原文:https://overiq.com/c-examples/c-program-to-print-pascal-triangle/

最后更新于 2020 年 9 月 24 日


什么是帕斯卡三角?

帕斯卡三角形是二项式系数的三角形阵列。看起来是这样的:

row -> 0                   1     
row -> 1                1     1     
row -> 2             1     2     1     
row -> 3          1     3     3     1     
row -> 4       1     4     6     4     1     
row -> 5    1     5     10    10    5     1

以下是计算三角形中任何给定位置的值的公式:

[
\ begin { pmamatrix } n \ \ k \ end { pmamatrix } = \ frac { n!}{k!(n-k)!}
]

其中n代表行号,k代表列号。请注意,这些行从0开始,最左边的一列是0。因此,为了找出第 4 行第 2 列的值,我们这样做:

[
\ begin { pmamatrix } 4 \ \ 2 \ end { pmamatrix } = \ frac { 4!}{2!(4-2)!} = \frac{4!}{2!2 号!} = \frac{43}{21} = 6
]

下面是一个 C 程序,它根据用户输入的行数打印帕斯卡三角形:

/**************************************
 * C Program to print Pascal Triangle
***************************************/

#include<stdio.h> // include stdio.h
unsigned long int factorial(unsigned long int);

int main() 
{
    
    
    int n;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    printf("\n");

    // loop for number of rows
    for(int i = 0; i <= n; i++)
    {
    
    
        // loop to print leading spaces at each line
        for(int space = 0; space < n - i; space++)
        {
    
    
            printf("   ");
        }

        // loop to print numbers in each row
        for(int j = 0; j <= i; j++)
        {
    
    
            printf("%-5d ", factorial(i) / (factorial(j) * factorial(i-j) ) );
        }

        // print newline character
        printf("\n");
    }    

    return 0;
}

unsigned long int factorial(unsigned long int n)
{
    
    
    unsigned long int f = 1;

    while(n > 0)
    {
    
    
        f = f * n;
        n--;
    }

    return f;   
}

现在试试

预期输出:

Enter number of rows: 10

                              1     
                           1     1     
                        1     2     1     
                     1     3     3     1     
                  1     4     6     4     1     
               1     5     10    10    5     1     
            1     6     15    20    15    6     1     
         1     7     21    35    35    21    7     1     
      1     8     28    56    70    56    28    8     1     
   1     9     36    84    126   126   84    36    9     1     
1     10    45    120   210   252   210   120   45    10    1


推荐阅读:



C 程序:打印弗洛伊德三角形

原文:https://overiq.com/c-examples/c-program-to-print-floyds-triangle/

最后更新于 2020 年 9 月 24 日


弗洛伊德的三角形是这样的:

1     
2     3     
4     5     6     
7     8     9     10    
11    12    13    14    15    
16    17    18    19    20    21

下面是一个打印弗洛伊德三角形的 C 程序:

/***************************************
 * C Program to print Floyd's Triangle
 ***************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    
    
    int n, k = 1;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");       

    // loop for number of lines    
    for(int i = 1; i <= n; i++)
    {
    
    
        //loop  to print numbers in each line
        for(int j = 1; j <= i; j++)
        {
    
                
            printf("%-5d ", k++);            
        }

        printf("\n");
    }  

    return 0;
}

现在试试

预期输出:

Enter number of lines: 5

1     
2     3     
4     5     6     
7     8     9     10    
11    12    13    14    15


推荐阅读:



Python 教程

Python 入门

原文:https://overiq.com/python-101/intro-to-python/

最后更新于 2020 年 9 月 6 日


Python 是由 Guido Van Rossum 创建的高级通用编程语言。它于 1991 年公开发行。所谓高级,我们指的是一种对程序员隐藏细节的语言。此外,它还用来指人类容易理解的计算机语言。Python 以其简单性和可读性而闻名。

Python 的特性

Python 很简单

Python 是最容易上手的语言之一。用 Python 编写的程序看起来非常像英语。由于其简单性,大多数入门级编程课程都使用 Python 向学生介绍编程概念。

Python 是可移植的/独立于平台的

Python 是可移植的,这意味着我们可以在各种不同的操作系统中运行 Python 程序,而无需任何更改。

Python 是一种解释语言

Python 是一种解释语言。像 C、C++这样的语言是编译语言的例子。

用高级语言编写的程序称为源代码或源程序,源代码中的命令称为语句。一台计算机不能执行用高级语言编写的程序,它只能理解仅由 0 和 1 组成的机器语言。

有两种类型的程序可供我们将高级语言翻译成机器语言:

  1. 编译程序
  2. 解释者

编译程序

编译器一次将整个源代码翻译成机器语言,然后执行机器语言。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

解释者

另一方面,解释器将高级语言逐行翻译成机器语言,然后执行。Python 解释器从文件顶部开始,将第一行翻译成机器语言,然后执行。这个过程一直重复,直到文件结束。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

像 C、C++这样的编译语言使用编译器将高级代码翻译成机器语言,而像 Python 这样的解释语言使用解释器将高级代码翻译成机器语言。

编译语言和解释语言的另一个重要区别是,编译语言的性能略好于使用解释语言编写的程序。然而,我确实想指出编译语言的这种优势正在慢慢消失。

Python 是强类型的

强类型语言不会自动将数据从一种类型转换为另一种类型。像 JavaScript 和 PHP 这样的语言被称为松散类型语言,因为它们可以自由地将数据从一种类型转换为另一种类型。考虑以下 JavaScript 代码:

price = 12
str = "The total price = " + 12
console.log(str)

输出:

The total price = 12

在这种情况下,在将12添加到字符串之前;JavaScript 首先将数字12转换为字符串"12",然后将其附加到字符串的末尾。

然而,在 Python 中,像str = "The total price = " + 12这样的语句会产生错误,因为 Python 不会自动将数字12转换为字符串。

现在试试

一大套图书馆

Python 有大量的库,这使得添加新功能变得容易,而无需重新发明轮子。我们可以在 https://pypi.python.org/pypi 访问这些图书馆。

下面是我从初级 Python 程序员那里听到的两个常见问题。

我可以使用 Python 创建什么类型的应用?

我们可以使用 Python 创建以下类型的应用:

  1. Web 应用
  2. 安卓应用
  3. 图形用户界面应用
  4. 比赛
  5. 科学应用
  6. 系统管理应用
  7. 控制台应用

名单还在继续…

谁用 Python?

以下是使用 Python 的知名公司的小列表:

  1. 收纳盒
  2. 唱片公司
  3. Reddit
  4. Quora
  5. 浏览器名
  6. 谷歌
  7. 油管(国外视频网站)

我想这足以让你印象深刻。

在下一课中,我们将学习如何安装 Python。

**注:**本教程源代码可在https://github.com/overiq/intro-to-python获得。