Page 1 of 3123

PHP 计算相对路径

PHP No Comments »398 views

在PHP编程中,经常会遇到相对路径的计算,然而标准库中没有提供相应的函数,我只好自己编写了一个函数用于计算相对路径。

<?
function pathconvert($cur,$absp)//当前文件,目标路径
{
	$cur = str_replace('\\','/',$cur);
	$absp = str_replace('\\','/',$absp);
	$sabsp=explode('/',$absp);
	$scur=explode('/',$cur);
	$la=count($sabsp)-1;
	$lc=count($scur)-1;
	$l=max($la,$lb);
 
	for ($i=0;$i<=$l;$i++)
	{
		if ($sabsp[$i]!=$scur[$i])
			break;
	}
	$k=$i-1;
	$path="";
	for ($i=1;$i<=($lc-$k-1);$i++)
		$path.="../";
	for ($i=$k+1;$i<=($la-1);$i++)
		$path.=$sabsp[$i]."/";
	$path.=$sabsp[$la];
	return $path;
}
 
$path=pathconvert("/home/web/test/a.php","/home/data/d.png");
// $path="../../data/d.png"
?>
标签:, , ,

高精度计算对象

計算機科學 3 Comments »235 views

高精度加、减、单乘、双乘、比较大小、读入、输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
 
using namespace std;
 
template<int LIM,int MAX> class hp
{
	public:
		//vars
		int sect[MAX];
		int scnt;
		//constructors
		hp()
		{
			scnt=1;
			sect[0]=0;
		}
		//functions
		void copy(const hp<LIM,MAX> &A)
		{
			for (int i=0;i<A.scnt;i++)
				sect[i]=A.sect[i];
			scnt=A.scnt;
		}
		void copy(int A)
		{
			scnt=0;
			while (A)
			{
				sect[scnt++]=A % LIM;
				A /=LIM;
			}
		}
		void print()
		{
			int i,k;
			printf("%d",sect[scnt-1]);
			for (i=scnt-2;i>=0;i--)
			{
				k=LIM/10;
				while(sect[i]<k)
				{
					printf("0");
					k/=10;
				}
				if (sect[i])
					printf("%d",sect[i]);
			}
		}
		void read(int LIE)
		{
			char A[LIM*MAX];
			int len,i,j,k,b=0;
			scanf("%s",A);
			len=strlen(A);
			k=len % LIE;
			j=scnt=len / LIE;
			if (k)
			{
				scnt++;
				j=scnt-1;
				sect[j]=0;
				for (i=0;i<k;i++)
				{
					sect[j]*=10;
					sect[j]+=A[b++]-'0';
				}
			}
			for (j--;j>=0;j--)
			{
				sect[j]=0;
				for (i=0;i<LIE;i++)
				{
					sect[j]*=10;
					sect[j]+=A[b++]-'0';
				}
			}
		}
		void plus(hp<LIM,MAX> &A,int offset=0)
		{
			int sc=scnt > A.scnt + offset  ? scnt : A.scnt + offset;
			int i,j,up=0;
			for (i=0;i<sc;i++)
			{
				j=i - offset;
				if (j<0) continue;
				if (i>=scnt) sect[i]=0;
				if (j>=A.scnt) A.sect[j]=0;
				sect[i]+=A.sect[j] + up;
				up=sect[i] / LIM;
				sect[i]%=LIM;
			}
			scnt=sc;
			if (up) sect[scnt++]=up;
		}
		void minus(hp<LIM,MAX> &A)
		{
			int sc=scnt;
			int i,lend=0;
			for (i=0;i<sc;i++)
			{
				if (i>=A.scnt) A.sect[i]=0;
				sect[i]-=A.sect[i] + lend;
				lend=0;
				if (sect[i]<0)
				{
					lend=1;
					sect[i]+=LIM;
				}
			}
			scnt=sc;
			if (lend) scnt--;
			for (;scnt>1 && sect[scnt-1]==0;scnt--);
 
		}
		void multiply(int p)
		{
			if (p==0)
			{
				scnt=1;
				sect[0]=0;
				return;
			}
			int sc=scnt;
			int i,up=0;
			for (i=0;i<sc;i++)
			{
				if (i>=scnt) sect[i]=0;
				sect[i]=sect[i] * p + up;
				up=sect[i] / LIM;
				sect[i]%=LIM;
			}
			scnt=sc;
			if (up) sect[scnt++]=up;
		}
		void multiply(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> T,C;
			int sc=A.scnt;
			int i;
			for (i=0;i<sc;i++)
			{
				T=(*this);
				T.multiply(A.sect[i]);
				C.plus(T,i);
			}
			scnt=sc;
			(*this)=C;
		}
		int compare(hp<LIM,MAX> &A)
		{
			if (scnt > A.scnt)
				return 1;
			if (scnt < A.scnt)
				return -1;
			for (int i=scnt-1;i>=0;i--)
			{
				if (sect[i] > A.sect[i])
					return 1;
				if (sect[i] < A.sect[i])
					return -1;
			}
			return 0;
		}
		//operators
		bool operator == (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==0;
		}
		bool operator < (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==-1;
		}
		bool operator > (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==1;
		}
		bool operator <= (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==-1 || t==0;
		}
		bool operator >= (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==1 || t==0;
		}
		void operator =(hp<LIM,MAX> A)
		{
			copy(A);
		}
		void operator =(int A)
		{
			copy(A);
		}
		void operator +=(hp<LIM,MAX> &A)
		{
			plus(A);
		}
		void operator -=(hp<LIM,MAX> &A)
		{
			minus(A);
		}
		void operator *=(hp<LIM,MAX> &A)
		{
			multiply(A);
		}
		void operator *=(int A)
		{
			multiply(A);
		}
		hp<LIM,MAX> operator +(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.plus(A);
			return C;
		}
		hp<LIM,MAX> operator -(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.minus(A);
			return C;
		}
		hp<LIM,MAX> operator *(int A)
		{
			hp<LIM,MAX> C(*this);
			C.multiply(A);
			return C;
		}
		hp<LIM,MAX> operator *(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.multiply(A);
			return C;
		}
};
 
