C语言删除代码中的注释

题目:

 

Write a program to remove all comments from a C program.

 

#include <stdio.h>

void remove_comment(int c);

void main(){

	int c; 

	c = 0; 

	while((c = getchar()) != EOF){
		remove_comment(c);
	}
}

void remove_comment(int c){
	
	int d;
		
	if(c == '/'){

		if((d = getchar()) == '*'){

			while((c = getchar()) !='*'&&(d = getchar()) != '/'){
				;
			}

			d = getchar();
		}
		else if(d == '/'){
			
			while((d = getchar()) != '\n'){
				;
			}
		}
		else{
			putchar(c);
			putchar(d);
		}
	}
	else{
		
		putchar(c);
	}
}

删除注释,就是要删除 //…… 和 /*……*/两种片段

删除,其实就是不打印,也就是要移动当前打印的位置,在上述两种片段中不作任何打印。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我作为AI语言模型无法提供完整的数据结构C语言代码及详细注释。但我可以为您提供一些数据结构C语言的基本概念和相关学习资源。 数据结构是计算机科学的一个重要概念,它指的是一组数据的组织方式、存储方式以及操作方式。数据结构可以分为线性结构和非线性结构。常见的线性结构有数组、链表、栈和队列等,非线性结构有树、图等。 在C语言,可以使用结构体和指针来实现各种数据结构。例如,链表可以使用结构体和指针实现,代码如下: ``` //定义链表节点结构体 struct ListNode { int val; struct ListNode* next; }; //创建链表 struct ListNode* createList(int* nums, int numsSize) { struct ListNode* head = NULL; struct ListNode* tail = NULL; for (int i = 0; i < numsSize; i++) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = nums[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } //遍历链表 void traverseList(struct ListNode* head) { struct ListNode* p = head; while (p != NULL) { printf("%d ", p->val); p = p->next; } printf("\n"); } //插入节点到链表 struct ListNode* insertList(struct ListNode* head, int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; if (head == NULL) { head = node; } else { struct ListNode* p = head; while (p->next != NULL) { p = p->next; } p->next = node; } return head; } //删除链表节点 struct ListNode* deleteList(struct ListNode* head, int val) { if (head == NULL) { return NULL; } if (head->val == val) { struct ListNode* p = head; head = head->next; free(p); return head; } struct ListNode* p = head; while (p->next != NULL && p->next->val != val) { p = p->next; } if (p->next != NULL) { struct ListNode* q = p->next; p->next = q->next; free(q); } return head; } //销毁链表 void destroyList(struct ListNode* head) { struct ListNode* p = head; while (p != NULL) { struct ListNode* q = p->next; free(p); p = q; } } int main() { int nums[] = {1, 2, 3, 4, 5}; int numsSize = sizeof(nums) / sizeof(int); struct ListNode* head = createList(nums, numsSize); traverseList(head); head = insertList(head, 6); traverseList(head); head = deleteList(head, 3); traverseList(head); destroyList(head); return 0; } ``` 以上代码演示了如何使用结构体和指针实现链表的创建、遍历、插入、删除和销毁等操作。 如果您想深入学习数据结构和算法,可以参考以下学习资源: - 《算法导论》 - 《数据结构与算法分析》 - 《数据结构与算法》(王道考研系列) - LeetCode网站上的数据结构和算法题目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值