// // main.c // C_Test // // Created by wangpeng on 15/6/16. // Copyright (c) 2015年 wangpeng. All rights reserved. //
#include <stdio.h>
int main(int argc, const char * argv[]) {
void test6(); test6();
printf("program end!");
return 0; }
/* 数据类型强制转换时的精度问题: */ void test1(){ float f1 = 12.3f; float f2 = 78.9f; printf("f1 = %d\n",(int)f1);//结果:f1 = 12 printf("f2 = %d\n",(int)f2);//结果:f2 = 78 //没有四舍五入,而是只取整数部分 }
/* printf和scanf截取有效数字时的精度问题: */ void test2(){ float f1 = 3.1415926f; printf("f1 = %.2f\n",f1);//结果:f1 = 3.14 printf("f1 = %.3f\n",f1);//结果:f1 = 3.142 //有四舍五入 }
/* 算术运算符/符合赋值运算符/自增自减运算符的优先级问题: */ void test3(){ int i = 5, j = 5, p, q; // p=i+++i+++i++; // q=j+++j+++j++; printf("p = %d,q = %d,i = %d,j = %d\n",p,q,i,j);
int a = 5, b = 5, result; result = a += b; printf("result = %d,a = %d, b = %d\n",result,a,b); }
/* i++和++i的问题 */ void test4(){ int i = 3,j = 3,result1 = 0,result2 = 0;
result1 = i++; printf("i = %d,i++ = %d\n",i,result1);//result1 = 3
result2 = ++j; printf("j = %d,++j = %d\n",j,result2);//result2 = 4 }
/* 真假问题,值得注意的是负数以及非零整数取反的问题 */ void test5(){ int a = 1,b = 2,c = 3,d = 4; printf("c = %d\n",c); c = a > b; printf("c = %d\n",c); printf("d = %d\n",d); printf("!d = %d\n",!d); printf("!55 = %d\n",!55); printf("!!55 = %d\n",!!55); int result = -1; result = a>a+b<c+d; printf("result = %d\n",result); int x = -2,y = -1,z; z = x&&y; printf("z = %d,!x = %d,!y = %d\n",z,!x,!y); }
/* if-else计算器,可以换成switch语句,也可直接换成输入数学式子,具体代码自行实现,很简单的,这里就不一一展示了 */ void test6(){ int a,b; char op; float c = 0.0f; printf("输入第一个数字:\n"); scanf("%d",&a);
printf("输入一个符号:\n"); scanf("%c%c",&op,&op);
printf("输入第二个数字:\n"); scanf("%d",&b);
if (op=='+') { printf("%c\n",op); c=a+b; } else if(op=='-'){ printf("%c\n",op); c=a-b; }else if (op=='*'){ printf("%c\n",op); c=a*b; }else if (op=='/'){ printf("%c\n",op); c=a/b; }else{ printf("%c not a op\n",op); }
printf("%d %c %d = %.2f\n",a,op,b,c); }
|