【格院】学习C语言之入坑 IP Lab 1

概述

作为刚入大一、刚入格院的你们,大多数人初识编程,一定对这门学习C语言编程的 IP 课程充满了好奇与向往。同时,在学习完本课程的实验部分后,你将收获许多有用的知识,同时有能力写出一些较为复杂的C语言程序。下面就让我们话不多说,开始这次实验吧!

1.1 基本介绍

所谓程序,就是一组计算机能识别和执行的指令。每一条指令使计算机执行特定的操作。只要让计算机执行这个程序,计算机就会“自动地”执行各条指令,有条不紊地进行工作。为了使计算机系统能实现各种功能,需要成千上万个程序。这些程序大多数是由计算机软件设计人员根据需要设计好的,作为计算机的软件系统的一部分提供给用户使用。

1.2 基本配置

1.2.1 编译环境

推荐使用 Visual Studio Code 或 Visual Studio 或 Dev C++。其在使用时可能需要配置路径,具体问题参见网上教程或询问老师(小编懒得找了) 。

1.2.2 参考书目

推荐参考《C Primer Plus》中译本或原版,及清华大学出版社出版的《C程序设计(第五版)》。

一、Part 1

1.3 The Hello World!

Lab handout 中给出了一段程序:

#include <stdio.h>
int main()
{
    printf("Hello,world!"); // wait for a keypress
    getchar();
    return 0;
}

这段程序作为经典的输出 “hello world” 的程序,大概是每个新手程序猿的开始。代码很经典。其中有一些点值得注意:
头文件
头文件是扩展名为.h的文件,包含了 C 函数声明和宏定义,被多个源文件中引用共享。有两种类型的头文件,分别是程序员编写的头文件和编译器自带的头文件。比如第一行是引用stdio.h头文件便是编译器自带的头文件。这个头文件中包括了一下所用的函数,如printf(),getchar(),main()等,是必不可少的头文件。
对于程序员编写的头文件,一般包括但不限于自定义的各种宏定义,以及对在其他函数及main.c文件中所使用的各种重要常数、变量、自定义结构体、共用体、枚举(这三类后面会学)、函数的声明及定义。
在程序中要使用头文件,需要使用 C 预处理指令 #include 来引用它。

main函数
main函数,又称主函数,是绝大部分C程序唯一的入口,要求有返回值。该返回值返回给(如操作系统)来表明该程序的执行状况。返回 0 代表程序正常执行成功,返回非 0 值代表程序异常结束,因此返回值需要是int整型,于是有了int main()的规范。
在本程序的main()函数内,printf()用于打印字符、数字等,getchar()则用于接受一个字符,这里则是一般用于接收继续执行程序的换行符(换行符也是一个字符!!!),return 0则是表示main() 函数返回 0.
最后输出如下:

Hello,world!

1.4 Student Population

题干要求如下:

Write a short program that displays the number of students in the different cohorts as a list;

  • Print a message similar to the above example.
  • No line should be longer than 80 characters you will need to use multipleprintf()statements.
  • Use both types of comments (single line and multi-line).
  • Use all these escape sequences: \n, , \, ', "

同时给出表格:

EEECEIE
240220100

这里需要我们读入三个值并输出。读入值非常简单,只需使用scanf()scanf_s()函数即可。当然要注意,scanf()函数需要将值存入变量的地址中,因此需要填入&a, &b等。随后,使用printf()函数输出。同时,为了更具可操作性,也可以加入一些提示词语。

int a,b,c;
    printf("Please enter the popluation of EEE!\n");
    scanf_s("%d",&a);
    printf("Please enter the popluation of CE!\n");
    scanf_s("%d",&b);
    printf("Please enter the popluation of IE!\n");
    scanf_s("%d",&c);
    printf("-----------------------------------------\n");
    printf("EEE\t%d\nCE\t%d\nIE\t%d\n",a,b,c);
    return 0;

值得注意的是,%d将由后面部分替换 不同符号对应不同类型,常用如下:

%d%ld%f%lf%c%s
intlong intfloatlong floatcharchar*(字符串)

最后输出如下:

Please enter the popluation of EEE!
240
Please enter the popluation of CE!
220
Please enter the popluation of IE!
100
EEE 240
CE 220
IE 100

2.3.1 Computing the Dimensions of a Box

You are required to write a program, which has three variables with values, height, length and width having values 4, 5, and 6 respectively. Then you are required to calculate the volume of the box, which is the product of all three variables.

