linux下的代碼對比程序設計

操作系統爲linux,可以移植到vc中,採用的是linux下的vim編輯器。vc下嘗試也可以運行

主要是考慮到在學習編程期間需要有很多的例子需要模仿網上或者書上的東西,然後,vim下沒有提示功能,所以只能手動輸入,這就有可能造成代碼的輸入錯誤,然後吧,就是悲劇,如果代碼少的話還好,可以根據提示找錯,順便鍛鍊自己的糾錯能力,但是錯誤滿屏或者跟多就悲劇了吧,所以,就自己動手做了個文件對比程序。主要用來比較代碼,因爲是控制檯的,所以地址的輸入可能就有點問題。

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <sstream>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef vector<string> Vec; //定義vector容器的數據類型
typedef Vec::iterator Iter; //定義vector容器迭代器類型
Vec Vcom; //讀取文件內容並存儲的vec
Vec Vres; //全局的存儲文件不同的內容vec
bool do_open(string path);
bool i_compare(Vec a,Vec b);
bool createFlie();
int main()
{
        string ready="";
        cout<<"It can compare two file,are you ready?(y/Y or n/N): ";
        cin>>ready;
        if(ready!="y"||ready!="Y")
        {
                Vec test1,test2;
                string path1,path2;
                bool is_ok=false;
                cout<<"Input the first file path: ";
                while(!is_ok) //循環輸入地址
                {
                        cin>>path1;
                        if(do_open(path1))
                        {
                                is_ok=true;
                                test1=Vcom;
                        }
                        else
                        {
                                cout<<"the first file con't open"<<endl;
                                cout<<"Input first path again: ";
                                is_ok=false;
                        }
                }
                is_ok=false;
                cout<<"Input the second file path: ";
                while(!is_ok)
                {
                        cin>>path2;
                        if(do_open(path2))
                        {
                                is_ok=true;
                                test2=Vcom;
                        }
                        else
                        {
                                cout<<"the second file con't open"<<endl;
                                cout<<"Input second path again: ";
                                is_ok=false;
                        }
                }
                if(i_compare(test1,test2))
                {
                        cout<<"first file equal second file!"<<endl;
                }
                else
                {
cout<<"first file con't equal second file!"<<endl;
if(createFlie())
cout<<"compare create ok !"<<endl;
else
cout<<"compare create error !"<<endl;
//-----------------------------------//
                }
        }
        else
        {
                cout<<"Thank you to use"<<endl;
        }
return 0;
        
}



//打開文件並按行存儲到vector中

bool do_open(string path)
{
        Vcom.clear();
        ifstream fin(path.c_str());  //文件流操作
        if(!fin.is_open())
                return false;
        Iter it;
        string buffer;
        string::iterator sit;
        while(!fin.eof())
        {
                getline(fin,buffer,'\n');
                sit=buffer.begin();
                while(sit!=buffer.end())
                {
                        if(*sit==' ')
                                sit=buffer.erase(sit);
                        else
                                ++sit;
                }
                Vcom.push_back(buffer);
        }
        if(Vcom.empty())
                return false;
        fin.close();
        return true;
}



//比較文件內容,主要就是比較兩個vector中的行
bool i_compare(Vec a,Vec b)
{
        
        string buffer;  
        int len=abs((int)a.size()-(int)b.size()); //求絕對值,這裏必須要強制類型轉換爲int型

//-----------填充vector,使size相同
if(a.size()>b.size())
        {
int i;
for(i=0;i<len;i++)
b.push_back("");
        }
        else
        {
int i;
for(i=0;i<len;i++)
a.push_back("");
        }

//------------
        int con=1;
Iter it_a=a.begin();
        Iter it_b=b.begin();
        while(it_a!=a.end())
        {

//-------------獲取不同的行內容和行號  對比着存儲在vres中
                if(((*it_a).compare(*it_b)))
                {
stringstream strstream;
                        strstream<<"--"<<con<<" line: ";
                        buffer="a:"+strstream.str()+*it_a;
                        Vres.push_back(buffer);
buffer="b:"+strstream.str()+*it_b+"\n";
Vres.push_back(buffer);
                }

//-------------
                ++it_a;
                ++it_b;
                ++con;
        }
if(!Vres.empty())
return false;
else
return true;
}



//創建結果文件
bool createFlie()
{
        string path;
cout<<"Default path?(y/n):";
cin>>path;
if(path=="y")
path="/home/tgomc/linhai/debug";
else
{
        cout<<"input the new path:";
        cin>>path;
}
path=path+"/com.txt";
        ofstream file(path.c_str());//覆蓋創建
        Iter it;
        for(it=Vres.begin();it!=Vres.end();it++)
        {
                file<<*it<<endl;
        }
file.close();
        if(Vres.empty())
return false;
else
return true;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章