openjudge 去除C程序中的注释(大模拟)
来源:互联网 发布:java excel导入导出 编辑:程序博客网 时间:2024/05/16 07:18
去除C程序中的注释
- 查看
- 提交
- 统计
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
C程序的注释用/*...*/来表示。请写一个程序,将输入的C程序源代码中的注释去掉,输出去掉注释之后的源代码。
用于测试的C代码保证符合语法,不使用C++的//注释语法。
注意,C语言不允许出现嵌套注释。具体来说,对于/*/**/"*/",如果不允许嵌套注释,那么它表示字符串"*/";如果允许嵌套注释,它表示一个引号"。
还请注意,字符串中出现的注释符/*属于字符串的一部分,注释中出现的双引号"属于注释的一部分。
- 输入
- 符合语法的C代码文本文件。代码每行不超过200个字符。
- 输出
- 去掉注释后的C代码。要求只能去掉注释,不可以做其他的修改,比如调整缩进,去除注释之外的换行符等。
- 样例输入
#include #include #include /*Hash Search: Hash function: division method; handling collisions: open addressing's linear probing. In this exercise, M is the basic area's length, all keys are non negative integers.*/#define M 11int hash(int key){return key % M;}void init_hash(int* hashtable){int i;for(i = 0; i < M; ++i){hashtable[i] = -1;}}/*return value: 1:found, *position is the key's index; 0:not found, *position is where to insert the key; -1:overflow. */int search_hash(int* hashtable, int key, int* position){int i, h = hash(key);for(i = 0; i < M; ++i){if(key == hashtable[h]){*position = h;return 1;}if(-1 == hashtable[h]){*position = h;return 0;}h = (h+1) % M;}*position = -1;return -1;}/*return value: 1:inserted, 0:overflow*/int insert_hash(int* hashtable, int key){int position, result;result = search_hash(hashtable, key, &position);if(-1 == result)return 0;hashtable[position] = key;return 1;}void main(){int hashtable[M];init_hash(hashtable);srand(time(NULL));int i, j, key;for(i = 0; i < 8; ++i) /*make a hash table with 8 elements*/{key = rand() % 50;insert_hash(hashtable, key);printf("Insert %d\n", key);for(j = 0; j < M; ++j)printf("%3d", hashtable[j]);printf("\n");}printf("Please input the key to search:\n");scanf("%d", &key);i = search_hash(hashtable, key, &j);if(1 == i)printf("Found!Its index is %d\n", j);elseprintf("Not found!\n");}
- 样例输出
#include #include #include #define M 11int hash(int key){return key % M;}void init_hash(int* hashtable){int i;for(i = 0; i < M; ++i){hashtable[i] = -1;}}int search_hash(int* hashtable, int key, int* position){int i, h = hash(key);for(i = 0; i < M; ++i){if(key == hashtable[h]){*position = h;return 1;}if(-1 == hashtable[h]){*position = h;return 0;}h = (h+1) % M;}*position = -1;return -1;}int insert_hash(int* hashtable, int key){int position, result;result = search_hash(hashtable, key, &position);if(-1 == result)return 0;hashtable[position] = key;return 1;}void main(){int hashtable[M];init_hash(hashtable);srand(time(NULL));int i, j, key;for(i = 0; i < 8; ++i) {key = rand() % 50;insert_hash(hashtable, key);printf("Insert %d\n", key);for(j = 0; j < M; ++j)printf("%3d", hashtable[j]);printf("\n");}printf("Please input the key to search:\n");scanf("%d", &key);i = search_hash(hashtable, key, &j);if(1 == i)printf("Found!Its index is %d\n", j);elseprintf("Not found!\n");}
- 提示
- 注意字符串,字符,转义字符的情况。
看看自己有没有考虑
"a\"/*ccc*/"
这种情况。 - tips:提示很暖人,输入输出那么大一坨,挺吓人的。需要解决的问题:
- 1.字符串输入的问题,一次读一行,字符串拼接。使用getline();
- 2.输出需要注意三大点,1)注释内部的情况 2)注释外部非字符串的部分 3)注释外部字符串部分
- 具体来说:
- 两个标志变量:判断是否是注释部分的aflag,判断是否是字符串部分的sflag;
- 1注释内部的情况:
- 1)输入的字符均不输出
- 2)判断是否到达注释结尾。
- 2注释外部非字符串的部分
- 1)注释开始
- 2)字符串开始
- 3)正常输出
- 3注释外部字符串的部分
- 1)字符串结束(判断方式为双引号且前面没有转义字符)
- 2)转义字符+双引号,直接输出
- 3)正常字符串部分,直接输出
- 代码如下,注释很清楚:
#include<iostream> #include<cstring>#include<string>using namespace std;//分别代表注释,字符串的标志 int aflag,sflag; string ss,s;int main(){int index=0;//控制输出的下标 while(getline(cin,ss))s+=ss+'\n';//因为最后的换行符不会被输入,所以手动添加上 while(index!=s.size()-1){//如果既不在注释中,又不在字符串中 ,并且下标没有越界 while(!aflag&&!sflag&&index<s.size()-1) {//如果是注释的开始符号 if(s[index]=='/'&&s[index+1]=='*'){aflag=1;index+=2;} //如果是字符串的开始符号 else if(s[index]!='\\'&&s[index+1]=='"'){cout<<s[index]<<s[index+1];sflag=1;index+=2; }//如果是普通字符 else cout<<s[index++]; } //如果不在注释中但是在字符串中 while(!aflag&&sflag&&index<s.size()-1) { //如果当前字符是转义字符,下一个字符是” if(s[index]=='\\'&&s[index+1]=='"'){ cout<<s[index]<<s[index+1]; index+=2; } //如果当前字符是字符串的结束字符 else if(s[index]=='"') { cout<<s[index++]; sflag=0; } //如果是其他字符 else{ cout<<s[index++]; } } //如果是在注释内部 while(aflag&&index<s.size()-1) { if(s[index]=='*'&&s[index+1]=='/'){ aflag=0; index+=2; } else { index++; } } }return 0;}
阅读全文
0 0
- openjudge 去除C程序中的注释(大模拟)
- 去除C程序中的注释
- 去除C程序中的注释
- 去除c中的注释
- 来个去除c程序中的注释的小程序
- 习题课第一次作业:1、去除C程序中的注释
- 去除程序中的中文注释
- 去除C/C++中的注释
- 面试训练去除c/c++中的注释
- 去除c/c++代码中的注释
- 去除c/c++代码中的注释
- c语言实现去除代码中的注释
- 在C语言中利用有限状态自动机模拟编译器去除代码中/**/类注释的程序实现
- 删除C程序中的注释
- 删掉C程序中的注释
- c去除注释
- C++去除程序注释实现
- c语言版去除源代码注释
- 51Nod-1442-士兵的旅行
- WebService简介
- aop aspectj pdf [收录]
- iOS
- 时间函数
- openjudge 去除C程序中的注释(大模拟)
- 上了两个月的班,感觉大学一年白读。
- 1030 Dijkstra+DFS
- loadrunner测试springboot的api接口
- POJ 1035 Spell checker 串
- 内网容器化实践
- HDU 3746 Cyclic Nacklace(Next 数组性质)
- Codeforces Round #412 D. Dynamic Problem Scoring (枚举 + 贪心)
- 【08】Bootstrap — 按钮