c语言中clrscr是什么_Turbo C的编程环境

实验一 熟悉Turbo C的编程环境

一:目的要求

1.熟悉Turbo C的编程环境;

2.知道程序编辑、链接、执行的基本步骤;

3.学习如何查错并修改程序;

4.上机前预习编写好程序。

二:实验内容与步骤

调试以下两个程序

/* The first C Program*/

#include <stdio.h>

void main()

{

printf(“Hello,World!n”);

}

#include <stdio.h>

void main()

{

int a, b, sum;

a = 123;

b = 456;

sum = a + b;

printf(“sum is %dn”, sum);

}

调试通过后,自己修改程序,使程序出错,并读懂错误提示,进行修改。

实验二 数据输入输出格式的程序设计

一.目的要求

1.进一步熟悉调试程序的方法;

2.熟练掌握格式输入与格式输出函数的使用。

3.掌握字符与ASCⅡ值之间的转换方法。

c语言中clrscr是什么_Turbo C的编程环境

编辑

添加图片注释,不超过 140 字(可选)

4.上机前预习编写好程序;

c语言中clrscr是什么_Turbo C的编程环境

编辑

添加图片注释,不超过 140 字(可选)

c语言中clrscr是什么_Turbo C的编程环境

编辑

添加图片注释,不超过 140 字(可选)

利用接口库函数dos.h的调用

1.类型定义结构类型struct date.d;

2.getdate(&d);

3.printf(“%d/%d/%d”,d.da_year, d.da_mon, d.da_day)。

五、程序提示:

#include<conio.h>

#include<dos.h>

main()

{

char c1;

struct date d;

clrscr();

getdate(&d);

···.

···

···

getch(); 暂停等待输入任意字符(或按键)

}

解答:

#include<stdio.h>

#include<conio.h>

#include<dos.h>

main()

{

char c1;

struct date d;

clrscr();

getdate(&d);

printf(“tt%d/%d/%d”,d.da_year, d.da_mon, d.da_day);

printf(“n* * * * * * * * * * * * *n”);

printf(“* Menu *n”);

printf(“* 1.Input *n”);

printf(“* 2.Output *n”);

printf(“* 3.End *n”);

printf(“* * * * * * * * * * * * *n”);

printf(” Enter Number=> n”);

printf(” Ch=———-> “);

gotoxy(20,8);

c1=getch();

gotoxy(20,8);

putchar(c1);

gotoxy(9,9);

putchar(c1);

gotoxy(20,9);

printf(“%d”,c1);

}

实验三 选择结构程序设计

一:目的要求

1.熟悉用关系运行与逻辑运行符的应用;

2.掌握if语句与switch语句的使用;

3.上机前按实验要求预习编写出完整的程序,才允许上机。

二:实验内容与步骤

1. 在书店买书 ,以100本为限。如果买 1 本不打折扣 ;买2本打折10%; 买3本折扣为 15 %,买3本以上折扣为 20 %。设书本数为 x,单价为 20.00元。

请使用else if多分支结构或if嵌套结构设计实现该算法的C程序。

2. 从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A;80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;score<60,等级为E。

请使用switch 语句实现上述功能。

(提示:将成绩整除10,把score转变为处于1~10之间的整数,从而转化成switch语句中的case标号)

三:选做实验

实验指导书56页第二章选择结构的任意实验

解答:

一、

#include<conio.h>

#include<stdio.h>

main()

{

int n;

float x,y;

clrscr();

printf(“Please Enter The Number:”);

gotoxy(25,1);

scanf(“%d”,&n);

x=20.00;

y=0;

if (n<0)

printf(“Sorry,you put the wrong number!”);

else if (n==0)

printf(“Please buy at least 1 book!”);

else if (n==1)

{ y=x;

printf(“——You should pay:%.2f”,y*n);

}

else if (n==2)

{ y=x*0.9;

printf(“——You should pay:%.2f”,y*n);

}

else if (n==3)

{ y=x*0.85;

printf(“——You should pay:%.2f”,y*n);

}

else if (n<=100)

{ y=x*0.8;

printf(“——You should pay:%.2f”,y*n);

}

else if (n>100)

printf(“Sorry,you can buy less then 100 books.”);

}

