简易计算器-数据类型,运算符,控制流程
《一》计算两个整数的和
代码:
#include <stdio.h>
int main() {
/*计算两个数之和*/
int a,b,c; //自定义整数型变量a,b,c,d
a = 1; //赋值a为1
b = 2; //赋值b为2
c = a+b; //运算
printf("%d+%d=%d ",a,b,c); //输出
return 0;
}
原理,定义三个整数型变量a,b,c。将a赋值为1,b赋值为2,让c=a+b,最后输出结果C
补充:%d的作用是以十进制输出整数变量a,b,c的内容。第一个%d对应a,第二个%d对应b,第三个%d对应c
所以%d和分号外的值是对应的。
运行结果:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
1+2=3
Process finished with exit code 0
练习:计算三个整数的和,输出 12=34=56=102
代码:
#include <stdio.h>
int main() {
/*计算三个数之和*/
int a,b,c,d; //自定义整数型变量a,b,c,d
a = 12; //赋值
b = 34; //赋值
c = 56; //赋值
d = a+b+c; //运算
printf("%d+%d+%d=%d ",a,b,c,d);
return 0;
}
《二》键盘输入两个整数
代码:
#include <stdio.h>
int main() {
/*键盘输入两个整数*/
int a, b, c; //自定义整数型变量a,b,c
printf("Please enter the first number: ");
scanf("%d", &a); //输入一个值并将其赋值给a
printf("Please enter the second number: ");
scanf("%d", &b); //输入一个值并将其赋值给b
c = a + b; //运算
printf("%d+%d=%d ", a, b, c); //输出结果
return 0;
}
原理:
1.scanf("%d", &a);这句的作用是,在键盘输入一个值,然后将这个值赋值给a
2/scanf("%d", &b); 这句的作用是,在键盘输入一个值,然后将这个值赋值给b
结果:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
Please enter the first number:1
Please enter the second number:2
1+2+=3
Process finished with exit code 0
练习:键盘输入三个值求和
代码:
#include <stdio.h>
int main() {
/*输入三个数之和*/
int a, b, c,d; //自定义整数型变量a,b,c,d
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个值并将其赋值给a
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个值并将其赋值给b
printf("Please enter a third number: "); //输出提示语
scanf("%d", &c); //输入一个值并将其赋值给c
d=a+b+c; //运算结果
printf("%d+%d+%d=%d ", a, b, c,d); //输出运算结果
return 0;
}
《三》计算加法
代码:
#include <stdio.h>
int main() {
/*计算加法*/
char opt; //自定义一个字符型变量opt
int a, b, c; //自定义整数型变量a,b,c
printf("operation: "); //输出提示语operation:
scanf("%c",&opt); //输入一个运算符
if (opt == '+'){ //判断输入的字符是否为“+”,若为“+”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
c = a+b; //运算c=a+b的值
printf("%d+%d=%d ", a, b, c);
} else{ //若不为“+”则执行在里面的内容
printf("Illegal operation!");
}
return 0;
}
原理:
1.char opt;这里使用关键字char定义了一个字符类型常量opt,随后用于存放加法运算符"+"、
2.if…else…可当作,如果是则运行什么什么,如果不是则运行什么什么
结果1:当输入的运算符为“+”时
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
operation:+
Please enter the first number:1
Please enter the second number:2
1+2=3
Process finished with exit code 0
结果2:当输入的运算符不为“+”时
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
operation:-
Illegal operation!
Process finished with exit code 0
练习:输入两个整数判断是否相等
#include <stdio.h>
int main() {
/*数值判断*/
int a, b; //自定义整数型变量a,b
printf("Please enter the first number: "); //输出提示语operation:
scanf("%d",&a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d",&b); //输入一个数值
if (a == b){ //判断输入的数值a是否等于b,如果是则输出Equal
printf("Equal");
} else{ //若a,b,不相等则执行在里面的内容
printf("%d is not equal to %d",a,b);
}
return 0;
}
结果1:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
Please enter the first number:1
Please enter the second number:2
1 is not equal to 2
Process finished with exit code 0
结果2:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
Please enter the first number:1
Please enter the second number:1
Equal
Process finished with exit code 0
练习2,输出一个字符的ASXII编码
#include <stdio.h>
int main() {
/*输出ASCII码*/
char ascii;//定义一个char型
printf("Please enter a character"); //提示语:请输入一个字符
scanf("%c", &ascii); //该语句为输入字符
printf("The ASCII of character '%c' is %d",ascii,ascii);
return 0;
}
结果:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
Please enter a characterd
The ASCII of character 'd' is 100
Process finished with exit code 0
练习:输入两个数,判断最大值。
#include <stdio.h>
int main() {
/*数值判断*/
int a, b; //自定义整数型变量a,b
printf("Please enter the first number: "); //输出提示语operation:
scanf("%d",&a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d",&b); //输入一个数值
if (a > b){ //判断输入的数值a是否大于b,如果是则输出
printf("Maximum value: %d",a);
} else{ //若a小于b则执行在里面的内容
printf("Maximum value: %d",b);
}
return 0;
}
原理:通过scanf依次键入 ,a,b两个值,然后通过比较运算符“>”相比较。
结果:
C:\Users\13539\CLionProjects\untitled\cmake-build-debug\untitled.exe
Please enter the first number:2
Please enter the second number:1
Maximum value: 2
Process finished with exit code 0
《四》计算减法和乘法除法取余
代码:
#include <stdio.h>
int main() {
/*计算加法*/
char opt; //自定义一个字符型变量opt
int a, b, c; //自定义整数型变量a,b,c
printf("operation: "); //输出提示语operation:
scanf("%c",&opt); //输入一个运算符
if (opt == '+'){ //判断输入的字符是否为“+”,若为“+”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
c = a+b; //运算c=a+b的值
printf("%d %c %d=%d ", a, opt,b, c);
} else if (opt == '-') { //若不为“+”则执行在里面的内容 //判断输入的字符是否为“-”,若为“-”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
c = a - b; //运算c=a+b的值
printf("%d %c %d=%d ", a, opt,b, c);
} else if (opt == '*') { //若不为“+”则执行在里面的内容 //判断输入的字符是否为“-”,若为“-”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
c = a * b; //运算c=a+b的值
printf("%d %c %d=%d ", a, opt,b, c);
} else if (opt == '/') { //若不为“+”则执行在里面的内容 //判断输入的字符是否为“-”,若为“-”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
if (b != 0) {
c = a / b; //运算c=a+b的值
printf("%d %c %d=%d ", a, opt,b, c);
} else if (b=0){
printf("Denominator cannot be 0");
return 1;
}
} else if (opt == '%') { //若不为“+”则执行在里面的内容 //判断输入的字符是否为“-”,若为“-”则执行下一行,若不是则执行else内的内容
printf("Please enter the first number: "); //输出提示语
scanf("%d", &a); //输入一个数值
printf("Please enter the second number: "); //输出提示语
scanf("%d", &b); //输入一个数值
if (b != 0) {
c = a % b; //运算c=a+b的值
printf("%d %c %d=%d ", a, opt,b, c);
} else if (b=0){
printf("Denominator cannot be 0");
return 1;
}
} else {
printf("Illegal operation!");
}
return 0;
}
练习:
输入0-6,输出星期一到星期日。若不是0-6,输出非法数据
代码:
#include <stdio.h>
int main() {
int a; //定义一个输入的数据
printf("Please enter an integer of 0-6 : "); //输入0-6
scanf("%d", &a); //获取到数据
if (a == 0){ //判断a 是否为下列值
printf(" Sunday\n");
}else if(a == 1) {
printf(" Monday\n");
} else if(a == 2) {
printf(" Tuesday\n");
}else if(a == 3) {
printf(" Wednesday\n");
}else if(a == 4) {
printf(" Thursday\n");
}else if(a == 5) {
printf(" Friday\n");
}else if(a == 6) {
printf(" Saturday\n");
} else{
printf(" invalid data!"); //若非0-6则输出该句!
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter an integer of 0-6 :3
Wednesday
Process finished with exit code 0
练习:
将百分制转化为等级制
代码:
#include <stdio.h>
int main() {
int a; //定义一个输入的数据
printf("Please enter an integer of 0-100 : "); //输入一个0-100的数
scanf("%d", &a); //获取到数据
if (a > 100){ //判断a 是否为下列值
printf(" invalid data!\n");
}else if(a >=95) {
printf(" A\n");
} else if(a >=85) {
printf("B\n");
}else if(a >=70) {
printf(" C\n");
}else if(a >=60) {
printf(" D\n");
}else if(a >=0) {
printf(" D\n");
}else{
printf(" invalid data!"); //若非0-6则输出该句!
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter an integer of 0-100 :87
B
Process finished with exit code 0
练习:
输入一个数,判断奇偶性,若小于0,则输出非法数据
#include <stdio.h>
int main() {
int a; //定义一个输入的数据
printf("Please enter a number: ");//提示输入一个数据
scanf("%d",&a); //获得数据a
if (a<0){ //判断数据是否小于0
printf(" invalid data"); //若下于0则输出数据错误
} else if(a%2==0){ //判断a%2是否为0;
printf(" %d Even numbers" ,a); //若为0则输出该数为偶数
}else {
printf(" %d Singular", a); //若不为零则输出为奇数
}
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter a number:5
5 Singular
Process finished with exit code 0
练习:
平年与闰年的判断
#include <stdio.h>
int main() {
int a; //定义一个输入的数据
printf("Please enter the year: "); //提示:输入一个年份
scanf("%d",&a); //获取输入的年份
if (a<=0){ //判断该年份是否有效
printf(" invalid data");
} else if(a%100==0&&(a%400==0)){ //判断该年份是否能被100和400同时整除,若可以则为闰年
printf(" %d is the year of prosperity" ,a);
} else if(a%100!=0&&(a%4==0)){ //判断年份不是100的倍数但能被4整除的也为闰年,否则为平年
printf(" %d is the year of prosperity" ,a);
}else {
printf(" %d is the year of the year", a);
}
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter the year: 2010
2010 is the year of the year
Process finished with exit code 0
练习:
输入一个字母,进行大小写切换
#include <stdio.h>
int main() {
char letter; //定义一个字符类型 letter
printf("Please enter a letter : "); //提示:输入字母
scanf("%c",&letter); //键入字母
if (letter>='a'&&(letter<='z')){ //判断是否处于小写字母a-z
printf("%c is Lowercase letters",letter); //输出小写字母
printf("The corresponding capital letter is: %c\n",letter-('a'-'A')); //输出其大写字母
}else if(letter>='Z'&&(letter<='Z')){ //判断是否处于小写字母A-Z
printf(" %c is Capital", letter); //输出大写字母
printf("The corresponding lowercase letter is : %c\n",letter+('a'-'A')); //输出小写字母
} else{ //若不在范围内属于非字母
printf("invalid data");
}
return 0;
}
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter a letter :e
e is Lowercase lettersThe corresponding capital letter is: E
Process finished with exit code 0
《五》重复执行计算
while关键字
代码:
#include <stdio.h>
int main(){
char opt; //定义一个字符,用于输入运算符
int a,b,c; //定义三个数值
while (1){ //进行while循环 1,代表为真
printf("Please enter calculation formula :"); //提示输入计算公式
scanf("%d %c %d",&a,&opt,&b); //获取到输入的计算公式
if (opt == '+'){ //判断输入的是否为加法公式
c=a+b;
}else if (opt=='-'){ //判断输入的是否为减法公式
c=a-b;
}else if (opt=='*'){ //判断输入的是否为乘法公式
c=a*b;
}else if (opt=='/'){ //判断输入的是否为除法公式
c=a/b;
}else if (opt=='%'){ 判断输入的是否为取余公式
c=a%b;
}else{
printf("Illegal operation");
return 1;
}
printf("%d %c %d = %d \n" ,a,opt,b,c);
}
printf(" You have exited the computer");
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter a number :1 / 2
1 / 2 = 0
Please enter a number :
2 /2
2 / 2 = 1
Please enter a number :
3
*6
3 * 6 = 18
Please enter a number :
练习:输入一个整数,然后按照从大到小依次输出前n个奇数
#include <stdio.h>
int main() {
int a; //定义一个整数类型a
int i= 0; //定义一个整数类型,并赋值0
printf("Please enter an integer :"); //提示:输入一个整数
scanf("%d",&a); //键入数值a
if(a<1){ //判断a是否小于1,
printf("invalid data");
}else {
while (i<a){ //进行while循环,该循环每进行一次i的数值+1,当i的数值大于键入的a时退出循环
printf(" %d\n",2*i+1);
i=i+1;
}
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter an integer :9
1
3
5
7
9
11
13
15
17
Process finished with exit code 0
练习:输出前n个数的和
#include <stdio.h>
int main(){
int a; //定义一个整数型a
int b = 1; //定义一个整数型b赋值为1
int c = 0; //定义一个整数型c赋值0
printf("Please enter a number:"); //输出提示语
scanf("%d",&a); //获取到整数型a的值
if(a<1){ //判断键入的a是否小于1,如果是,程序退出,否则执行while循环;
printf("illegal value");
return 0;
}
while (b<=a){ //判断b循环后是否与输入的a相等,不是则继续执行。
c=c+b; //实际上是b的循环自增。
b=b+1;
}
printf("The sum of the first %d numbers is:%d",a,c);
return 1;
}
输出结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter a number:100
The sum of the first 100 numbers is:5050
Process finished with exit code 1
练习:输出n!
《六》支持最大整数运算
更改计算机的字符类型为long型
#include <stdio.h>
int main(){
char opt; //定义字符型opt,以便输入运算符;
long long number1; //定义变量长整型(long)number1
long long number2; //定义变量长整型(long)number2
long long result; //定义变量长整型(long)result
while (1){
printf("\n>>>\n");
scanf("%lld""%c""%lld",&number1, &opt,& number2);
if(opt == '+'){
result = number1 +number2;
} else if (opt == '-'){
result = number1 - number2;
}else if (opt == '*'){
result = number1 * number2;
}else if (opt == '/'){
result = number1 / number2;
} else if (opt == '%'){
result = number1 % number2;
} else{
printf("illegal value");
continue;
}
printf("%lld %c %lld = %lld",number1,opt,number2,result,"\n" );
}
printf("You have exited the computer!\n");
return 0;
}
输出结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
>>>
100*100
100 * 100 = 10000
>>>
12345*12345
12345 * 12345 = 152399025
>>>
100-200
100 - 200 = -100
>>>
计算int类型最大支持的阶乘数,严格按照int类型支持的最大范围(-32768~32767):
#include <stdio.h>
int main(){
int a = 32767; //定义a为最大的int类型值
int c = 1; //定义c的值为1
int i =2; //定义i的值为2
while (c<a){ //进行while循环,该循环每进行一次i的数值+1,当i的数值大于键入的a时退出循环
printf(" %d! = %d\n",i-1,c); //i-1的目的是输出第几个数。
c=c*i;
i=i+1;
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
Process finished with exit code 0
练习:输出1-7内的一个数的阶乘,
#include <stdio.h>
int main(){
int n; //定义一个整数n;
int i = 1; //定义
int product = 1;
printf("Please enter a number : ");
scanf("%d",&n);
if ((n<0 )||( n>8)){
printf("illegal value!");
return 1;
}
while (i<=n){
product = product * i;
i++;
}
printf("%d!=%d",n,product);
return 0;
}
计算long类型最大支持的阶乘数,严格按照long类型支持的最大范围(-9223372036854775808~9223372036854775807);
#include <stdio.h>
int main(){
long long a =9223372036854775807; //定义a为最大的log类型值
long long c = 1; //定义c的值为1
long long i =2; //定义i的值为2
while (c<a){ //进行while循环,该循环每进行一次i的数值+1,当i的数值大于键入的a时退出循环
printf(" %lld! = %lld\n",i-1,c); //i-1的目的是输出第几个数。
c=c*i;
i=i+1;
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
21! = -4249290049419214848
22! = -1250660718674968576
从结果可知:21!开始为负数失真,可推断long型最大阶乘数为20
练习:输出1-20内的一个数的阶乘,
#include <stdio.h>
int main(){
long long n;
long long i = 1;
long long product = 1;
printf("Please enter a number : ");
scanf("%d",&n);
if ((n<0 )||( n>21)){
printf("illegal value!");
return 1;
}
while (i<=n){
product = product * i;
i++;
}
printf("%lld!=%lld",n,product);
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter a number :17
17!=355687428096000
Process finished with exit code 0
《七》退出计算机
补充逻辑运算符:
| x | y | x&&y | x||y | x! |
| 假 | 假 | 假 | 假 | 真 |
| 假 | 真 | 假 | 真 | 真 |
| 真 | 假 | 假 | 真 | 假 |
| 真 | 真 | 真 | 真 | 假 |
设计计算机退出机制:
#include <stdio.h>
int main(){
char opt; //定义字符型opt,以便输入运算符;
long long number1; //定义变量长整型(long)number1
long long number2; //定义变量长整型(long)number2
long long result; //定义变量长整型(long)result
printf("Enter 0 0 0 to exit the calculator\n"
"");
while (1){
printf("\n>>>\n");
scanf("%lld""%c""%lld",&number1, &opt,& number2);
if ((number1 == 0) &&(opt == 0 )&&(number2 == 0 )){ //使用if语句判断输入的三个参数,是否都为0
break;
}
if(opt == '+'){
result = number1 +number2;
} else if (opt == '-'){
result = number1 - number2;
}else if (opt == '*'){
result = number1 * number2;
}else if (opt == '/'){
result = number1 / number2;
} else if (opt == '%'){
result = number1 % number2;
} else{
printf("illegal value");
continue;
}
printf("%lld %c %lld = %lld",number1,opt,number2,result,"\n" );
}
printf("You have exited the computer!\n");
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Enter 0 0 0 to exit the calculator
>>>
1+2
1 + 2 = 3
>>>
0 0 0
illegal value
>>>
练习:键入一个数,判断其是为素数,还是合数。
#include <stdio.h>
int main(){
int n;
int i = 2;
printf("Please enter an integer :");
scanf("%d", &n);
if(n<2){
printf("invalid data!" );
return 1;
}
while (i<n){
if(n%i==0){
printf("%d is Composite number !" ,n);
break;
}
i = i+1;
}
if (i == n ){
printf("%d is prime number!", n);
}
return 0;
}
结果1:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter an integer :4
4 is Composite number !
Process finished with exit code 0
结果2:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter an integer :17
17 is prime number!
Process finished with exit code 0
练习:鸡兔同笼
#include <stdio.h>
int main(){
int head;
int leg;
int chick;
int rabbit;
printf("Please enter the number of head :");
scanf("%d",&head);
printf("Please enter the number of leg :");
scanf("%d",&leg);
if((head<2)||(leg<6)){
printf("invalid data\n");
return 1;
}
chick=1;
while (chick<head){
rabbit=head-chick;
if (chick * 2 + rabbit *4 == leg){
break;
}
chick = chick+1;
}
if (chick == head){
printf("unsolvable!");
}else {
printf("chick : %d only, rabbit : %d only." ,chick,rabbit );
}
return 0;
}
结果:
C:\Users\13539\CLionProjects\c_hello\cmake-build-debug\c_hello.exe
Please enter the number of head :35
Please enter the number of leg :94
chick : 23 only, rabbit : 12 only.
Process finished with exit code 0