200 likes | 415 Views
《 高级语言程序设计 I》 实验指导. 实验一 程序设计基础( 1 ) 目的: 1 、熟悉实验环境( Turbo C & Lcc-win32 & C-Free ),推荐使用 C-Free ; 2 、会编写简单的 C 语言程序; 内容: 1 、输入并运行程序“ Hello , World” 2 、输入、运行并理解课本程序:华氏温度转摄氏温度 3 、请参照例题,编写一个 c 程序,输出以下信息: ******************** Very Good ********************
E N D
实验一 程序设计基础(1) 目的: 1、熟悉实验环境(Turbo C & Lcc-win32 & C-Free),推荐使用C-Free; 2、会编写简单的C语言程序; 内容: 1、输入并运行程序“Hello,World” 2、输入、运行并理解课本程序:华氏温度转摄氏温度 3、请参照例题,编写一个c程序,输出以下信息: ******************** Very Good ******************** 4、编写程序实现从键盘上输入两个整数,输出此两数之和 <选做> 5、编写一个c程序,输入a,b,c三个值,输出其中最大者。
实验二 程序设计基础(2) 目的: 1、熟练掌握C语言的基本数据类型; 2、熟练掌握变量的定义和不同类型变量的使用; 3、熟练掌握常量的表示和使用; 内容: 1、数据类型:四种基本数据类型(int, char, float, double);带修饰符的数据类型(short int, long int, signed char, unsigned char, unsigned int)。 2、常量:就是在程序运行过程中,其值不能被改变的量。包括整型常量、实型常量(浮点型常量)、字符常量和字符串常量。
(1)整型常量 #include <stdio.h> #define A1 123 main() { printf(“%d”,A1); } 1)分析:将常量A1分别定义为123、-123、3298730219、-3298730219、23478659087,程序运行结果有什么不同?原因是什么? 2)分析:将常量A1分别定义为026、-0154、0x21、-02x21、0x2c6,程序运行结果有什么不同?原因是什么? 3)将程序修改如下: #include <stdio.h> #define A1 3298730219u main() { printf(“%d, %u”,A1,A1); } 分析:将常量A1分别定义为3298730219u、-1298730219u、4294967296u、4294967295u,程序运行结果有什么不同?原因是什么?
(2)实型常量 #include <stdio.h> #define A1 1234567.8912345 main() { printf(“%f”,A1); } 分析:将常量A1分别定义为123456.111111f、123456.111111,1234567.98707487f,123456789123.98707487f,123456789123.98707487,1234567890123456789.12345678,程序运行结果有什么不同?原因是什么?
(3)字符串常量——运行以下程序,分析运行结果(3)字符串常量——运行以下程序,分析运行结果 #include <stdio.h> #define STR1 “Hello World” #define STR2 “Hello ”“World” #define STR3 “Hello ” “World” #define STR4 “Hello ”\ “World” main() { printf(“%s\n”,STR1); printf(“%s\n”,STR2); printf(“%s\n”,STR3); printf(“%s\n”,STR4); }
2-1 写出以下程序运行结果,并说明每一转义字符的含义。 main() { char c1=‘a’, c2=‘b’, c3=‘c’, c4=‘\101’, c5=‘\116’ ; printf (“a%c b%c\t c%c\t abc\n”, c1,c2,c3); printf(“\t b%c %c”,c4,c5); } 2-2 main() 能否改写成 main() { char c1,c2; { int c1,c2; c1=97; c2=98; c1=97; c2=98; printf (“%c %c”, c1,c2); printf(“%c %c”, c1,c2); } } 分析运行时会打印出什么信息?为什么?
<选做> 2-3 改写以下程序 #include <stdio.h> int strlen(char s[]) { int i; i=0; while(s[i]!='\0') ++i; return i; } main() { int len; len=strlen("hello"); printf("the length of string is %d\n",len); } (1)增加输入语句,使程序能够根据用户输入,计算出不同的字符串长度。 (2)不使用strlen函数定义,直接用系统函数求出字符串长度。
double float 高 long unsigned char, short int 低 实验三 数据类型与运算符 目的: 1、熟练掌握算术运算(+, -, *, /, %)的含义和使用 2、熟练掌握关系运算(>, <, >=, <=)、逻辑运算(!, &&, ||)和等号运算(==,!=)的含义和使用 3、熟练掌握运算的优先级和运算方向(见课本53页) 4、熟练掌握类型转换方向 (1)横向箭头表示一定要有的转换 (2)即使是两个float数据运算也要 转换成double型 (3)箭头只表示转换方向,并不表示 转换次序,如果int型数据与double型 数据运算,则直接转换成double型,不 用经过中间转换。
内容: 3-1 求下面算术表达式的值(编写程序,求出结果,并分析运算过程) (1) x+a%3*(int)(x+y)%2/4 设x=2.5, a=7, y=4.7 ((int)(x+y)表示强制类型转换,即强制把(x+y)的运算结果转换为int型) (2) (float)(a+b)/2+(int)x%(int)y 设a=2, b=3, x=3.5, y=2.5 3-2 写出下面表达式运算后a的值,设原来a=12 (1) a+=a (a+=a表示a=a+a) (2) a-=2 (3) a*=2+3 (4) a/=a+a (5) a%=(n%=2) (n的值等于5) (6) a+=a-=a*=a (编写程序,求出结果,并分析运算过程) 注意运算符的优先级以及运算的顺序是自左至右还是自右至左 3-3 编写程序,调用库函数,求出以下函数值 (1)cos3.5678 (2)lg90 (3)
实验四 目的: 1、进一步理解Increment和Decrement操作 2、进一步理解不同数据类型对程序运行结果的影响 3、初步学会使用分支语句、循环语句与简单的函数调用 内容: P13 (1-3) Modify the temperature conversion program to print a heading above the table. P13 (1-4) Write a program to print the corresponding Celsius to Fahrenheit table. P46 4-3 Write the function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F.
实验五 目的: 1、通过比较Condition Expression与If-Else,熟练掌握Condition Expression 2、掌握位运算与逻辑运算的区别 内容: P48 (2-4) Write an alternate version of squeeze(s1, s2) that deletes each character in s1 that matches any character in the string s2. P48 (2-5) Write the function any(s1, s2), which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2. (The standard library function strpbrk does the same job but returns a pointer to the location.) P52 (2-10) Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else. <Optional> P49 (2-7) Write a function invert(x, p, n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
实验六 目的: 1、熟练掌握C语言程序的组成:Statement与Blocks 2、熟练使用分支语句,理解If-Else语句与Switch语句的区别 3、熟练使用循环语句,掌握While语句与For语句的语法结构 内容: P58 (3-1) Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time. P63 (3-3) Write a function expand(s1, s2) that expands shorthand notations like a-z in the string s1 into the equivalent compete list abc…xyz in s2.
实验七 目的: 1、熟练使用分支语句,理解If-Else语句与Switch语句的异同 2、掌握循环语句格式,使用While、For和Do-while语句的熟练编程 3、掌握嵌套循环语句的执行 内容: 7-1 编程计算并输出y=ex和 其中x=1,2,…,20 注意:引用到数学库函数(math.h)中的函数exp(x)和sqrt(x) 请分别用for、while和do-while语句实现 7-2、“鸡兔同笼问题”是我国古代的一个著名数学问题,讲的是:在同一个笼子中养着鸡和兔,但只知道鸡和兔的总数是m,鸡和兔的总脚数为n,试求鸡和兔各有多少只? 7-3、任给一成绩(按百分制),按以下规则输出其相应的类别。 90~100 优秀(excellent), 80~89 良好(good) 70~79 中等(mid), 60~69 及格(pass) 60分以下 不及格(fail)
7-4、百钱买百鸡问题,公鸡每只5元,母鸡每只3元,小鸡一元两只,问100元钱买一百只鸡有多少种方案?输出每种方案中公鸡、母鸡和小鸡的数量。7-4、百钱买百鸡问题,公鸡每只5元,母鸡每只3元,小鸡一元两只,问100元钱买一百只鸡有多少种方案?输出每种方案中公鸡、母鸡和小鸡的数量。 (提示:使用多重循环) 7-5、求Fibonacci数列:1,1,2,3,5,8,…的前40个数,即 F1=1 (n=1) F2=1 (n=2) Fn=Fn-1+Fn-2 (n>=3) 7-6、两个乒乓球队进行比赛,各出3人,甲队为A,B,C三人,乙对为X,Y,Z三人,已抽签决定比赛三名单。有人向队员打听比赛的名单,A说他不和X比赛,C说他不和X,Z比,请编程序找出三对赛手的名单。 7-7、译密码。为使电文保密,往往按一定的规律将其转换成电文,收报人再按约定的规律将其译回原文。例如:将A变成字母E,a变成字母e,即变成其后的第四个字母。如”China!”变成“Glmre!” 输入一行字符,要求输出其相应的密码。
实验八 目的: 1、熟练掌握字符串操作 2、初步了解函数调用 内容: 8-1(p20 1-8):Write a program to count blanks, tabs, and newlines. 8-2(p21 1-12):Write a program that prints its input one word per line. 8-3(p27 1-15):Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. 8-4(p73 4-2):Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
实验九 目的: 1、熟练掌握函数的定义结构和函数调用 return-type function-name (argument declarations) { declarations and statements [return expression] } 2、注意区分实际参数与形式参数 实参与形参之间的数据传递方式是“值传递”或“单向传递”。即只能将实参的值传递给形参,不能将形参的值传递给实参,因为实参与形参分配的是不同存储单元,形参的变化不会影响到对应的实参。(如果形参是数组名,则传递的是数组首地址) 3、熟练掌握各种不同变量的定义与使用
内容: 9-1:写出下列程序运行的结果 (1)#include “stdio.h” int z; f(int x) { x=2; z=3; z+=x; } main() { z=5; f(z); printf(“z=%d\n”,z); } (2)#include “stdio.h” main() { int a=1, b=2, c=3; ++a; c+=++b; printf(“First:%5d%5d%5d\n”,a,b,c); { float b=2.0; int c; c=b*3; a+=c; printf(“Second:%5d%5.1f%5d\n”,a,b,c); } printf(“Third:%5d%5d%5d\n”,a,b,c); }
9-2:编写一个函数求两个整数的最大公约数和最小公倍数,在主函数中输入数据。9-2:编写一个函数求两个整数的最大公约数和最小公倍数,在主函数中输入数据。 9-3:编写一个判断素数的函数,在主函数中输入一个整数,输出是否素数的信息。
实验十 目的: 1、初步掌握递归思想,能够编写简单的递归程序 2、掌握宏定义的应用 内容: 10-1:运用printd函数的思想,编写一个递归版本的itoa函数,即通过递归调用把整数转换成字符串。 10-2:编写一个递归版本的reverse(s)函数,把字符串s颠倒过来。 10-3:设有以下用宏定义的输出格式: #define NL putchar(‘\n’) #define PR(format,value) printf(“value=%format,(value)”) #define PRINT1(format,x) PR(format,x); NL #define PRINT2(format,x1,x2) PR(format,x1); PRINT1(format,x2) 如果在程序中有如下的宏引用: PR(d,i); PRINT1(d,j); PRINT2(d,k,l); 写出宏展开后的情况,并写出各输出结果。设i=3, j=4, k=5, l=6。