learning target:
"Ming Jie C Language" Third Edition
Chapter 2
Operations and Data Types
Learning Content:
2-1 Operation 2-2 Data Typestudy-time:
7-9pm, October 23, 2020
Study notes:
2-1 Operation
Operators and operands
#include<stdio.h>
int main(void)
{
int vx,vy;
puts("请输入两个整数。");
printf("整数vx:");scanf("%d",&vx);
printf("整数vy:");scanf("%d",&vy);
printf("vx+vy=%d\n", vx+vy);
printf("vx-vy=%d\n", vx-vy);
printf("vx*vy=%d\n", vx*vy);
printf("vx/vy=%d\n", vx/vy);
printf("vx%%vy=%d\n", vx%vy);
return 0;
}
1. Symbols that can perform operations like + and * are called operators . The variable or constant that is the object of operation is called the operand .
2.
Operator |
---|
product of a*ba and b |
quotient of a/ba and b |
the remainder obtained by dividing a%ba by b |
the sum of a+ba and b |
ab The difference between a and b |
3. Use the printf function to enter %.
The% in the format string has the function of conversion instructions. Therefore, when you want to output %, you must write %%.
Exercise 2-1
/* 显示前者是后者的百分之几 */
#include <stdio.h>
int main(void)
{
int a,b;
puts("请输入两个整数。");
printf("整数x:");scanf("%d",&a);
printf("整数y:");scanf("%d",&b);
printf("x的值是y的%.0f%%\n.",((double)a/b)*100);
return 0;
}
Exercise 2-2
/* 读取两个整数,然后输出它们的和以及积。 */
#include <stdio.h>
int main(void)
{
int a, b;
puts("请输入两个整数。");
printf("整数a:");scanf("%d",&a);
printf("整数b:");scanf("%d",&b);
printf("它们的和是%d, 积是%d",a+b, a*b);
return 0;
}
2-2 Data Type
type of data
#include<stdio.h>
int main(void)
{
int n;
double x;
n=9.99;
x=9.99;
printf("int 型变量n的值:%d\n",n);
printf(" n/2:%d\n",n/2);
printf("double型变量x的值:%f\n",x);
printf(" x/2.0:%f\n",x/2.0);
return 0;
}
1. Integer types like 5 and 37 are called integer constants. Constants that contain decimals like 3.14 are called floating-point constants. Usually integer constants are of type int, and floating-point constants are of type double.
2. When the variable of double type is output through the printf function, %f is required, and when the value is assigned through the scanf function, the string %lf is required.
Exercise 2-3
/* 显示出读取的实数的值 */
#include <stdio.h>
int main(void)
{
double a;
printf("请输入一个实数:");scanf("%lf",&a);
printf("你输入的是%f",a);
return 0;
}
Data types and operations
"int/int"=int
"double/double"=double
"double/in"=double
"int/double"=double
Practice 2-4
#include<stdio.h>
int main(void)
{
int n1,n2,n3,n4;
double d1,d2,d3,d4;
n1 = 5/2;
n2=5.0/2.0;
n3=5.0/2;
n4=5/2.0;
d1=5/2;
d2=5.0/2.0;
d3=5.0/2;
d4=5/2.0;
printf("n1=%d\n",n1);
printf("n2=%d\n",n2);
printf("n3=%d\n",n3);
printf("n4=%d\n\n",n4);
printf("d1=%f\n",d1);
printf("d2=%f\n",d2);
printf("d3=%f\n",d3);
printf("d4=%f\n",d4);
return 0;
}
Type conversion
(double) (a+b) Type conversion expression: convert the result of a+b into double type.
Exercise 2-5
/* 读取两个整数的值,计算出前者是后者的百分之几,并用实数输出结果。 */
#include <stdio.h>
int main(void)
{
int a, b;
puts("请输入两个整数。");
printf("整数a:");scanf("%d",&a);
printf("整数b:");scanf("%d",&b);
printf("a是b的%f%%\n",((double)a/b*100));
return 0;
}
Conversion instructions
#include<stdio.h>
int main(void)
{
int a,b,c;
int sum;
double ave;
puts("请输入三个整数");
printf("整数a");scanf("%d",&a);
printf("整数b");scanf("%d",&b);
printf("整数c");scanf("%d",&c);
sum=a+b+c;
ave=(double)sum/3;
printf("它们的合计值是%5d。\n",sum); //输出99999
printf("它们的平均值是%5.1f。\n,ave); //输出999.9
return 0;
}
%5d… Display a decimal integer with at least 5 digits.
%5.1f… Display at least 5 digits floating point number. However, only one digit is displayed after the decimal point.
%0 (0 flag) 9 (minimum field width). 9 (precision) f (conversion specifier)
exercise 2-6
/* 读取表示身高的整数,显示出标准体重的实数值。标准体重根据公式
(身高-100)*0.9进行计算,所得结果保留一位小数。 */
#include <stdio.h>
int main(void)
{
int a;
printf("请输入您的身高:");scanf("%d",&a);
printf("您的标准体重是%.1f公斤",(a - 100) * 0.9);
return 0;
}