自学内容网 自学内容网

高级语言期末2009级A卷(计算机学院)

1.编写函数,打印下列序列0,1,1,2,3,5,8,13,21,34...(斐波那契序列)的前n项

#include <stdio.h>

int main() {
int x=0,y=1,z,n;
scanf("%d",&n);
if(n==1)
printf("%d\n",x);
if(n==2)
printf("%d\n%d\n",x,y);
if(n>2) {
printf("%d\n%d\n",x,y);
for(int i=2; i<n; i++) {
z=x+y;
x=y;
y=z;
printf("%d\n",z);
}
}
}

2.编写程序打印下列三角矩阵。要求:下三角矩阵的各行值由名为yanghui的函数来计算完成;打印的行数在主函数中由键盘输入;打印在主函数进行。

1 1

1 2 1

1 3 3 1

1 4 6 4 1 

1 5 10 10 5 1

#include <stdio.h>
#include <stdlib.h>

void show(int **arr,int n) {
for(int i=0; i<n; i++) {
for(int j=0; j<=i; j++)
printf("%3d ",arr[i][j]);
printf("\n");
}
}

int main() {
int n;
scanf("%d",&n);
int **arr=(int **)malloc(n*sizeof(int*));
for(int i=0; i<n; i++)
arr[i]=(int *)malloc(n*sizeof(int));
for(int i = 0; i < n; i++)
for(int j = 0; j <= i; j++)
if(j == 0 || j == i)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j - 1]+ arr[i - 1][j];
show(arr,n);
}

3.编写函数,实现两个字符串变量值的交换。并编写主函数,在其中输入两个字符串,然后调用该函数,最后打印输出交换后的两个字符串变量值。要求:形式参数是指针(最多不超过100个字符)注:不允许用strcpy函数直接拷贝实现,两个字符串长度不一定相等

#include <stdio.h>

void swapStrings(char** str1, char** str2) {
char* temp = *str1;
*str1 = *str2;
*str2 = temp;
}

int main() {
char str1[100], str2[100];
scanf("%s",&str1);
scanf("%s",&str2);
swapStrings(&str1, &str2);
printf("%s\n", str1);
printf("%s\n", str2);
return 0;
}

4.从键盘读入10名评委的评分。扣除一个最高分及一个最低分,然后统计总分,并除以8,最后得到这个选手的最后得分(打分采用百分制)并输出之

#include <stdio.h>

int main(){
int n=10,max=0,min=999;
int score,sum=0;
while(n!=0){
scanf("%d",&score);
if(score>max)
max=score;
if(score<min)
min=score;
sum+=score;
n--;
}
sum=sum-max-min;
printf("%lf%%",1.0*sum/8*100);
}

5.编写函数(函数名为CreatList),建立一个通讯录链表。该通讯录的信息包括姓名和手机号码。在主函数中读取该链表,并将链表的信息写入文件address.txt中

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
char name[20];
int num;
struct node *next;
}node;

struct node *CreatList(int n){
struct node *head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
struct node *pre=head;
int i;
for(i=0;i<n;i++){
struct node *p=(struct node*)malloc(sizeof(struct node));
scanf("%s %d",&p->name,&p->num);
p->next=pre->next;
pre->next=p;
}
return head->next;
}

void writelist(struct node *head){
FILE *file;
if((file=fopen("address.txt","w"))==NULL){
printf("open error");
exit(0);
}
struct node *p=head;
while(p!=NULL){
fprintf(file,"%s %d",&p->name,&p->num);
p=p->next;
}
fclose(file);
}

原文地址:https://blog.csdn.net/m0_56210953/article/details/136346728

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!