C語言實驗作業

陈欣然發表於2024-03-14

實驗任務一
(1)

//列印一個字元小人

include <stdio.h>

int main()
{
printf(" o \n");
printf("\n");
printf("I I\n");

return 0;

}

(2)

include <stdio.h>

int main()
{
printf(" o \n");
printf("\n");
printf("I I\n");

printf(" o \n");
printf("<H>\n");
printf("I I\n");


return 0;

}

(3)

include <stdio.h>

int main()
{
printf(" o o \n");
printf(" \n");
printf("I I I I\n");

return 0;

}

實驗任務二

//從鍵盤上輸入三個資料作為三角形邊長,判斷其能否構成三角形
//構成三角形的條件:任意兩邊之和大於第三邊

include <stdio.h>

int main()
{
float a, b, c;

scanf_s("%f%f%f", &a, &b, &c);

if (a + b > c && a + c > b && b + c > a)
	printf("能構成三角形\n");
else
	printf("不能構成三角形\n");

return 0;

}

實驗任務三

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;

}

如果去掉本行程式碼,程式在第二次輸入後會立刻結束,因此getchar()程式碼在這裡起到結束後的停頓緩衝作用。
//getchar()把末尾Enter鍵吃掉

實驗任務四

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);
    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("%f,%lf",&x,&y);
	scanf("%lf%lf", &x, &y);
    printf("x=%f,y=%lf\n", x, y);

    system("pause");
    return 0;

}

實驗任務五

include<stdio.h>

include<stdlib.h>

int main()
{
int year;
year = 1e9 / (60 * 60 * 24 * 365) + 0.5;

printf("10億秒約等於%d年\n", year);

system("pause");

return 0;

}

實驗任務六

(1)

include <stdio.h>

include <math.h>

int main()
{
double x, ans;

scanf("%lf", &x);
ans = pow(x, 365);
printf("%.2f的365次方: %.2f\n", x, ans);

return 0;

}

(2)

include <stdio.h>

include <math.h>

int main()
{
double x, ans;

while(scanf("%lf", &x) != EOF)
{
   ans = pow(x, 365);
   printf("%.2f的365次方: %.2f\n", x, ans);
   printf("\n");
}


return 0;

}

實驗任務七

include <stdio.h>

include <stdlib.h>

int main()
{
double c, f;

while(scanf("%lf", &c) != EOF)
{
	f=9*c/5+32;
	printf("攝氏度c=%.2lf,華氏度f=%.2lf\n", c, f);
	 
}

system("pause");
return 0;

}


實驗任務八

include <stdio.h>

include <math.h>

int main()
{
int a, b, c;
double area, s, s1;

while (scanf_s("%d%d%d", &a, &b, &c) != EOF)
{
    s = (a + b + c) / 2.0;
    s1 = s * (s - a) * (s - b) * (s - c);
    area = sqrt(s1);
    printf("a = %d, b = %d, c = %d, area = %.3f\n", a, b, c, area);
}

return 0;

}

相關文章