找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 948|回复: 7

[讨论]谁也来编这个程序,我正在编呢,题目见正文

[复制链接]
发表于 2009-11-2 01:37:18 | 显示全部楼层 |阅读模式 IP:江苏扬州
谁来编这个程序,我正在编呢
/*
C++风格的注释(//) 在某些C编译器里通不过,
写个程序读入一个文件,并存为另一个文件,
将注释全改为C风格的哦!
注意有些特殊情况。
*/

这题目是偶想出来的,感觉挺有意思,大家试试啊~
发表于 2009-11-2 01:37:19 | 显示全部楼层 IP:江苏扬州
这帖人气帅吧!嘿嘿……
回复

使用道具 举报

发表于 2009-11-2 01:37:20 | 显示全部楼层 IP:江苏扬州
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream fin("a.in");
ofstream fout("a.out");
for(string s; getline(cin,s); fout<<endl){
int len=s.length(),flag=0;
for(int i=1; i<len; i++){
if(s[i-1]=='"')
while(1){
i++;
if(s[i-1]=='"'){
i++;
break;
}
}
if(s[i-1]=='/'&&s[i]=='/'){
s[i]='*';
flag=1;
break;
}
}
fout<<s;
if(flag)fout<<"*/";
}
}
//前面有错,改一下
回复

使用道具 举报

