河南工程学院
计算机学院
C语言程序设计实验报告册
学期: 2020~2021学年第1学期
专业: 软件工程
班级: 2021
学号: 202052213120
姓名: 刘兴要
指导教师: 张天伍
实验二数组
一、实验学时:2学时
二、实验目的
掌握二维数组、字符数组与字符串的关系以及字符串变量的表示,熟练字符串处理函数的应用。
三、实验内容
1.用选择法对10整数排序。
2.有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中的
英文大写字母、小写字母、数字、空格以及其它字符的个数。
3.将两个字符串连接起来,不要用strcat函数。
4.(飞机定票系统)一家小型航空公司刚刚购买了计算机,用于新的自动化
定票系统。总裁要求您为新系统编制程序。需要分配该航空公司的唯一一
架飞机每个航班的座位(容量:10个座位)。
提示:程序应该轮流显示下列菜单:
Please type 1 for “first class”
Please type 2 for “economy”
如果输入1,则程序应该分配头等舱内的座位(座位1 ~ 5)。如果输入2 ,则程序应该分配经济舱的座位(座位 6 ~ 10)。然后,程序输出登机证,说明座位号,以及它位于飞机的头等舱,还是经济舱内。
使用一个数组来代表飞机的座位安排。将数组的所有元素初始化为0,以说明所有座位都是空的。当分配每个座位时,将数组的对应元素设置为1,以说明不能再次使用那个座位号。当然,程序不应该分配已经分配过的座位。
当头等舱满员时,程序应询问是否可以安排在经济舱内(等等)。如果可以,
则进行适当的座位分配。如果不可以,则输出信息“Next Flight leaves in
3 hours”.
四、主要仪器设备及耗材
硬件:计算机一台
软件:DEV-CPP或其他软件
五、实验步骤
1. 复习数组相关内容,编写以上题目源程序
2. 输入源程序,编译、连接直到没有错误
3. 根据实验步骤,撰写实验报告
六、程序清单
程序一:
#include
#include
#define N 10
int main(){
int a[N],i,j,temp=0;
for(i=0;i<10;i++)//循环输入
scanf("%d",&a[i]);
for(i=0;i<10;i++)//前一项循环
for(j=i+1;j<10;j++)//后一项循环
if(a[i]>a[j]){//前后项元素比较
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("升序为:\n");
for(i=0;i<10;i++)
printf("%3d",a[i]); //每个元素空三格输出
}
程序二:
#include
#include
#include
void sta(char str[][80]);//声明统计函数定义字符串数组
int main(){
char str[3][80]={{"We are meant to face the stage of study and work until we retire."},
{"Which seems everybody does the same thing in their life. "},
{"They start to wonder about the meaning of life. 1234 56"} };
for (int i=0;i<3;i++) {//遍历行输出
printf("%s", str[i]);
}
printf("\n");
sta(str);//调用声明的函数
}
void sta(char str[][80]) {
int s=0,c=0,l=0,n=0,other=0;//声明空格大写字母小写字母数字其他字符变量并初始化
for (int i=0;i<3;i++){
for (int j=0;j<80;j++){
if(str[i][j]==' '){//统计空格数量
s++;
}
else if(str[i][j]>'A'&&str[i][j]<'Z'){//大写字母判断
c++;
}
else if(str[i][j]>='a'&&str[i][j]<='z'){//小写字母判断
l++;
}
else if(str[i][j]>'0'&&str[i][j]<'9'){//数字判断
n++;
}
else if(str[i][j]!='\0'){//其他字符判断
other++;
}
}
}
printf("大写字母个数为%d\n",c);//输出
printf("小写字母个数为%d\n",l);
printf("空格个数为%d\n",s);
printf("数字个数为%d\n",n);
printf("其他字符个数为%d\n",other);
}
程序三:
#include
int main()
{
int a = 2;
int b = 0;
int tic1 = 0;
int tic2 = 0;
int fi[5] = {0, 0, 0, 0, 0};
int ec[5] = {0, 0, 0, 0, 0};
while (a != 0)
{
/* code */
printf("Please type 1 for first class\n");
printf("Please type 2 for economy\n");
printf("请输入你想要的舱位\n");
scanf("%d", &b);
switch (b)
{
case 1:
if(tic1 == 5)
{
printf("头等舱已坐满,请选择经济舱\n");
break;
}
if (fi[tic1] == 0)
{
/* code */
fi[tic1] = 1;
printf("你的座位号为%d\n", tic1+1);
printf("你在头等舱内\n");
tic1++;
}
break;
case 2:
/* code */
if (ec[tic2]==0)
{
ec[tic2] =1;
/* code */
printf("你的座位号为%d\n", tic2+6);
printf("你在经济舱内\n");
tic2++;
}
else
{
printf("Next Flight leaves in 3 hours\n");
}
break;
case 3:
a = 0;
break;
default:
printf("error,Please type the correct number\n");
break;
}
}
printf("Program end\n");
}
七、实验中出现的问题及解决的方法
实验一:无法确定两个数两两相比较的次数刚开始以为是比较10次,经过检测是比较9次。
实验二:每组80个字符输入有问题有时候空格会没法计算。
实验三:无。
实验四:无。