二、

#include<stdio.h>

#include<conio.h>

main()

{

int t;

float n;

clrscr();

printf(“Please Enter The Score:”);

gotoxy(24,1);

scanf(“%f”,&n);

t=n/10;

if (n<0)

printf(“Please Write The Right Score!”);

else if (n>100)

printf(“Is He A God?”);

else

switch(t)

{

case 10:printf(“A+”);break;

case 9:printf(“A”);break;

case 8:printf(“B”);break;

case 7:printf(“C”);break;

case 6:printf(“D”);break;

default:printf(“E”);break;

}

}

实验四 循环控制

一:目的要求

1.熟悉用while语句,do-while 语句和for语句实现循环的方法;

2.掌握在程序设计中用循环方法实现各种算法;

3.掌握计算程序运行所占机时的计算方法;

4.上机前按实验要求预习编写出完整的程序,才允许上机。

二:实验内容与步骤

目标:100匹马驮100担货;

假设:大马一匹驮3担,中马一匹驮2担,小马两匹驮一担;

组合方法1:大马、中马、小马每种不能少于一匹;

组合方法2:对马匹种类无限制,可以缺少一种或缺二种

问题: 1. 采用组合方法1,用while求解,输出所有组合法,输出组合的总数?

2.采用2种组合,用do-while求解,输出所有组合法,输出组合的总数?

3.采用2种组合,用三重或二重for循环求解,输出所有组合法,输出组合的总数?

4.除打印结果和多少种组合法外,还要分别打印三种算法所费机时。

提示:计算一种算法所占机时的程序提示:

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount;

clrscr();

start=clock(); /* start = time();*/

big=1; middle=1; small=2;

ncount=0;

printf(“This a while programn”);

while (big<=33)

{

.

}

end=clock(); /* end = time();*/

printf(“The num of method1 is: %dn”,ncount);

printf(“and the time is: %5.1f timen”,difftime(end,start));

/*printf f(“”The difference is :%5.1f secondn”, difftime(end,start)/18.2);*/

……}

解答:

一、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=1; middle=1; small=2,i=0,n=0;

ncount=0;

printf(“This a while programn”);

while (big<=33)

{

for(small=2;small<=100;small=small+2)

for(middle=1;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf(“Big=%d,Middle=%d,Small=%d,n=%d,i=%dn”,big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf(“n=%d “,n);

printf(“big=%d small=%d middle=%d “,big,small,middle);

printf(“n”);*/

}

end=clock();

printf(“The num of method1 is: %dn”,ncount);

printf(“and the time is: %5.1f timen”,difftime(end,start));

}

二、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf(“This a while programn”);

do

{

for(small=0;small<=100;small=small+2)

for(middle=0;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf(“Big=%d,Middle=%d,Small=%d,n=%d,i=%dn”,big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf(“n=%d “,n);

printf(“big=%d small=%d middle=%d “,big,small,middle);

printf(“n”);*/

}

while(big<=34);

end=clock();

printf(“The num of method1 is: %dn”,ncount);

printf(“and the time is: %5.1f timen”,difftime(end,start));

}

三、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf(“This a while programn”);

for(big=0;big<=34;big++)

{

for(small=0;small<=100;small=small+2)

for(middle=0;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf(“Big=%d,Middle=%d,Small=%d,n=%d,i=%dn”,big,middle,small,n,i);

ncount++;

}

}

/*

printf(“n=%d “,n);

printf(“big=%d small=%d middle=%d “,big,small,middle);

printf(“n”);*/

}

end=clock();

printf(“The num of method1 is: %dn”,ncount);

printf(“and the time is: %5.1f timen”,difftime(end,start));

}

实验五 函数

一、 目的要求

1. 掌握函数的定义和调用方法;

2. 掌握函数实参与行参的对应关系的,以及“值传递”的方式;

3. 掌握求最大公约数和最小公倍数的方法;

4. 按实验内容要求完成全程程序设计后才允许上机。

二、 实验内容与步骤

1. 设计一个函数f,求二个数的最大公约数int f(int x,int y);

2. 设计一个函数g,求二个数的最小公倍数int g(int x,int y);

3. 从键盘输入三个浮点数,求三个数中最大数和最小数的差值。

