f[x_]:=Which[-2Pi≤x<-Pi,1,-Pi≤x<0,-1,0≤x<Pi,1,Pi≤x<2Pi,-1]
df[x_]:=Which[-2Pi≤x<-Pi,0,-Pi≤x<0,0,0<x<Pi,0,Pi≤x<2Pi,0]
p=Plot[f[x],{x,-2Pi,2Pi},PlotRange->{-2,2},PlotStyle->{Thickness[0.01],RGBColor[1,0,0]}]
dp=Plot[df[x],{x,-2Pi,2Pi},PlotRange->{-2,2},PlotStyle->{Thickness[0.01],RGBColor[1,0,0]}]

a0=(1/(2*Pi))*(∫_(-Pi)^Pi)f[x]\[DifferentialD]x//FullSimplify
c1=(1/Pi)*(∫_(-Pi)^Pi)f[x]*Cos[n*x]\[DifferentialD]x//TrigReduce
c2=(1/Pi)*(∫_(-Pi)^Pi)f[x]*Sin[n*x]\[DifferentialD]x//TrigReduce

a[n_]:=c1
b[n_]:=c2

g[x_,N_]:=1*a0+(∑_(n=1)^N)(1*a[n]*Cos[n*x]+1*b[n]*Sin[n*x])
q=Plot[g[x,50],{x,-2Pi,2Pi},PlotRange->All,PlotStyle->{Thickness[0.01],RGBColor[0,0,1]}]
Show[p,q]

g[x_]=