C语言表达式编程应用及输入输出函数
// ex1.cpp #include <stdio.h> int main() { int a=5, b=7, c=100, d, e, f; d = a/b*c; e = a*c/b; f = c/b*a; printf("d=%d, e=%d, f=%d\n",d,e,f); return 0; }

a/b*c
a*c/b
c/b*a
原因 均为整形数据
d:a/b=0 0*c=0
e:a*c=700 700/b=71
f:c/b=14 14*a=70
// ex2.cpp #include <stdio.h> int main() { int x=1234; float f=123.456; double m=123.456; char ch='a'; char a[]="Hello, world!"; int y=3, z=4; printf("%d %d\n", y, z); printf("y=%d, z=%d\n", y,z); printf("%8d,%2d\n", x,x); printf("%f, %8f, %8.1f, %0.2f, %.2e\n",f,f,f,f,f); printf("%lf\n",m); printf("%3c\n", ch); printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a); return 0; }

d:按十进制整数输出、输入
c:按字符输出、输入
f:按浮点数输出、输入
// ex3.cpp #include <stdio.h> int main() { double x,y; char c1,c2,c3; int a1,a2,a3; scanf("%d%d%d",&a1,&a2,&a3); printf("a1=%d,a2=%d,a3=%d\n",a1,a2,a3); scanf("%c%c%c",&c1,&c2,&c3); printf("c1=%c,c2=%c,c3=%c\n",c1,c2,c3); scanf("%lf%lf",&x,&y); printf("x=%.1lf,y=%.1lf\n",x,y); return 0; }

// ex4.cpp // 判断字符类型 #include <stdio.h> int main() { char x; x = getchar(); if(x>='0'&&x<='9') printf("%c是数字字符\n", x); else if(x>='a'&&x<='z'||x>='A'&&x<='Z') printf("%c是英文字母\n", x); else printf("%c是其它字符\n", x); return 0; }



// ex5.cpp #include <stdio.h> int main() { char ans1, ans2; printf("复习了没? (输入y或Y表示复习了,输入n或N表示没复习) : "); ans1 = getchar(); getchar(); printf("\n动手敲代码了没? (输入y或Y表示敲了,输入n或N表示木有敲) : "); ans2 = getchar(); if(ans1=='Y'&&ans2=='Y'||ans1=='Y'&&ans2=='y'||ans1=='y'&&ans2=='y'||ans1=='y'&&ans2=='Y') printf("\n罗马不是一天建成的:)\n"); else printf("\n罗马不是一天毁灭的。。。\n"); return 0; }



# include <stdio.h> # include <math.h> int main() { int n,sum; scanf("%d",&n); if(n>=1&&n<=10) { sum=(1-pow(2,n))/(1-2); printf("n=%d,sum=%d",n,sum); } else printf("你输入的数据不在范围内"); return 0; }




浙公网安备 33010602011771号