C language BASIC programming of core technology (III.)

Source: Internet
Author: User

1 General description

Operator overview
Arithmetic operators
Self-increment auto-decrement operator
Assignment operators
Relational operators
logical operators
Trinocular operator

2 Overview of Operators

The core of the computer is the calculation of data, and at the program language level is the use of various operators to complete the preceding data types of arithmetic, relational, logical, bitwise operators, they have their own priority and binding, priority and operator-related, that is, who first calculate, who calculates, The binding is calculated from left to right in the case of equality of precedence.

3 Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations, and the result of the operation is still numeric, which is the largest data type involved in the operation. The C language supports the five commonly used arithmetic operators (+ 、-、 *,/,%), where/that is, the modulo operator, which is used to find the remainder of two integers, the sign of the result of the remainder operation is the same as the divisor.

Using arithmetic operations to implement numeric inversion

#include "Common.h"Integer reversal void Int_reversal () {printf("Please enter a reversed integer \ n");//Suppose the input is10000-99999An integer betweenintNum scanf"%d", &num);//Gets the number of bits, 10 digits, and hundred, thousands, thousand digits of the integerintNum_val = num%Ten;intTen_val = num/Ten%Ten;intHundred_val = num/ -%10;intThousand_val = num/ +%10;intTen_thousand_val = num/10000;printf("num = %dafter reversal", Num_val*10000+ten_val*1000+hundred_val*100+thousand_val*10+ten_thousand_val);}

Using arithmetic operations to implement the modulo operator

#include "Common.h"/* arithmetic operator for modulo operation*/void Alg () {intone =Ten;intboth =3;intresult = One/two;//Modulo is to seek the remainder,Ten/3=3...1The result of finding the remainder is1    printf("Ten%3 result is %d\ n", result);//Using arithmetic operations to complete the modulo operation result =Ten- (Ten/3) *3;printf("arithmetic operation of ten%3 result is %d\ n", result);}

Using arithmetic operators to complete the temperature conversion (Fahrenheit and Celsius temperature conversion)
Calculation formula for Fahrenheit temperature
Fahrenheit temperature = Celsius temperature *1.8+32

#include "common.h"void convert(){    float f;//华氏温度    float c;//摄氏温度    printf("请输入华氏温度\n");    scanf_s("%f", &f);    // 注意不能写成5/9*(f-32),5/9两个整数相除结果是0      c = (f +3259;    printf("摄氏温度:%-5.2f", c);    system("pause");}
4 self-increment decrement operator

The function of the self-increment (–) decrement (–) operator is to increase the value of the variable by only 1 or 1.
The self-increment decrement operator is typically used in loops as a way to modify the loop condition.
+ + and – can be placed in front of or behind a variable when the + + or – placed in front of the variable means that it will be increased by 1 and then participate in the operation, placed in the variable after the first to participate in the operation since the increase of 1, the difference is that one is the first self-increment and then execute the program, the other is to execute the program and then self-increment, the result of the final
The increment operator takes precedence over the arithmetic operator

#include "common.h"/*    自增运算符  自增运算符的优先级高于算术运算符*/void autoinc(){    int9;    printf("%d\n",num--);//先打印num的值 也就是9 再减1    printf("%d\n",num);//最后的结果就是8}

Countdown using loops, self-reduction operations, and thread interrupts

#include "common.h"/*倒计时*/void timer(){    int50//倒计时50秒    while (second>0){        printf("倒计时%d秒\n",second);        second--;        Sleep(1000);    }    printf("Time Over");}

Use loops to calculate an even number of 1-100 or less

#include "common.h"void getsum(){    //计算1-100之间的偶数和    int2;    intsum0;    while100){        sum += i;        2;        printf("i=%d\t\tsum=%d\n",i,sum);    }    printf("1-100之内的偶数和为%d\n",sum);}
5 Assignment operators

The assignment operator is to assign the expression or constant value to the right of "=" to the variable on the left side of "=", which performs a data type conversion when assigned, or automatically converts the value on the right side to the type of the left variable. If the assignment operator is to the right of a complex expression, it is evaluated in terms of the right-to-left of the expression.

#include"Common.h"/ * Assignment operator * /void Assigment () {intnum =10.5;an automatic type conversion is done when//assignment, where a double is converted to an int and then assigned to the NUM variable    //3 = num + 1;//Only variables can be assigned, constants cannot be    //num + 1 = 3; An expression cannot beprintf"num=%d\n", num); num-=1;//num =num-1    int Copy=++num;//assignment is a complex expression that begins right-to-left calculation    Copy+=Copy-Ten;//==>copy=copy+copy-10==>copy=10printf"copy=%d\n",Copy);}
6 Relational operators

