/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: 十六周任务四 * 作 者: 石丽君 * 完成日期: 2012 年 6 月 5 日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: 要求编写C++程序,读WolfSheep.nls,去除其中所有的注释,并保存到文件WS_nocomment.nls 中。 例如,WolfSheep.nls 中下面的一段代码: ask patches [ set pcolor green ] ; check GRASS? switch. ; if it is true, then grass grows and the sheep eat it. if it false, then the sheep don't need to eat if grass? [ ask patches [ set countdown random grass‐regrowth‐time ; initialize grass grow clocks randomly set pcolor one‐of [green brown] ] ] 经过处理后,在WS_nocomment.nls 中,上面划线部分的注释将全部不存在。 提示1:任务的另一种直白的解读是:读入每一行,复制每一行分号前面的部分。或者说,读入每 一行,逐个复制文件中的字符,如果出现分号,分号及其后的文字将不再复制。 * 问题描述: * 程序输出: * 程序头部的注释结束 */ #include<iostream> #include<fstream> using namespace std; int main() { ifstream readFile; ofstream writeFile; char ch[200];int n; readFile.open("WolfSheep.nls",ios::in); writeFile.open("WS_nocomment.nls",ios::out); while(!readFile.eof()) { readFile.getline(ch,200,'\n'); n=strlen(ch); for(int i=0;i<n;i++) { if(ch[i]!=';') writeFile<<ch[i]; else break; } writeFile<<'\n'; } readFile.close(); writeFile.close(); cout<<"finish!"<<endl; system("pause"); return 0; }
十六周任务四将分号后的文字去掉
最新推荐文章于 2024-09-24 16:32:50 发布