三、 函数的定义要求

在main( )中实现下列操作

1. a=inNumber( );

2. b= inNumber( );

3. c=f(a,b);

4. d=g(a,b);

5. 输出a,b,c,d。

解答:

一、

#include<stdio.h>

#include<math.h>

void main()

{

int a,b,c,d;

clrscr();

a=inNumber();

b=inNumber();

c=f(a,b);

d=g(a,b);

printf(“a=%d,b=%d,c=%d,d=%dn”,a,b,c,d);

}

int f(int x,int y)

{

int i,t,a;

float p1,p2;

if (x>=y)

t=y;

else

t=x;

for (i=1;i<=t;i++)

{

p1=x%i;

p2=y%i;

if (p1==0 && p2==0)

{

a=i;

}

}

return a;

}

int g(int x,int y)

{

int i,t,p1,p2,a;

if (x>=y)

t=x;

else

t=y;

for (i=t;i<=(x*y);i++)

{

p1=i%x;

p2=i%y;

if (p1==0 && p2==0)

{

a=i;break;

}

}

return a;

}

int inNumber(int x)

{

int a;

a=0;

while (a==0)

{

scanf(“%d”,&x);

if (x>0)

a=1;

else

{

printf(“Wrong!n”);

a=0;

}

};

return x;

}

二、

#include<stdio.h>

void main()

{

float a[3],x,y;

int i,j;

clrscr();

for (i=0;i<3;i++)

scanf(“%f”,&a[i]);

for (i=0;i<3;i++)

for (j=0;j<2;j++)

if (a[j]>a[j+1])

{

x=a[j];

a[j]=a[j+1];

a[j+1]=x;

}

y=a[2]-a[0];

printf(“ny=Max{a[i]}-Min{a[i]}:nt%.3fnt(%f)”,y,y);

}

实验六 数组

一、 目的要求

1. 掌握数组的定义、赋值和输入输出的方法;

2. 掌握清屏函数clrscr()的调用方法;

3. 掌握产生随机数函数randomize()的初始化及调用方法;

4. 上机前按实验要求预习,完成全部程序设计后才允许上机。

二、 实验内容与步骤

已知二维数组a[5][5],完成下列要求

(1) 输入数据

a[i][j]=random(100); /*产生100以内随机数*/

(2) 显示数组各元素,要求整齐排列;

(3) 将第1与第5行对调后,再显示之;

(4) 求出每行元素的最大值,并指出其行号和列号。

三、 输入随机数的要求

#include <stdlib.h>

#define RMAX 5

#define cMAX 5

#define nMAX 100

main()

{ . /*变量初始化说明*/

.

clrscr(); /*调清屏函数清屏*/

randomize(); /*在初始化后调用产生随机数函数*/

.

.

a[i][j]=random(nMAX);

.

.

.

}

解答:

#include <stdlib.h>

#define RMAX 5

#define cMAX 5

#define nMAX 100

main()

{

int a[5][5],i,j,temp[1][5],t,max,x,y;/*定义变量为整型(我的程序里面有些定义的变量没用,懒得删除了)*/

clrscr();/*清屏*/

randomize();/*在初始化后调用产生随机数函数*/

for (i=0;i<5;i++)

for(j=0;j<5;j++)

a[i][j]=random(nMAX);/*给数组a[i][j]赋随机值*/

for (i=0;i<5;i++)

{ for(j=0;j<5;j++)

printf(“%dt”,a[i][j]);/*输出数组a[i][j]*/

printf(“n”);/*在输出完一行后自动换行(不理解的话可以暂时删掉这句话,然后运行程序看结果)*/

}

for(j=0;j<5;j++)/*这个循环是用于换行的*/

{

temp[0][j]=a[0][j];/*将数组a的第一行的每个值都赋予temp(这里的temp是一个1*5的数组)*/

a[0][j]=a[4][j];/*将数组a的第一行的每个值替换成第五行的值*/

a[4][j]=temp[0][j];/*将数组a的第五行的每个值替换成temp的值*/

}

printf(“n”);/*输出一个回车*/

for (i=0;i<5;i++)

{ for(j=0;j<5;j++)

printf(“%dt”,a[i][j]);/*将换行后的新数组a输出出来*/

printf(“n”);/*在输出完每一行后自动换行*/

}

for (i=0;i<5;i++)

{

max=a[i][0];/*定义一个最大值变量max,令其暂时等于第i行的第一个数*/

for (j=0;j<5;j++)/*让max和该行的所以数比较,如果有某个数大于max,max变为该数的值*/

if (a[i][j]>max)

{

max=a[i][j];

}

for (j=0;j<5;j++)/*令max和第i行的每个数进行比较,输出所有等于max的数及该数的坐标*/

if(max==a[i][j])

printf(“na[%d][%d]=%d”,i,j,max);

}

}

