C語言第二次實驗 非常有趣!
阿新 • • 發佈:2020-11-04
//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; }
//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("%1f\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; }
// ex3.cpp #include <stdio.h> #include <stdlib.h> int main() { double x,y; char c1,c2,c3; int a1,a2,a3; scanf("%d%d%d",&a1, &a2, &a3); printf("%d,%d,%d\n",a1,a2,a3); scanf("%c%c%c", &c1, &c2, &c3); printf("\'%c\'\'%c\'\'%c\'\n",c1,c2,c3); scanf("%lf, %lf", &x, &y); printf("%.1lf, %.1lf\n", x, y); system("pause"); return 0; }
//ex4.cpp #include<stdio.h> int main(){ char x; x=getchar(); if( x >= 48 && x <= 57 ) printf("%c是數字字元", x); else if ( x >= 65 && x <= 90 ) printf("%c是大寫字母字元",x); else if( x >= 97 && x <= 122 ) printf("%c是小寫字母字元",x); else printf("%c是其他字元",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'||ans1=='Y') && (ans2=='y'||ans2=='Y')) printf("\n羅馬不是一天建成的\n:)"); else printf("\n羅馬不是一天毀滅的\n"); return 0; }
#include<stdio.h> #include<math.h> int main(){ int n,a1=1,q=2; double sum; scanf("%d",&n); sum = (a1*(1-pow(q,n)))/(1-q); printf("當n=%d時,,sum=%.0lf",n,sum); return 0; }