高级语言期末2007级重修(计算机学院)

1.编程序从键盘输入任意一个整数,求这个整数各位数字中零的个数

#include <stdio.h>

int getzero(char *str){
	int i=0;
	int count=0;
	while(str[i]!='\0'){
		if(str[i]=='0')
			count++;
		i++;
	}
	return count;
}

int main(){
	char str[100];
	scanf("%s",&str);
	printf("%d",getzero(str));
}

2.求由十个自然数构成的数组a中所有的素数之和。素数是只能被1和本身整除且大于1的数

要求:

主函数main中,由键盘读入a的十个自然数

编写并使用返回类型为bool的函数isprime,判断一个自然数是否为素数

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

bool Isprime(int n) {
	if(n<=1)
		return false;
	for(int i=2; i<=sqrt(n); i++) {
		if(n%i==0)
			return false;
	}
	return true;
}
 
int getsum(int *list,int size){
	
	int sum=0;
	for(int i=0;i<size;i++)
		if(Isprime(list[i])){
			printf("%d\n",list[i]);
			sum+=list[i];
		}
	return sum;
}

int main(){
	int list[]={1,2,3,4,5,6,7,8,9};
	int size = sizeof(list)/sizeof(list[0]);
	printf("%d",getsum(list,size));
}

3.对一个长度不超过100的字符串做如下处理:

编写函数对字符串中的各个字符按递增顺序进行排列(字符串结束符‘\0’不包含在其中)

编写递归函数,在字符串中顺序检索给定的字符

要求:上述所有函数不允许使用string.h中提供的字符串操作函数

#include <stdio.h>

void sort(char *str, int n) {
    for (int i = 0; i < n - 1; i++) 
        for (int j = 0; j < n - 1 - i; j++) 
            if (str[j] > str[j + 1]) {
                char temp = str[j];
                str[j] = str[j + 1];
                str[j + 1] = temp;
            }
}

int find(char *str,int index,char key){
	if(str[index]=='\0')
		return -1;
	if(str[index]==key)
		return index;
	return find(str,index+1,key);
}

int main() {
    char str[100] = "dnuaifnuaomomcbgualmldar";
    int size=0;
    while(str[size]!='\0')
    	size++;
    sort(str, size);
    printf("%s", str);
    return 0;
}

4.每个学生信息卡片包括学生姓名和出生日期,从键盘依次输入若干学生信息,来创建一个用于管理学生信息的单向链表。

给出该链表中每个结点的数据类型定义

编写程序,依次输入每个学生信息,最后将链表中所有学生信息存入文本文件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);
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值