1、设计一个函数fc,统计数组中偶数和奇数的个数,数组元素个数不多于10个。编写main函数,正确调用fc函数,实现对数组a的统计,输出统计结果。
#include "stdio.h"
int j=0;
int fun(int a[],int n)
{
int i,c=0;
for(i=0;i<n;i++)
if(a[i]%2==0) c=c+1;
else j=j+1;
return(c);
}
int main()
{
int i,num[5];
for(i=0;i<5;i++)
scanf("%d",&num[i]);
printf("oushu: %d \n",fun(num,5));
printf("jishu: %d \n",j);
return 0;
}
第二篇:C语言程序设计实验报告(下学期)
《C语言程序设计》实验报告
指导教师 xxx 实验时间:20xx年5月20日 学院 计算机 专业 计算机大类强化
班级 0490801 学号 08100525 姓名 xxx 实验室 xxx
实验题目:C语言程序设计上机实验总结
一、实验目的及要求
这个学期C实验课程难度增加了不少,主要围绕指针展开,主要内容有数组、结构体、链表、文件,以及各种排序的算法等。重点是指针、地址、数组之间的关系;通过指针操作数组的方法;掌握数组、指针作为函数参数的方法;结构体和链表、文件联系起来,程序模块化,以及各种排序的算法。
二、实验内容
下面选了几个比较经典的题目,作为这个学期实验的一点总结。
1.自定义函数sorted完成冒泡法对10个数的排序。程序要求在main函数中调用自定义函数sorted。
#include<stdio.h>
#define N 10
void sorted(int _INPUT[]);
void main()
{
int sz[N];
int tp;
printf("Please input %d number(s)\n",N);
for(tp=0;tp<N;tp++)
{
scanf("%d",&sz[tp]);
}
sorted(sz);
printf("\n\n***********\n\n");
for(tp=0;tp<N;tp++)
{
printf("* %d\n",sz[tp]);
}
}
void sorted(int _INPUT[])
{
int a,b,temp=_INPUT[0];
for(a=1;a<N;a++)
1
{
for(b=1;b<N-a;b++)
{
if(_INPUT[b]>_INPUT[b-1])
{
temp=_INPUT[b];
_INPUT[b]=_INPUT[b-1];
_INPUT[b-1]=temp;
}
}
}
}
冒泡排序是非常经典而基础的算法,两个嵌套的for循环的范围起到关键作用! a=1;a<N;a++和b=1;b<N-a;b++是基础,只有清楚的推理才能保证不出错。
——————————————————————————————————
2.编写程序实现两矩阵相乘,并输出其结果。
#include<stdio.h>
#define A 3
#define B 2
#define C 3
void main()
{
int jz1[A][B],jz2[B][C],out[A][C];
int tp1,tp2,tp3;
int sum=0;
printf("Please input a matrix %d x %d \n",A,B);
for(tp1=0;tp1<A;tp1++)
{
for(tp2=0;tp2<B;tp2++)
scanf("%d",&jz1[tp1][tp2]);
}
printf("Please input a matrix %d x %d \n",B,C);
for(tp1=0;tp1<B;tp1++)
{
for(tp2=0;tp2<C;tp2++)
scanf("%d",&jz2[tp1][tp2]);
}
2
for(tp1=0;tp1<C;tp1++)
{
for(tp2=0;tp2<A;tp2++)
{
sum=0;
for(tp3=0;tp3<B;tp3++)
{
sum=sum+jz1[tp2][tp3]*jz2[tp3][tp1];
}
out[tp1][tp2]=sum;
}
}
printf("\n\n***************\n\n");
for(tp1=0;tp1<A;tp1++)
{
for(tp2=0;tp2<C;tp2++)
{
printf("\t%d\t",out[tp2][tp1]);
}
printf("\n");
}
printf("\n\n***************\n\n");
}
另一道算法比较重要的题目,采用了更加复杂的三层for嵌套。
sum=sum+jz1[tp2][tp3]*jz2[tp3][tp1]是数学上的矩阵相乘算法的体现,同样需要在纸上先推理出来,否则就很难正确写出乘积式中行列的变量。
——————————————————————————————
3. 编写程序实现以下功能,将程序的运行结果存入test.txt文本文件中。
(1) 创建一单链表,包含10个学生的学号、姓名以及3门课的成绩;
(2) 输出每门课的平均分,以及低于平均分学生的数量;
(3) 按学生总分进行排序(降序);
(4) 实现查找功能,如输入某一学生的学号,要求输出其相关信息,如果没
有该学生信息,则输出提示信息“没有找到”;
(5) 实现添加学生信息的功能;
(6) 实现删除学生信息的功能;
#include "stdio.h"
#include "stdlib.h"
3
#include "string.h"
#define N 3
struct lianbiao
{
unsigned int xuehao;
char xingming[10];
unsigned int chengji[N];
unsigned int sum;
struct lianbiao *nextptr;
};
typedef struct lianbiao lb;
void display(lb *head);
void lbave(lb *head)
{
unsigned short count,temp1,belowcount[N];
float ave[N];
lb * ptr;
if(head==NULL)
{
printf("\n\n链表为空,不能操作\n");
}
else
{
for(temp1=0;temp1<N;temp1++)
{
ave[temp1]=0;
belowcount[temp1]=0;
}
for(temp1=0;temp1<N;temp1++) //统计平均分 {
count=0;
for(ptr=head;ptr!=NULL;ptr=ptr->nextptr)
{
ave[temp1]=ave[temp1]+ptr->chengji[temp1]; count++;
}
ave[temp1]=ave[temp1]/count;
}
for(temp1=0;temp1<N;temp1++) //统计低于平均分的人数
4
{
for(ptr=head;ptr!=NULL;ptr=ptr->nextptr)
{
if(ptr->chengji[temp1]<ave[temp1]) belowcount[temp1]++; }
}
printf("\n\n");
for(temp1=0;temp1<N;temp1++)
{
printf("\n第%d科平均分为:%5.1f\n",temp1,ave[temp1]);
printf("此门课程低于平均分的人数为:%d\n\n",belowcount[temp1]); }
}
}
//原先的排序,不成功
/*while(temp0<=lbcount)
{
for(start=head;start!=NULL;start=start->nextptr)
{
sum1=0;
for(temp1=0;temp1<N;temp1++) sum1=sum1+start->chengji[temp1];
for(ptr=start;ptr!=NULL;ptr=ptr->nextptr)
{
sum2=0;
for(temp2=0;temp2<N;temp2++) sum2=sum2+ptr->chengji[temp2];
if(sum1<sum2)
{
templb.xuehao=start->xuehao;
strcpy(templb.xingming,start->xingming);
for(temp3=0;temp3<N;temp3++)
templb.chengji[temp3]=start->chengji[temp3];
templb.nextptr=start->nextptr;
start->xuehao=ptr->xuehao;
strcpy(start->xingming,ptr->xingming);
for(temp3=0;temp3<N;temp3++)
start->chengji[temp3]=ptr->chengji[temp3];
start->nextptr=ptr->nextptr;
5
ptr->xuehao=templb.xuehao;
strcpy(ptr->xingming,templb.xingming);
for(temp3=0;temp3<N;temp3++)
ptr->chengji[temp3]=templb.chengji[temp3];
ptr->nextptr=templb.nextptr;
temp0++;
}
}
}
}*/
lb *lbpaixu(lb *head)
{
unsigned short lbcount=0;
lb *start=head,*ptr;
lb *prvptr;
lb prehead,*tempptr;
prehead.nextptr=head;
if(head==NULL)
{
printf("\n\n链表为空,不能操作\n");
}
else
{
//printf("\n\n%d\n",lbcount);
for(ptr=head;ptr->nextptr!=NULL;)
{
if(ptr->sum < ptr->nextptr->sum)
{
for(prvptr=&prehead;prvptr->nextptr!=ptr;prvptr=prvptr->nextptr); //找到ptr之前的节点
prvptr->nextptr=ptr->nextptr;
tempptr=ptr->nextptr->nextptr;
ptr->nextptr->nextptr=ptr;
ptr->nextptr=tempptr;
6
if(prvptr!=&prehead)
ptr=prvptr;
}
else
ptr=ptr->nextptr;
}
head=prehead.nextptr;
display(head);
}
return head;
}
void lbfind(lb *head)
{
lb *ptr=head,*prvptr,prehead;
unsigned int input,temp1,temp2=0;
char inputch;
prehead.nextptr=head;
if(head==NULL)
{
printf("\n\n链表为空,不能操作\n");
}
else
{
printf("\n\n输入查找的学号:");
scanf("%d",&input);
for(;ptr!=NULL;ptr=ptr->nextptr)
{
if(ptr->xuehao==input)
{
printf("\n%d\t%s",ptr->xuehao,ptr->xingming); for(temp1=0;temp1<N;temp1++)
printf("\t%d",ptr->chengji[temp1]); printf("\t%d",ptr->sum);
temp2=1;
printf("\n按Y删除此行,其他任意键不删除\n"); inputch=getch();
7
if(inputch=='y' || inputch=='Y')
{
for(prvptr=&prehead;prvptr->nextptr!=ptr;prvptr=prvptr->nextptr); //找到ptr之前的节点
prvptr->nextptr=ptr->nextptr;
//free(ptr);
}
}
}
if(temp2==0) printf("\n没有找到\n\n");
}
}
void fnewnode(lb *head)
{
lb *newnode;
unsigned short temp;
newnode = (lb*)malloc(sizeof(lb));
printf("\n\n输入学号、姓名、%d科成绩\n",N);
scanf("%d",&newnode->xuehao);
scanf("%s",newnode->xingming);
for(temp=0;temp<N;temp++)
scanf("%d",&newnode->chengji[temp]);
newnode->nextptr=NULL;
for(;head->nextptr!=NULL;head=head->nextptr);
head->nextptr=newnode;
}
void display(lb *head)
{
unsigned short temp1;
printf("\n\n学号\t姓名\t成绩\n\n");
if(head==NULL);
else
{
while(head!=NULL)
{
printf("\n%d\t%s",head->xuehao,head->xingming);
for(temp1=0;temp1<N;temp1++)
8
printf("\t%d",head->chengji[temp1]); printf("\t%d",head->sum);
head=head->nextptr;
}
}
}
void filef(lb *head)
{
unsigned short temp1;
FILE *lbfile;
lbfile=fopen("test3.txt","w+");
for(;head!=NULL;head=head->nextptr)
{
fprintf(lbfile,"\n%d\t%s",head->xuehao,head->xingming); for(temp1=0;temp1<N;temp1++)
fprintf(lbfile,"\t%d",head->chengji[temp1]); fprintf(lbfile,"\t%d",head->sum);
}
fclose(lbfile);
printf("\n完成\n");
}
void main()
{
lb *start,*head=NULL;
char input;
unsigned int temp0;
//predef
lb stu0={1001,"a",34,24,65};
lb stu1={1002,"b",55,36,97};
lb stu2={1003,"c",87,83,46};
lb stu3={1004,"d",32,99,100};
lb stu4={1005,"e",29,43,64};
lb stu5={1006,"f",99,78,36};
lb stu6={1007,"g",58,56,78};
lb stu7={1008,"h",37,15,98};
lb stu8={1009,"i",56,61,46};
9
lb stu9={1010,"j",45,76,23};
head=&stu0;
stu0.nextptr=&stu1;
stu1.nextptr=&stu2;
stu2.nextptr=&stu3;
stu3.nextptr=&stu4;
stu4.nextptr=&stu5;
stu5.nextptr=&stu6;
stu6.nextptr=&stu7;
stu7.nextptr=&stu8;
stu8.nextptr=&stu9;
stu9.nextptr=NULL;
for(start=head;start!=NULL;start=start->nextptr)
{
start->sum=0;
for(temp0=0;temp0<N;temp0++)
start->sum=start->sum+start->chengji[temp0];
}
while(1)
{
printf("\n1:输入信息\n2:输出平均分\n3:按总分排序\n4:显示所有学生信息\n5:查找学号 , 找到后可以删除\n6:将当前数据输出到文件\n");
input=getch();
switch(input)
{//49 50 51 52 对应键盘上的 "1","2","3","4"键
case 49: fnewnode(head);
for(start=head;start!=NULL;start=start->nextptr)
{
start->sum=0;
for(temp0=0;temp0<N;temp0++)
start->sum=start->sum+start->chengji[temp0];
}
break;
case 50: lbave(head);break;
case 51: head=lbpaixu(head);break;
case 52: display(head);break;
case 53: lbfind(head);break;
case 54: filef(head);break;
default: ;
10
}
}
}
这是一道比较复杂的题目了,需要的功能很多。链表的创建与各种处理是基础,完成创建链表后还有其他的功能需要完善,比如菜单,输出链表到文件等。其中链表的排序是最大的难点,需要极为清晰的分析和对链表结构的正确理解。我先前做的排序一直不能成功,就是缺乏对链表真实结构的正确理解。虽然大概结构清楚了,但是在复杂的分析过程中却不能运用。这表明真正掌握链表还需要大量的练习才能完成。
三、体会及收获
在这学期的C语言的学习中,让我了解到了C的大部分基础知识,程序渐渐变复杂了,我也开始对编程有点兴趣了。我觉得这学期最大的收获就是我了解到编程并不是想象中那么难,很多东西自己可以做到。同时我也体会到编写一个程序是需要很多时间精力的,而且细节部分决不能粗心大意,应该推理的部分就要认真分析。大部分算法都是需要很多的练习才能掌握的。
11