实验七 指针

一、 目的要求

1. 掌握指针的定义和使用指针变量;

2. 学会使用字符串的指针和指向数组的指针变量;

3. 学会使用指向函数的指针变量;

4. 按实验内容要求完成全程程序设计后才允许上机。

二、 实验内容与步骤

设计一个函数,它有三个形参

(1) 被查找的字符串str;

(2) 待查找的字符xCh;

(3) 在字符串str中xCh出现的位置i=0,1,…

它的返回值是在str中xCh 出现的次数(若str中无xCh,则返回值=0)

三、 上机要求

1、 键入待查的字符xCh;

2、 键入被查的字符串str;

3、 调用该函数;

4、 打印它的返回值和出现的位置;

5、 允许重复执行,每次以清屏开始(用循环语句控制重复执行)。

四、 提示

xCh在str出现位置应设计为一整型指针,以便记下0~N个位置(整数)。

#include<stdio.h>

void main()

{

char temp;

int run(),j;

int (*prun)();

temp=’Y’;

while(temp!=’N’||temp!=’n’)

{

if(temp==’Y’||temp==’y’)

{

prun=run;

j=(*prun)();

if (j==0)

{

printf(“Can Not Find The xCh! j=%d”,j);

}

else

{

printf(“nj=%d”,j);

}

printf(“nParden>Y/N:”);

fflush(stdin);

temp=getch();

}

if(temp==’N’||temp==’n’)

break;

if(temp!=’Y’&&temp!=’y’)

{

printf(“Wrong!You can only put Y(N) or y(n)nPlease put again(Y/N):”);

fflush(stdin);

temp=getch();

}

}

}

int run (char xCh,char str[100],int i)

{

int j;

char *p;

clrscr();

printf(“xCh=”);

xCh=getch();

printf(“%cnstr=”,xCh);

gets(str);

p=&str[0];

i=0;

j=0;

while(*p)

{

if (*p==xCh)

{

j++;

printf(“xCh :%dt”,i);

}

p=p+1;

i++;

}

return j;

}

Mian()版:

#include<stdio.h>

void main()

{

int i,j;

char xCh,str[100],*p,temp;

temp=’Y’;/*给temp赋初值Y,防止第一个while循环无法运行*/

while(temp!=’N’||temp!=’n’)/*如果temp不等于n或N时,进行循环*/

{

if(temp==’Y’||temp==’y’)/*当temp为y或Y时,进行下列循环,用于进行题目要求的操作*/

{

clrscr();/*清屏*/

printf(“xCh=”);/*在屏幕输出提示xCh=*/

xCh=getch();/*从屏幕读取一个字符赋给xCh(getch()和getchar()的区别:前者只要输入一个字符就结束输入过程,后者需要按回车或空格后才结束输入过程)*/

printf(“%cnstr=”,xCh);/*在屏幕xCh=后面输出刚才输入的xCh的值,并提示用户输入str(因为我们用getch(),输入完字符后会自动结束xCh的输入进入下一指令的执行(在本题中,下一指令是:printf(“%cnstr=”,xCh);),而不在屏幕输出刚才输入的字符)【不理解的话把该句改成{printf(“nstr=”);}看看输出结果就知道了】*/

gets(str);/*输入str*/

p=&str[0];/*将指针地址指向str这个字符串的首字符位置*/

i=0;

j=0;

while(*p)/*当p所指向的字符不为空字符时,进行判断循环*/

{

if (*p==xCh)/*当p所指向的字符为所需寻找的xCh时,进行以下操作*/

{

i++;/*i自加1,用于累计str中xCh的数目*/

printf(“xCh :%dt”,j);/*输出xCh在str中出现的位置*/

}

p=p+1;/*指针地址移向str的下个字符*/

j++;/*j用于记录此时p的位置,在str中第一个字符时=0,第二个时=1,以此类推*/

}

if (i==0)/*当str中没有xCh这个字符时,i=0*/

{

printf(“Can Not Find The xCh! i=%d”,i);/*在屏幕中提示无法找到str中的xCh,并输出i=0*/

}

else

{

printf(“ni=%d”,i);/*n为换行*/

}

printf(“nParden>Y/N:”);

fflush(stdin);/*清空计算机缓存*/

temp=getch();/*从屏幕中读取一个字符赋给temp*/

}

if(temp==’N’||temp==’n’)/*当temp为N或n时*/

break;/*跳出循环*/

if(temp!=’Y’&&temp!=’y’)/*当输入的temp不为Y、y、N、n时*/

{

printf(“Wrong!You can only put Y(N) or y(n)nPlease put again(Y/N):”);

fflush(stdin);

temp=getch();

}

}

}

