数组程序设计实验报告.docx
《数组程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《数组程序设计实验报告.docx(12页珍藏版)》请在冰点文库上搜索。
![数组程序设计实验报告.docx](https://file1.bingdoc.com/fileroot1/2023-6/8/d37372b6-de28-4848-8010-3c7a26ee9101/d37372b6-de28-4848-8010-3c7a26ee91011.gif)
数组程序设计实验报告
实验报告
田晶晶
201011041056
一、实验名称:
数组程序设计实验
二、实验目的
1掌握一位数组、二维数组的定义、引用方法;
2掌握字符数组的定义与引用,常用字符串处理函数;
3掌握与数组有关的一些常用算法。
三、实验内容
1从键盘输入一段英文,找出长度最长和最短的单词,以及出现次数最多的字符。
(1)从键盘输入一段英文,找出长度最长的单词。
实验代码:
#include
#include
charword(charstr[])
{inti,k,flag=0,max=0;
charstr1[10],ch[10];
for(i=0;str[i]!
='\0';i++)
if((65<=str[i]&&str[i]<=90)||(97<=str[i]&&str[i]<=122))
{if(flag==0)
{
k=0;
ch[k]=str[i];
flag=1;}
elseif(i==strlen(str)-1)
{k++;
ch[k]=str[i];
ch[k+1]='\0';
if(k+1>max)
{max=k+1;
strcpy(str1,ch);
}
}
else
{k++;
ch[k]=str[i];
}
}
elseif(flag==1)
{ch[k+1]='\0';
flag=0;
if(k+1>max)
{max=k+1;
strcpy(str1,ch);
}
}
printf("输入的字符串中,最长的单词有%d个字母,该单词是%s\n",max,str1);
}
main()
{chara[100];
printf("请输入一行字符:
\n");
gets(a);
word(a);
}
实验运行结果
实验中出现的问题:
如果从键盘键入的英文中有两个或两个以上长度最长且相同的单词,则程序只能输出第一个单词,此问题并未解决。
(2)从键盘键入一段英文,找出长度最短的单词
实验代码
#include
#include
voidXX(charstring[])
{
inti,j,word=0,wordstart=0;
intzishu,xiaozishu=10;
for(i=0;string[i]!
='\0';i++)
{
if(string[i]=='')
{
word=0;
}
elseif(word==0)
{
word=1;
zishu=0;
for(j=i;string[j]!
=''&&string[j]!
='\0';j++)
{
zishu++;
}
if(zishu{
xiaozishu=zishu;
wordstart=i;
}
}
}
for(i=wordstart;string[i]!
=''&&string[i]!
='\0';i++)
{
putchar(string[i]);
}
}
voidmain()
{
charstr[80];
gets(str);
XX(str);
getch();
printf("\n");
}
实验运行结果
实验中出现的问题
如果从键盘输入的英文中,最短单词并不唯一,本程序只能输出第一个,此问题还未解决。
(3)从键盘输入一段英文,找出出现次数最多的字符。
实验代码
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#definemax_LEN100
struct_NODE
{
charch;
intnum;
struct_NODE*next;
};
struct_NODE*_STRCMP(struct_NODE*CNODE,charch)
{
intflag=0;
if(CNODE==NULL)
{
CNODE=(struct_NODE*)malloc(sizeof(struct_NODE));
CNODE->ch=ch;
CNODE->num=1;
CNODE->next=NULL;
}
else
{
struct_NODE*temp1,*temp2,*temp;
temp1=CNODE;
while(temp1!
=NULL)
{
if(temp1->ch==ch)
{
temp1->num++;
flag=1;
break;
}
temp1=temp1->next;
}
if(!
flag)
{
temp=CNODE;
temp2=(struct_NODE*)malloc(sizeof(struct_NODE));
temp2->ch=ch;
temp2->num=1;
temp2->next=temp;
CNODE=temp2;
}
}
returnCNODE;
}
voidprint(struct_NODE*CNODE)
{
struct_NODE*temp;
intnum=0;
while(CNODE!
=NULL)
{
if(CNODE->num>num)
{
num=CNODE->num;
temp=CNODE;
}
CNODE=CNODE->next;
}
printf("字符串中出现最多的字母及其个数:
%c%d\n",temp->ch,temp->num);
return;
}
intmain()
{
struct_NODE*znode;
charstr[max_LEN];
intlen;
inti;
znode=NULL;
scanf("%s",str);
len=strlen(str);
for(i=0;i{
znode=_STRCMP(znode,str[i]);
}
print(znode);
return0;
}
实验运行结果
实验中出现的问题
在一些实验中,本程序并不能给出正确的结果,此问题还未找出原因,没有解决。
2输出魔方阵,所谓魔方阵就是指这样的方阵,它的每一行、每一列和对角线之和均相等。
要求输出由1-n*n之间的自然数构成的魔方阵。
实验代码
#include
voidmain()
{
inta[16][16];
inti,j,k,n,p=1;
while(p)
{
printf("EntertheRows(From1to15andtheNumbermustbeanodd):
\n");
scanf("%d",&n);
if((n!
=0)&&(n<=15)&&(n%2))p=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=0;
j=n/2+1;
a[1][j]=1;
for(k=2;k<=n*n;k++)
{
i--;
j++;
if((i<1)&&(j>n))
{
i+=2;
j--;
}
else
{
if(i<1)i=n;
if(j>n)j=1;
}
if(a[i][j]==0)
a[i][j]=k;
else
{
i+=2;
j--;
a[i][j]=k;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
实验运行结果
实验中出现的问题
本程序只能输出n=1,3,5,7,9,11,13,15的魔方阵,对于此范围之外的数字没有作用。
371个人围成一圈,从第一个人开始从1报号,每次报号顺序递增,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个学生为止。
问此人原来的位置是多少号?
实验代码
#include
voidmain()
{
shortp[71];
shorti,j,n=2,countdown=71;
for(i=0;i<71;i++)p[i]=i+1;
p[2]=0;
while(countdown!
=0)
{
for(i=0;i<3;i++)