实验1_C语言输入输出和简单程序应用编程
实验任务1
实验程序
task1_1.c
// 打印一个字符小人 #include <stdio.h> #include <stdlib.h> int main() { printf(" 0 \n"); printf("<H> \n"); printf("I I\n"); printf(" 0 \n"); printf("<H> \n"); printf("I I\n"); system("pause"); return 0; }
task1_2.c // 打印一个字符小人 #include <stdio.h> #include <stdlib.h> int main() { printf(" 0 0 \n"); printf("<H> <H>\n"); printf("I I I I \n"); system("pause"); return 0; }
运行结果
task1_1.c

task1_2.c

实验任务2
实验程序
//从键盘上输入三个数据作为三角形边长,判断其能否构成三角形 //构成三角形的条件:任意两边之和大于第三边 #include<stdio.h> #include<stdlib.h> int main () { double a, b, c; //输入三边边长 scanf_s("%lf%lf%lf", &a, &b, &c); // 判断能否构成三角形 if(a+b>c && a+c>b && b+c>a) printf("能构成三角形\n"); else printf("不能构成三角形\n"); system("pause"); return 0; }
运行结果
![]()
实验任务3
实验程序
#include <stdio.h> #include <stdlib.h> int main() { char answer_1, answer_2; printf("今天早起锻炼了吗?是请输入Y或y,不是请输入N或n\n"); answer_1 = getchar(); getchar(); // 此处接收一个回车符,转入answer_2的接收 // printf("今天认真学习了吗?是请输入Y或y,不是请输入N或n\n"); answer_2=getchar(); if((answer_1 == 'Y' && answer_2 == 'Y') || (answer_1 == 'y' && answer_2 == 'y'))//当answer_1和answer_2同时为Y或者y时执行if下列语句,否则执行else下面的语句// printf("\n罗马不是一天建成的,坚持坚持!\n"); else printf("\n冰冻三尺非一日之寒,赶紧行动起来!\n"); system("pause"); return 0; }
运行结果


实验任务4
实验程序
#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("a1 = %d, a2 = %d, a3 = %d\n", a1, a2, a3); getchar(); //接收一个回车,保证下一组数据正确接收// scanf("%c %c %c", &c1, &c2, &c3); printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); getchar(); scanf("%lf %lf", &x, &y); //此处不在末尾加\n,有\n会导致程序在输入双精度浮点数后需要额外输入一个非空白字符才能继续。// printf("x = %lf, y=%lf\n", x, y); return 0; }
运行结果

实验任务5
实验程序
#include <stdio.h> #include <stdlib.h> int main() { int year; double second; second = 1e+9; //数值太大,用指数来接收// year= (int)(second /(365*24*3600)+0.5);//此处进行四舍五入// printf("10亿秒约等于%d年\n", year); system("pause"); return 0; }
运行结果

实验任务6
实验程序
#include<stdio.h> #include<math.h> int main() { double x, answer; while(scanf_s("%lf", &x)!=EOF) //由此可以实现多次计算// { answer = pow(x,365); //幂次计算用pow(),并非使用^来表示// printf("%lf^365=%lf", x,answer); } return 0; }
运行结果



实验任务7
实验程序
#include <stdio.h> int main() { double F, C; while(scanf("%lf", &C) != EOF) { F=32+9.0/5.0*C; //此处不能写成9/5,其取整为1,需修改为浮点数除法// printf("%lf摄氏温度对应的华氏温度是%.2lf",C,F); } return 0; }
运行结果

实验任务8
实验程序
#include <math.h>
#include <stdio.h>
int main()
{
double a, b, c, s, p;
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF) //实现多次输入运行//
{
// 判断能否构成三角形
if(a+b>c && a+c>b && b+c>a)
{
p=1.0/2.0*(a+b+c);
s=sqrt(p*(p-a)*(p-b)*(p-c)); //海伦公式//
printf("三角形面积为%.3lf\n", s);
}
else
{
printf("不能构成三角形");
}
}
return 0;
}
运行结果



浙公网安备 33010602011771号