Relational operators are mainly comparing two numeric values (integer, float, and character) or expressions, integers and floating-point numbers are compared by size, and character comparisons are asc| | The size of the code, floating-point values are not recommended = = and! = for relational operations.
The result of the relational operation is 1 (logical true) or 0 (bare metal false), and C + + or Java is replaced by a Boolean type of TRUE or false.
The common relational operators include greater than (>), less than (<), greater than or equal (>=), less than Equals (<=), equals (= =), and not equal to (! =), and the relational operators start from left to right.

The result of the operation of the relational operator 1 means that the logical true 0 represents the logical false

#include "common.h"/*关系运算符的结果*/void relation_sample(){    printf("%d\n",10>8//true    printf("%d\n",10<8);//false}

Print 26 uppercase and lowercase letters using relational operators and loop implementations

#include "Common.h"/ * Relational operator * /voidRelation () {//loop increment print lowercase letters    printf("lowercase letter \ n");Charc =' A '; while(c<=' Z '){Putchar(c);    C + +; }//Cycle decrement print uppercase letters    printf("\ n Capital letter \ n");Charv =' Z '; while(v >=' A '){Putchar(v);    v--; }printf("\ n");}
7 Logical operators

The logical operators are primarily the logical relationships that determine two or more expressions, and there are three logical operators:
Logic and (&&): When both sides of the expression are 1 o'clock, the result is 1, otherwise 0
Logical OR (| |): The result of an expression on either side of the expression is 1, the result is 1, otherwise 0
Logical non (!): Reverse the result of an expression, 0 change to 1, 1 to 0

#include "Common.h"/ * Logical operator * /voidLogic () {//define interview conditions#define Age#define WORKING life 5    intAgeintWorkinglife;printf("Please enter your age and working life \ \");scanf("%d", &age);scanf("%d", &workinglife);printf("The requirements for the interview are%d years of age and working years%d", age, working life);//logic with two conditions to meetAges > Age &&workinglife > Working years?printf("logic and can participate in this round of interviews \ n") :printf("logic and age are too small or not working long enough to participate in this round of interviews \ n");//logic does not meet a conditionAge> Age | | Workinglife > Working life?printf("logic or can participate in this interview \ n") :printf("logic or age is too small or not working long enough to participate in this round of interviews \ n");//Logical non-     intValue =0;printf("The result of value after the logical non-operation is%d\n\n",!value);//0 Logical non-subsequent value is 1 inverse ....}

There is a short circuit between logic and logic or both, that is, when the value of the left expression is computed to clarify the result of the entire expression, the result of the right expression is not computed, and the short-circuit phenomenon is formed.

#include "Common.h"/* LogosShort-circuit characteristics of a series operation*/void Logic_short_circurt () {int x=0;int y= -;x&& + +y;//x=0means that the result of the entire expression is0, does not executeySelf-increment1Re-engagement in logic and operationsprintf("x =%d\ty=%d\ n",x,y);x=1;x|| ++y;//1means that the result of the entire expression is1, do not execute + +y    printf("x =%d\ty=%d\ n",x,y);}
83 Mesh Operator

The trinocular operator is a simple if else structure whose representation is expression?expression1:expresson2, and if the result of the expression is true, then the expression expression1 after the question mark is executed, otherwise: After the expression Expresson2.

Use the three-mesh operator to make random number judgments and perform corresponding operations

#include "Common.h"/* Three mesh operator*/void Tenery () {time_t ts;//Declaring a Time variableSrand((unsignedint) Time(&ts));//Defines a random number seed, with an unsigned integer of timeintnum =Rand()%100;//Random valuesprintf("num = %d\ n", num); Num >= -?printf("Num>70"),system("Calc") :printf("Num<70") ,system("Notepad");}

Convert uppercase letters to lowercase using the three-mesh operator

#include "Common.h"/ * Three mesh operator * /voidGetcharcontent () {CharCharcontent =getchar ();//Save the input characters to the charcontent variable    //Determine if the input is in uppercase or lowercase lettersCharcontent >=' A '&&charcontent <=' Z '?printf("you entered capital letters.") :printf("You entered a lowercase letter.");//Use the three-mesh operator to convert uppercase letters to lowercaseCharcontent >=' A '&& charcontent <=' Z '? Charcontent + = +,printf("charcontent =%c", charcontent):printf("Charcontent%c", charcontent);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C language BASIC programming of core technology (III.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.