本文主要是介绍数据结构实验1:动态分配内存实现三元组的各项功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现动态分配内存的三元组;
在定义方面
我们应该创建一个结构体指针,让他可以动态分配内存。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>typedef int Status;void menu() {printf("请选择\n");printf("1.初始化一个三元组\n");printf("2.得到这个三元组最小值\n");printf("3.得到这个三元组最大值\n");printf("4.设置这个三元组第index个元素的值\n");printf("5.得到这个三元组第index个元素的值\n");printf("6.给这个三元组对应元素值乘一个因子\n");printf("7.给定两个三元组对应元素值相加变成一个新的三元组\n");printf("8.这两个三元组对应元素值相减变成一个新的三元组\n");printf("9.打印第1个三元组\n");printf("10.释放存储空间\n");
}typedef struct {//创建一个结构体int a;int b;int c;
} Triplet; //重命名为TripletTriplet* initTriplet(int a, int b, int c) {//用指针动态分配内存// 分配内存空间以存储 Triplet 结构体Triplet* t = (Triplet*)malloc(sizeof(Triplet));// 检查是否成功分配内存if (t) {// 如果成功分配内存,则为结构体的成员赋值t->a = a;t->b = b;t->c = c;}// 返回指向 Triplet 结构体的指针return t;
}int minTriplet(Triplet* t) {//取最小值,int min = t->a;if (t->b < min)min = t->b;if (t->c < min)min = t->c;return min;
}int maxTriplet(Triplet* t) {int max = t->a;if (t->b > max)max = t->b;if (t->c > max)max = t->c;return max;
}void setTripletElement(Triplet* t, int index, int value) {switch (index) {case 0:t->a = value;break;case 1:t->b = value;break;case 2:t->c = value;break;default:printf("Invalid index\n");}
}int getTripletElement(Triplet* t, int index) {switch (index) {case 0:return t->a;case 1:return t->b;case 2:return t->c;default:printf("Invalid index\n");return 0;}
}void multiplyTriplet(Triplet* t, int factor) {//元素乘以一个相同的因子t->a *= factor;t->b *= factor;t->c *= factor;
}Triplet* addTriplets(Triplet* t1, Triplet* t2) {if (!t1 || !t2)return NULL;Triplet* result = (Triplet*)malloc(sizeof(Triplet));//动态分配一个新的三元组if (result) {result->a = t1->a + t2->a;result->b = t1->b + t2->b;result->c = t1->c + t2->c;}return result;
}Triplet* subtractTriplets(Triplet* t1, Triplet* t2) {if (!t1 || !t2)return NULL;Triplet* result = (Triplet*)malloc(sizeof(Triplet));if (result) {result->a = t1->a - t2->a;result->b = t1->b - t2->b;result->c = t1->c - t2->c;}return result;
}void printTriplet(Triplet* t) {if (t)printf("(%d, %d, %d)\n", t->a, t->b, t->c);
}Status DestroyTriplet(Triplet* t) {if (t) {free(t);//释放空间内存return 0;}else {return 1;}
}int main() {Triplet* t1 = NULL;Triplet* t2 = NULL;int n;int t = 1;menu();scanf("%d", &n);while (t) {switch (n) {case 1:printf("请输入第一个三元组的元素:");int a, b, c;scanf("%d%d%d", &a, &b, &c);t1 = initTriplet(a, b, c);break;case 2:printf("最小值:%d\n", minTriplet(t1));break;case 3:printf("最大值:%d\n", maxTriplet(t1));break;case 4:int i, j;printf("请输入你要设置第几个元素:");scanf("%d", &i);printf("请输入你要设置为什么:");scanf("%d", &j);setTripletElement(t1, i, j);printTriplet(t1);break;case 5:int index;printf("请输入你要获取第几个元素的值:");scanf("%d", &index);printf("第%d个元素的值为:%d\n", index, getTripletElement(t1, index));break;case 6:int factor;printf("请输入你要乘以的元素:");scanf("%d", &factor);multiplyTriplet(t1, factor);printTriplet(t1);break;case 7:printf("请输入第二个三元组的元素:");scanf("%d%d%d", &a, &b, &c);t2 = initTriplet(a, b, c);printTriplet(addTriplets(t1, t2));break;case 8:printf("请输入第二个三元组的元素:");scanf("%d%d%d", &a, &b, &c);t2 = initTriplet(a, b, c);printTriplet(subtractTriplets(t1, t2));break;case 9:printTriplet(t1);break;case 10:DestroyTriplet(t1);DestroyTriplet(t2);t1 = NULL;t2 = NULL;break;case 11:t = 0;break;default:printf("非法输入,请重新输入\n");break;}if (t != 0) {scanf("%d", &n);}}return 0;
}
这篇关于数据结构实验1:动态分配内存实现三元组的各项功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!