Professional Documents
Culture Documents
it
ea‘
C’pr
ogr
am t
oreadapost
fi
xexpr
essi
on,
eval
uat
eitanddi
spl
ayt
her
esul
t.
(
UseSt
ati
cImpl
ement
ati
onofSt
ack)
.
#i
ncl
ude<st
dio.
h>
i
ntst
ack[
20]
;
i
ntt
op=-
1;
v
oidpush(
intx)
st
ack[
++t
op]=x;
i
ntpop(
)
r
etur
nst
ack[
top-
-]
;
i
ntmai
n()
charexp[
20]
;
char*
e;
i
ntn1,
n2,
n3,
num;
pr
int
f("
Ent
ert
heexpr
essi
on:
:")
;
scanf
("%s"
,exp)
;
e=exp;
whi
l
e(*
e!='
\0'
)
{
i
f(
isdi
git
(*e)
)
num =*
e-48;
push(
num)
;
el
se
n1=pop(
);
n2=pop(
);
swi
tch(
*e)
case'
+'
:
n3=n1+n2;
br
eak;
case'
-
':
n3=n2-n1;
br
eak;
case'
*'
:
n3=n1*n2;
br
eak;
case'
/'
:
n3=n2/n1;
br
eak;
push(
n3)
;
e++;
pr
int
f("
\nTher
esul
tofex
pressi
on%s=%d\
n\n"
,exp,
pop(
));
r
etur
n0;
B)Wri
tea‘C’program t
oacceptthenamesofci
ti
esandst
oret
hem i
nar
ray
.Accepttheci
ty
namefrom useranduseli
nearsearchal
gor
it
hm t
ocheckwhet
hert
heci
tyi
spresenti
narr
ayor
not
.
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
ri
ng.
h>
t
ypedefst
ructci
ty
{
charname[
30]
;
i
ntcode;
}
recor
d;
r
ecor
dci
ty[
100]
;
i
ntr
ead_
fil
e(r
ecor
d*a)
i
nti
=0;
FI
LE*
fp;
i
f(
(fp=f
open(
"sor
tedci
ti
es.
txt
",
"r
"))
!=NULL)
whi
l
e(!
feof
(fp)
)
f
scanf
(fp,
"%s%d"
,a[
i]
.name,
&a[
i]
.code)
;
i
++;
r
etur
n(i
-1)
;
v
oidwr
it
e_f
il
e(r
ecor
d*a,
i
ntn)
i
nti
=0;
FI
LE*
fp;
i
f(
(fp=f
open(
"sor
ted_
cit
ies.
txt
",
"w"
))!
=NULL)
f
or(
i=0;
i
<n;
i
++)
f
pri
ntf
(fp,
"\
n%s\
t%d"
,a[
i]
.name,
a[i
].
code)
;
v
oidsor
t(r
ecor
d*a,
i
ntn)
{
i
nti
,
j;
r
ecor
dt;
f
or(
i=1;
i
<n;
i
++)
f
or(
j=0;
j
<n-
i;
j
++)
i
f(
str
cmp(
a[j
].
name,
a[j
+1]
.name)
>0)
t
=a[
j]
;
a[
j]
=a[
j+1]
;
a[
j+1]
=t;
i
ntr
ead_
fil
e1(
recor
d*a)
i
nti
=0;
FI
LE*
fp;
i
f(
(fp=f
open(
"sor
ted_
cit
ies.
txt
",
"r
"))
!=NULL)
whi
l
e(!
feof
(fp)
)
f
scanf
(fp,
"%s%d"
,a[
i]
.name,
&a[
i]
.code)
;
i
++;
}
r
etur
n(i
-1)
;
v
oidb_
sear
ch(
recor
d*a,
i
ntn,
charkey
[20]
)
i
ntl
,
h,mi
d;
l
=0;
h=n-
1;
whi
l
e(h>=l
)
mi
d=(
l+h)
/2;
i
f(
str
cmp(
key
,a[
mid]
.name)
==0)
pr
int
f("
\nSTDcode:
%d\
n",
a[mi
d].
code)
;
br
eak;
el
sei
f(
str
cmp(
key
,a[
mid]
.name)
<0)
h=mi
d-1;
el
se
l
=mi
d+1;
i
f(
h<l
)
pr
int
f("
\nci
tynoti
nli
st\
n")
;
mai
n()
{
charkey
[20]
;
i
ntn,
m;
n=r
ead_
fil
e(ci
ty)
;
sor
t(ci
ty,
n);
wr
it
e_f
il
e(ci
ty,
n);
pr
int
f("
\nent
erci
tyname\
n")
;
scanf
("%s"
,key
);
b_
sear
ch(
cit
y,n,
key
);
Q2Wr i
teaPHPprogram forcourseregi
str
ati
onofLearneri nani nst
it
utethatacceptName,
Coursetobeadmitt
ed,Mobi l
enumberusinginputformv ali
dationsuchasNameshoul dbeonl
y
stri
ngofchar
act
er,mobilenumbershouldcontaindigi
tswi t
hv ali
dlengthandsoon.andgive
feedbackt
oLearnerwit
hr egi
strat
iondet
ail
sincl
udingregistr
ationnumber .
[
25M]
<?
php
/
/def
inev
ari
abl
est
oempt
yval
ues
$nameEr
r=$emai
l
Err=$mobi
l
enoEr
r=$gender
Err=$websi
teEr
r=$agr
eeEr
r="
";
$name=$emai
l=$mobi
l
eno=$gender=$websi
te=$agr
ee="
";
/
/Inputf
iel
dsv
ali
dat
ion
i
f($_
SERVER[
"REQUEST_
METHOD"
]=="
POST"
){
/
/St
ri
ngVal
i
dat
ion
i
f(empt
yempt
y($_
POST[
"name"
])
){
$nameEr
r="
Namei
srequi
red"
;
}el
se{
$name=i
nput
_dat
a($_
POST[
"name"
])
;
/
/checki
fnameonl
ycont
ainsl
ett
ersandwhi
tespace
i
f(!
preg_
mat
ch(
"/^
[a-
zA-
Z]*
$/"
,$name)
){
$nameEr
r="
Onl
yal
phabet
sandwhi
tespacear
eal
l
owed"
;
/
/Emai
lVal
i
dat
ion
i
f(empt
yempt
y($_
POST[
"emai
l
"])
){
$emai
l
Err="
Emai
li
srequi
red"
;
}el
se{
$emai
l=i
nput
_dat
a($_
POST[
"emai
l
"])
;
/
/checkt
hatt
hee-
mai
laddr
essi
swel
l
-for
med
i
f(!
fi
lt
er_
var
($emai
l
,FI
LTER_
VALI
DATE_
EMAI
L)){
$emai
l
Err="
Inv
ali
demai
lfor
mat
";
/
/NumberVal
i
dat
ion
i
f(empt
yempt
y($_
POST[
"mobi
l
eno"
])
){
$mobi
l
enoEr
r="
Mobi
l
enoi
srequi
red"
;
}el
se{
$mobi
l
eno=i
nput
_dat
a($_
POST[
"mobi
l
eno"
])
;
/
/checki
fmobi
l
enoi
swel
l
-for
med
i
f(!
preg_
mat
ch(
"/^
[0-
9]*
$/"
,$mobi
l
eno)){
$mobi
l
enoEr
r="
Onl
ynumer
icv
aluei
sal
l
owed.
";
/
/checkmobi
l
enol
engt
hshoul
dnotbel
essandgr
eat
ort
han10
i
f(st
rl
en(
$mobi
l
eno)!
=10){
$mobi
l
enoEr
r="
Mobi
l
enomustcont
ain10di
git
s."
;
/
/URLVal
i
dat
ion
i
f(empt
yempt
y($_
POST[
"websi
te"
])
){
$websi
te="
";
}el
se{
$websi
te=i
nput
_dat
a($_
POST[
"websi
te"
])
;
/
/checki
fURLaddr
esssy
ntaxi
sval
i
d
i
f(!
preg_
mat ch(
"/\
b(?:
(?:
htt
ps?
|ft
p):
\/\
/|www\
.)[
-a-
z0-
9+&@#\
/%?
=~_
|!
:,
.
;]
*[
-a-
z0-
9+&@#\/%=~_|
]/
i"
,$websit
e)){
$websi
teEr
r="
Inv
ali
dURL"
;
/
/Empt
yFi
eldVal
i
dat
ion
i
f(empt
yempt
y($_
POST[
"gender
"]
)){
$gender
Err="
Genderi
srequi
red"
;
}el
se{
$gender=i
nput
_dat
a($_
POST[
"gender
"]
);
}
/
/CheckboxVal
i
dat
ion
i
f(!
isset
($_
POST[
'
agr
ee'
]
)){
$agr
eeEr
r="
Acceptt
ermsofser
vicesbef
oresubmi
t.
";
}el
se{
$agr
ee=i
nput
_dat
a($_
POST[
"agr
ee"
])
;
f
unct
ioni
nput
_dat
a($dat
a){
$dat
a=t
ri
m($dat
a);
$dat
a=st
ri
psl
ashes(
$dat
a);
$dat
a=ht
mlspeci
alchar
s($dat
a);
r
etur
n$dat
a;
?
>
<h2>Regi
str
ati
onFor
m</
h2>
<spancl
ass="
err
or"
>*r
equi
redf
iel
d</
span>
<br
><br
>
<f
orm met
hod="
post
"act
ion="
<?phpechoht
mlspeci
alchar
s($_
SERVER[
"PHP_
SELF"
])
;?>">
Name:
<i
nputt
ype="
text
"name="
name"
>
<spancl
ass="
err
or"
>*<?
phpecho$nameEr
r;?
></
span>
<br
><br
>
E-
mai
l
:
<i
nputt
ype="
text
"name="
emai
l
">
<spancl
ass="
err
or"
>*<?
phpecho$emai
l
Err
;?></
span>
<br
><br
>
Mobi
l
eNo:
<i
nputt
ype="
text
"name="
mobi
l
eno"
>
<spancl
ass="
err
or"
>*<?
phpecho$mobi
l
enoEr
r;?
></
span>
<br
><br
>
Websi
te:
<i
nputt
ype="
text
"name="
websi
te"
>
<spancl
ass="
err
or"
><?
phpecho$websi
teEr
r;?
></
span>
<br
><br
>
Gender
:
<i
nputt
ype="
radi
o"name="
gender
"val
ue="
mal
e">Mal
e
<i
nputt
ype="
radi
o"name="
gender
"val
ue="
femal
e">Femal
e
<i
nputt
ype="
radi
o"name="
gender
"val
ue="
other
">Ot
her
<spancl
ass="
err
or"
>*<?
phpecho$gender
Err
;?></
span>
<br
><br
>
Agr
eet
oTer
msofSer
vice:
<i
nputt
ype="
checkbox"name="
agr
ee"
>
<spancl
ass="
err
or"
>*<?
phpecho$agr
eeEr
r;?
></
span>
<br
><br
>
<i
nputt
ype="
submi
t"name="
submi
t"v
alue="
Submi
t"
>
<br
><br
>
</
for
m>
<?
php
i
f(
isset
($_
POST[
'
submi
t'
])
){
i
f(
$nameErr==""&&$emai
l
Err=="
"&&$mobi
l
enoEr
r=="
"&&$gender
Err=="
"&&$websi
teEr
r
==""&&$agr
eeErr==""
){
echo"
<h3col
or=#FF0001><b>Youhav
esucessf
ull
yregi
ster
ed.
</b></
h3>"
;
echo"
<h2>YourI
nput
:</
h2>"
;
echo"
Name:
".$name;
echo"
<br
>";
echo"
Emai
l
:".
$emai
l
;
echo"
<br
>";
echo"
Mobi
l
eNo:
".$mobi
l
eno;
echo"
<br
>";
echo"
Websi
te:
".$websi
te;
echo"
<br
>";
echo"
Gender
:".
$gender
;
}el
se{
echo"
<h3><b>Youdi
dn'
tfi
l
ledupt
hef
orm cor
rect
ly.
</b></
h3>"
;
?
>
</
body
>
</
html
>
Q.
3 Wr
it
eanRPr
ogr
am t
ocal
cul
ateDeci
mal
int
obi
nar
yofagi
vennumber
.
bi
nar
y<-
funct
ion(
p_number
){
bsum<-
0
bexp<-
1
whi
l
e(p_
number>0){
di
git
<-p_
number%%2
p_
number
<-f
loor
(p_
number/2)
bsum<-
bsum +di
git*bexp
bexp<-
bexp*10
r
etur
n(bsum)
p_
number
<-r
eadl
i
ne(
"Deci
mal
number
?:"
)
p_
number
<-as.
numer
ic(
p_number
)
bsum<-
binar
y(p_
number
)
cat
("
Binar
y:"
,bsum)