$$ f(x)= \left\{ \begin{array}{ll} x^2 & -\pi\leq x < -\frac{\pi}{2} \\ \sin(x) & -\frac{\pi}{2}\leq x< 0 \\ x-\pi & 0\leq x < \frac{\pi}{2} \\ e^x & \frac{\pi}{2}\leq x < \pi \\ \end{array} \right. $$
Basically what I'm trying to do is find the Fourier coefficients like this: $$ \begin{align*} a_0&=\frac{1}{2\ell}\int_{-\ell}^\ell f(x)dx \\ a_k&=\frac{1}{\ell}\int_{-\ell}^\ell f(x)\cos(\frac{k\pi x}{\ell})dx \\ b_k&=\frac{1}{\ell}\int_{-\ell}^{\ell} f(x)\sin(\frac{k\pi x}{\ell})dx \end{align*}$$ with $\ell=\pi$. Am I correct to assume that I can get the coefficient by integrating like this? $$ a_k=\frac{1}{\pi}\left[\int_{-\pi}^{-\frac{\pi}{2}}x^2\cos(kx)dx+\int_{-\frac{\pi}{2}}^0\sin(x)\cos(kx)dx+\int_0^{\frac{\pi}{2}}(x-\pi)\cos(kx)dx+\int_{\frac{\pi}{2}}^\pi e^x\cos(kx)\right] $$
And if so, how can I simplify, because right now I'm getting something very convoluted like this: $$ a_k=\frac{ \left( 8-\pi^2k^2 \right) \sin \left( \frac{k\pi}{2} \right) -4k\pi\cos \left( \frac{k\pi}{2} \right) +8k\pi\cos \left( k\pi \right) }{4\pi k^3}+\frac{1}{\pi \left( k^2-1 \right) }+ \left( -\frac{1}{2k}\sin \left( \frac{k\pi}{2} \right) +\frac{1}{k^2\pi}\cos \left( \frac{k\pi}{2} \right) -\frac{1}{k^2\pi} \right) +\frac{e^{\pi}k\sin \left( k\pi \right) -e^{\frac{\pi}{2}}k\sin \left( \frac{k\pi}{2} \right) +e^{\frac{\pi}{2}}k\cos \left( \frac{k\pi}{2} \right) -e^{\pi}k\cos \left( k\pi \right) }{k^2+1} $$
Am I doing something wrong? I haven't even bothered with $b_k$ because i feel like I'm doing something wrong.
You are correct to split up the integral like that, and you should expect a messy answer owing to the fact that you have a piecewise-defined function.
Some simplifications can be made to the trig functions in your answer. A list I have in a personal document is as follows: for any integer $\newcommand{\ds}{\displaystyle} \newcommand{\pf}[2]{\left( \frac{#1}{#2} \right)} \newcommand{\case}[1]{\begin{cases} #1 \end{cases}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \newcommand{\para}[1]{\left( #1 \right)} \newcommand{\al}[1]{\begin{align*} #1 \end{align*}} n$,
$$\al{ \sin(n \pi) &= 0\\ \cos(n \pi) &= (-1)^n\\ \sin \pf{n\pi}{2} &= \case{ 1, & n = 4k+1 \text{ for a } k \in \Z \\ -1, & n = 4k+3 \text{ for a } k \in \Z \\ 0, & \text{otherwise}} \\ &= \frac{(-1)^{(n-1)/2} \Big( 1 - (-1)^n \Big)}{2} \\ &= \frac{(-1)^{\floor{(n-1)/2}} + (-1)^{\floor{n/2}}}{2} \\ \cos \pf{n\pi}{2} &= \case{ 1, & n = 4k \text{ for a } k \in \Z \\ -1, & n = 4k+2 \text{ for a } k \in \Z \\ 0, & \text{otherwise}} \\ &= \frac{(-1)^{n/2} \Big( 1 - (-1)^{n-1} \Big)}{2}\\ &= \frac{(-1)^{\floor{n/2}} + (-1)^{\floor{(n+1)/2}}}{2}\\ \sin \para{ \frac{2n+1}{2} \pi } &= (-1)^n\\ \cos \para{ \frac{2n+1}{2} \pi } &= 0 } $$