C指针 PartA

老湿说这部分比较长, 于是分两天讲, 于是这是PartA、

=,= 指针无非就是指针的概念, 指针的基本使用方法, 指针运算, 还有二级指针, 还是直接上习题

函数指针的使用:

写一个程序,根据用户的输入数据算出结果:
只写出四种运算就可以。
例如:
1 + 2 = 3
1 * 2 = 2
1 – 2 = -1
1 / 2 = 0
#include <stdio.h>
int plus(int a, int b);
int mult(int a, int b);
int minu(int a, int b);
int divi(int a, int b);
void main(){
	int (*cal[])(int, int) = {plus, mult, minu, divi};
	int i, a, b;
	char sign[] = {'+','*','-','/'};
	printf("Input a and b:\n");
	scanf("%d %d", &a, &b);
	for(i=0;i<4;i++){
		printf("%d %c %d = %d\n", a, sign[i], b, cal[i](a,b));
	}
}
int plus(int a, int b){
	return a+b;
}
int mult(int a, int b){
	return a*b;
}
int minu(int a, int b){
	return a-b;
}
int divi(int a, int b){
	return a/b;
}
下一道题是定积分函数的...我数学不好一下子搞不定...下次再说吧...
 
然后是一个链表的
使用结构体打造链表中节点,使用指针将零散的节点串成链表,进行链表的增加节点,查询某节点,删除某节点的操作。
试着打造双向链表以及除了头指针还有尾指针的链表。
#include <stdio.h>
#include <stdlib.h>
struct link{
	link *last;
	int value;
	link *next;
}*head = NULL, *end = NULL, *temp;
void add(link* node);
void drop(link* node);
link* find(int value);
void print(link* node);
void main(){

	temp = (struct link *)malloc(sizeof(link));
	(*temp).value = 0;
	add(temp);
	print(head);

	temp = (struct link *)malloc(sizeof(link));
	(*temp).value = 1;
	add(temp);
	print(head);

	temp = (struct link *)malloc(sizeof(link));
	(*temp).value = 2;
	add(temp);
	print(head);

	drop(find(1));
	print(head);
}
void add(link* node){
	if(head==NULL){
		head = node;
		end = node;
		(*node).last = NULL;
		(*node).next = NULL;
	}else{
		(*end).next = node;
		(*node).last = end;
		(*node).next = NULL;
		end = node;
	}
}
void drop(link* node){
	if(node==NULL)
		return;
	link* temp = head;
	while((*temp).next!=node){
		temp = (*temp).next;
	}
	(*(*node).next).last = temp;
	(*temp).next = (*node).next;
	free(node);
	node = NULL;
}
link* find(int value){
	link* temp = head;
	while(temp!=NULL){
		if((*temp).value == value)
			return temp;
		temp = (*temp).next;
	}
	return temp;
}
void print(link* node){
	if(node==NULL){
		printf("End.\n");
		return;
	}
	printf("N[%d] -> ", (*node).value);
	print((*node).next);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值