发表于 2009-11-2 01:37:24 | 显示全部楼层 IP:江苏扬州
以我的一个文件(就是我的程序)为范本运行了一下,发现一些问题:
1、似乎进入了死循环,CPU一直100%,窗口一直不消失;
2、几种特殊情况不行,如:
/*...
...//...
...*/
将被改成/*...
.../*...*/
...*/
显然通不过编译。
如:cout<<".\".//.....\"...";
输出是:cout<<".\"./*.....\"..."; */
3、到这一句时 if('"'==line[i] || '/'==line[i]) {
中断了,导致输出不完整,输出文件内容就停在这儿了。
回复

使用道具 举报

发表于 2009-11-2 01:37:25 | 显示全部楼层 IP:江苏扬州
//我的程序又很长,不如你的精悍,但还没发现BUG
//CharStack.h我就不帖出来了,占地方
//晕,怎么在这里缩进这么难看。。。。

/******************************************
*
* Copyright by NJU_SE_SirX
* 2006.2.3
******************************************/
//本程序将C++风格的注释(//)转换为C风格(/* */)的注释,并将新文件保存为原文件名+_NEW
//只转换纯文本文档
//考虑到了几种特殊情况:
// 1.cout<<"....\"...\"..."; //....."...."......."........ 用栈解决
// 2./*....
// ...//...
// ...*/ 判断是否在注释中
//至于如://..//..//////...因为是直接查找第一个出现的 // ,所以没问题
#include "CharStack.h"
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h> //getch()
using namespace std;
string getTrueFile(); //确保要修改的文件存在
class File {
const string newTag;
string fName;
ifstream inFile;
ofstream outFile;
bool inComment;
const string outName() const; //得到新文件名称
void proLine(const string &line); //处理1行
void setLine(const string &line,int index); //输出新行
public:
File(const string &name,const string &tag="_NEW");
~File();
void process(); //处理文件
void print(bool cls=true) const; //输出新文件
};
File::File(const string &name,const string &tag) :fName(name),newTag(tag),inComment(false) {
inFile.open(fName.c_str());
outFile.open(outName().c_str());
}
File::~File() {
cout<<"■";
system("pause");
}
const string File::outName() const {
int partion=-1;
for(int i=fName.length()-1;i>0;i--) {
if('.'==fName[i]) {
partion=i;
break;
}
}
if(-1==partion)
return fName.substr(0,fName.length()) + newTag;
//else
return fName.substr(0,partion) + newTag + fName.substr(partion,fName.length()-partion+1);
}
void File::proLine(const string &line) {
CharStack stack; //用来处理 "
for(int i=0;i<line.length();i++) {
//特殊情况:" 与 // 包含问题
if('"'==line[i] || '/'==line[i]) {
//1. "
if('"'==line[i]) {
if(i>0 && '\\'==line[i-1])
continue; //排除 \"
//else
if(stack.isEmpty())
stack.push('"');
else stack.pop();
}
//2. /
else if(stack.isEmpty()) {
if(i>0 && '*'==line[i-1]) {
inComment=false;
continue;
}
if(i<line.length()-1) {
if('*'==line[i+1])
inComment=true;
if('/'==line[i+1]) {
setLine(line,i);
return;
}
}
}
}
}
outFile<<line<<endl;
}
void File::setLine(const string &line,int index) {
string newLine;
if(!inComment)
newLine=line.substr(0,index) + "/*" + (((index+2)==line.length())?" ":line.substr(index+2,line.length()-index-2)) + "*/";
else newLine=line.substr(0,index) + "\"//\"" + (((index+2)==line.length())?" ":line.substr(index+2,line.length()-index-2));
outFile<<newLine<<endl;
}
void File::process() {
string line;
while(getline(inFile,line))
proLine(line);
cout<<"■转换成功,新文件已存为【"+outName()+"】。"<<endl;
}
void File::print(bool cls) const {
if(cls)
system("cls");
cout<<"【"+outName()+"】"<<endl;
cout<<"--------------------------------------------------------------------------------";
string line;
ifstream in(outName().c_str());
while(getline(in,line))
cout<<line<<endl;
cout<<"--------------------------------------------------------------------------------";
cout<<"【End】"<<endl;
}

string getTrueFile() { //这个函数真难!用了大概半小时解决!主要是VC6中的getline(...)“双回车”问题,绕开了
ifstream *inFile;
char fName[30]={0};
while(true) {
cout<<"■请输入源文件名称(含扩展名,如.cpp) :";
cin.getline(fName,20,'\n'); //trouble here!用 ifstream* 解决
if(0==fName[0])
continue;
inFile=new ifstream(fName);
if(*inFile)
break; //打开文件成功
//else
delete inFile;
cout<<"└文件【"<<fName<<"】不存在,"<<"请重新输入。\n"<<endl;
for(int i=0;i<30;i++)
fName[i]=0;
}
return fName;
}
int main() {
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<" 〓〓〓 C++注释转换器 v1.0版 〓〓〓\n"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
File f(getTrueFile());
f.process();
char sign;
while(true) {
cout<<"└需要查看吗?(Y/N) : ";
sign=getch();
cout<<endl;
if('Y'==sign || 'y'==sign) {
f.print(true);
break;
}
else if('N'==sign || 'n'==sign)
break;
cout<<" 请正确输入(Y/N)!"<<endl;
}

return 0;
}



附件: 只有本站会员才能下载或查看附件,请您 登录 或 注册
回复

使用道具 举报

发表于 2009-11-2 01:37:27 | 显示全部楼层 IP:江苏扬州
3、到这一句时 if('"'==line[i] || '/'==line[i]) {
中断了,导致输出不完整,输出文件内容就停在这儿了。
这个没搞明白什么意思,你是说我的程序在运行过程中会卡在这吗?
前面的我再考虑一下
回复

使用道具 举报

发表于 2009-11-2 01:37:29 | 显示全部楼层 IP:江苏扬州
针对你提出的问题修改的
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream fin("a.in");
ofstream fout("a.out");
int bflag=0;
for(string s; getline(fin,s); fout<<endl){
int len=s.length(),flag=0;
for(int i=1; i<len; i++){
if(s[i-1]=='/'&&s[i]=='*') bflag=1;
if(s[i-1]=='*'&&s[i]=='/') bflag=0;
if(bflag) continue;
if(s[i-1]=='"'&&(i-1>0)&&s[i-2]!='\''&&s[i+1]!='\'')
while(1){
i++;
if(s[i-1]=='"'&&s[i-2]!='\\'){
i++;
break;
}
}
if(s[i-1]=='/'&&s[i]=='/'){
s[i]='*';
flag=1;
break;
}
}
fout<<s;
if(flag)fout<<"*/";
}
}
回复

使用道具 举报

发表于 2009-11-2 01:37:31 | 显示全部楼层 IP:江苏扬州
这个OK了~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 23:33 , Processed in 0.176222 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表