推荐第1篇:《C语言程序设计》实验报告
指导教师学院专业班级学号姓名实验室
实验题目:
实验要求:
实验内容及步骤:
运行结果
心得体会:
推荐第2篇:高级语言程序设计实验报告 C语言实验报告
C语言程序设计实验报告
实验二 循环结构程序设计
班级 2012196 学号 201219628 姓名 李明月
一、实验目的
(1)掌握用while语句,do-while语句和for语句实现循环的方法; (2)掌握循环结构的嵌套;
(3)掌握break语句和continue语句的使用方法。
二、实验内容及步骤
1.相传国际象棋是古印度舍罕王的宰相达依尔发明的。舍罕王十分喜欢象棋,决定让宰相自己选择何种赏赐。这位聪明的宰相指着8×8共64格的象棋盘说:陛下,请您赏给我一些麦子吧,就在棋盘的第一个格子中放1粒,第2格中放2粒,第3格放4粒,以后每一格都比前一格增加一倍,依此放完棋盘上的64个格子,我就感恩不尽了。舍罕王让人扛来一袋麦子,他要兑现他的许诺。 国王能兑现他的许诺吗?
程序1:试编程计算舍罕王共要多少粒麦子赏赐他的宰相,这些麦子合多少立方米?(已知1立方米麦子约1.42e8粒) 总粒数为:sum=1+2+22+23+„+263 程序代码:
#include int main( ) //定义一个主函数 { int i; double t=1,sum=1,v; //定义变量
for(i=1;i
return 0; } 运行结果:
1
2.求完数。
程序2:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6的因子为1,2,3,而6=1+2+3,因此6是“完数”。编程找出1000之内的所有完数,输出所有的完数(要求:一行显示6个数);
程序代码:
#include int main() { int i,j,sum,n=0; printf(\" 1000以内的完数有:\\n\"); for(i=1;i
sum=0; for(j=1;j
if(i%j==0)
{
sum=sum+j;
} } if(sum==i)
{ printf(\" %d\",i);
n=n+1;
if(n%2==0)
printf(\"\\n\");
} } printf(\"\\n\"); return 0; } 运行结果:
2
3.打印九九乘法表
程序3:编程输出如下上三角形式的九九乘法表。
1 2 3 4 5 6 7 8 9---- - - -\\n\"); for(i=1;i
代表行 输出空格达到来使得向右对齐代表列
输出行与列的乘积 3 1 2 3 4 5 6 7 8 9
运行结果:
三、问题讨论
break语句和continue语句在循环结构中使用时有何区别?举例说明。
break语句是跳出整个循环过程,不再判断执行循环的田间是否成立,并且break语句不能用于循环语句和switch语句之外的任何其他语句中。而continue语句则只是结束本次循环,即跳过循环体中下面尚未执行的语句,接着进行下一次是否执行循环的判定。
例子:
#include int main( ) { int i; for(i=100;i
printf(\"%d\",i); } printf(\"\\n\"); return 0; } 输出:
但是换成break之后:
四、实验心得
个人感觉对于这几个循环语句还是for循环最为灵活,不进可以用于循环次数一句in个确定的情况,还可用于不确定的情况下,它也完全可以代替while语句。并且是程序看起来更为简洁明了。
5
推荐第3篇:C语言程序设计 实验报告2.3
C语言程序设计
实验报告
专业
班级
日期
11月26日
成绩
实验组别
第
2(2.3) 次实验
指导教师
李开
学生姓名
学号
同组人姓名
实验名称 流程控制实验
一、实验目的
(1)掌握复合语句、if语句、switch语句的使用,熟练掌握for、while、do-while三种基本的循环控制语句的使用,掌握重复循环技术,了解转移语句与标号语句。
(2)熟练运用for、while、do-while语句来编写程序。 (3)练习转移语句和标号语句的使用。
(4)使用集成开发环境中的调试功能:单步执行、设置断点、观察变量值。
二、实验任务
3.2 实验内容及要求 1.源程序改错
下面是计算s=n! 的源程序,在这个源程序中存在若干语法和逻辑错误。要求在计算机上对这个例子程序进行调试修改,使之能够正确完成指定任务。例如,8!=40320。 #include void main() { int i,n,s=1; printf(\"Please enter n:\"); scanf(\"%d\",n); for(i=1,i
2.源程序修改替换
(1)修改第1题,分别用while和do-while语句替换for语句。
(2)修改第1题,输入改为“整数S”,输出改为“满足n!≥S的最小整数n”。例如输入整数40310,输出结果为n=8。
3.程序设计
(1) 假设工资税金按以下方法计算:x < 1000元,不收取税金;1000 ≤ x < 2000,收取5%的税金;2000 ≤ x < 3000,收取10%的税金;3000 ≤ x < 4000,收取15%的税金;4000 ≤ x < 5000,收取20%的税金;x>5000,收取25%的税金。输入工资金额,输出应收取税金额度,要求分别用if语句和switch语句来实现。
(2)将输入的一行字符复制到输出,复制过程中将一个以上的空格字符用一个空格代替。
(3)打印如下杨辉三角形。
1 /*第0行 */ 1 1 /*第1行 */ 1 2 1 /*第2行 */ 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
jjCCii每个数据值可以由组合计算(表示第i行第j列位置的值),而的计算如下:
Ci0
1 (i=0,1,2,„) CijCij1*(i-j1) / j (j=0,1,2,3,„,i) 本程序中为了打印出金字塔效果,要注意空格的数目。一位数之间是3个空格,两位数之间有2个空格,3位数之间只有一个空格,程序编制过程中要注意区分。
(4)编写一个程序,将用户输入的任意正整数逆转,例如,输入1234,输出4321。
4.选做题
32f(x)3x4x5x130满足精度e=10-6 编写一个程序,用牛顿迭代法求方程的一个近似根,并在屏幕上输出所求近似根。
x0axxkf(xk)/f\'(xk),其中f\'(x)是牛顿迭代法:求方程近似根的迭代公式为k1函数f(x)的导函数。牛顿迭代法首先任意设定的一个实数a来作为近似根的迭代初值x0,然后用迭代公式计算下一个近似根x1。如此继续迭代计算x2, x3, „, xn, 直到xnxn1精度e
,此时值xn即为所求的近似根。
2.3:
1.源程序改错: (源程序)
#include void main() { int i,n,s=1; printf(\"Please enter n:\"); scanf(\"%d\",n); for(i=1,i
(修改后程序) #include int main(void) {
int i,n,s=1;
printf(\"Please enter n:\");
scanf(\"%d\",&n);
for(i=1;i
s=s*i;
printf(\"%d! = %d\",n,s);
return 0; }
2.源程序修改替换
(1)修改第1题,分别用while和do-while语句替换for语句。 (while)
#include int main(void) { int i=1,n,s=1; printf(\"Please enter n:\"); scanf(\"%d\",&n); while(i
(do-while) #include int main(void) { int i=1,n,s=1; printf(\"Please enter n:\"); scanf(\"%d\",&n); do {s=s*i; i++;} while(i
(2)修改第1题,输入改为“整数S”,输出改为“满足n!≥S的最小整数n”。例如输入整数40310,输出结果为n=8。
#include int main(void) { int i,n=1,s; printf(\"Please enter s:\"); scanf(\"%d\",&s); for(i=1;n= %d\",i-1,s); return 0; }
3.程序设计
(1) 假设工资税金按以下方法计算:x < 1000元,不收取税金;1000 ≤ x < 2000,收取5%的税金;2000 ≤ x < 3000,收取10%的税金;3000 ≤ x < 4000,收取15%的税金;4000 ≤ x < 5000,收取20%的税金;x>5000,收取25%的税金。输入工资金额,输出应收取税金额度,要求分别用if语句和switch语句来实现。
(switch语句) #include void main() {
printf(\"输入你的工资 \");
int t;
float x;
scanf(\"%f\",&x);
t = (int)(x/1000.0);
switch(t){
case 0:
printf(\"no tax!\\n\");
break;
case 1:
printf(\"%f\\n\",0.05*x);
break;
case 2:
printf(\"%f\\n\",0.10*x);
break;
case 3:
printf(\"%f\\n\",0.15*x);
break;
case 4:
printf(\"%f\\n\",0.20*x);
break;
case 5:
printf(\"%f\\n\",0.25*x);
break;
default:
printf(\"%f\\n\",0.25*x);
break;
}
return 0; }
(if语句)
#include \"stdio.h\" int main(int argc, char *argv[]) {
float x, tax;
scanf(\"%f\", &x);
if(x
{
printf(\"Wrong input.\\n\");
return 1;
}
if(x
{
tax = 0.00;
}
else if(x>=1000&&x
{
tax = 0.05 * x;
}
else if(x>=2000&&x
{
tax = 0.10 * x;
}
else if(x>=3000&&x
{
tax = 0.15 * x;
}
else if(x>=4000&&x
{
tax = 0.20 * x;
}
else
{
tax = 0.25 * x;
} printf(\"Tax of salary %.2f YUAN is %.2f YUAN.\\n\", x, tax); }
(2)将输入的一行字符复制到输出,复制过程中将一个以上的空格字符用一个空格代替。
#include int main() { char c,flag; for(flag=0;(c=getchar())!=\'\\n\';) {
if(c==\' \'){
if(flag==0){
putchar(c);
flag=1;
}
}
else{
putchar(c);
flag=0;
}
}
return 0; }
(3)打印如下杨辉三角形。
1 /*第0行 */ 1 1 /*第1行 */ 1 2 1 /*第2行 */ 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
jjCCii每个数据值可以由组合计算(表示第i行第j列位置的值),而的计算如下:
Ci01
(i=0,1,2,„) CijCij1*(i-j1) / j (j=0,1,2,3,„,i) 本程序中为了打印出金字塔效果,要注意空格的数目。一位数之间是3个空格,两位数之间有2个空格,3位数之间只有一个空格,程序编制过程中要注意区分。
#include int Combination(int a,int b); int Triangle(int n); int main(void){ printf(\"input the number for triangle of Yanghui\\n\"); Triangle(9); } int Combination(int a,int b){ int t,p,s=1,q=1; if(a==0||b==0){ printf(\"%4d\",q); } else{ for(t=b-a+1;t
(4)编写一个程序,将用户输入的任意正整数逆转,例如,输入1234,输出4321。 #include #include int main(void) { int a,i,c,n=0; char m; for(a=0; m=getchar(),m!=\'\\n\';a++){ c=m-\'0\'; n=n+c*pow(10,a); } printf(\"%d\\n\",n); }
4.选做题
-6 编写一个程序,用牛顿迭代法求方程f(x)3x4x5x130满足精度e=10的一个近似根,并在屏幕上输出所求近似根。
32x0axxkf(xk)/f\'(xk),其中f\'(x)是牛顿迭代法:求方程近似根的迭代公式为k1函数f(x)的导函数。牛顿迭代法首先任意设定的一个实数a来作为近似根的迭代初值x0,然后用迭代公式计算下一个近似根x1。如此继续迭代计算x2, x3, „, xn, 直到xnxn1精度e ,此时值xn即为所求的近似根。
#include #include double f(double x); double f_(double x); double root(double m); int main() { double m=1; printf(\"%lf\\n\",root(m)); return 0; }
double f(double x) { return(3*x*x*x-4*x*x-5*x+13); } double f_(double x) { return(9*x*x-8*x-5); } double root(double m) { double n; n=m-f(m)/f_(m); if(fabs(m-n)
四、实验体会
了解更多有关C语言的语法知识和循环结构以及熟练了调试过程。
推荐第4篇:C语言程序设计(1,2)实验报告
实验一 C程序的运行环境和方法
一、实验目的
1.了解所用的计算机系统。
2.了解在该系统上如何进行编辑、编译、连接和运行一个C程序。 3.通过运行简单的C程序了解C程序的特点。
二、实验内容和步骤
1.熟悉所用的系统。了解Windows资源管理器的使用方法:文件的查看、复制、运行等方法,Visual C++所在目录,文本文件的建立方法。 2.进入Visual C++,并新建一个C++源程序文件。
3.熟悉Visual C++的集成环境,了解各菜单项有哪些子菜单。 4.输入下面的程序(教材中的例1.1),注意区分大小写。
#include void main() { printf(\"This is a C program.\\n\"); } 编译并运行程序。 结果为:
5.关闭工作区,新建一个程序,然后对教材中的例1.2重复4中的操作(即只将程序改为例1.2中的程序,其它操作步骤相同)。 其程序为:
#include int main() { int a,b,sum; a=123; b=456; sum=a+b; printf(\"sum is %d\\n\",sum); return 0; } 运行结果为:
6.关闭工作区,新建一个程序,然后输入并运行一个需要在运行时输入数据的程序
#include void main() {int a,b,c; int max(int x,int y); printf(\"input a and b:\\n\"); scanf(\"%d,%d\",&a,&b); c=max(a,b); printf(\"\\nmax=%d\\n\",c); } int max(int x,int y) {int z; if(x>y) z=x; else z=y; return(z); } (1)运行程序,若程序有错,则修改错误后继续运行程序,当没有错误信息时输入:2,5并按Enter键,查看运行结果。
其运行结果为:
(2)将程序的第三行改为:int a;b;c;然后按F9看结果如何,将其修改为int a,b,c;将子程序max的第3,4行合并为一行,运行程序,看结果是否相同。 将程序的第三行改为:int a;b;c;运行结果为:
将子程序max的第3,4行合并为一行,运行程序,其结果相同,结果为:
7.运行一个自己编写的程序,程序的功能是输出两行文字。 其程序为:
#include void main() { printf(\"我是中国人!\\n\"); printf(\"我深深的爱着我的祖国!\\n\"); } 运行结果为:
实验二 数据类型、运算符和表达式
一、实验目的
1.掌握C语言数据类型,熟悉如何定义一个整型、字符型和实型的变量,以及对它们赋值的方法。
2.掌握不同数据类型之间赋值的规律。
3.学会使用C的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(――)运算符的使用。
4.进一步熟悉C程序的编辑、编译、连接和运行的过程。
二、实验内容和步骤
1.输入并运行下面的程序 #include void main() {char c1,c2; c1=\'a\'; c2=\'b\'; printf(\"%c %c\",c1,c2); } (1)运行此程序 其结果为:
(2)加入下面的一个语句作为“}”前的最后一个语句:
printf(\"%d,%d\\n\",c1,c2); 其结果为:
(3)将第3行改为:
int c1,c2; 然后再运行程序,并观察结果是否相同。 相同,其结果为:
(4)将第3行改为int c1,c2;将第4,5行依次改为:
c1=a;c2=b; c1=\"a\";c2=\"b\" c1=300;c2=400; 每改为一次后运行程序,观察结果。 其程序为: #include void main() {int c1,c2; c1=\'a\',c2=\'b\'; c1=300;c2=400; printf(\"%c %c\\n\",c1,c2); printf(\"%d %d\\n\",c1,c2); } 其结果为:
2.分析教材第3章习题3.5中的程序的运行结果,然后输入该程序并运行,将运行结果与前面分析的结果对比。 其程序为:
#include int main() {int a,b; float x,y; char c1,c2; scanf(\"a=%d b=%d\",&a,&b); scanf(\"%f %e\",&x,&y); scanf(\"%c %c\",&c1,&c2); printf(\"a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\\n\",a,b,x,y,c1,c2); return 0; } 其运行结果为:
3.输入并运行下面的程序
#include void main() {int a,b; unsigned c,d; long e,f; a=100; b=-100; e=50000; f=32767; c=a; d=b; printf(\"%d,%d\\n\",a,b); printf(\"%u,%u\\n\",a,b); printf(\"%u,%u\\n\",c,d); c=a=e; d=b=f; printf(\"%d,%d\\n\",a,b); printf(\"%u,%u\\n\",c,d); } 请对照程序和运行结果分析: 运行结果为:
(1)将一个负整数赋给一个无符号的变量,会得到什么结果。画出它们在内存中的表示形式。
(2)将一个大于32767的长整数赋给一个整型变量,会得到什么结果。画出它们在内存中的表示形式。
(3)将一个长整数赋给无符号的变量,会得到什么结果。画出它们在内存中的表示形式。 4.输入习题3.10 (1)运行程序,注意i,j,m,n的值。 (2)将第4,5行改为:
m=i++; n=++j; 再运行。 (3)将程序改为:
#include void main() {int i,j; i=8;j=10; printf(\"%d,%d\\n\",++i,++j); i=8;j=10; printf(\"%d,%d\\n\",i++,j++); i=8;j=10; printf(\"%d,%d\\n\",++i,i); i=8;j=10; printf(\"%d,%d\\n\",i++,i); } 运行程序并分析运行结果。 其结果为:
5.按习题3.6的要求编程并上机运行:
要将“China”译成密码,密码规律是:用原来的字母后面第4个字母代替原来的字母。例如,字母“A”后面第4个字母是“E”,用“E”代替“A”,因此,“China”应译为“Glmre”。请编一程序,用赋初值的方法使c1、c
2、c
3、c
4、c5这5个变量的值分别为’C’,\'h\', \'i\',\'n\', \'a\',经过计算,使c1、c
2、c
3、c
4、c5分别变为:\'G\',\'l\',\'m\',r\',\'e\',并输出.程序提示:
main函数算法如下:
定义char型变量 c1,c2,c3,c4,c5; 给字符型变量赋值 c1=c1+4; c2=c2+4; c3=c3+4; c4=c4+4; c5=c5+4; 输出c1,c2,c3,c4,c5 其程序为: #include int main() {char c1=\'C\',c2=\'h\',c3=\'i\',c4=\'n\',c5=\'a\'; c1=c1+4; c2=c2+4; c3=c3+4; c4=c4+4; c5=c5+4; printf(\"paword is %c%c%c%c%c\\n\",c1,c2,c3,c4,c5); return 0; } 其运行结果为:
推荐第5篇:C语言程序设计—函数—实验报告
实 验 报 告
专业
软 件 工 程
班级
X
班
学号_ XXXXXXXXXXX_
姓名
实验日期:201X年X月X日
报告退发 (订正 、重做)
课程
C程序设计实验
实验名称
函
数
一、实验目的
① 熟练掌握C程序中函数的定义; ② 掌握函数的调用,函数参数的传递; ③ 熟练掌握函数的嵌套调用和递归调用;
二、实验环境(描述实验的软件、硬件环境)
① 软件环境:windows xp/win7等操作系统,Microsoft Visual C++ 6.0编译器; ② 硬件环境:PC机一台
三、实验内容、步骤和结果分析
题目一:编写一个用来判断质数(素数)的函数。
要求:
(1)在main函数中调用该函数对输入的数值进行判断,并输出判断结果; (2)当输入数值
#include #include /*=======判断质数(素数)的函数=======*/ int judgePrime(int n) {
} /*=======判断质数(素数)的函数=======*/ int main() {
printf(\"\\n提示:输入一个数字来判断是否是质数(素数),当输入数值1;) { printf(\"Please enter a number:\"); for(int i=2;i
} return 1; if(n%i==0) return 0; else continue;
}
} scanf(\"%d\",&num); if(num
题目二:使用习题1中所编写的判断质数的函数验证哥德巴赫(Goldbach)的1+1猜想-----任何一个>=6的偶数都可以表示成两个素数之和
要求:
① 屏幕提示用户一个>=6的偶数;
② 输出这个偶数等于2个素数之和的表达式; ③ 当输入数据
#include #include /*=======判断质数(素数)的函数=======*/ int judgePrime(int n) {
for(int i=2;i
if(n%i==0) return 0; else continue;
} } return 1;//true-->1,false-->0 /*=======验证哥德巴赫猜想的函数(调用judgePrime)=======*/ int proveGoldbach(int m) {
} int main() {
}
printf(\"\\n提示:输入一个偶数来验证哥德巴赫猜想,当输入数值=6;) {
} int temp; printf(\"Please enter a number:\"); scanf(\"%d\",&num); temp=proveGoldbach(num); if(temp==0)//当输入小于6或者不是偶数时结束 return 0; printf(\"==================================\"); if(m
} if(judgePrime(j)==1&&judgePrime(m-j)==1)//判断两数是否都等于质数 { } printf(\"\\t%d = %d + %d\\n\",m,j,m-j); return 0;//判断是否大于等于6或者不是偶数 for(int j=2;j
题目三:编写一个求阶乘的函数,接着调用该函数实现组合的求解要求:
① 提示用户输入n和m的数值; ② 输出的 结果
#include /*=======阶乘函数=======*/ int fac(int n) {
} /*=======组合函数=======*/ int combine(int m,int n) { } /*=======main函数=======*/ void main() {
int m,n; int res; printf(\"\\n提示:这是求组合求和公式的函数,输入两个整数(分别为上标和下标)\\n\\n\"); scanf(\"%d%d\",&m,&n); if(m>n)//判断上标大于小标重新输入 goto loop; return fac(n)/(fac(n-m)*fac(m)); int f; if(n
}res=combine(m,n); printf(\"\\tResult is %d\\n\",res);
题目四:编写一个求矩阵中最大元素以及最小元素的函数selectMaxAndMin(),在main函数中调用该函数
要求:
最大值和最小值的输出操作,可以放在selectMaxAndMin()函数中
#include #define ROW 4//矩阵的行 #define COLUMN 4//矩阵的列
/*=======在矩阵中选择最大最小数的函数=======*/ void selectMaxAndMin(int arry[ROW][COLUMN])//用指针更方便 {
int tempMax,tempMin; if(arry[0][0]>arry[0][1])//赋初始值 {
} else {
} for(int i=0;i
}
} for(int j=0;j
} if(arry[i][j]>tempMax) { } if(arry[i][j]
} int mat[ROW][COLUMN]; printf(\"Please enter a %d X %d matrix:\\n\",ROW,COLUMN); for(int i=0;i
} selectMaxAndMin(mat); return 0; for(int j=0;j
题目五:编写一个编码原文的函数,对于给定的内容,按照敌方的规律对其进行编码
要求:
某日,我军某部截获了一份敌方电报,经过仔细分析后发现:该电文中所有的字母字符都是经过某种运算后得到的,然那些非字母字符则没有经过任何处理。例如,原文中的字符A对应电文中的字符E,原文中的字符b对应电文中的字符f,原文中的字符W对应电文中的字符A,原文中的字符!在电文中仍为!
#include #define SIZE 20
//定义大小 /*=======密码翻译的函数=======*/ char trans(char a) {
} //主函数 int main(void) {
printf(\"请输入原文:\\n\\n\\n\"); char str[SIZE]; gets(str); printf(\"\\n\\n\\n\"); if((a>=\'a\'&&a=\'A\'&&a
} if(a>=\'w\'||a>=\'W\') { } else { return a; } return a-22; return a+4;
}printf(\"正在破解……\\n\\n\\n\"); printf(\"破译的原文是:\"); for(int i=0;i
} printf(\"\\n\"); str[i]=trans(str[i]); if(str[i]==\'\\0\')//判断是否结束 break; printf(\"%c\",str[i]);
题目六:编写一个函数,求解输入矩阵的转置矩阵并输出
#include #define ROW 3//矩阵的行 #define COLUMN 4//矩阵的列 /*=======矩阵的输入函数=======*/ void matInput(int mat[][COLUMN]) {
} /*=======原始矩阵的打印函数=======*/ for(int i=0;i
for(int j=0;j
} printf(\"请输入矩阵元素 [%d][%d]=:\",i,j); scanf(\"%d\",&mat[i][j]);
void matPrint(int mat[][COLUMN]) {
} /*=======转置矩阵的打印函数=======*/ void matTrasPrint(int mat[][COLUMN]) {
} /*=======main函数=======*/ int main(void) {
} printf(\"==================================\"); printf(\"请输入一个 %d X %d 的矩阵:\\n\",ROW,COLUMN); int mat[ROW][COLUMN]; matInput(mat); printf(\"==================================\"); printf(\"原始矩阵为:\\n\"); matPrint(mat); printf(\"==================================\"); printf(\"原始矩阵的转置矩阵为\\n\"); matTrasPrint(mat); return 0; int arr[COLUMN][ROW]; for(int i=0;i
} for(int j=0;j
} printf(\"\\n\"); arr[i][j]=mat[j][i]; printf(\"%3d\",arr[i][j]); for(int i=0;i
} for(int j=0;j
题目七:用户输入的字符串,以相反的顺序打印出来。
要求:利用递归函数调用形式
#include #define SIZE 20 /*=======相反输出字符串的函数=======*/ void reverse(char a[]) {
int i=0; if(a[i]!=\'\\0\') {
} int main(void) {
}printf(\"\\t提示:输入一串字符以相反的顺序打印\\n\"); char str[SIZE]; gets(str); reverse(str); printf(\"\\n\"); return 0;
} else return; reverse(&a[i+1]);//递归调用 printf(\"%c\",a[i]);
四、讨论
推荐第6篇:C语言程序设计上机实验报告
黔南民族师范学院 C语言程序设计上机实验报告
系部:计算机科学系 年级:2013 级 班级:
姓名:
学号:
实验时间:
实验成绩:
2013年月日
实验三顺序结构程序的设计
一 实验名称:顺序结构程序的设计
二.实验环境:windows XP系统,VC++6.0软件
三.实验目的:
四.实验内容:
五.算法描述流程图
六.源程序
七.测试数据及运行结果
八.实验心得
实验成绩评分要求
1、原创性发现抄袭或雷同成绩为0分
2、正确性程序正确60分
3、可读性格式清楚,有注释,变量命名规范20分
4、健壮性对特殊测试数据有考虑有测试10分
5、效率程序运行效率高10分
推荐第7篇:C语言程序设计II实验报告
第二部分 实验内容
一、实验目的
1、掌握一维数组的定义和引用
2、掌握一维数组的初始化方法
3、了解与一维数组有关的应用编程方法
二、实验意义
数组的定义,元素引用特征和数组的编程特色,同时可接触到更多的经典算法,的,有了这些基础后,数组的实验能让
三、实验重点
1 2
四、实验难点
1.从键盘输入10个整数存放在一维数组里,用冒泡法进行排序输出。
2.从键盘输入10个整数,找出最大的数并输出该数及其下标。
3.用scanf函数给二维数组a[3][4]输入12个数据,并求出12个数据中正数之和,每一行的最小值以及下标值。
推荐第8篇:C语言程序设计扫雷游戏实验报告
中南大学
程序设计基础实践报告
学生姓名 张兰兰 学 院 信息科学与工程学院 专业班级 物联网工程1301班 完成时间 2014.1 题 目 设计一个和window系统类似的小游戏【挖地雷】
《计算机程序设计实践》错误!未指定书签。
目
录
1.课程设计内容 .............................................................................................3 2.课程设计目的 .............................................................................................3 3.背景知识 ....................................................................................................3 4.工具/准备工作 ............................................................................................5 5.设计步骤、方法 .........................................................................................5
5.1.步骤1:步骤名称(二级标题) .......................................................错误!未定义书签。
步骤1.1:步骤名称(三级标题) ..............................................................................5 5.1.1.5.2.5.3.步骤2:步骤名称 ..........................................................................错误!未定义书签。 步骤n:步骤名称 ..........................................................................错误!未定义书签。
6.设计结果及分析 .........................................................................................5 7.设计结论 ..................................................................................................15 8.问题及心得体会 .......................................................................................16 9.对本设计过程及方法、手段的改进建议 ....................................................17 10.参考文献 ..................................................................................................17
《计算机程序设计实践》错误!未指定书签。
case 4:
system(\"cls\");
printf(\"请输入棋盘行列数(小于20且用逗号隔开):\");
scanf(\"%d,%d\",&x,&y);
getchar();
printf(\"请输入雷的个数:\");
scanf(\"%d\",&b);
getchar();
break;
}
break;
}
else
{
printf(\"输入错误请重新输\\n\");
}
} while(begin) {
system(\"cls\");
InitialValue_game(&g);
InitialMine_game(&g);
print_game(&g,2);
NO=1;
while(NO)
{
printf(\"Please choose:\");
scanf(\"%d,%d\",&i,&j); getchar(); if(-1
{
switch(openmap(&g,i-1,j-1))
{
case 1:
《计算机程序设计实践》错误!未指定书签。
print_game(&g,1);
printf(\"游戏结束!按回车键再来一局\\n\");
if(getchar()==\'\\n\') begin=1;
else begin=0;
NO=0;
break;
case 2:
if(judge_game(&g)==b)
{
printf(\"win!!\\n\");
print_game(&g,1);
printf(\"按回车键再来一局\\n\");
if(getchar()==\'\\n\') begin=1;
else begin=0;
NO=0;
break;
}
else print_game(&g,2);NO=1;break;
default:break;
}
}
else
{
printf(\"你输入错误!请重新输入\\n\");
} } } }
推荐第9篇:C语言程序设计扫雷游戏实验报告
中南大学
程序设计基础实践报告
学生姓名 张兰兰 学 院 信息科学与工程学院 专业班级 物联网工程1301班 完成时间 2014.1 题 目 设计一个和window系统类似的小游戏【挖地雷】
《计算机程序设计实践》403591717.docx 目
录
1.课程设计内容 .............................................................................................3 2.课程设计目的 .............................................................................................3 3.背景知识 ....................................................................................................3 4.工具/准备工作 ............................................................................................5 5.设计步骤、方法 .........................................................................................5
5.1.步骤1:步骤名称(二级标题) .......................................................错误!未定义书签。
步骤1.1:步骤名称(三级标题) ..............................................................................5 5.1.1.5.2.5.3.步骤2:步骤名称 ..........................................................................错误!未定义书签。 步骤n:步骤名称 ..........................................................................错误!未定义书签。
6.设计结果及分析 .........................................................................................5 7.设计结论 ..................................................................................................15 8.问题及心得体会 .......................................................................................16 9.对本设计过程及方法、手段的改进建议 ....................................................17 10.参考文献 ..................................................................................................17
《计算机程序设计实践》403591717.docx
printf(\"请输入棋盘行列数(小于20且用逗号隔开):\");
scanf(\"%d,%d\",&x,&y);
getchar();
printf(\"请输入雷的个数:\");
scanf(\"%d\",&b);
getchar();
break;
}
break;
}
else
{
printf(\"输入错误请重新输\\n\");
}
} while(begin) {
system(\"cls\");
InitialValue_game(&g);
InitialMine_game(&g);
print_game(&g,2);
NO=1;
while(NO)
{
printf(\"Please choose:\");
scanf(\"%d,%d\",&i,&j); getchar(); if(-1
{
switch(openmap(&g,i-1,j-1))
{
case 1:
print_game(&g,1);
printf(\"游戏结束!按回车键再来一局\\n\");
《计算机程序设计实践》403591717.docx
if(getchar()==\'\\n\') begin=1;
else begin=0;
NO=0;
break;
case 2:
if(judge_game(&g)==b)
{
printf(\"win!!\\n\");
print_game(&g,1);
printf(\"按回车键再来一局\\n\");
if(getchar()==\'\\n\') begin=1;
else begin=0;
NO=0;
break;
}
else print_game(&g,2);NO=1;break;
default:break;
}
}
else
{
printf(\"你输入错误!请重新输入\\n\");
} } } }
推荐第10篇:2《C语言程序设计》实验报告[优秀]
武汉大学东湖分校创建时间:2010-5-2 21:21:00
《C语言程序设计》实验报告
年级:2009级(必修)
学号:姓名:专业:
实验题目:
输入一个班N个学生(N为符号常量,自定人数)的学号和每个学生考试三门功课(数学、英语、计算机基础)的成绩。编程计算出每个学生的总分和平均分,并按学生成绩优劣排序,最后打印一张按高分到低分名次排序的成绩单。要求:
1)排序用一个函数实现。
2)打印的成绩单表项包括:序号,学号、数学、英语、计算机、总分、平均分。
3)按实验报告电子模板格式填写实验内容。
可用多个函数或多个源文件实现
可以用数组法、指针法、结构体法编写程序。
源程序清单:
主要标识符说明:(说明变量、函数、指针、结构体等)
实验结果贴图:
实验环境:
intel Pentium 4 计算机,windowsXP操作系统,Microsoft Visual C++ 6编译环境。
教师评语:
成绩:教师签名:签名日期:年月日
第11篇:《c语言程序设计》上机实验报告要求
《c语言程序设计》上机实验报告要求
1.实验环境:
软件系统:使用的软件环境
硬件系统:机型说明
2.实验目的:掌握如何编辑、编译、链接调试运行c程序
3.实验内容:
(1) 掌握顺序结构程序设计.P26 ,p49,p62 3.2~3.7
(2) 掌握选择结构程序设计(if 和switch语句的用法)p4.2~
(3) 循环结构程序设计(while, dowhile,for语句的用法)。
(4) 掌握数组的定义、输入和输出的方法,字符数组、字符串函数的使用。。
(5) 了解函数的定义,熟悉函数实参与形参的“值传递”方式,掌握函数
的嵌套调用和递归调用方法。
(6) 熟悉指针含义及其使用。
(7) 熟悉结构体和共用体的使用。
(8) 熟悉文件的使用。
4.实验要求:
(1) 输入编写的源程序,检查程序有无错误(语法和逻辑错误),有则改
之。
(2) 编译和连接,仔细分析编译信息,如有错误应找出原因并改正。
(3) 运行程序,输入数据,分析结果。
5.实验结果:
输出程序清单和运行结果。(要求把原题内容,调试好的程序和其结果一并打
印),
6.实验体会
分析运行结果,本次调试程序取得的经验(遇到的问题,解决的方法等)。
第12篇:C语言实验报告《逻辑结构程序设计》
学号:__________ 姓名:__________ 班级:__________ 日期:__________
指导教师:__________ 成绩:__________
实验二 逻辑结构程序设计
一、实验目的
1、学会正确使用逻辑运算符和逻辑表达式
2、熟练掌握if语句和switch语句
3、熟练掌握用while语句、do-while语句和for语句实现循环的方法
4、掌握在程序设计中的常用算法(如递推等)
二、实验内容
1、输入百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。90分以上为‘A’,80——89分为‘B’,70——79分为‘C’,60——69分为‘D’,60分以下为‘E’。(习题5.6)
2、求1!+2!+3!+4!+ … +20!(习题6.4)
3、编写程序并输出下列图形:
*******
*****
***
*
三、实验步骤与过程
物理实验报告 ·化学实验报告 ·生物实验报告 ·实验报告格式 ·实验报告模板
四、程序调试记录
第13篇:C语言程序设计第四次(2.8)实验报告
C语言程序设计
实验报告
专业
班级
日期
11月26日
成绩
实验组别
第
3(2.7) 次实验
指导教师
李开
学生姓名
学号
同组人姓名
实验名称 实验8 指针实验
一、实验目的
(1)熟练掌握指针的说明、赋值、使用。
(2)掌握用指针引用数组的元素,熟悉指向数组的指针的使用。
(3)熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。 (4)掌握指针函数与函数指针的用法。 (5)掌握带有参数的main函数的用法。
二、实验任务
8.2 实验内容及要求 1.源程序改错
2.源程序完善、修改、替换 3.跟踪调试 4.程序设计 5.选做题
8.3 指定main函数的参数
三、实验步骤及结果
(要求给出源程序和程序运行结果。另外,根据实验内容,记录编辑、编译、链接、调试程序的操作过程和实验现象) 8.2 实验内容及要求 1.源程序改错
下面程序是否存在错误?如果存在,原因是什么?如果存在错误,要求在计算机上对这个例子程序进行调试修改,使之能够正确执行。 #include void main(void) { float *p; scanf(\"%f\",p); printf(\"%f\\n\",*p); }
存在,错误为指针一开始没有初始化,而sacnf传入的是float型指针指向的地址,我们并不知道系统能给我们分配什么地址,所以说我们输入的地址很有可能使程序崩溃。 修改后代码:
#include int main(void) { float *p; float a[10];//这里可以换成其他数字 p=&a[0]; scanf(\"%f\",p); printf(\"%f\\n\",*p); return 0; }
2.源程序完善、修改、替换
(1)下面的程序通过函数指针和菜单选择来调用字符串拷贝函数或字符串连接函数,请在下划线处填写合适的表达式、语句、或代码片段来完善该程序。 #include #include void main(void) { char*(*p)(char a[],char b[]); char a[80],b[80],c[160],*result=c; int choice,i; do{
printf(\"\\t\\t1 copy string.\\n\");
printf(\"\\t\\t2 connect string.\\n\");
printf(\"\\t\\t3 exit.\\n\");
printf(\"\\t\\tinput a number (1-3) please!\\n\");
scanf(\"%d\",&choice); }while(choice5); switch(choice){ case 1:
p=strcpy;
break;
case 2:
p=strcat;
break; case 3:
goto down; } getchar(); printf(\"input the first string please!\\n\"); i=0; gets(a); printf(\"input the second string please!\\n\"); i=0; gets(b);
result= p(a,b); printf(\"the result is %s\\n\",result); down: ; } (2)请上机运行第(1)题程序,使之能按要求输出下面结果:((输入)表示该数据是键盘输入数据)
1 copy string. 2 connect string. 3 exit. input a number (1-3) please! 2 (输入)
input the first string please! the more you learn, (输入) input the second string please! the more you get.(输入)
the result is the more you learn,the more you get.
3.跟踪调试
#include char *strcpy(char *,char *); void main(void) { char a[20],b[60]=\"there is a boat on the lake.\"; printf(\"%s\\n\",strcpy(a,b)); } char *strcpy(char *s,char *t) { while(*s++=*t++) ; return (s); } (1)单步执行。进入strcpy时watch窗口中s为何值?返回main时, watch窗口中s为何值?
进入strcpy时:
返回main时:
(2)排除错误,使程序输出结果为: there is a boat on the lake.
#include void *strcpy(char *,char *); int main(void) { char a[30],b[60]=\"there is a boat on the lake.\"; strcpy(a,b); printf(\"%s\\n\",a); return 0; } void *strcpy(char *s,char *t) { while(*t!=\'\\0\') { *s++=*t++; } *s=\'\\0\';//将函数类型设置为void型,然后不再返回直接打印a字符串即可 }
4.程序设计
(1)一个长整型变量占4个字节,其中每个字节又分成高4位和低4位。试从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。 #include int main(void) { int n,i; scanf(\"%x\",&n); char *p=(char *)&n; int high_half,low_half; for(i=3; i>=0; i--){ low_half = p[i] & 0x0f; if (low_half
return 0; }
(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。编写一个函数,它将每一行中连续的多个空格字符压缩为一个空格字符。在调用函数中输出压缩空格后的各行,空行不予输出。
#include #define N 10 void reducespace(char *p,int n); int main() { int i; char *p[N],arr[N][81]; printf(\"Input %d-line strings:\\n\",N); for (i = 0; i
*(p + i) = arr[i];
printf(\"the %d line:\", i + 1);
gets(*(p + i)); } printf(\"\\n\\nafter compreion:\\n\"); for (i = 0; i
reducespace(*(p + i),i); } putchar(10); return 0; } void reducespace(char *p,int n)//削减空格的函数,将多行空格压缩成一个空格 { int flag = 1, i, j = 0, sum = 0; char temp[81]; for (i = 0; *(p + i) != \'\\0\'; i++) {
if (*(p + i) != \' \'&&flag)
{
temp[j++] = *(p + i);
sum++;
}
if (*(p + i) == \' \'&&flag)
flag = 0;
if (*(p + i) != \' \'&&!flag)
{
temp[j++] = \' \';
temp[j++] = *(p + i);
flag = 1;
sum++;
}
temp[j] = \'\\0\'; } if (sum)
printf(\"The %d line:%s\\n\", n+1,temp); else
printf(\"Of the %d line there are all spaces\\n\", n + 1); }
(3)输入n个整数,排序后输出。排序的原则由命令行可选参数-d决定,有参数-d时按递减顺序排序,否则按递增顺序排序。要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。(main函数参数的处理见8.3节)
#include #include #include int main(int argc,char *argv[]) { void downsort(char* *p,int m); void upsort(char * *q,int k); int n=0; while(n*q[j+1]) t=*q[j],*q[j]=*q[j+1],*q[j+1]=t; for(i=0;i
(4)设某个班有N个学生,每个学生修了M门课程(用#define定义N、M)。输入M门课程的名称,然后依次输入N个学生中每个学生所修的M门课程的成绩并且都存放到相应的数组中。编写下列函数:
a.计算每个学生各门课程平均成绩; b.计算全班每门课程的平均成绩;
c.分别统计低于全班各门课程平均成绩的人数;
d.分别统计全班各门课程不及格的人数和90分以上(含90分)的人数。
在调用函数中输出上面各函数的计算结果。(要求都用指针操作,不得使用下标操作。) #include #define M 2//course #define N 2//students int main(void) {
char courses[M][30],students[N][20];
float tables[N][M];
int c,s;
char *co=&courses[0][0];//courses[2]
co+2*30
char *st=&students[0][0];
float *gr=&tables[0][0];
for(c=0;c
{
printf(\"Please Input The %d Course\\n\",c+1);
scanf(\"%s\",courses[c]);
}//course names
for(s=0;s
{
printf(\"Please Input The %d Student\\n\",s+1);
scanf(\"%s\",students[s]);
}//students names
for(s=0;s
{
printf(\"For students : %s\\n\",students[s]);
printf(\"Input his/her grades:\\n\");
for(c=0;c
{
printf(\"Please Input The %s \\n\",courses[c]);
scanf(\"%f\",&tables[s][c]);
}
}//tables
float sum=0;
for(s=0;s
{
sum=0;
printf(\"the average grades of %s:\\n\",st+s*20);
for(c=0;c
{
sum+=*(gr+s*M+c);
}
printf(\"%f\\n\",sum/M);
}//(1)
float sum_=0;
for(c=0;c
{
sum_=0;
printf(\"the average grades of %s:\\n\",co+c*30);
for(s=0;s
{
sum_+=*(gr+s*M+c);
}
printf(\"%f\\n\",sum_/N);
}//(2)
for(c=0;c
{
sum_=0;
printf(\"the number whose grades under average of %s\\n\",co+c*30);
int cou=0;
for(s=0;s
{
sum_+=*(gr+s*M+c);
}
for(s=0;s
{
if(*(gr+s*M+c)
{cou++;}
}
printf(\"%d\\n\",cou);
cou=0;
}//(3)
for(c=0;c
{
printf(\"the number whose grades under 60 of %s\\n\",co+c*30);
int cou=0;
for(s=0;s
{
if(*(gr+s*M+c)
cou++;
}
printf(\"%d\\n\",cou);
cou=0;
}
for(c=0;c
printf(\"the number whose grades higher than 90 of %s\\n\",co+c*30);
int cou=0;
for(s=0;s
{
if(*(gr+s*M+c)>=90)
cou++;
}
printf(\"%d\\n\",cou);
cou=0;
}
return 0; }
5.选做题
(1)设有N位整数和M位小数(N=20,M=10)的数据a,b。编程计算a+b并输出结果。 如:12345678912345678912.1234567891 + 98765432109876543210.0123456789 #include \"stdio.h\" #include \"ctype.h\" #define N 100 /* N表示参与运算数据最长的长度 */ void add_decimals(char *a,char *b,int *c,int n,char *d); void add_inte(char *a,char *b,int c,int n,char *d); int main(void) { char a1[N],b1[N],c1[N+1],a2[N],b2[N],c2[N+1]; char c; int q,w,e,r;//to count q=0; while((c=getchar())!=\'.\') { if(isdigit(c)) *(a1+q)=c; q++; } *(a1+q)=\'\\0\'; w=0; while((c=getchar())!=\'\\n\') { if(isdigit(c)) *(b1+w)=c; w++; } *(b1+w)=\'\\0\'; e=0; char d; while((d=getchar())!=\'.\') { if(isdigit(d)) *(a2+e)=d; e++; } *(a2+e)=\'\\0\'; r=0; while((d=getchar())!=\'\\n\') { if(isdigit(d)) *(b2+r)=d; r++; } *(b2+r)=\'\\0\';//input
int max,max_; if(w>r) { max=w; int t; for(t=r;te) { max_=q; int y; for(y=0;y
} else { max_=e; int y; for(y=0;y
return 0; } void add_decimals(char *a,char *b,int *c,int n,char *d) { int o,jin=0; for(o=n-1;o>-1;o--) { if(*(a+o)+*(b+o)-2*\'0\'+jin>=10) {*(d+o)=*(a+o)+*(b+o)+jin-1*\'0\'-10; jin=1; } else { *(d+o)=*(a+o)+*(b+o)-\'0\'; jin=0; }
} *(d+n)=\'\\0\'; *c=jin; } void add_inte(char *a,char *b,int c,int n,char *d) { int o; for(o=n-1;o>0;o--) { if(*(a+o)+*(b+o)-2*\'0\'+c>=10) {*(d+o+1)=*(a+o)+*(b+o)-1*\'0\'-10; c=1; } else { *(d+o+1)=*(a+o)+*(b+o)-1*\'0\'; c=0; }
} if(*a+*b+c-2*\'0\'>=10) { *(d+1)=*a+*b+c-\'0\'-10; *d=\'1\'; } else {*(d+1)=*a+*b+c-\'0\'; *d=\' \';} }
(2)编写使用复杂声明char *(*p[2])(const char *,const char *);的程序。 提示:p中元素可为strcmp、strstr等函数名。 8.3 指定main函数的参数
选择“project/ set programs\' arguments…”菜单命令,即可打开图2.12所示的对话框,在“Program arguments”文本框中输入main函数的参数。注意只输入命令行中文件名后的参
数,文件名不输人。
图2.12 输入main
函数的参数
四、实验体会
好好写代码,不该跟老师吵,星期三的晚上应该按课程来讲只是写代码,不能干其他事情,对此我深刻检讨。
第14篇:C语言与程序设计实验报告六
《C语言及程序设计》实验报告
实验六 选择、循环结构程序设计
实验目的:
进一步对选择结构和循环结构进行程序设计。 实验内容:
1、打印输出如下图所示的图形:
A
BBB
CCCCC
DDDDDDD #include \"stdio.h\" #include \"conio.h\" main() {
int i,j,k;
for(i=0;i
{
for(j=0;j
printf(\" \");
for(k=0;k
printf(\"%c\",\'A\'+i);
printf(\"\\n\"); }
getch(); }
2、编程序解决百钱买百鸡问题。公元前5世纪,我国数学家张丘建在《算经》中提出“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
提示:采用穷举法解决该问题。先设变量,列出数学中的方程式,然后用for循环处理穷举。
#include #include \"conio.h\"
《C语言及程序设计》实验报告
main() {int x,y,z; for(x=0;x
第15篇:简单的C语言程序设计实验报告完美版
本科实验报告专用纸
课程名称
C语言程序设计
成绩评定
实验项目名称
简单的C语言程序设计
实验项目编号
实验项目类型
验证型
实验地点
指导教师
学生姓名
学号
学院
专业
一、
实验目的
1. 掌握各种类型数据的输入输出的方法,能正确使用各种格式转换符。
2. 学会正确使用逻辑运算符和逻辑表达式。 3. 熟练掌握if语句和switch语句。 4. 结合程序掌握一些简单的算法。
二、实验内容和实验要求
实验内容1::试编写程序,用getchar函数读入两个字符给c1,c2,然后分别用putchar函数和printf函数输出这个字符。
实验要求:(1) 输入事先已编好的程序,并运行该程序。分析运行结果是否正确。
(2)比较用Printf函数和putchar函数输出字符的特点。
实验内容2:给出一百分制成绩,要求输出成绩等级’A’,’B’,’C’,’D,’E’。90分以上为’A’,80-89分为’B’,70-79分为’C’,60-69分为’D’,60分以下为’E’。
第1页(共11页) 本科实验报告专用纸(附页)
实验要求:(1)事先编写好程序,要求分别用if语句和switch语句来实现。
(2)输入程序,并运行该程序。分析运行结果是否正确。
(3)再运行一次程序,输入分数为负值(如-70),这显然是输入是出错,不应该给出等级,修改程序,使之能正确处理任何数据,当输入数据大于100和小于0时,通知用户“输入数据错”,程序结束。
三、主要仪器设备
仪器:计算机
实验环境: windowsXP+visual c++6.0
四、实验过程
实验内容1:
原理:1)用getchar函数输入两个字符赋给c1,c2; 2) 用putchar函数输出这两个字符; 3) 用printf函数输出这两个字符。
(1)源程序
第2页(共11页) 本科实验报告专用纸(附页)
(2)在visual c++6.0 环境下运行程序 (3)调试结果:
(4)运行结果:
可见运行结果正确
(5)比较用Printf函数和putchar函数输出字符的特点。
printf和putchar函数是输出函数,向显示器屏幕输出数据。
printf是格式输出函数,可按指定的格式显示任意类型的数据
第3页(共11页) 本科实验报告专用纸(附页)
putchar是字符显示函数,只能显示单个字符。
实验内容2:
一)用if语句编写该程序
原理:1) 用scanf函数从计算机终端输入成绩g; 2) 用if语句的嵌套结构选择相应成绩的等级; 3) 用printf函数输出该成绩等级。 (1)源程序:
(2)在visual c++6.0 环境下运行程序 (3)调试结果:
第4页(共11页) 本科实验报告专用纸(附页)
(4) 运行结果:
1)把0~100之间不同等级成绩依次输入,得运行 结果:
运行结果正确
第5页(共11页) 本科实验报告专用纸(附页)
2)再运行一次程序,输入分数101和 -20,得运 行结果:
此运行结果显然错误,当输入数据大于100和小于0时不该给出等级,故程序不能正确处理大于100和小于0的数据。
3)修改程序,使之能正确处理任何数据,当输入数据大于100和小于0时,通知用户“输入数据错误”。仍利用if语句扩大处理数据的范围。源程序如下:
第6页(共11页) 本科实验报告专用纸(附页)
在visual c++6.0 环境下运行该程序 调试结果:
重新输入分数101和 -20,得运行结果:
运行结果正确
二) 用switch语句编写该程序
原理:1) 用scanf函数从计算机终端输入成绩g; 2) 用switch语句判断整数类型表达式g/10的值并与各case标号比较,从而实现多分支选择。 3) 用printf函数输出该成绩等级。 (1)源程序:
第7页(共11页) 本科实验报告专用纸(附页)
(2)在visual c++6.0 环境下运行程序 (3)调试结果:
(4)运行结果:
1)把0~100之间不同等级成绩依次输入,得运行结果
第8页(共11页) 本科实验报告专用纸(附页)
运行结果正确。
2)再运行一次程序,输入分数200,101和 -2,得运 行结果:
此运行结果显然是错误的,当输入数据大于100和小于0时,不应该给出等级,故此程序不能正确处理大于100和小于0
第9页(共11页) 本科实验报告专用纸(附页)
的数据。
3)修改程序,使之能正确处理任何数据,当输入数据大于100和小于0时,通知用户“输入数据错误”。在原源程序的switch语句前加上一个if语句。若g>100或g
在visual c++6.0 环境下运行该程序 调试结果:
重新输入分数200,101和 -2,,得运行结果:
第10页(共11页) 本科实验报告专用纸(附页)
运行结果正确。
五、实验心得
通过该实验,我对用if语句实现选择结构及其嵌套结构,用switch语句实现多分支选择结构的几种做法更加熟练,熟悉了几种输出输入函数的调用,从而对C语言程序设计有了较好的认识。
第11页(共11页)
第16篇:《C++程序设计》实验报告
注意:以下蓝色文字部分根据情况增删
实验1熟悉VC++6.0集成开发环境
一、实验时间:
2011年9月20日
二、实验地点:
第7机房
三、指导教师:
邓谱
四、实验目的:
1.了解VC++6.0的主要特点;
2.熟悉VC++6.0的集成开发环境;
3.学习用VC++6.0编写简单的C++应用程序,掌握C++程序的上机调试步骤。
五、实验内容:
1.P19 第1题:在屏幕上输出字符串“欢迎大家学习VC++6.0!”
【编程思想】
【流程图】
【源程序】见上机作业“实验1\\1_1\\1_1.cpp”
问题1:不知道如何在VC++6.0里输入源程序
解决方法:
(1)首先建立“Win32 Console Application”工程;
(2)再向工程中添加源文件;
(3)在添加的源文件中输入代码。
以上的详细操作流程见“Visual C++ 6.0集成开发环境.doc”文件。
问题2:error LNK2001: unresolved external symbol _WinMain@16:连接错误2001:未解决的外部符号_WinMain@16
问题原因:创建工程时选的创建“Win32 Application”,这是创建图形用户界面的C++工程,而程序是字符界面,两者不一致导致该错误。
解决方法:创建工程时应选“Win32 Console Application”
问题3:error C1010: unexpected end of file while looking for precompiled header directive:错误C1010:在查找预编译头时遇到意外的文件结尾。
问题原因:在选择创建工程种类时选择了“a simple application”,该工程还包含stdafx..h和stdafx..cpp两个文件,这两个文件是为编译系统进行预编译服务的,需要在源程序开始部分使用#include \"stdafx.h\"进行引用,如果未进行引用,会导致该错误。
解决方法1:在选择创建工程种类时选择第一项“an empty project”
解决方法2:如果工程文件列表中(窗口左边)有stdafx..h和stdafx..cpp两个文件,则源程序开始要使用#include \"stdafx.h\"进行引用。
问题4:error C2143: syntax error : miing \';\' before \'}\':错误C2143:语法错误:在\'}\'之前丢失了\';\'
问题5:……
2.P19 第2题:求两个整数之和
……
六、实验总结:
(将实验过程中或学习本章节的上机过程中值得记录的内容按1.2.3.分条写下来)
第17篇:C#程序设计实验报告
实验报告书写要求
实验报告原则上要求学生手写,要求书写工整。若因课程特点需打印的,标题采用四号黑体,正文采用小四号宋体,单倍行距。纸张一律采用A4的纸张。
实验报告书写说明
实验报告中实验目的和要求、实验仪器和设备、实验内容与过程、实验结果与分析这四项内容为必需项。教师可根据学科特点和实验具体要求增加项目。
填写注意事项
(1)细致观察,及时、准确、如实记录。 (2)准确说明,层次清晰。
(3)尽量采用专用术语来说明事物。
(4)外文、符号、公式要准确,应使用统一规定的名词和符号。 (5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现,以零分论处。
实验报告批改说明
实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验报告的批改成绩采用五级记分制或百分制,按《金陵科技学院课堂教学实施细则》中作业批阅成绩评定要求执行。
实验报告装订要求
实验批改完毕后,任课老师将每门课程的每个实验项目的实验报告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课程的实验大纲。
金陵科技学院实验报告
实验项目名称: C#基础编程 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 10月5日-10月19日 实验成绩: 批改教师: 批改时间:
金陵科技学院实验报告
实验1 C#基础编程
一、实验目的
1、熟悉Visual Studio .NET开发环境。
2、掌握C#应用程序的基本操作过程。
3、掌握C#的数据类型,运算符以及表达式的使用。
4、掌握分支和循环语句的使用方法。
5、掌握一维数组,二维数组及数组型数组的使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录 (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有Visual Studio .NET软件。
四、实验步骤
1、分析题意。
2、根据题目要求,新建项目。
3、编写并输入相关的程序代码。
5、运行与调试项目。
6、保存项目。
五、实验内容
1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace one.first {
cla Program
{
static void Main(string[] args)
{
System.Console.WriteLine(\"我叫王蕾!\");
}
} }
2、编写一个简单的Windows应用程序,在窗体Load事件中书写代码,标签中显示你的姓名。
using System; using System.Collections.Generic; using System.ComponentModel;
金陵科技学院实验报告
using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace one.second {
public partial cla Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = \"Windows 程序\";
Label lblShow = new Label();
lblShow.Location = new Point(20, 30);
lblShow.AutoSize = true;
lblShow.Text = \"王蕾!\";
this.Controls.Add(lblShow);
}
} }
3、编写一个一个程序,用来判断输入的是大写字母,小写字母,数字还是其他的字符。
using System; using System.Collections.Generic; using System.Text;
namespace one.third {
cla Program
{
static void Main(string[] args)
{
Console.WriteLine(\"请输入一个字符:\");
char c = Convert.ToChar(Console.ReadLine());
if ((c>=\'a\'&&c=\'A\'&&c
Console.WriteLine(\"这是一个字母\");
if (char.IsDigit(c))
Console .WriteLine(\"这是一个数字\");
金陵科技学院实验报告
}
} }
4、分别用while,do-while,for循环求1到100的和。
using System; using System.Collections.Generic; using System.Text;
namespace one.forth.one {
cla Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
while (i
{
sum = sum + i;
i++;
}
Console.WriteLine(\"1到100的自然数之和为:\" + sum);
}
} } using System; using System.Collections.Generic; using System.Text;
namespace one.forth.two {
cla Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
do
{
sum = sum + i;
i++;
}
while (i
Console .WriteLine(\"1到100的自然数的和为:\" + sum );
}
}
金陵科技学院实验报告
} using System; using System.Collections.Generic; using System.Text;
namespace one.forth.three {
cla Program
{
static void Main(string[] args)
{
int i , sum = 0;
for (i = 1; i
{
sum = sum + i;
}
Console.WriteLine(\"1到100的自然数的和为:\" + sum);
}
} }
5、定义一个一维数组,用随机数为此赋值,用foreach循环输出其中的内容。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace first.five {
cla Program
{
static void Main(string[] args)
{
int[] a = {0,1,2,3,4};
foreach (int i in a)
{
Console.WriteLine(a[i]);
}
}
} }
6、实现二维数组的输入和输出。
using System; using System.Collections.Generic; using System.Linq;
金陵科技学院实验报告
using System.Text;
namespace first.six {
cla Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
{
for (int i = 0; i
{
for (int j = 0; j
{ Console.WriteLine(a[i, j]); }
}
}
}
} }
7、实现数组型数组的输入和输出。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace first.seven {
cla Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
for (int i = 0; i
{
for (int j = 0; j
{
Console.WriteLine(a[i][j]);
}
}
}
} }
六、实验体会(遇到问题及解决办法,编程后的心得体会)
刚开始编程的时候觉得无从下手,尽管我们已经学了好几种高级编程语言,但每个都
金陵科技学院实验报告
有其独特的地方,稍不留神就会混淆。
通过这次实验,我体会到课后复习巩固的重要性。在编程的时候,很多内容都不记得,需要去翻书。不得不说,实验是巩固课程的好方法!本次实验,我熟悉Visual Studio .NET开发环境;掌握了C#应用程序的基本操作过程;掌握了C#的数据类型,运算符以及表达式的使用;掌握了分支和循环语句的使用方法以及一维数组,二维数组及数组型数组的使用。
金陵科技学院实验报告
实验项目名称: 类与对象 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 10月26日-11月9日 实验成绩: 批改教师: 批改时间:
金陵科技学院实验报告
实验2 类与对象
一、实验目的、要求
(1)掌握类的定义和使用;
(2)掌握类的数据成员,属性的定义和使用;
(3)掌握方法的定义,调用和重载以及方法参数的传递; (4)掌握构造函数的定义和使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有Visual Studio .NET软件。
四、实验步骤
1、分析题意;
2、根据题目要求,新建项目;
3、编写并输入相关的程序代码;
5、运行与调试项目;
6、保存项目。
五、实验内容
1、定义一个方法,实现两个数的交换(分别把参数按值传递和按引用传递)。
using System; using System.Collections.Generic; using System.Text;
namespace second.one {
cla Program
{
static void Main(string[] args)
{
Swaper s = new Swaper();
Console.WriteLine(\"输入x的值:\");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(\"输入y的值:\");
int b=Convert.ToInt32(Console.ReadLine());
金陵科技学院实验报告
Console.WriteLine(s.Swap(a, b));
Console.WriteLine(s.Swap(ref a,ref b));
}
cla Swaper
{
public string Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return 后:x={0},y={1}\",x,y);
}
public string Swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format(\"按引用传参交换之后:x={0},y={1}\", x, y);
}
}
} }
2、定义一个方法,实现数组的排序。using System; using System.Collections.Generic; using System.Text;
namespace second.two {
cla Program
{
string.Format(\"
按
值
传
参
交
换
之
金陵科技学院实验报告
public cla sort
{
public void change(int[] a)
{
Console.WriteLine(\"排序前,数组顺序为:\");
show(a);
int i, j, m;
for (i = 0; i
{
m = a[i];
j = ib;
}
}
static void Main(string[] args)
{
sum s = new sum();
int a = 10;
int b = 3;
int m, n;
s.ab(out m, out n, a, b);
Console.WriteLine(\"{0}+{1}={2};{0}-{1}={3}\",a,b,m,n);
金陵科技学院实验报告
}
} }
5、用构造函数重载,实现矩形的面积,圆的面积,梯形的面积;
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace secong.five {
cla Program
{
public cla square
{
public double area;
public square() { }
public square(double a)
{
area = a * a * 3.14;
}
public square(double a, double b)
{
area = a * b;
}
public square(double a, double b, double h)
{
area = (a + b) / 2 * h;
}
}
static void Main(string[] args)
{
double a, b, h,area;
a = 2; b = 5; h = 3;
金陵科技学院实验报告
square s = new square(a,b);
Console.WriteLine(\"求矩形面积,长为a={0},宽为b={1},面积area={2}\",a,b,s.area);
square i = new square(a);
Console.WriteLine(\"求圆形面积,半径a={0},面积area={1}\", a, i.area);
square j = new square(a, b, h);
Console.WriteLine(\"求梯形面积,上底为a={0},下底为b={1},高为h={2}面积area={3}\", a, b,h, j.area);
}
} }
6、设计一个windows应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号,姓名,语文,数学和英语成绩,要求:
1)能查询每个学生的总成绩。 2)能显示全班前三名的名单。
3)能显示单科成绩最高分和不及格的学生名单。 4)能统计全班学生的平均成绩。
5)能显示各科成绩不同分数段的学生人数的百分比。
Student类: using System; using System.Collections.Generic; using System.Text; namespace Test2_6 {
public cla Student
{
public string stuNo;
public string name;
public double chinese;
public double math;
public double english;
public double sumScore
{
金陵科技学院实验报告
get { return chinese + math + english; }
}
} } StudentList类: using System; using System.Collections.Generic; using System.Text; namespace Test2_6 {
public cla StudentList:Student
{
int snums;
public Student[] stu=new Student[50];
public StudentList()
{
snums = 0;
}
public void addstu(Student s)
{
stu[snums] = s;
snums++;
}
public int searchstu(string name)
{
int i;
for (i = 0; i
{
if (stu[i].name == name) break;
}
if (i == snums) return -1;
else return i;
}
//给所有成绩排序,用后面实现前三名的排名
金陵科技学院实验报告
public void ProThree()
{
for (int i = 0; i
{
int k = i;
for (int j = i + 1; j
if (stu[j].sumScore >stu[k].sumScore) k = j;
if (k != i)
{
Student temp;
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
}
//显示单科成绩的最高分
public int HighScore(int k)
{
int p = 0;
if (k == 0)
{
for (int i = 1; i
if (stu[i].math >stu[p].math) p = i;
}
else if (k == 1)
{
for (int i = 1; i
if (stu[i].chinese >stu[p].chinese) p = i;
}
else
{
for (int i = 1; i
if (stu[i].chinese >stu[p].chinese) p = i;
金陵科技学院实验报告
}
return p;
}
//显示不及格名单
public string BuhgName(int k)
{
string name=\" \";
if (k == 0)
{
for (int i = 0; i
if (stu[i].math
}
else if (k == 1)
{
for (int i = 0; i
if (stu[i].chinese
}
else
{
for (int i = 0; i
if (stu[i].english
}
return name;
}
public string getHL()
{
string Maxer = \" \", Loser = \" \";
Maxer += \"单科数学最高:\" + stu[HighScore(0)].name + \"\\n\";
Maxer += \" 单科语文最高:\" + stu[HighScore(1)].name + \"\\n\";
Maxer += \" 单科英语最高:\" + stu[HighScore(2)].name + \"\\n\";
Loser += \"单科数学挂科名单:\" +BuhgName(0) + \"\\n\";
Loser += \"单科语文挂科名单:\" + BuhgName(1) + \"\\n\";
Loser += \"单科英语挂科名单:\" + BuhgName(2) + \"\\n\";
金陵科技学院实验报告
return Maxer + \"\\n\" + Loser;
}
//全班的平均成绩
public string SumScore()
{
double sum = 0;
double avg=0;
for (int i = 0; i
{
sum = sum + stu[i].sumScore;
}
avg = sum / snums;
return \"班级总分平均分:\"+avg;
}
//各科成绩不同分数段的学生百分比
//英语成绩各分数段百分比
public string PerC()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i
{
if ((stu[i].chinese >90) && (stu[i].chinese
{
sumC1++;
}
else if ((80
{
sumC2++;
}
金陵科技学院实验报告
else if((70
{
sumC3++;
}
else if((60
{
sumC4++;
}
else
{sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return \"语文成绩百分比:\"+\"\\n\"+\"90~100:\"+per1+\" 80~90:\"+per2+\" 80~70:\"+per3+\" 70~60:\"+per4+\" 60以下的:\"+per5;
}
//数学成绩各分数段百分比
public string PerM()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i
{
if ((stu[i].math>90) &&(stu[i].math
{
sumC1++;
}
else if ((80
{
金陵科技学院实验报告
sumC2++;
}
else if ((70
{
sumC3++;
}
else if ((60
{
sumC4++;
}
else
{ sumC5++; }
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format(\"数学成绩百分比:\" + \"\\n\" + \"90~100:\" + per1 + \" 80~90:\" + per2 + \" 80~70:\" + per3 + \" 70~60:\" + per4 + \" 60以下的:\" + per5);
}
//英语成绩各分数段百分比
public string PerE()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i
{
if ((stu[i].english >90) && (stu[i].english
{
sumC1++;
金陵科技学院实验报告
}
else if ((80
{
sumC2++;
}
else if ((70
{
sumC3++;
}
else if ((60
{
sumC4++;
}
else
{ sumC5++; }
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format(\"数学成绩百分比:\" + \"\\n\" + \"90~100:\" + per1 + \" 80~90:\" + per2 + \" 80~70:\" + per3 + \" 70~60:\" + per4 + \" 60以下的:\" + per5);
}
} } From窗体代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
21
金陵科技学院实验报告
using System.Windows.Forms; namespace Test2_6 {
public partial cla Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public StudentList sl = new StudentList();
private void btnAdd_Click(object sender, EventArgs e)
{
Student s = new Student();
s.stuNo = txtStuNo.Text;
s.name = txtName.Text;
s.chinese = Convert.ToDouble(txtChina.Text);
s.math = Convert.ToDouble(txtMath.Text);
s.english = Convert.ToDouble(txtEng.Text);
sl.addstu(s);
MeageBox.Show(\"添加成功\");
}
private void btnSearch_Click(object sender, EventArgs e)
{
int pos = sl.searchstu(this.textBox1.Text);
if (pos != -1)
{
label7.Text = this.textBox1.Text + \"的总成绩:sl.stu[pos].sumScore;
}
else { MeageBox.Show(\"不存在这个人!\"); }
}
private void btnFinish_Click(object sender, EventArgs e)
{
label7.Text = \"前3名:\"+\"\\n\";
22
\" + 金陵科技学院实验报告
for (int i = 0; i
{
sl.ProThree();
label7.Text+= sl.stu[i].name+\"\\n\";
}
label7.Text += sl.getHL()+\"\\n\";
label7.Text += Convert.ToString(sl.SumScore())+\"\\n\";
label7.Text += sl.PerC()+\"\\n\";
label7.Text += sl.PerM()+\"\\n\";
label7.Text += sl.PerE()+\"\\n\";
}
} }
六、实验体会(遇到问题及解决办法,编程后的心得体会)
通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。值得注意的是:本次实验中return的使用以及所在的位置,类型转换时也经常用到
23
金陵科技学院实验报告
实验项目名称: 继承与多态 实验学时: 6 同组学生姓名: 实验地点: 1318 实验日期: 11月16日-11月30日 实验成绩: 批改教师: 批改时间:
24
金陵科技学院实验报告
实验3 继承与多态
一、实验目的、要求
(1)掌握类的继承性与多态性;
(2)掌握虚方法的定义以及如何使用虚方法实现多态; (3)掌握抽象类的定义以及如何使用抽象方法实现多态;
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有Visual Studio .NET软件。
四、实验步骤
1、分析题意;
2、根据题目要求,新建项目;
3、编写并输入相关的程序代码;
5、运行与调试项目;
6、保存项目。
五、实验内容
1、设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。
Student类: using System; using System.Collections.Generic; using System.Text; namespace Test3_1 {
public abstract cla Student
{
protected string name;
protected int age;
public static int number;
public Student(string name, int age)
{
this.name = name;
this.age = age;
25
金陵科技学院实验报告
number++;
}
public string Name
{
get { return name; }
}
public abstract double Average();
}
public cla Pupil : Student
{
protected double chinese;
protected double math;
public Pupil(string name, int age, double chinese, double math)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override double Average()
{
return (chinese + math) / 2;
}
}
public cla Middle : Student
{
protected double chinese;
protected double math;
protected double english;
public Middle(string name, int age, double chinese, double math, double english)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
26
金陵科技学院实验报告
this.english = english;
}
public override double Average()
{
return (chinese + math + english) / 3;
}
}
public cla College : Student
{
protected double required;
protected double elective;
public College(string name, int age, double required, double elective)
: base(name, age)
{
this.required = required;
this.elective = elective;
}
public override double Average()
{
return (required + elective) / 2;
}
} } Form窗体内的代码:
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test3_1 {
public partial cla Form1 : Form
{
27
金陵科技学院实验报告
public Form1()
{
InitializeComponent();
}
private void btnSmall_Click(object sender, EventArgs e)
{
Pupil ),Convert.ToDouble(txtMath.Text));
lblShow.Text += \"总人数:\" +Convert.ToString( Student.number) + \",\" + \"姓名:\" + p.Name + \",\" + \"小学生\" + \",\" + \"平均成绩为:\" + p.Average() +\"\\n\";
}
private void btnMiddle_Click(object sender, EventArgs e)
{
Middle Convert.ToInt32(txtAge.Text),
m
=
new
Middle(txtName.Text,
Convert.ToDouble(txtChinese.Text), p
=
new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.TextConvert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));
lblShow.Text += \"总人数:\" + Convert.ToString(Student.number) + \",\" + \"姓名:\" + m.Name + \",\" + \"中学生\" + \",\" + \"平均成绩为:\" + m.Average() + \"\\n\";
}
private void btnBig_Click(object sender, EventArgs e)
{
College Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtMath.Text));
lblShow.Text += \"总人数:\" + Convert.ToString(Student.number) + \",\" + \"姓名:\" + c.Name + \",\" + \"大学生\" + \",\" + \"平均成绩为:\" + c.Average() + \"\\n\";
}
}
c
=
new
College(txtName.Text,
Convert.ToDouble(txtChinese.Text),
28
金陵科技学院实验报告
}
2、设计一个Windows应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。
Figure类代码: using System; using System.Collections.Generic; using System.Text; namespace Test3_2 {
public abstract cla Figure
{
public abstract double Area();
}
public cla Circle:Figure
{
double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return radius * radius * 3.14;
}
}
public cla JUxing:Figure
{
double chang;
double kuan;
public JUxing(double c, double k)
{
this.chang = c;
this.kuan = k;
}
29
金陵科技学院实验报告
public override double Area()
{
return chang * kuan;
}
}
public cla San:Figure
{
double bian;
double heigth;
public San(double b, double h)
{
this.bian = b;
this.heigth = h;
}
public override double Area()
{
return bian * heigth / 2;
}
} } Form窗体代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test3_2 {
public partial cla Form1 : Form
{
public Form1()
{
30
金陵科技学院实验报告
InitializeComponent();
}
private void btnCircle_Click(object sender, EventArgs e)
{
Circle c=new Circle(Convert.ToInt32(TxtChang.Text));
lblShow.Text = \"圆的面积为:\" + c.Area();
}
private void btnJu_Click(object sender, EventArgs e)
{
JUxing
j
=
new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));
lblShow.Text = \"矩形的面积为:\" + j.Area();
}
private void btnSan_Click(object sender, EventArgs e)
{
San
s
=
new
San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));
lblShow.Text = \"三角形的面积为:\" + s.Area();
}
} }
3、定义一个Person类,包含姓名字段和一个方法,早上8:30学生开始上课,教师开始讲课。分别用new关键字,虚方法,抽象类实现多态性。
New关键字: using System; using System.Collections.Generic; using System.Text;
namespace third.three {
cla Program
{
static void Main(string[] args)
31
金陵科技学院实验报告
{
Student s=new Student(\"学生\");
Teacher t=new Teacher(\"教师\");
Console.WriteLine(s.name+s.work());
Console.WriteLine(t.name+t.work());
Console.ReadLine();
}
}
public cla Person
{
public string name;
public interface method
{ string work();}
}
public cla Student:Person
{
public Student(string name)
{ this.name = name; }
public string work()
{ return \"早上8:30开始上课\"; }
}
public cla Teacher:Person
{
public Teacher (string name)
{ this.name = name; }
public string work()
{ return \"开始讲课\"; }
} } 虚方法: using System; using System.Collections.Generic; using System.Text;
32
金陵科技学院实验报告
namespace third.three.two {
cla Program
{
static void Main(string[] args)
{
Student s = new Student(\"张三\",\"学生\");
PersonWork(s);
Teacher t=new Teacher(\"李斯\",\"教师\");
PersonWork(t);
}
private static void PersonWork(Person Person)
{ Console.WriteLine(Person.Work()); }
}
public cla Person
{
public string name;
public Person(string name)
{ this.name = name; }
public virtual string Work()
{ return string.Format(\"Person{0}:早上8:30开始\",name);}
}
public cla Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{ this.type = type; }
public override string Work()
{
return string.Format(\"Person{0}:早上8:30开始上课\", name);
}
}
public cla Teacher : Person
33
金陵科技学院实验报告
{
private string type;
public Teacher(string name, string type)
: base(name)
{ this.type = type; }
public override string Work()
{
return string.Format(\"Person{0}:开始讲课\", name);
}
} }
抽象类: using System; using System.Collections.Generic; using System.Text;
namespace third.three.three {
cla Program
{
static void Main(string[] args)
{
Student s = new Student(\"张三\", \"学生\");
PersonWork(s);
Teacher t = new Teacher(\"李斯\", \"教师\");
PersonWork(t);
}
private static void PersonWork(Person person)
{
Console.WriteLine(person.Work());
}
}
public abstract cla Person
34
金陵科技学院实验报告
{
public string name;
public Person(string name)
{ this.name = name; }
public abstract string Work();
}
public cla Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(\"Person{0}:早上8:30开始上课\", name);
}
}
public cla Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format(\"Person{0}:开始讲课\", name);
}
} }
35
金陵科技学院实验报告
六、实验体会(遇到问题及解决办法,编程后的心得体会)
通过本次实验,我理解了类的继承性与多态性;掌握了虚方法的定义以及如何用虚方法来实现多态;掌握了抽象类的定义以及如何用抽象方法来实现多态。 这次实验与前两次不同,采用Windows应用程序,既涉及到代码段也涉及到界面的设计。所以,勉强通过实验。
36
金陵科技学院实验报告
实验项目名称: 接口、文件和流 实验学时: 6 同组学生姓名: 实验地点: A205 实验日期: 12月7日-12月21日 实验成绩: 批改教师: 批改时间:
金陵科技学院实验报告
实验4 接口、文件和流
一、实验目的
(1)掌握接口的定义及使用方法;
(2)掌握流,序列化和反序列化的概念和使用方法; (3)掌握流文件的读写操作类及其使用方法;
(4)掌握OpenFileDialog,SaveFileDialog等控件的使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有Visual Studio .NET软件。
四、实验步骤
1、分析题意;
2、根据题目要求,新建项目;
3、编写并输入相关的程序代码;
5、运行与调试项目;
6、保存项目。
五、实验内容
1、定义一个Person类,包含姓名字段和一个方法,早上8:30学生开始上课,教师开始讲课。用接口来实现。
using System; using System.Collections.Generic; using System.Text; namespace Test4_1 {
cla Program
{
static void Main(string[] args)
{
Student s = new Student(\"张三\",\"学生\");
Console.WriteLine(s.Work());
Teacher t = new Teacher(\"李四\",\"老师\");
Console.WriteLine(t.Work());
}
金陵科技学院实验报告
public abstract cla Person
{
public string name;
public Person(string name)
{
this.name = name;
}
}
interface IPerson
{
string type { get;}
string Work();
}
public cla Student :Person, IPerson
{
public string type
{
get { return string.Format(\"老师\"); }
}
public Student(string name, string type)
: base(name)
{
this.name=name;
}
public string Work()
{
return string.Format(\"Person{0}:早上8:30开始上课\", name);
}
}
public cla Teacher :Person, IPerson
{
public string type
{
金陵科技学院实验报告
get { return string.Format(\"学生\"); }
}
public Teacher(string name, string type)
: base(name)
{
this.name = name;
}
public string Work()
{
return string.Format(\"Person{0}:早上8:30开始讲课\", name);
}
}
} }
2、声明一个接口Iplayer,包含5个接口方法:播放,停止,暂停,上一首和下一首。在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口。
MP3类(包含Iplayer接口,AVI类): using System; using System.Collections.Generic; using System.Text; namespace Test4_2 {
interface IPlayer
{
string Play();
string Stop();
string Pause();
string Pre();
string Next();
}
public cla MP3:IPlayer
{
public string Play()
金陵科技学院实验报告
{
return \"正在播放MP3歌曲!\";
}
public string Stop()
{
return \"停止播放MP3歌曲!\";
}
public string Pause()
{
return \"暂停播放MP3歌曲!\";
}
public string Pre()
{
return \"播放上一首MP3歌曲!\";
}
public string Next()
{
return \"播放下一首MP3歌曲!\";
}
}
public cla AVI : IPlayer
{
public string Play()
{
return \"正在播放AVI歌曲!\";
}
public string Stop()
{
return \"停止播放AVI歌曲!\";
}
public string Pause()
{
return \"暂停播放AVI歌曲!\";
}
金陵科技学院实验报告
public string Pre()
{
return \"播放上一首AVI歌曲!\";
}
public string Next()
{
return \"播放下一首AVI歌曲!\";
}
} } Form窗体里代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test4_2 {
public partial cla Form1 : Form
{
IPlayer iplayer;
MP3 mp3;
AVI avi;
public Form1()
{
InitializeComponent();
}
private void btnMp3_Click(object sender, EventArgs e)
{
mp3 = new MP3();
iplayer = (IPlayer)mp3;
}
金陵科技学院实验报告
private void btnPlay_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Play();
}
private void btnUp_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Pre();
}
private void btnStop_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Stop();
}
private void btnPause_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Pause();
}
private void btnNext_Click(object sender, EventArgs e)
{
label1.Text = iplayer.Next();
}
private void btnAvi_Click(object sender, EventArgs e)
{
avi = new AVI();
iplayer = (IPlayer)avi;
}
} }
3、实现对文本文件的读写操作,用文件操作控件打开和保存文件。
using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace Test4_3
金陵科技学院实验报告
{
public partial cla Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
StreamWriter
sw = StreamWriter(saveFileDialog1.FileName,true);
sw.WriteLine(DateTime.Now.ToString());
sw.WriteLine(txtSource.Text);
sw.Close();
}
private void btnShow_Click(object sender, EventArgs e)
{
StreamReader sr = StreamReader(saveFileDialog1.FileName);
txtShow.Text = sr.ReadToEnd();
sr.Close();
}
} }
4、实现对二进制文件的读写操作。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;
new
new
金陵科技学院实验报告
namespace Test4_4 {
public partial cla Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
FileStream
fs
= FileStream(@\"d:\\Data\\student.dat\",FileMode.Append,FileAcce.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Int32.Parse(txtStuNo.Text));
bw.Write(TxtName.Text);
bool isMale;
if (rdoMan.Checked)
isMale = true;
else
isMale = false;
bw.Write(isMale);
fs.Close();
bw.Close();
}
private void btnShow_Click(object sender, EventArgs e)
{
lstShow.Items.Clear();
lstShow.Items.Add(\"学号\\t姓名\\t性别\");
FileStream
fs
= FileStream(@\"d:\\Data\\student.dat\",FileMode.Open,FileAcce.Read);
BinaryReader br = new BinaryReader(fs);
fs.Position = 0;
while (fs.Position != fs.Length)
{
new
new
金陵科技学院实验报告
int s_no = br.ReadInt32();
string name = br.ReadString();
string sex = \"\";
if (br.ReadBoolean())
sex = \"男\";
else
sex = \"女\";
string
result String.Format(\"{0}\\t{1}\\t{2}\",s_no,name,sex);
lstShow.Items.Add(result);
}
br.Close();
fs.Close();
}
} }
5、实现对象序列化和反序化。
Student类: using System; using System.Collections.Generic; using System.Text; namespace Test4_5 {
[Serializable]
public cla Student
{
public int sno;
public string name;
public bool sex;
public Student(int s_no, string name, bool isMale)
{
this.sno = s_no;
this.name = name;
this.sex = isMale;
=
金陵科技学院实验报告
}
} } StudentList类: using System; using System.Collections.Generic; using System.Text; namespace Test4_5 {
[Serializable]
public cla StudentList
{
private Student[] list = new Student[100];
public Student this[int index]
{
get
{
if (index = 100)
return list[0];
else
return list[index];
}
set
{
if (!(index =100))
list[index] = value;
}
}
} } Form下的代码: using System; using System.Collections.Generic; using System.ComponentModel;
金陵科技学院实验报告
using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace Test4_5 {
public partial cla Test9_32 : Form
{
private StudentList list = new StudentList();
private int i = 0;
public Test9_32()
{
InitializeComponent();
}
private void Test9_32_Load(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
string file = @\"d:\\data\\student.dat\";
Stream
stream
= FileStream(file,FileMode.OpenOrCreate,FileAcce.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream,list);
stream.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
int sno = Int32.Parse(txtStuNo.Text);
bool isMale;
if (rdoMan.Checked)
isMale = true;
new
第18篇:C程序设计实验报告
浙江理工大学信息学院
C程序设计实验报告
实验名称: 学时安排:
实验类别:
实验要求:1人1组
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的
二、实验设备介绍
软件需求: Visual C++ 6.0或CFree3.5以上版本
硬件需求: 对于硬件方面的要求,建议配置是Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间、CD-ROM驱动器、能支持24位真彩色的显示卡、彩色显示器、打印机。
三、实验内容
四、程序清单
五、运行结果
六、实验心得
第19篇:C语言程序设计
1.Problem A: Hello world! Description Xiao_ming有两个哥哥,大哥叫Da_min,二哥叫Er_min。三兄弟放学回家,父母分别跟他们打招呼。
Input 无
Output 请输出: Hello Da_min, Hello Er_min, Hello Xiao_ming!
Sample Input Sample Output Hello Da_min, Hello Er_min, Hello Xiao_ming!
HINT 请注意换行符
Append Code #include int main() {
printf("Hello Da_min,\n");
printf("Hello Er_min,\n");
printf("Hello Xiao_ming!\n"); } 2. Problem B: 求圆的面积和周长 Description 从键盘输入圆的半径,求圆的面积和周长,圆周率取3.14。
Input 输入一个浮点型数据,有效数字不会超过十进制的6位。
Output 输出为两行。
第一行为圆的面积,第二行为圆的周长,格式见sample。
Sample Input 3
Sample Output Area: 28.260000 Perimeter: 18.840000
HINT 了解浮点类型的输入、输出和算术运算符
Append Code 法一
#include int main() { double r,p,s; scanf("%lf",&r); p=2*3.14*r; s=3.14*r*r; printf("Area: %lf\n",s); printf("Perimeter: %lf\n",p); } 法二
#include #define pi 3.14 int main() {
double r;
scanf("%d",&r);
printf("Area: %lf\n",pi*r*r);
printf("Perimeter: %lf\n",2*pi*r);
}
3.
Problem C:平均值 Description 求3个数的平均值。
Input 输入只有一行,为3个较小的整数。
Output 输出为这3个整数的平均值,保留3位小数。
Sample Input 1 2 3 Sample Output 2.000
HINT 注意除法运算对整型数据和浮点型数据是不一样的。
Append Code #include int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); printf("%.3lf",(a+b+c)/3.0); }
4.Problem D: 求字符的值 Description 从键盘输入3个字符(不含双字节字符),分别输出每个字符的十进制值(ASCII码)、八进制值和十六进制值。
Input 输入为3个字符。
Output 输出为3行。
每一行为每个字符(对应输入顺序)的十进制、八进制和十六进制值,用空格分隔开。每个输出的值占3个字符,不足3个字符前面补0。
Sample Input 0 A
Sample Output 048 060 030 032 040 020 065 101 041
HINT 了解字符值的存储和整型的关系。
Append Code #include int main() { char a,b,c; scanf("%c%c%c",&a,&b,&c); printf("%.3d %.3o %.3x\n",a,a,a); printf("%.3d %.3o %.3x\n",b,b,b); printf("%.3d %.3o %.3x\n",c,c,c); }
5.
Problem A: 最简单的程序 Description 输出一行“Hello world!”。
Input 无
Output Hello world!
Sample Input Sample Output Hello world!
HINT Append Code #include int main() {
printf("Hello world!"); }
6. Problem B: 算术基本运算 Description 计算两整数x和y(0
Input 输入只有一行,格式见sample。
Output 输出为多行,按顺序每行输出x,y的和、差、积、商、余数、x的平方和y的三次方,格式见sample
Sample Input x = 11, y = 3
Sample Output x + y : 14 xy : %d\n",x-y);
printf("x * y : %d\n",x*y);
printf("x / y quotient: %d, remainder: %d\n",x/y,x%y);
printf("x ^ 2 : %d\n",x*x);
printf("y ^ 3 : %d\n",y*y*y); } 7.Problem C: 自增自减运算 Description C语言中有自增运算++、自减运算--,并且可以前置和后置。
编写一个程序:对输入的一个整数a,输出a++、a--、++a、--a的值。
Input 一个整数a,a是很小的整数。
Output 输出为5行,第一行为a的值,后面依次为--a、a--、a++、++a的值。
5行输出纵向a要对齐,“:”对齐,“:”前后各一个空格,后面的数值不用对齐。每行行尾都有回车。
Sample Input 0
Sample Output a : 0 --a : -1 a-- : 0 a++ : 0 ++a : 1
HINT Append Code #include int main() { int a,x; scanf("%d\n",&x); a=x; printf(" a
: %d\n",a); printf("--a
: %d\n",--a); a=x; printf(" a-- : %d\n",a--); a=x; printf(" a++ : %d\n",a++); a=x; printf("++a
: %d\n",++a); }
8. Problem F: 绝对值 Description 求整型数据和浮点型数据的绝对值。
Input 输入两个数,第一个是整数,第二个是浮点数。
Output 输出为两行,第一行为整数的绝对值,第二行为浮点数的绝对值,注意浮点数的绝对值不输出无意义的0。
Sample Input -1 1
Sample Output 1 1
HINT 求绝对值可以用标准库函数来完成,也可以自己判断。注意浮点数的输出格式。求绝对值的函数在哪个头文件?貌似很多人会搞错,包括很多编书的人!
Append Code #include //#include int main(){
int n;
float m;
scanf("%d%f",&n,&m);
//n=abs(n);
//m=fabs(m);
if(n
{
n=-n;
}
if(m
{
m=-m;
}
printf("%d\n",n);
printf("%g",m);
return 0; }
9. Problem A: 奇数还是偶数? Description 输入一个整数,判读它是奇数还是偶数。
Input 输入只有一行,为一个100以内的正整数。
Output 输出为一行。
若输入为偶数则输出“even”,奇数输出“odd”。 Sample Input 30
Sample Output even
HINT 用整数运算可以解决,练习“?:”表达式。
Append Code #include int main(){
int n;
scanf("%d",&n);
if(n>=0&&n
if(n%2==0)printf("even\n");
else printf("odd\n");
}
return 0; }
10.Problem B: 简单的打折计算 Description 商店规定:消费满n元,可以打八八折。设某件商品标价m元,输入购买的件数x,计算出需要支付的金额(单位:元),精确到分。
Input 输入只有一行,三个整数m、n和x,且0
Sample Input 95 300 4
Sample Output 334.40
HINT 了解浮点型的输出控制,注意整型和浮点型混合运算过程中的数据类型转换。
Append Code #include int main() {
int m,x,n,a;
float b;
scanf("%d%d%d",&m,&n,&x);
0
x
m
a=m*x;
if(a>n)
b=0.88*a;
else
b=a;
printf("%.2f\n",b);
}
11. Problem C: 判断闰年 Description 输入一个正整数的年份,判断是否为闰年。
Input 输入只有一行,为一个10000以内的正整数。
Output 输出为一行。
若输入为闰年偶数则输出“Yes”,否则输出“No”。
Sample Input 2010
Sample Output No
HINT 了解逻辑运算符和关系运算符。
Append Code #include int main() {
int x;
scanf("%d",&x);
if(x>0&&x
{
if(x%4==0&&x%100!=0)
printf("Yes\n");
else if(x%400==0)
printf("Yes\n");
else
printf("No\n");
}
else
printf("error"); }
12.Problem D: 水仙花数 Description 如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153。
Input 一个整数x,100
Output x是水仙花数,则输出“YES”,否则为“NO”。
Sample Input 153
Sample Output YES
HINT Append Code #include int main() {
int a,b,c,x;
scanf("%d",&x);
a=x/100;
b=x/10%10;
c=x%10;
if(x==a*a*a+b*b*b+c*c*c)
printf("Yes");
else
printf("No"); }
13.Problem E: 三个数比较大小 Description 从键盘上输入0~100之间的三个数,按从小到大的顺序输出。
Input 输入只有一行,为三个整数。
Output 按从小到大输出这三个数。
Sample Input 15 10 20
Sample Output 10 15 20
HINT 用if语句判断各种情况可以解决这个问题。
Append Code #include int main() {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>=b)
{
if(b>=c)
printf("%d %d %d\n",c,b,a);
else if(c>=a)
printf("%d %d %d\n",b,a,c);
else
printf("%d %d %d\n",b,c,a);
}
else
{
if(a>=c)
printf("%d %d %d\n",c,a,b);
else if(b>=c)
printf("%d %d %d\n",a,c,b);
else
printf("%d %d %d\n",a,b,c);
} }
14.Problem F: 奇怪的求和之一 Description 给定3个正整数a、b和c,按照如下规则求和: 如果这个数字是偶数,则累加到和中;
如果这个数字是奇数,则将大于该数的最小偶数累加到和中。
Input 三个正整数,均在100以内。
Output 一个和。
Sample Input 2 3 5
Sample Output 12
HINT 如果不会使用分支语句, 可使用条件运算符判断到底将哪个数累积到和中。
Append Code #include int main() {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a%2!=0)a++;
if(b%2!=0)b++;
if(c%2!=0)c++;
printf("%d\n",a+b+c);
}
15.Problem G: 你过线了吗? Description 经过四年的学习,你决定报考我国著名的“285”高校之一的北青大学,经过认真的复习,残酷的考试,终于知晓了自己的考试成绩,也知道了北青大学的录取分数线,请你编程判断,自己过线了吗? Input 输入有2行,第一行有4个正整数,分别表示三门课程的分数线以及总分分数线。第二行有3个非负整数,分别表示你考的三门课程的成绩。
Output 如果你的三门课程成绩都不低于相应课程的分数线,且你的考试总分也不低于北青大学的总分分数线要求,则输出“congratulations”,否则输出“sorry”。
Sample Input 70 80 70 240 80 80 82
Sample Output congratulations
HINT 如果你不会使用分支语句,同样可以使用条件运算符实现该程序。
Append Code #include int main() {
int a,b,c,d,e,f,g,h;
scanf("%d %d %d %d\n%d %d %d",&a,&b,&c,&d,&e,&f,&g);
h=e+f+g;
if(e>=a&&f>=b&&g>=c&&h>=d)
printf("congratulations");
else
printf("sorry"); } 16.Problem I: A+B Problem (II) : Input/Output
Practice Description 计算a+b,0
Input 输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。
Output 每行输出一个a+b的和,顺序与输入对应。
Sample Input 2 1 2 10 20
Sample Output 3 30
HINT N给出了测试样例数,用for循环处理方便。
Append Code #include int main() {
int n,a,b,i; Scanf(“%d”,&n); For(i=0;i
17.
Problem H: A+B Problem Description 计算a+b,0
Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。
Output 每行输出一个a+b的值,顺序与输入对应。
Sample Input 1 2 10 20
Sample Output 3 30
HINT OJ系统上测试输入结束符为EOF(End Of File),其值为-1。用scanf()把文件所有内容读完后,会读到EOF,所以可以用来判断输入是否完成,测试时可以用Ctrl+Z产生EOF。本题解法参看FAQ。 Append Code #include int main(){
int a,b;
while(scanf("%d%d",&a,&b)!=EOF){
printf("%d\n",a+b);
}
return 0; }
18.Problem A: 判断是否是元音字母 Description 输入一个英文字母,判断是否是元音字母。元音字母是:a,e,i,o u,A,E,I,O,U
Input 输入一个英文字母
Output 是元音字母,输出“yes”,否则输出“no”,行尾没有回车。
Sample Input A
Sample Output yes
HINT Append Code #include void main() {
char ch;
scanf("%c",&ch);
if(ch>='A'&&ch
ch=ch+32;
if(ch=='a')
printf("yes");
else if(ch=='e')
printf("yes");
else if(ch=='i')
printf("yes");
else if(ch=='o')
printf("yes");
else if(ch=='u')
printf("yes");
else
printf("no");
}
19.Problem B: 按顺序输出三个数 Description 对于输入的3个整数,按照从小到大的顺序输出。
Input 输入3个int类型内的整数,两两之间用一个空格隔开。
Output 按照从小到大的顺序输出上述三个数,两两之间用一个空格隔开。
Sample Input 2 1 3 Sample Output 1 2 3
HINT Append Code #include void main() {
int a,b,c,temp;
scanf("%d%d%d",&a,&b,&c);
if(a
{
temp=a;
a=b;
b=temp;
}
if(a
{
temp=a;
a=c;
c=temp;
}
if(b
{
temp=b;
b=c;
c=temp;
}
printf("%d %d %d\n",c,b,a); }
20.Problem E: Description 判断输入整数的奇偶性。
判断奇偶数 Input 输入为一个整数。
Output 输出只有一行,代表判断结果。如果输入的整数n是一个偶数,输出: n is an even number.否则输出:
n is an odd number.见样例。
Sample Input 12
Sample Output 12 is an even number.
HINT Append Code #include int main() {
int a;
scanf("%d",&a);
if(a%2==0)
printf("%d is an even number.",a);
else
printf("%d is an odd number.",a); }
21.Problem A: 判断两个整数的大小关系 Description 输入2个整数a和b,如果a>b,则输出1,否则输出0。
Input 两个整数a和b,均不超过int类型的表示范围。
Output 表示a>b的结果:如果a>b,则输出1,否则输出0。
Sample Input 3 4
Sample Output 0
HINT
Append Code #include int main() { int a,b; scanf("%d %d",&a,&b); if (a>b)
printf("1");
else
printf("0"); return 0; } 22.Problem D: 成绩的等级 Description 把百分制的考试成绩转换成五级制的成绩: 90~100:Excellent 80~89:Good 70~79:Average 60~69:Pa 0~59:Failing
不在0~100之间的输入是非法数据,输出“Error”。
Input 输入多行,每行一个整数。
Output 输入所对应的成绩等级。
Sample Input -1 81 92 35 68 72 100
Sample Output Error Good Excellent Failing Pa Average Excellent HINT 用switch语句解决这个问题比较方便。
Append Code #include int main() {
int score;
while(scanf("%d",&score)!=EOF)
{
if (score100)
printf("Error\n");
else
{
switch (score/10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:printf("Failing\n");break;
case 6:printf("Pa\n");break;
case 7:printf("Average\n");break;
case 8:printf("Good\n");break;
case 9:
case 10:printf("Excellent\n");break;
}
}
}
return 0; }
23.Problem E: 输出是m的倍数或n的倍数、但不是
m和n的公倍数的数 Description 输出1~k之间是m的倍数或n的倍数、但不是m和n的公倍数的数,其中1
Input 输入三个整数,依次为k、m、n。
Output 从小到大输出符合题意的所有整数,两数之间用一个空格分开。
Sample Input 15 2 3
Sample Output 2 3 4 8 9 10 14 15
HINT 难点在于输出格式的控制:空格在数的中间,学会用循环时边界情况的特殊处理。
Append Code #include int main() { int k,m,n,a,i=1; scanf("%d %d %d",&k,&m,&n); if(m
a=m; else
a=n; printf("%d",a); for(i=a+1;i
}
if((i%m==0&&i%n!=0)||(i%n==0&&i%m!=0))
printf(" %d",i); } return 0;
24.Problem B: 两整数相加减 Description 计算a+b和a-b。
Input 输入为一对整数a和b。a,b用空格分开。
Output 输出a+b和a-b的计算结果,各占一行。
Sample Input 1 2
Sample Output 3 -1
HINT Append Code #include int main() { int a,b; scanf("%d %d",&a,&b);
printf("%d\n",a+b);
printf("%d\n",a-b); } 25 Problem C: 它满足条件吗? Description 需要判断给定的一个整数是否同时满足如下三个条件: 1.它是一个完全平方数。 2.它是一个偶数。 3.它是一个正数。
注:若一个数能表示成某个自然数的平方的形式,则称这个数为完全平方数。例如:
0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529 Input 一个int范围内的整数。
Output 如果输入的数字满足条件,则输出yes,否则输出no。
Sample Input 100
Sample Output yes
HINT 注意逻辑表达式判断三个条件的顺序。 如果你不会使用分支语句,同样可以使用条件运算符实现该程序。
库函数sqrt()可以用于求一个数的平方根。
Append Code #include #include int main() { int a; scanf("%d",&a);
if(a==sqrt(a)*sqrt(a)&&a%2==0&&a>0)
printf("yes");
else
printf("no"); }
26.Problem F: 多路分支 Description 编写一个完整的程序,运行时向用户提问"你考试考了多少分?(0-100)"接受输入后判断其等级并显示出来等级: 优:90
Input 输入任意一个整数分数值,显示等级; 再输入任意一个整数分数值,显示等级; ....直到测试数据较充分,可输入-1止。
Output 对任意输入的分数值,输出对应的等级,直到输入的数为-1时才退出运行.Sample Input 102 100 90 80 70 60 50 0 -80 -1
Sample Output grad must between 0 and 100 优
优
良
中
中
差
差
grad must between 0 and 100 grad must between 0 and 100
HINT Append Code #include int main() {
int x;
while(scanf("%d",&x)!=EOF)
{
if (x100)
printf("grad must between 0 and 100\n");
else if(x>=90)
printf("优\n");
else if(x>=80)
printf("良\n");
else if(x>=60)
printf("中\n");
else if(x>=0)
printf("差\n");
}
return 0; }
27 Problem D: 有多少人? Description 学校举行运动会,如果全体学生按照3人一队列队,则多了1个人;如果按照4人一队列队,则多了2个人;如果按照5人一队排队,则多了3个人。请问这个学校有多少学生?
Input 一个int类型的正整数N,是学生人数的上界,即:该校学生数不超过N。
Output 所有可能的学生数,每个数占一行。
Sample Input 200
Sample Output 58 118 178
HINT Append Code #include #include int main() {
int n,i;
scanf("%d",&n);
for(i==1;i
printf("%d\n",i); }
return 0; }
28 Problem C: 正负数各有几个? Description 输入若干个整数,求其中正数、负数的个数。
Input 输入分为2行:第一行是一个数字N>0,表示下面有N个整数。第2行是N个整数,都是int类型的。
Output 输出所输入的N个整数的正数个数和负数个数,并用空格分开2个输出。 Sample Input 10
1 2 3 4 5 -1 -2 -3 -4 -5
Sample Output 5 5
HINT 貌似还有一种叫做0的数。
Append Code #include int main() {
int n,a,i,num1=0,num2=0;
scanf("%d",&n); for(i=0;i
{
scanf("%d",&a);
if(a>0)
num1++ ;
else if(a
num2++;
}
printf("%d %d\n",num1,num2);
return 0; }
29 Problem A: A+B Problem (III) : Input/Output
Practice Description 计算a+b,0
Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0时表示输入结束,0 0不参与运算。
Output 每行输出一个a+b的值,顺序与输入对应。
Sample Input 1 2 10 20 0 0
Sample Output 3 30
HINT 练习break的使用。
Append Code #include int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF)
{
if(a!=0||b!=0)
}
printf("%d\n",a+b);
else
break;
return 0; } 30 Problem B: A+B Problem (IV) : Input/Output
Practice Description 计算a+b,0
Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。
Output 每行输出一个a+b的值,顺序与输入对应。每个格式样例之间用一个空行分隔开。
Sample Input 1 2 10 20 15 35
Sample Output 3 30 50
HINT 由于输出的和比空行多一个,所以全部计算放在一个循环里是不行的,必须要特殊处理开头或者结尾。
Append Code #include int main() { int a,b,n=0; while (scanf("%d %d",&a,&b)!=EOF) {
n++;
if(n==1)
printf("%d\n",a+b);
else
printf("\n%d\n",a+b);
}
return 0; }
31 Problem C: n个数的最大值和最小值 Description 找出n个数中最大的数和最小的数,并将它们的值输出出来。
Input 输入为n+1个整数,都在int类型范围内。这些数可能用若干空格或者换行符分隔开。 输入的第1个数为n,表示后续有n个数输入。从输入的第2个数开始,求出直到第n+1个数中最大的数和最小的数。
Output 输出为两行,格式见sample。
Sample Input 3 0 1 -1
Sample Output The maximum number is 1. The minimum number is -1.
HINT 分隔符是空格还是回车都是空白符,对scanf("%d")来说没有区别;先读入n,然后用for循环就很容易控制读入n个数的过程。
Append Code #include int main() {
int n,i,max,min;
scanf("%d",&n);
int a[n];
for(i=0; i
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=0; i
{
if(max
max=a[i];
if(min>a[i])
min=a[i];
}
printf("The maximum number is %d.\n",max);
printf("The minimum number is %d.",min);
return 0; } 32.Problem D: 求100以内的素数 Description 素数是只能被1和自身整除的正整数,根据数学定义1不是素数。素数也叫质数。
Input 输入为两个整数m和n,满足0
Output 从大到小输出m~n之间的所有素数,一个素数一行。如果m~n之间没有素数,则不输出任何数。
输出的所有数在两行“=====”之间。
Sample Input 2 12
Sample Output ===== 11 7 5 3 2 =====
HINT 利用素数的数学规律可以很容易的解出此题,题目给出的数据范围是关键。
Append Code #include #include int main() { int m,n,i,j,k,t; scanf("%d %d",&m,&n); printf("=====\n"); for(i=n;i>=m;i--) {
t=0;
for(j=2;j
if(i%j==0)
t=1;
if(t==0&&i>1)
printf("%d\n",i); } printf("====="); return 0; } 33.Problem E: 是否构成三角形? Description 给出三个整数,代表三条边的长度,判断这三条边的长度是否能构成一个三角形?
Input 第一行是n(1
接下来有n行,每一行包含三个整数,表示三个边长(1
Output 如果三条边能构成三角形,输出YES,否则输出NO
Sample Input 3 1 2 3 2 8 7 20 20 1
Sample Output NO YES YES
HINT Append Code #include int main() { int a,b,c,n,i; scanf("%d",&n); for(i=0;i
{scanf("%d%d%d",&a,&b,&c);
if(a+b>c&&a+c>b&&b+c>a)
printf("YES\n"); else printf("NO\n"); }
return 0; } 34.Problem C: 简单的整数排序 Description 对给出的若干整数按从小到大排序。
Input 输入的第一个数为n(n
Output 按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。
Sample Input 10 3 9 1 5 2 8 5 6 7 3
Sample Output 1 2 3 3 5 5 6 7 8 9
HINT 排序前必须把所有的整数都存储下来。因为只有最多1000个数,1秒的时间足够任何排序算法运行处结果来。
Append Code #include int main() {
int c,i,n,j;
int a[1000];
scanf("%d",&n);
for (i=0;i
scanf("%d",&a[i]);
for(i=1;i
{
for(j=0;j
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
printf("%d",a[0]);
for(i=1;i
printf(" %d",a[i]);
return 0; }
35.Problem A: Description
购物的路程 Saya和Kudo一起去购物。假定她们逛的街是一条直线,而商铺是这条直线上的一些点。她们将车停在该直线最左端的店铺处,然后从左向右开始逛每一个店铺,然后从最右边的店铺再返回到停车处。你的任务是计算她们走了多少路。
Input 输入有多组。每一组的第一行是N(0
Output 对每组输入,输出她们走的路长。
Sample Input 4
24 13 89 37 6 7 30 41 14 39 42 0
Sample Output 152 70
HINT Append Code #include int main() {
int n,i,max,min,a[100001];
while(scanf("%d",&n)&&n!=0)
{
scanf("%d",&a[0]);
min=max=a[0];
for(i=1;i
{
scanf("%d",&a[i]);
if(a[i]>max)
max=a[i];
if(a[i]
min=a[i];
}
printf("%d\n",(max-min)*2);
}
return 0; } 36.Problem B: 求累加和 Description 编程求min~max的累加和(含min和max),其中max>=min>0。部分程序已经给出,请填充其中的空白语句,并提交填充后的完整程序。
Input 输入为多行。第一行是一个整数N>0,表示后面有N个测试用例。后面有N行,每行包含2个整数,分别是min和max。
Output 输出为N行,每个测试用例的计算结果占据一行。每行的格式为: case i:sum=s.
其中i表示测试用例的编号(从1开始),s是该测试用例对应的累加和(设不超过int的表示范围)。
Sample Input 3 1 10 1 100 1 1
Sample Output case 1:sum=55. case 2:sum=5050. case 3:sum=1.
HINT Append Code #include int main() {
int n,i,j,max,min,sum;
scanf("%d",&n);
for(i=0;i
{ sum=0;
scanf("%d%d",&min,&max);
if(max==min)
printf("case %d:sum=%d.\n",i+1,min);
else
{
for(j=min;j
sum=sum+j;
printf("case %d:sum=%d.\n",i+1,sum);
}
} return 0; } 37.Problem G: 顺序输出字母 Description 按字母顺序输出两个字母st和ed之间的所有字母,但不包括st和ed。不输出逆序。
Input 两个字母st和ed,都是大写字母,用一个空格分开。
Output 在一行内按顺序输出st和ed之间的所有字母,但不包括st和ed。 例如:
输入为A和E,则输出为BCD;
输入为A和B,则什么字母也不输出,只有一个空行; 输入为E和A,也是什么字母也不输出, 只有一个空行。 最后要输出一行(行尾不回车): ***END***
Sample Input A C
Sample Output B
***END***
HINT Append Code #include int main() {
char st,ed,a;
int i;
scanf("%c%c%c",&st,&a,&ed);
{
for(i=st+1;i
printf("%c",i);
printf("\n***END***");
}
return 0; } 38.Problem F: 单字母变换 Description Tom和Jack是密码学爱好者,他们在聊天时经常使用一些暗语。他们使用的一种最简单的暗语是:将要说的每句话里面的英文字母变成这个字母之后的某个字母。现在要求你写一个程序,将一个字母变成它之后的某个字母。
Input 输入有2个:一个大写字母c和一个正整数d(0
Output 输出字母c之后的第d个字母。大小写与c一致。如果c之后的某个字母已经超出'Z',则再从字母'A'开始计数。
如:c='A',d=3,则输出应为:D。 若:c='Y',d=3,则输出应为:B。
Sample Input A 3
Sample Output D
HINT Append Code #include int main() {
char c;
int d;
scanf("%c%d",&c,&d);
if(c+d
printf("%c",c+d);
else
printf("%c",c+d-26);
return 0; }
39.Problem B: 登录密码验证 Description 编写一个程序,模拟用户登录系统的密码验证过程。系统提供给用户的密码长度最长为20个字符,若密码输入错误可以再次输入。但为了保证用户密码安全,若连续输入密码错误超过5次就会锁定账号一段时间。
Input 输入为若干个串,至EOF结束。输入的第一个串是用户的正确密码,后面的串为模拟用户登录时的输入的密码。
Output 每次输入错误的密码,输出一个“Wrong!”,若输入的密码为正确的,输出一个“Welcome!”,并结束密码测试。若前5次输入的密码都是错误的,则后面的输入中不管是否有正确的密码都输出“Out of limited!”。
Sample Input abcdefg
123456 kkkkkkkk abcdefg
Sample Output Wrong! Wrong! Welcome!
HINT 输入可以用scanf("%s")处理,密码比较用字符串的比较可以完成。
Append Code #include #include int main() {
char a[20],b[20];
int i,j=1;
scanf("%s",a);
while(scanf("%s",b)!=EOF)
{
if(j
{
if((strcmp(a,b)==0))
{
printf("Welcome!\n");
break;
}
else
printf("Wrong!\n");
j++;
}
else printf("Out of limited!\n");
} } 40.Problem C: 兔子的繁殖问题 Description 假设一对兔子每月能生一对小兔(一雌一雄),每对小兔出生后的下一个月是没有繁殖能力的,至出生后的第三个月开始又可以每月生一队小兔,问从一对刚出生的小兔开始,经过若干个月后一共有多少兔子(假设在此过程中兔子没有死亡)?
这个问题是意大利数学家菲波那契(Fibonacci)在他1202年出版的《算盘全书》中提出来的,从第一对刚出生的小兔开始每月的兔子数被乘坐菲波那契序列。
Input 输入的第一个数为n,接下来有n个数字。每个数字为一个月份m(m
Output 输出为n行,每行为第m个月后的兔子总数。
第20篇:C 语言程序设计
《C 语言程序设计》主要在计算机软件、计算机网络、计算机应用、通信技术、智能电子、电子信息工程、多媒体以及核工程与核技术等专业中开设,是电子信息类、机械类等工科专业的重要专业基础课。
本校从2001至今,每年都开设《C语言程序设计》课程,由于,我校最开始时的特殊情况,主要把重点放在C语言中的各类重点、难点的语法、语义上,使得学生经过比较复杂和痛苦的过程后,对C语言在使用上很难做到自己编写程序。而且很多学生产生了畏惧心理。
2004年,学院成立计算机科学与技术系,为了更好学习和掌握C语言,计算机科学与技术专业和计算机维护专业(计算机应用技术前身)除了开设《C语言程序设计》课程外,在后续一学期还开设了《C语言程序设计课程设计》,使得计算机专业学生编程思想和能力得到了极大提高。
2005年,为了让我院工科学生更好的掌握C语言编程以及通过四川省计算机二级考试,在《C语言程序设计》课程后设置《C语言程序设计课程设计》,极大地提高了学生的动手机会和能力,过级率显著提高!
2007年,依托我校建设精品课程的契机,我们将C语言中的各个知识点进行重新规整,提出适用于三本院校学生的语言设计学习的方法。我们提出,先了解C语言,再学习C语言,再用C语言的思想。即在开始时让学生初步知道C语言,然后引入比较简单的运算符以及基本输入、输出和简单的顺序结构程序设计。通过这样的方式,我们发现,学生在C语言的学习就不会从开始的时候陷入大量难懂的语法、语义和数据结构中。有了以上铺垫再层层进入,最终进入复杂的各类数据结构以及指针等学习。通过以上的内容规整后,教学效果有明显提高。