自学内容网 自学内容网

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

1.验证角谷猜想:任意给定一个整数,若为偶数则除以2;若为奇数则乘三再加一,得到一个新的自然数之后按照上面的法则继续演算,若干次后得到的结果必为1。

#include <stdio.h>

int main() {
int n,count=0;
scanf("%d",&n);
while(n!=1) {
if(n%2==1) {
printf("%d*3+1=%d\n",n,n*3+1);
n=n*3+1;
} else {
printf("%d/2=%d\n",n,n/2);
n=n/2;
}
count++;
}
printf("%d times",count);
}

2.编程实现:输入正整数N,若N在1~20之间,则输出相应数字的英文单词,否则输出提示信息overflow。如:输入1,输出one,输入25,输出overflow。
要求:使用字符串数组,保存相应输出的英文字符串;且必须使用此数组实现上述功能

#include <stdio.h>

char a[21][10] = {{"overflow"},{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},
{"twelve"},{"thirteen"},{"fourteen"},{"fifteen"},{"sixteen"},{"seventeem"},{"eighteen"},{"nineteem"},{"twenty"}};//建立对照英文表
int main()
{
int N;
scanf("%d",&N);
printf("%d = ",N);
if(N >= 1 && N <= 20)
printf("%s",a[N]);
else
printf("%s",a[0]);

}

3.从键盘上输入一个字符串,把该字符串中的小写字母转换成大写字符,并将变换后的字符串输出的文件test.txt中,编程实现上述功能

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

int main() {
FILE *file;
if((file=fopen("test.txt","w"))==NULL) {
printf("open error");
exit(0);
}
char str[100];
scanf("%s",&str);
int i=0;
while(str[i]!='\0') {
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-'a'+'A';
i++;
}
fputs(str,file);
fclose(file);
}

4.编写一个递归函数,实现在已递增排序的整数数组中进行二分检索

#include <stdio.h>
 
int search(int *a,int left,int right,int key) {
while(left<=right) {
int mid=(left+right)/2;
if(a[mid]==key)
return mid;
else if(a[mid]<key)
return (a,mid+1,right,key);
return(a,left,mid-1,key);
}
return -1;
}

5.将满秩方阵n等价变换成一个上三角矩阵,(化为阶梯型)。(背下来)
要求:以float a[N][N]作为函数deduce的唯一形参,且函数返回类型为void,编写函数deduce实现上述功能

#include <stdio.h>
#include <stdlib.h>
#define N 3

void deduce(float a[N][N])
{
for(int i = 0; i < N - 1; i++)//共进行n-1行化简 
{
for(int j = i + 1; j < N; j++)//第i+1行开始化简 一直化简到最后一行 
{
float r = a[j][i] / a[i][i];
for(int k = i; k < N; k++)//对第j行的第k个元素开始化简 
a[j][k] -= r*a[i][k]; 
} 
}
}

6.每个学生信息卡片包括学生姓名和出生日期,从键盘依次输入若干学生信息,来创建一个用于管理学生信息的单向链表
①请给出该链表中各个结点的数据类型定义
②请编写程序,按照姓名的次序,依次插入每个学生信息,最后将链表中所有学生信息存入文本文件student.txt中

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

typedef struct date {
int year,month,day;
} date;

typedef struct card {
char name[20];
struct date birth;
struct card* next;
} card;

struct card* create(int n) {
struct card *head=(struct card*)malloc(sizeof(struct card));
head->next=NULL;
struct card* pre=head,*p;
for(int i=0; i<n; i++) {
p=(struct card*)malloc(sizeof(struct card));
scanf("%s",&p->name);
scanf("%d %d %d",&p->birth.year,&p->birth.month,&p->birth.day);
while(pre->next!=NULL&&strcmp(p->name,pre->next->name))
pre=pre->next;
p->next=pre->next;
pre->next=p;
pre=head;
}
return head->next;
}

void write(struct card *head) {
FILE *file;
if((file=fopen("student.txt","w"))==NULL) {
printf("open error");
exit(0);
}
struct card* p = head;
while(p!=NULL) {
fprintf(file,"%s\t%d %d %d\n",p -> name,p->birth.year,p->birth.month,p->birth.day);
p = p -> next;
}
fclose(file);
}

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

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