实验1 C语言输入输出和简单程序编写

实验结论

1.实验任务1

task1_1源代码

#include<stdio.h>
#include<stdlib.h>

int main()
{
	printf(" O \n");
	printf("<H>\n");
	printf("I I\n");
	printf(" O \n");
	printf("<H>\n");
	printf("I I\n");
	system("pause");
	return 0;
}

task1_1运行结果

image

task1_2源代码

#include<stdio.h>
#include<stdlib.h>

int main()
{
	printf(" O        O\n");
	printf("<H>      <H>\n");
	printf("I I      I I\n");

	system("pause");
	return 0;
}

task1_2运行结果

image

2.实验任务2

task2源代码

// 从键盘上输入三个数据作为三角形边长,判断其能否构成三角形
// 构成三角形的条件: 任意两边之和大于第三边

#include <stdio.h>
#include<stdlib.h>

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

    // 输入三边边长
    scanf("%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;
}

task2运行测试结果

image

image

image

3.实验任务3

task3源代码

#include <stdio.h>
#include<stdlib.h>
int main()
{
	char ans1, ans2;  // 用于保存用户输入的答案

	printf("每次课前认真预习、课后及时复习了没? (输入y或Y表示有,输入n或N表示没有) :  ");
	ans1 = getchar(); // 从键盘输入一个字符,赋值给ans1

	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");
	system("pause");

	return 0;
}

task3运行测试结果

image

image

image

image

问题回答

  • 把源代码line9(本文在line10)即 getchar();去掉后的结果图为
    image

    • 原因:第一次输入为 y↵,本文程序line8的getchar()获取到y存入ans1中,而由于去掉了line10,结果导致程序(未去掉line10时)line13的getchar()获取到了第一次输入中y后面的并存入了ans2中,从而使程序不进行第二次输入便输出了结果.

4.实验任务4

能正确运行的task4源代码

#define _CRT_SECURE_NO_WARNINGS 1
#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);//错误:a1->&a1
	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);//错误:%f->%lf
	printf("x = %f, y = %lf\n",x, y);

	system("pause");

	return 0;
}

数据输入及运行结果

image

5.实验任务5

能正确运行的task5源代码

// 计算10亿秒约等于多少年,并打印输出
#include<stdlib.h>
#include <stdio.h>

int main() 
{
    int year;
    double year_s;
    year_s = 1.0e+9 / 60 / 60 / 24 / 365;
    year = year_s + 0.5;

    printf("10亿秒约等于%d年\n", year);
    system("pause");
    return 0;
}

运行测试结果截图

image

6.实验任务6

task6_2源代码

#define _CRT_SECURE_NO_WARNINGS 1
#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;

}

运行测试截图

image

7.实验任务7

task7源代码

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{

	double c, f;

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


	return 0;
}

运行测试截图

image

8.实验任务8

task8源代码

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>

int main()
{

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

	}


	return 0;
}

运行测试截图

image

实验总结

  • vs对于多个main函数导致的冲突还能把暂时不需要参与编译的源文件的属性中的从生成中排除改为
    image
    ps:不要忘了在编译的时候把需要编译的文件改为
  • task8在line12第一次写成s = (a + b + c) / 2;结果因为a,b,c都是整型最后因为是整型的原因,s被截取了整数部分导致最后结果不正确
  • 依然存在不小心漏掉;的问题
posted @ 2025-03-04 17:39  yjk2053  阅读(47)  评论(0)    收藏  举报