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

本文介绍了C语言中的多个编程实例,包括判断二维空间中点的优劣、统计整数输入流中特定数值的出现次数、递归获取整数的特定位数、计算三点构成的三角形面积,以及构建字符链表存储大写字母。
摘要由CSDN通过智能技术生成

1.编bool型函数,判断二维空间中的某点是否优于另一点。优于关系定义为:在二维空间中,某点(A1,A2)优于(B1,B2),当且仅当A1>B1,A2>B2

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

typedef struct point{
	float x;
	float y;
}point;

bool bigpoint(struct point *p1,struct point *p2){
	if(p1->x>p2->x&&p1->y>p2->y)
		return true;
	return false;
}

2.编程序,统计以100为结束符的整数输入流中-1,0,+1的出现次数并输出

#include <stdio.h>

int main(){
	int temp;
	scanf("%d",&temp);
	int count0=0,count1=0,countd1=0;
	while(temp!=100){
		if(temp==1)
			count1++;
		else if(temp==-1)
			countd1++;
		else if(temp==0)
			count0++;
		scanf("%d",&temp);
	}
	printf("count0 = %d count1 = %d count-1 = %d",count0,count1,countd1);
}

3.编写字符型递归函数digit(n,j),求整数n的从右边开始的第j个数字字符

#include <stdio.h>

int digit(int n,int j){
	if(j==1)
		return (n%10)+'0';
	else
		return digit(n/10,j-1);
} 

int main(){
	char temp = digit(12345,2);
	printf("%c",temp);
}

4.平面上有100个点,任意三个点均可构成三角形,编写程序,输入一个点的坐标,求面积最大的三角形,及其面积,面积公式S=\sqrt{s(s-a)(s-b)(s-c)},s=(a+b+c)/2

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

typedef struct Point{
    double x;
    double y;
} Point;

double calculateDistance(struct Point p1, struct Point p2) {
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return sqrt(dx * dx + dy * dy);
}

double calculateTriangleArea(struct Point p1, struct Point p2, struct Point p3) {
    double a = calculateDistance(p1, p2);
    double b = calculateDistance(p1, p3);
    double c = calculateDistance(p2, p3);
    double s = (a + b + c) / 2.0;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

int main() {
    struct Point points[10];
    int i, j, k;
    double maxArea = 0.0;
    struct Point p1, p2, p3;
    printf("请输入100个点的坐标:\n");
    for (i = 0; i < 10; i++) {
        printf("请输入第 %d 个点的 x 坐标:", i + 1);
        scanf("%lf", &points[i].x);
        printf("请输入第 %d 个点的 y 坐标:", i + 1);
        scanf("%lf", &points[i].y);
    }
    for (i = 0; i < 10; i++) 
        for (j = i + 1; j < 10; j++) 
            for (k = j + 1; k < 10; k++) {
                double area = calculateTriangleArea(points[i], points[j], points[k]);
                if (area > maxArea) {
                    maxArea = area;
                    p1 = points[i];
                    p2 = points[j];
                    p3 = points[k];
                }
    }
    printf("面积最大的三角形的顶点坐标为:\n");
    printf("P1: (%.2lf, %.2lf)\n", p1.x, p1.y);
    printf("P2: (%.2lf, %.2lf)\n", p2.x, p2.y);
    printf("P3: (%.2lf, %.2lf)\n", p3.x, p3.y);
    printf("面积最大的三角形的面积为:%.2lf\n", maxArea);
    return 0;
}

5.建立一个字符链表,要求

设计用于存储结点的数据类型

编函数建立链表,按顺序保存26个大写字母

#include <stdio.h>
#define N 26

typedef struct node {
	char ch;
	struct node *next;
} node;

struct node* create() {
	struct node *head=(struct node*)malloc(sizeof(struct node));
	head->next=NULL;
	for(int i=0; i<N; i++) {
		struct node *p=(struct node*)malloc(sizeof(struct node));
		p->ch='z'-i;
		p->next=head->next;
		head->next=p;
	}
	return head->next;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值