




已阅读5页,还剩43页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一次课熟悉winTC编译环境、熟悉C语言程序结构1.使用C 语言编译环境,输入下面的源程序。将你的程序命名为hello.c,然后编译运行它。/* program writes the words Hello, world to the screen*File : Hello.c* By : NJCIT* Date : 07-03-09*/#include main()printf(Hello, worldn);return(0);2.main() /*求两数之和*/ int a,b,sum; /* 这是变量定义*/ a=123;b=456; sum=a+b; printf(“sum is %dn”,sum); 回答下列问题:1. C语言中的标识由 字母 、 数字 和 下划线 组成,以 字母 和 下划线 开头,不可使用 关键字 。2. C语言源程序从 main 开始执行?每个C语言程序必须有一个且只能有一个主函数,主函数的名字为 main 。 C程序的函数由 函数头 和 函数体 两部分构成,函数头包括 函数属性 、 函数类型 、 函数名 和 形参表 ;函数体由一系列的语句组成,语句由 分 号结束,函数休包含在一对 花 括号中。程序中的注释内容是用符号 /* */ 界定。2. 计算机上实现C语言程序要经过 编辑 (产生*. C 文件)、 编译 (产生*. OBJ 文件)、 链接 (产生*. EXE 文件)和 调试 四个阶段。第二次课熟悉printf()函数、常见转义字符及各种数据类型的输出格式1.#include main()printf(n12345678901234567890);printf(nnnn a few new lines );printf(nttand nsome ntabs);printf(nand a beep just to be heard.an);printf(nthi);printf(s wor);printf(ks toon);return (0);(1) printf()函数的功能是什么?按指定的格式在屏幕上显示指定的内容(2) 在 printf()函数中n的起什么作用?回车换行(3) 在 printf()函数中t的起什么作用?水平跳格(4) 在 printf()函数中a的起什么作用?蜂呜器响2. main() int a=5,b=7,c=-1;float x=67.8564,y=-789.124;char c=A;long n=1234567;unsigned u=65535;printf(“%d%dn”,a,b);/*57*/printf(“%3d%3dn”,a,b); /* 5 7*/printf(“%f,%fn”,x,y); /*67.856400,-789.124000*/printf(“%-10f%-10fn”,x,y); /*67.856400 ,-789.124000*/printf(“%8.2f,%8.2f,%.4f,%.4f,%3f,%3fn”,x,y,x,y,x,y);/* 67.85, -789.12,67.8564,-789.1240,67.856400,-789.124000*/printf(“%e,%10.2en”,x,y);/*6.78564e+01, -7.9e+02*/printf(“%c,%d,%o,%xn”,c,c,c,c);/*A,65,101,41*/printf(“%ld,%lo,%xn”,n,n,n);/*1234567,4553207,d687*/printf(“%u,%o,%x,%dn”,u,u,u,u);/*65535,177777,ffff,-1*/printf(“%s,%5.3sn”,”COMPUTER”,”COMPUTER”);/*COMPUTER, COM*/3. 假设下面的例子都是完整程序的一部分,那么他们每一个将会输出什么?a. printf(Baa Baa Black Sheep.);b. printf(Have you any wool?n);c. printf(Begone!nO creature of lard!);d. printf(What?nNolnBonzo?n);e. int num;num = 2;f. printf(%d + %d = %d, num, num, num + num);4.加载,编译并运行下面的程序。显示输出界面然后回答下面的问题。#include main()char ch;int x;float y;double z;ch = A;printf(ch = %c and its ascii value is %d. What is ASCII I wonder?n,ch, ch);x = 10;printf(x = %dn, x);y = 3.1415926;printf(y = %fn, y);z = 4.75E5;printf(z = %lfn, z);return(0);1. 字符的A的ASCII码是多少?652. “%f”默认情况下小数点后面有几位数字?6位3. 程序中的字符被指定的值为A,为什么不是“A”?A为字常量,A为字符串常量4. 改变程序使它以10个字符位宽度和2位小数的形式输出浮点型数据。%f改为%10.2f5. 改变程序使它以6个字符位宽度输出整型数,左对齐。%d改为-6d%3.问答题1)C语言的基本数据类型有几种,分别是什么,并指出各种数据类型的关键字?变量类型字节长度值范围无符号数(unsigned)有符号数(signed)字符(char)10255或ASC码-128127整形(int)2065535-3276832767短整形(short)2065535-3276832767长整形(long)404294967295-21474836482147483647浮点(float)43.410-38 3.410+38双精度(double)81.710-308 1.710+308高精度(long double)103.410-4937 1.110+49322)描述C语言中标识符的组成: 标识符由字母(AZ、a z)、数字(09)和下划线组成,标识符必须以字母和下划线开始。不可使用关键字。3)常量和变量的区别是什么?常量:在程序运行过程中,其值不能被改变的量。变量:在程序运行过程中其值可以改变的量。4)在程序中如何使用变量? 在程序中,首先根据所需处理的数据类型选择合适的变量类型定义变量,然后用合适的运算符、表达式、函数对变量进行操作。5)从下面列出标识符中选出哪些可以用作合法的C用户定义标识符,哪些不能使用。为什么?(1) a3_b3 合法(2)void 非法:关键字(3) _123 合法 (4)123_ 非法:数字开头(5) IF 合法:但不好,容易与if混淆(6) INT 合法:但不好,容易与int混淆(7) For 合法:但不好,容易与for混淆 (8) printf 合法:但不好,与库函数printf同名 (9) WORD 合法: (10) define合法:但不好,容易与宏命令#define混淆 (11) _abc 合法: (12) sizeof 非法:关键字(13) answer 合法:(14) to 合法:(15)signed 非法:关键字(16) Case 合法:但不好,容易与case混淆(17)_if 合法:(18) extern 非法:关键字(19) putchar 合法:但不好,与库函数putchar同名(20) _double合法:6)请选出正确的数值和字符常量,说明类型;对于不正确的数,说明原因(1)0.0 正确:浮点数(2) 5L 正确:长整形(3) o13 错误:八进制数开头用数字0,而不是字母o(4) 0Xff 正确:整形数的十六进制形式,相当于十进制数255; (5) oxaa 错误:十六进制数开头用数字0加x,而不是字母o加x(6) 018 错误:八进制数中的数字应在0-7的范围内(7) 9861 正确,整形数的十进制形式(8) 011 正确,整形数的八进制形式(9) 3.987E-2 正确:浮点数的指数形式 (10) .987 正确:浮点数小数点前如为0,可省略(11) 0xab 正确:表示十六进制值为0xab的ASCII码字符 (12) 50. 正确:浮点数小数点后如为0,可省略(13) 8.9e1.2 错误:浮点数的指数应为整数(14) 1e1 正确:浮点数的指数形式(15)0xFF00 正确:整形数的十六进制形式(16) 0.825e2 正确:浮点数的指数形式(17)473 正确,整形数的十进制形式(18) OX4 错误:十六进制数开头用数字0加x,而不是字母o加x(19) “c” 错误:字符常量应用单引号,双引号是字符串(20)t 正确:字符常量,表示字母t的ASCII码(21) ” 错误: 右侧使用了双引号(22)0 正确:字符常量,表示数字0的ASCII码(23)0 正确:字符常量,表示十进制值为0的ASCII码(24) A 正确:字符常量,表示字母A的ASCII码4选择题(1)合法的字符常量是 。A、C、DA) t B) “A” C) a D)x32(2) 合法的字符常量是 。DA) 084 B) 84 C) ab D)x43(3)是C语言提供的合法的数据类型关键字。BA) Float B) signed C) integer D)Char(4)在以下各组标识符中,合法的标识符是。DA) A)B01 B)table_1 C) 0_t D) k%B) A)Fast_ B) void C)pbl D)C) A)xy_ B)longdouble C)*p D)CHARD) A) sj B)Int C)_xy D)w_y23(5)属于合法的C语言长整型常量的是 。A)5876273 B)0L C)2E10 D)(long)5876273(6)下面选项中,不是合法整型常量的是A)160 B)0xcdg C)01 D)0x48a第三次课熟悉scanf()函数的使用:1. 用下面的scanf()函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=A,c2=a;main() int a,b; float x,y; char c1,c2; scanf(“a=%d b=%d”,&a,&b); scanf(“ x=%f y=%e”,&x,&y); scanf(“ c1=%c c2=%c”,&c1,&c2);printf(“a=%d b=%d”,a,b); printf(“ x=%f y=%e”,x,y); printf(“ c1=%c c2=%c”,c1,c2);2.加载,编译并运行下面的程序然后回答下面的问题。#include main()int user_age;char user_name51;/* Get the users name */printf(Enter your name :);scanf(%s, user_name);/* Get the users age */printf(Enter your age in years :);scanf(%d, &user_age);/* Print out their name and age in days */printf(Gday %s, you are %d days oldn, user_name, user_age*365);return(0);1. 存储用户名的变量名是什么?2. 改写这个程序使他可以用一个单独的变量以天的形式存储用户的年龄。3.使用 scanf() 读取多行输入#include main()int user_age;char user_name51;/* Get the users name and age*/printf(Enter your name followed by your age in years (eg fred 23) :);scanf(%s %d, user_name, &user_age);/* Print out their name and age in days */printf(Gday %s, you are %d days oldn, user_name, user_age*365);return(0);1. 当你在回答问题时颠倒了年龄和姓名会出现什么情况?2. 当你在代码中省去了&时会出现什么情况?4编程题(1) 已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序来。#include Stdio.hmain(void)int a,b,c;printf(a=);scanf(%d,&a);printf(b=);scanf(%d,&b);printf(a=%d,b=%dn,a,b);c=a;a=b;b=c;printf(a=%d,b=%dn,a,b);getch();(2)若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=a,c2=b。想得到以下的输出格式和结果,请写出程序(包括定义变量类型和设计输出)。a= 3 b= 4 c= 5x=1.200000,y=2.400000,z=-3.600000x+y= 3.60 y+z=-1.20 z+x=-2.40u= 51274 n= 128765c1=a or 97(ASCII)c2=b or 98(ASCII)#include Stdio.hmain(void)int a=3,b=4,c=5;float x=1.2,y=2.4,z=-3.6;unsigned int u=51274;long n=128765;char c1=a,c2=b;printf(a=%2d b=%2d c=%2dn,a,b,c);printf(x=%f,b=%f,c=%fn,x,y,z);printf(x+y= %.2f,y+z=%.2f,z+x=%.2fn,x+y,y+z,z+x);printf(u= %u n= %ldn,u,n);printf(c1=%c or %d(ASCII)n,c1,c1);printf(c2=%c or %d(ASCII)n,c2,c2);getch();(3)输入一个华氏温度,要求输出摄氏温度,公式为:,取两位小数。#include Stdio.hmain(void)float c,f;printf(f=);scanf(%f,&f);c=5.0/9*(f-32);printf(c=%.2f f=%.2fn,c,f);getch();5.请判断以下表达式是否正确,若正确,写出表达式的值;若不正确,写出出错原因。各变量的类型说明如下:int i=8, j=3, k, a, b;unsigned long w=5, u;double x=1.42 , y=5.2 , t ;(1) k=i+ 8 (2) (int)x+0.4 1.4 (3) w+=-2 3 (4) y+=x+6.62(5) i/=j+12 0(6) k=-i 7 (7) f=3/2*(t=30.0-10.0) 20.0 (8) k=(a=2,b=3,a+b) 5(9) a+=a-=(b=4)*(a=3) -18 (10) a=2*a=3 错,赋值语句的左边应为变量,不可为表达式,2*a为表达式。(11) u=65535, j=-1,u=u+j (12) +(i+j) ? 错,可对变量进行自增运算,不可对表达式进行自增运算,i+j为表达式。(13) 2%(-3) 2 (14) -2%(-3) -26. 求以下表达式的值,假设所有变量都为整型。(1) (a=b=4,a+1,b+1,a+b)8(2) (a=2,b=5,ab?a+:b+,a+b)8(3) (x=8,x%=x+5)8(4) 30 % 6 / 207.写出下面各逻辑表达式的值。设:a=3,b=4,c=5.(1)a+bc&b=c0(2)a |b+c&b-c1(3)!(ab) & !c | 1 1(4)!(x=a)&(y=b)&00(5)!(a+b) + c 1 & b + c/2 1第四次课1. if语句应用(1)加载,编译并运行下面的程序,先使用一个正整数,然后再使用一个负数。有什么不同?#include main()int x;printf(Enter a number between -10 and +10 : );scanf(%d, &x);if (x 0) printf(nYour number is positiven);return(0);输入正数:显示“Your number is positive”输入负数:没有显示。(2)使用 if语句避免除0#include main()float x, y;printf(Enter a number to be inverted : );scanf(%f, &x);if(x!=0)y = 1/x;printf(The inverse of %f equals %fn, x, y);elsePrintf(“input error”)getch();1. 当你输入数字0时结果为多少?程序运行异常,看不到运行结果。2. 增加一个if语句使它只有当if x不等于0时进行计算。(程序中蓝色部分)3. 现在你输入0会出现什么情况?仍看不到显示,但程序运行正常,getch()起作用。4. 改进程序使之可以当输入数字0时打印出警告信息。(程序中红色部分)(3)编写 if语句写一个程序用来计算电阻上的功率。功率大小等于电阻上的电压值乘以流过电阻的电流值。完成计算后,使用一个if 语句判断功率是否低于0.5 瓦特。如果低于0.5 瓦特则输出“Okey to use half watt resistor”,否则输出“haff watt registor willnot be okey”。下面是程序的开始部分,请添加完成if语句。#include main()float power, voltage, current;printf(Enter the voltage : );scanf(%f, &voltage);printf(Enter the current : );scanf(%f, ¤t);power= voltage*current; /* Calculation here */if(power0.5)printf(“Okey to use half watt resistorn”);/* if check here */elseprintf(“haff watt registor will not be okeyn”);/* else here */return(0);如果功率小于0.5 瓦特,屏幕输出应该如下所示:Enter the voltage : 5Enter the current : 0.002okay to use half watt resistor.如果功率大于0.5瓦特:Enter the voltage : 5Enter the current : 200half watt resisitor will not be okay.2. 编程练习:(1) 有三个整数a,b,c,由键盘输入,输出其中最大的数。#include main()int a,b,c,max;printf(a=);scanf(%d,&a);printf(b=);scanf(%d,&b);printf(c=);scanf(%d,&c);max=a;if(bmax)max=b;if(cmax)max=c;printf(max=%dn,max);getch();(2) 编程输入整数a和b,若 大于100,则输出 百位以上的数字,否则输出两数之和。#include main()int a,b,c;printf(a=);scanf(%d,&a);printf(b=);scanf(%d,&b);if(a100 |b100) if(a100)printf(a=%dn,a); if(b100)printf(b=%dn,b); else printf(a+b=%dn,a+b);getch();(3) 有一函数:写一个程序,输入x的值,输出y值。#include main()float x,y;printf(x=);scanf(%f,&x);if(x=1 & x=10) y=3*x-11;printf(y=%fn,y);getch();(4)写一段程序计算两个并联电阻的阻值,使你的程序检查是有短路的(0欧姆)将导致0作为被除数,使用一个if语句或者三目运算避免这种情况(使用三目运算计算及检查是否除0)。提示: Rt = 1 / (1/R1 + 1/R2);你的输出应该如下所示:Enter the value of resistor 1 : 45Enter the value of resistor 2 : 5645.0 ohms in parallel with 56.0 ohms gives 24.95 ohms假如有一个电阻阻值为0的话,结果将如下所示:Enter the value of resistor 1 : 0Enter the value of resistor 2 : 450.0 ohms in parallel with 45.0 ohms gives 0.00 ohms#include main()float r1,r2;printf(Enter the value of resistor 1 :);scanf(%f,&r1);printf(Enter the value of resistor 2 :);scanf(%f,&r2);if(r1= =0 | r2= =0) printf(%.1f ohms in parallel with %.1f ohms gives 0.00 ohmsn,r1,r2);else printf(%.1f ohms in parallel with %.1f ohms gives %.2f ohmsn,r1,r2,1/(1/r1+1/r2);getch();第5次课1. switch语句应用(1)加载,编译并运行下面的程序,并回答问题。#include main()int num1, num2, ans;char arithOp;ans=0;printf(Enter number 1 - );scanf(%d, &num1);printf(Enter number 2 - );scanf(%d, &num2);printf(Enter an operator (+, - , * or /) - );fflush(stdin);scanf(%c, &arithOp);switch (arithOp) case +:ans = num1 + num2; break;case -:ans = num1 - num2; break;case *:ans = num1 * num2; break;case /:if(num2)ans = num1 / num2; else arithOp=0;printf(“devide 0 errorn”);break;default: printf(“operator errorn”);break;If(arithOp)printf(n%d %c %d = %d, num1, arithOp, num2, ans);fflush(stdin);getchar();return(0);1. 尝试输入一个不匹配case语句的操作符,会发生什么?表达式右边的运算结果为02. 将程序保存为switch-02.c并为“ans”添加乘(*)和除(/)的算法。(蓝色)3. 添加一个default语句来处理输入的运算符不符合case情况。(红色)4. 添加程序处理除数为零的情况。(绿色)2. 加载,编译并运行下面的程序,将文件保存为switch-01.c(2)加载,编译并运行下面的程序。#include main()int choice;printf(How many stars (1 to 10) do you want? );scanf(%d, &choice);switch (choice) case 10:printf(*);case 9:printf(*);break;case 8:printf(*);case 7:printf(*);case 6:printf(*);case 5:printf(*);break;case 4:printf(*);case 3:printf(*);case 2:printf(*);case 1:printf(*);break;default:printf(The number you asked for is out of rangen);return(0);1. 打印 9颗星和8颗星是不同的。描述这两种方法。9颗星由case 9:printf(*);break;显示;8颗星由case 8:printf(*);case 7:printf(*);case 6:printf(*);case 5:printf(*);break;显示;2. 编辑这段程序,使之工作在没有break语句的情况下。(3)任务1:完成下面的程序,使用if/else语句,判断输入的数是正数还是负数任务2:完成下面的程序,使用switch语句,判断输入的数是正数还是负数,并且要处理输入的数越界的情况。#include main()int num;printf(Program indicates whether number is +ve or -ven);printf(Enter a number between -5 and +5 :);scanf(%d, &num);/* 添加语句 */if(num=0 & num=-5 & num0 printf(the number is -ve n);else printf(the number is out of rager n);getch();#include main()int num;printf(Program indicates whether number is +ve or -ven);printf(Enter a number between -5 and +5 :);scanf(%d, &num);/* 添加语句 */Switch(num) case 5: case 4: case 3: case 2: case 1: printf(the number is +ve n);break; case 0: printf(the number is zeron);break; case -1:case -2: case -3:case -4:case -5: printf(the number is -ve n);break;default: printf(the number is out of rager n);break;getch();(4)本程序是从键盘输入一个10进制数,根据用户要求输出这个数对应的16进制、8进制或者10进制数。例:若用户输入“H”,则输出这个数的16进制数。任务1:添加if语句完成程序功能任务2:添加switch语句完成程序功能,注意对输入无效数制的处理。比如输入:X时,作何处理。#include main()int num;char choice;printf(Program prints octal or hex equivalent of numbersenteredn);printf(Enter an integer : );scanf(%d, &num);printf(Do you want Decimal, Octal or Hex (H, D, O) : );fflush(stdin);choice = getchar();/* 添加程序 */if(choice =H)printf(“Hex form of choice is%xn”,chioce);else if(choice =D)printf(“decimal form of choice is%dn”,chioce);else if(choice =O)printf(“Octal form of choice is%on”,chioce);else printf(“output form error”);getch();#include main()int num;char choice;printf(Program prints octal or hex equivalent of numbersenteredn);printf(Enter an integer : );scanf(%d, &num);printf(Do you want Decimal, Octal or Hex (H, D, O) : );fflush(stdin);choice = getchar();/* 添加程序 */switch(chioce)case H: printf(“Hex form of choice is%xn”,chioce);break;case D: printf(“decimal form of choice is%dn”,chioce);break;case 3: printf(“Octal form of choice is%on”,chioce);break;default: printf(“output form error break;”);getch();(5)给出一百分制成绩,要求输出成绩等级A,B,C,D,E。90分以上为A,8089分为B,7079分为C,6069分为D,60分以下为E。#include stdio.hmain() int cj; printf(input score please:); scanf(%d,&cj); if (cj100 | cj= 90) printf(score is %d grade A,cj); else if(cj = 80) printf(score is %d grade B,cj); else if(cj = 70) printf(score is %d grade C,cj); else if(cj = 60) printf(score is %d grade D,cj); else printf(score is %d grade E,cj); getch(); #include stdio.hmain() int cj; char ch; printf(input cj=); scanf(%d,&cj); switch (int)(cj/10) case 10: case 9: ch=A; break; case 8: ch=B; break; case 7: ch=C; break; case 6: ch=D; break; default: ch=E;break; printf(grade %c,ch); getch();(6)从键盘输入三个数,判断这三个数是否能构成三角形,如果是,输出“the three numbers could be the sides of a triangle”,如果不是输出“the three numbers couldt the sides of a triangle”;并判断这个三角形是不是直角三角形,如果是,输出“the numbers could be the sides of a right angle triangle”,如果不是,输出“the numbers couldt the sides of a right angle triangle”。#include Stdio.hmain(void)float a,b,c;printf(input length of three sides of the a triangle:);scanf(%f,%f,%f,&a,&b,&c);if(a+bc & b+ca & c+ab) printf(the three numbers could be the sides of a trianglen); if(a*a+b*b=c*c | b*b+c*c=a*a | c*c+a*a=b*b ) printf(the numbers could be the sides of a right angle trianglen); else printf(the numbers couldt the sides of a right angle trianglen); else printf(the three numbers couldt the sides of a trianglen);getch();第6次课 循环1. 写一个程序让用户输入一个112之间的数,程序将会输出这个数的乘法表。使用一个循环进行计算并输出一行,屏幕输出应该如下所示:Enter a number between 1 and 12: 55 times multiplication table1 x 5 = 52 x 5 = 103 x 5 = 154 x 5 = 205 x 5 = 256 x 5 = 307 x 5 = 358 x 5
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年工业互联网平台量子密钥分发技术产业创新与研发投入报告
- 2025年老年健康管理中慢性疼痛长期照护服务模式研究报告001
- 2025年新零售趋势分析报告:线上线下融合创新策略
- 考点攻克黑龙江省北安市7年级上册期末测试卷单元测评试题(详解版)
- 2025年教育信息化基础设施建设与教育信息化项目市场前景研究报告
- 2025年城市轨道交通智慧运维系统在地铁通信网络中的应用报告
- 基础强化人教版8年级数学下册《一次函数》专项训练试题(详解版)
- 护士企业编制面试题库及答案详解【必刷】
- 临床药物治疗学期末考试复习题库及答案详解【名校卷】
- 基础强化人教版8年级数学上册《全等三角形》专题训练试题(含答案解析版)
- 手术室的时间管理
- 传统养生与现代健康课件
- 2025年工会基础知识考试题库及参考答案
- 2024-2025学年四川省成都市蒲江县蒲江中学高三上学期调研摸底考试数学试卷
- 医疗健康新媒体运营方案
- 水利工程重点难点分析及管理措施
- 压力管道取证培训课件
- 山东省环境卫生作业计价定额编制说明
- 飞机机型培训课件
- 中国声乐作品课件图片
- 静态爆破监测方案(3篇)
评论
0/150
提交评论