/*fflush(stdin)*/

实验八 结构体与共用体

一、 目的要求

1、 掌握结构体类型变量与数组的定义和使用;

2、 学会使用指针变量和结构体指针数组;

3、 按实验内容要求完成全程程序设计后才允许上机。

二、 实验内容与步骤

1. 设计一个结构

struct student {

long no; /*学号*/

char name[10]; /*姓名*/

char sex; /*性别*/

int age; /*年龄*/

float score; /*平均成绩*/

}

2. 完成下列任务:

(1) 输入实际学生人数n (2<n<4);

(2) 输入每个学生的信息,组成结构数组,并输出;

(3) 统计男、女生人数并输出;

(4) 计算全班平均成绩并输出;

(5) 将低于全班平均成绩的学生信息按行输出

三、 上机要求

1. 可划分为若干个函数,或写成一个main( );

2. 要求输出格式有提示及相应数据。

#include<stdio.h>

struct student

{

long no; /*学号*/

char name[10]; /*姓名*/

char sex; /*性别(gender)*/

int age; /*年龄*/

float score; /*平均成绩*/

}s[3];

void main()

{

int i,n,t,m,na;

float av,sum;

float temp;

clrscr();

m=0;

sum=0;

printf(“The number of the studens:”);

scanf(“%d”,&n);

printf(“n”);

for(i=0;i<n;i++)

{

printf(“nNo.”);

scanf(“%ld”,&s[i].no);

printf(“nName:”);

na=0;

while(na==0)

{

scanf(“%s”,&s[i].name);

if(strlen(s[i].name)>10)

printf(“Wrong!You can only put 10 characters!nName:”);

else

na=1;

}

printf(“nGender:(W/M)”);

t=0;

while(t==0)

{

scanf(“%s”,&s[i].sex);

if(s[i].sex!=’W’&&s[i].sex!=’w’&&s[i].sex!=’m’&&s[i].sex!=’M’)

printf(“Wrong!nGender:”);

else

t=1;

}

if(s[i].sex==’m’||s[i].sex==’M’)

m++;

printf(“nAge:”);

scanf(“%d”,&s[i].age);

printf(“nScore:”);

scanf(“%f”,&temp);

s[i].score=temp;

}

for(i=0;i<n;i++)

{

sum=sum+s[i].score;

printf(“nNo.%ldnName:%snGender:%cnAge:%dnScore:%.2f”,s[i].no,s[i].name,s[i].sex,s[i].age,s[i].score);

}

av=sum/n;

printf(“nThe number of girl(s):%dnThe number of boy(s):%dn”,n-m,m);

printf(“nThe average of the score is:%.2f”,av);

for(i=0;i<n;i++)

{

if (s[i].score<=av)

printf(“nNo.%ld; Name:%s; Gender:%c; Age:%d; Score:%.2f”,s[i].no,s[i].name,s[i].sex,s[i].age,s[i].score);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 787013311@qq.com 举报,一经查实,本站将立刻删除。
(0)
上一篇 2023-06-20 15:29:49
下一篇 2023-06-20 15:34:52

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注