文档库 最新最全的文档下载
当前位置:文档库 › c语言实验答案

c语言实验答案

c语言实验答案
c语言实验答案

n");

printf("The first number:");

scanf("%d",&a);

printf("The second number");

scanf("%d",&b);

printf("Results as follows:\n");

printf("%d+%d=%d\n",a,b,a +b);

printf("%d-%d=%d\n",a,b,a -b);

printf("%d*%d=%d\n",a,b,a *b);

printf("%d+%d=%f\n",a,b,* a/b); .+1/n!的值。

#include<>

void main()

{

int n,p=1,i;

float sum=0;

printf("please input n:");

scanf("%d",&n);

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

{

p=p*i;

sum+=p;

}

printf("sum=%f\n",sum); }

.14

p=j;

switch(p) umber);

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

printf("%",stu[i].score[j ]);

printf("

ave=%.2f\n",average(stu[i])) ;umber);

printf("score:");

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

scanf("%f",&(stu[i].score[j])); } }

void main() {

struct student stu[3]; //定义结构体数组

input(stu); //数组名做参数,传递的是地址 output(stu); }

//静态链表的建立,有三个学生,包括学号(sno char(8)),姓名(sname char(20)),分数(grade float[]),

//定义结构体类型数组存学生信息,使用链表所有学生,实现链表的输入输出。

//静态的书上有原题目,我写成动态的了,用子函数的方式实现 #include <>

#include<> #include <> struct

stu

//定义全局结构体 {

char sno[8];

char sname[20]; float grade;

struct stu *next; } ;

//////////////////////////////////////////////////////// void main( ) {

struct stu *creat(struct stu *);

void print(struct stu*); struct stu *head; head=NULL; head=creat(head); print(head); }

///////////////////////////////////////////////////////// struct

stu

*creat(struct

stu*head) //建立链表 {

struct stu *p,*q; q=(struct

stu*)malloc(sizeof(struct stu)); //分配空间 printf("please input

sno:");

scanf("%s",q->sno); printf("please input sname:");

scanf("%s",q->sname); printf("please input grade:");

scanf("%f",&q->grade); head=q;

while(q->grade!=0) //当分数为0时结束 {

p=(struct

stu*)malloc(sizeof(struct stu));

printf("please input

sno:");

scanf("%s",p->sno); printf("please input sname:");

scanf("%s",p->sname);

printf("please input

grade:");

scanf("%f",&p->grade); q->next=p; q=p; }

q->next=NULL; return

head;

//返回链表的头指针 }

////////////////////////////

//////////////////////////////

void print(struct stu *head) //输出链表 { struct stu

*p; //设游标指针

p=head; //取得链表的头指针

printf("data:\n----------------------\n"); while(p->next!=NULL) {

printf("%s\n",p->sno);

printf("%s\n",p->sname);

printf("%f\n",p->grade);

printf("----------------------\n"); p=p->next;

} }

//递归方法实现快速排序算法。快速排序的基本原理是: //(1)选择一个充当划分较小和

较大元素的界限的元素,称其为

基准值。

//(2)将数组中的元素重新排列使得较大元素向数组尾端移动,较小元素向数组首端移动。 //如此在形式上将数组分成两部分,界限左边元素都小于基准值,而界限右边元素都大于基准值,此过程称为分解。

//在分解完成后,充当界限的数组首元素可能需要和中间某元素对调。

//(3)排序两个子数组中元素。因为基准值左边元素都小于基准值右边元素,所以将两个子数组分别排序后即使得整个数组有序。

#include<> #define N 10 void main() {

void fun(int *,int,int); int i,a[N]; printf("please input

array:");

for(i=0;i

fun(a,0,N-1); //调用fun 函数对数组排序

for(i=0;i

void fun(int *p,int left,int right) { int

i=left,j=right,middle,temp;

middle=p[(left+right)/2]; //求中间值

while(i<=j) //如果两边扫描的下标交错,就停止

{

while((p[i]

数 i++;

while((p[j]>middle) && (j>left))//从右扫描小于中值

的数 j--;

if(i<=j) //找到了一对值,交换 {

temp=p[j]; p[j]=p[i]; p[i]=temp; i++;

j--;

}

}

if(left

fun(p,left,j);

if(right>i) //当右边部分有值(right>i),递归右半边

fun(p,i,right);

}

相关文档