在这个实验中,我们需要使用运算符 * 以及 / 。对于整数,除法后如果不是整数,将四舍五入保留整数。同时,我们会遇到一种新的运算符:单精度浮点型 float。
对于计算体积,我们只需要使用公式 weight* width* height。对于计算重量,只需要volume/1.5,于是便有:

printf("The total volume:%.2f\n",height*lengh*width);
printf("Total weight:%.2f",(height*length*width)/1.5);

结果为:

The height of the box:4.00
The length of the box:5.00
The width of the box:6.00
The total volume:120.00
Total weight:80.00

2.4.1 Mathematical Expressions

这个实验要求我们求出一个指数式 ex=1+x+x2/2+x3/6+x4/24
从数学意义上说,这本质上是指数函数的麦克劳林展开式前 4 项。因此,只要使用scanf()函数得到 x 的值,并通过计算即可。

exp_x = 1+x+(float)x*x/2+(float)x*x*x/6+(float)x*x*x*x/24;
printf("exp_%d=%.2f\n",x,exp_x);

结果为

Please enter the value of x!
1
exp_1=2.71

3.1 Tax Calculation

这是Session 1 中较为复杂的一个实验,但本质上是一个选择结构,只需使用if语句和if else语句列出所有情况即可。
选择结构主要使用if语句

if (/* condition */)
{
    /* code */
}else if (/* condition */)
{
    /* code */
}

同时对于多选择,还有switch语句;

switch (expression)
{
case /* constant-expression */:
    /* code */
    break;

default:
    break;
}
BandTaxable salaryScottish tax rate
Basic rate£14,586 - £25,15820%
Intermediate rate£25,159 - £43,43021%
Higher rate£43,431 - £150,00041%
Top rate over£150,00046%

因此,只需对照表格,依照各区间的税率计算相加即可。同时, 为了代码的可读性,常数也可使用数组代替。示例如下:

int sal;
    float tax, txr[5] = {0.19, 0.20, 0.21, 0.41, 0.46},
               txn[5] = {12500, 14585, 25158, 43430, 150000};
    if (sal >= 0 && sal < txn[0])
    {
        tax = 0;
    }
    else if (sal >= txn[0] && sal < txn[1])
    {
        tax = (sal - txn[0]) * txr[0];
    }
    else if (sal >= txn[1] && sal < txn[2])
    {
        tax = (sal - txn[1]) * txr[1] + (txn[1] - txn[0]) * txr[0];
    }
    else if (sal >= txn[2] && sal < txn[3])
    {
        tax = (sal - txn[2]) * txr[2] + (txn[2] - txn[1]) * txr[1] + (txn[1] - txn[0]) * txr[0];
    }
    else if (sal >= txn[3] && sal < txn[4])
    {
        tax = (sal - txn[3]) * txr[3] + (txn[3] - txn[2]) * txr[2] + (+txn[2] - txn[1]) * txr[1] + (txn[1] - txn[0]) * txr[0];
    }
    else if (sal >= txn[4])
    {
        tax = (sal - txn[4]) * txr[4] + (txn[4] - txn[3]) * txr[3] + (txn[3] - txn[2]) * txr[2] + (txn[2] - txn[1]) * txr[1] + (txn[1] - txn[0]) * txr[0];
    }
    else

4.2 Ohm’s Law

这个实验需要我们写出自定义函数。其基本格式为

/* function-type */ /* function-name */(/* varible-types */ /* varibles */)
{
	/* contents */
	return /*return-value*/
}

这里需要我们写出计算电流及串联、并联电阻值的函数。根据基本物理定律,可得

float Ohm_Law(float u,float r)
{
    return(u/r);
}
float series(float a1,float a2)
{
    return(a1+a2);
}
float parallel(float a3,float a4)
{
    return(1/((1/a3)+(1/a4)));
}
float parallel_three(float a5,float a6,float a7)
{
    return(1/((1/a5)+(1/a6)+(1/a7)));
}

再在main函数中适当引用函数,便可计算出整个电路的电流。
值得注意的是,如果把函数写在main函数后面,便需要在main函数中声明。比如说对于并联电阻的函数声明,需要在使用函数前这样声明:

float parallel(float a3,float a4);

小结

在 Part 1 中,我们学习并运用了C语言程序的基本格式、变量类型、运算符、选择结构以及自定义函数。相比于计算机专业的学生,我们更注重于嵌入式系统中的程序,只需掌握基本的语法结构即可。为了更好的学习掌握C语言, 多实践、多写代码是必不可少的环节。只有多实践,才能对各种语法结构和逻辑关系更加熟悉。
同时大家注意保存代码呀,期末要提交的~~~
:这里推荐力扣 (LeetCode),可以提供更多的代码练习题。

  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值