string fun(string str1, string str2) { string temp = ""; bool flag = false; int j; for (int i = 0; i < str1.size(); i++) { if (!flag && str1[i] != str2[i]) { flag = true; j = i; } if (flag && str1[i] == '/') { temp += "../"; } } if (temp == "") temp += '/'; temp += str2.substr(j); return temp; }
public class RelativePath { public static void main(String[] args) { String a="/qihoo/app/a/b/c/d/new.c"; String b="/qihoo/app/a/b/c/d/new.c/1/2/test.c"; String sb=getRelativePath(a, b); System.out.println(sb); } public static String getRelativePath(String a, String b){ StringBuffer sb=new StringBuffer(); String newA=a.substring(1); String newB=b.substring(1); String[] newAs=newA.split("/"); String[] newBs=newB.split("/"); int start=-1,end=0; int len=newAs.length>newBs.length?newBs.length:newAs.length; boolean flag=false; for(int i=0;i<newAs.length;i++){ if(newAs[i].equals(newBs[0]) && flag==false){ flag=true; start=i; break; } } for(int j=0;j<len;j++){ if(!newAs[start+j].equals(newBs[j])){ end=j; break; } } for(int k=start;k<start+end;k++){ sb.append("../"); } for(int k=end;k<newBs.length;k++){ sb.append(newBs[k]).append("/"); } return sb.substring(0, sb.length()-1); } }
string Path( string b , string a )
{
int size= b .size()< a .size()? b .size(): a .size();
int i=1;
int last=0;
for (;i<size;i++)
{
if ( b [i]== a [i])
{
if ( b [i]== '/' )
{
last=i;
continue ;
}
}
else
{
break ;
}
}
int num=0;
for ( int j= a .size()-1;j>last;j--)
{
if ( a [j]== '/' )
{
num++;
}
}
string str= "../" ;
string res= "" ;
for ( int i=0;i<num;i++)
{
res+=str;
}
for ( int j=last+1;j< b .size();j++)
{
res+= b [j];
}
return res;
}
string fun(string str1, string str2)//str2相对于str1的相对路径 { string result; if(str1.size() == 0) { return str2; } if(str2.size() == 0) { return result; } size_t j = 0; for(size_t i = 0; i <str1.size(); ++i) { if(str1[i] != str2[i]) { break; } if(str1[i] == '/') { j = i; } } result = str2.substr(j + 1); int counter = 0; for(size_t k = j + 1; k < str1.size(); ++k) { if(str1[k] == '/') { ++counter; } } string temp; for(size_t i = 0; i < counter; ++i) { temp += "../"; } result = temp + result; return result; }
<?php function relativePath($aPath, $bPath) { $aArr = explode('/', $aPath); //explode函数用于切分字符串,返回切分后的数组,此处用'/'切分字符串 $bArr = explode('/', $bPath); $path = ''; for($i=0;$i < count($bArr);$i++){ if($aArr[$i] == $bArr[$i]){ $path .='../'; //$bArr[$i] = ''; unset($bArr[$i]); } } $path .=implode('/',$bArr); return $path; } echo relativePath('/qihoo/app/a/b/c/d/new.c', '/qihoo/app/1/2/test.c'); ?>
using namespace std;
}
/** * 在写一个函数,根据两文件的绝对路径算出相对路径。如 a="/qihoo/app/a/b/c/d/new.c",b="/qihoo/app/1/2/test.c',那么b相对于a的相对路径是"../../../../1/2/test.c" * @author luchunlong * * 2015年8月10日 下午1:49:02 */ public class AbsoluteToRelativeLocation { public String convert(String path1,String path2){ String result=""; path1.substring(1); //第一步,先除掉绝对路径的第一个“/”,以方便第二步划分 path2.substring(1); String[] array1=path1.split("/"); //第二步:通过“/”划分绝对路径,并将相同的部分移除 String[] array2=path2.split("/"); int len=array1.length<array2.length?array1.length:array2.length; for(int i=0;i<len;i++){ if(array1[i].equals(array2[i])){ path1=path1.substring(path1.indexOf("/")+1); path2=path2.substring(path2.indexOf("/")+1); }else{ break; } } System.out.println("不同部分:"); System.out.println("path1:"+path1); System.out.println("path2:"+path2); String[] array3=path1.split("/"); //第三步:对第二步产生的path1进行重新划分,将每一个“XXX/”前面部分换成"。。/" for(int i=0;i<array3.length-1;i++){ result+="../"; } return result+path2; //第四步:组装结果 } public static void main(String[] args){ String path1="/qihoo/app/a/b/c/d/new.c"; String path2="/qihoo/app/1/2/test.c"; AbsoluteToRelativeLocation convertor=new AbsoluteToRelativeLocation(); String relativePath=convertor.convert(path1, path2); System.out.println("relativePath:"+relativePath); } }
}
#coding:utf-8 a="/qihoo/app/a/b/c/d/new.c" b="/qihoo/app/1/2/test.c" def outer(a,b): a_list = a.split('/') b_list = b.split('/') a_max = len(a_list) b_max = len(b_list) num = min(a_max,b_max) path_pre = '' for i in range(num): if a_list[i] != b_list[i]: print path_pre + list2str(b_list[i:]) break else: path_pre += '../' def list2str(temp): str_temp = '' for index,item in enumerate(temp): if index != (len(temp)-1): str_temp += item + '/' else: str_temp += item return str_temp outer(a,b)
string path(string &a,string &b){ string *p=b,*q=b; int lena=a.size(a); int lenb=b.size(b); int count=0; for(int i=0;i<lena && i<lenb;i++){ if(a[i]=='/' &&b[i]=='/'){ count++; q=p+1; } else if(a[i]!=b[i]) break; p++; } p=""; for(int i=1;i<=count;i++) p+="../"; if(p=="") p+='/'; p+=p+q; return p; }
#include<iostream> #include<string> using namespace std; string result = "../"; string& relativepath(string& str1, string& str2) { int i = 0; while (str1[i]==str2[i]) { if (str1[i]=='/') { result += "../"; } ++i; } result += str2.substr(i); return result; } int main() { string s1 = "/qihoo/app/a/b/c/d/new.c"; string s2 = "/qihoo/app/1/2/test.c"; cout << relativepath(s1, s2) << endl; return 0; }
//大题测试,根据两个文件的绝对路径,输出文件A相对于B的相对路径 #include<iostream> #include<string> #include<vector> using namespace std; int main() { vector<char> result; string str1,str2; cin>>str1; cin.clear(); cin>>str2; int i=0; while(str1[i]==str2[i]) {++i;} int j=0; int count=0;//用来记录字符'/'的个数 for(;j<=i;++j)//统计'/'的个数 { if(str1[j]=='/') count++; } for(;count>=0;--count)//存储相应个数的‘../’ { result.push_back('.'); result.push_back('.'); result.push_back('/'); } for(;i<str2.length();++i) { result.push_back(str2[i]); } for(int m=0;m<result.size();m++) { cout<<result[m]; } cout<<endl; system("pause"); return 0;
}
}
class Solution(): def Relativedir(self, source, dist): #将路径进行分裂 source = source.split("/") dist = dist.split("/") print(source,dist) len_s = len(source) len_d = len(dist) #遍历至第一个不同的子路径 for i in range(min(len_s, len_d)): if source[i] != dist[i]: break print(len_s, len_d, i) reladir = [] #从源文件的路径向上回溯至公共路径的最低一层 for j in range(i, len_s - 1): reladir.append("..") #向目的文件遍历 for j in range(i, len_d): reladir.append(dist[j]) return "/".join(reladir) so = Solution() print(so.Relativedir("/qihoo/app/a/b/c/d/new.c", "/qihoo/app/1/2/test.c"))