typedef hp<10000,100> hpnum;
 
hpnum A,B;
 
hpnum qp(int P)
{
	hpnum A;
	if (P/2==1)
		A=2;
	else
		A=qp(P/2);
	A*=A;
	if (P%2==1)
		A*=2;
	return A;
}
 
void mason()
{
	int P;
	B=1;
	freopen("mason.in","r",stdin);
	freopen("mason.out","w",stdout);
	scanf("%d",&P);
	A=qp(P);
	A-=B;
	A.print();
}
 
void mul()
{
	char c;
	hpnum A,B,C;
	freopen("mul.in","r",stdin);
	freopen("mul.out","w",stdout);
	A.read(4);
	while ((c=getchar())==10 || c==13);
	ungetc(c,stdin);
	B.read(4);
	C=A*B;
	C+=B;
	C.print();
}
 
int main()
{
	//mason();麦森数
	//mul();高精度乘法例子
	return 0;
}
标签:, , ,

HAOI 2008 题目第一时间发布!

競賽歷程 2 Comments »538 views

l 上午 Read the rest of this entry »

标签:, , , , ,

CmYkRgB123 Online Judge System 比赛测试

設計開發 3 Comments »421 views

今天学校内部组织了一次比赛,经过多方努力,老师终于同意使用我的CmYkRgB123 Online Judge System来举办比赛。 比赛顺利完成,效果还不错。 COJS比赛

标签:, , , ,

C++中fstream的用法

計算機技術 No Comments »2,478 views

ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间;

在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符:

1、插入器(<<)
向流输出数据。比如说系统有一个默认的标准输出流(cout),一般情况下就是指的显示器,所以,cout<<”Write Stdout”<<’\n’;就表示把字符串”Write Stdout”和换行字符(‘\n’)输出到标准输出流。

2、析取器(>>)
从流中输入数据。比如说系统有一个默认的标准输入流(cin),一般情况下就是指的键盘,所以,cin>>x;就表示从标准输入流中读取一个指定类型(即变量x的类型)的数据。

Read the rest of this entry »

标签:, , , , ,
24 queries. 0.746 seconds. Designed by NattyWP .
Images by desEXign.