Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- SineWaver.cpp - now with a #define and an ERR_CODES enum!
- This program will draw sine waves that look like this:
- ,-'`'-, ,-'`'-,
- / \ /
- / \ /
- / \ /
- `'-.-'` `'-.-'`
- ,-'`'-,
- / \
- `'-.-'` `'-.-'`
- The program requires two arguments: how many periods to
- draw and the height of the wave. I might program in a
- "cosine" option to change whether the wave draws up
- or down to begin with. Ooh, and perhaps cross them too!
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string>
- #define CURVEOFFSET 2
- enum ERR_CODES {
- ERR_NO_ARGS = 1,
- ERR_NEG_PERIODS,
- ERR_TOO_SHORT,
- ERR_PEAK_VALI_LEN_MISMATCH,
- };
- char upCurve='/';
- char dnCurve='\\';
- std::string peakPattern=",-'`'-,";
- std::string valiPattern="`'-.-'`";
- class Wave
- {
- private:
- int qPeriod;
- int height;
- int waveX;
- int waveY;
- public:
- Wave(int qperFeed, int hiFeed) : qPeriod(qperFeed), height(hiFeed), waveX(0), waveY(hiFeed)
- {
- // derive the dimensions of the wave which will result
- // we need to check if the lengths of the peak and valley are equal
- int peakLen=peakPattern.length();
- int valiLen=valiPattern.length();
- if (!(peakLen==valiLen)) {
- fprintf(stderr,"Internal error: mismatch between peak and valley string patterns\n");
- exit(ERR_PEAK_VALI_LEN_MISMATCH);
- }
- // error checking passed. let's find out how wide our curve will be.
- for (int i=0;i<qPeriod;i++)
- {
- switch (i%4)
- {
- case 0: waveX+=valiLen; break;
- case 1: waveX+=(height-CURVEOFFSET); break;
- case 2: waveX+=peakLen; break;
- case 3: waveX+=(height-CURVEOFFSET); break;
- }
- }
- //printf("waveX = %d\nwaveY = %d\n",waveX,waveY);
- }
- void draw()
- {
- std::string thisRow;
- char thisChar;
- for (int q=1; q<=waveY; q++)
- {
- thisRow="";
- for (int r=1; r<=waveX; r++)
- {
- int charPos = r%(peakPattern.length()+2*(height-CURVEOFFSET)+valiPattern.length())+1;
- //fprintf(stderr,"%dx%d: %d\n",q,r,charPos);
- if (q==1)
- {
- if ((charPos > valiPattern.length() + (height-CURVEOFFSET)) &&
- (charPos <= valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()))
- thisChar=peakPattern[(charPos-peakPattern.length()-(height-CURVEOFFSET))-1];
- else
- thisChar=' ';
- }
- else if (q==waveY)
- {
- if (charPos <= valiPattern.length())
- thisChar=valiPattern[charPos-1];
- else
- thisChar=' ';
- }
- else
- {
- if ((charPos > valiPattern.length()) && (charPos <= (valiPattern.length() + height-2)) &&
- (charPos - valiPattern.length()) == (waveY-q))
- thisChar=upCurve;
- else if (charPos > (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) &&
- (charPos - (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) == (q-1)))
- thisChar=dnCurve;
- else
- thisChar=' ';
- }
- thisRow+=thisChar;
- }
- std::cout << thisRow << std::endl;
- }
- }
- int GetQPeriod() { return qPeriod; }
- int GetHeight() { return height; }
- };
- int main(int argc, char* argv[])
- {
- int noQuarterPeriod;
- int waveHeight;
- if (argc < 3)
- {
- fprintf(stderr,
- "This program requires at least two arguments.\n"
- "Usage: SineWaver [#-of-quarter-periods] [wave-height]\n");
- exit(ERR_NO_ARGS);
- }
- else
- {
- noQuarterPeriod = atoi(argv[1]);
- if (noQuarterPeriod < 1)
- {
- fprintf(stderr,
- "The number of quarter periods must be a positive number!\n"
- "You gave me %d instead.\n",noQuarterPeriod);
- exit(ERR_NEG_PERIODS);
- }
- waveHeight = atoi(argv[2]);
- if (waveHeight < 2)
- {
- fprintf(stderr,
- "The wave height is too short! The minimum is 2.\n"
- "You gave me a height of %d instead.\n",waveHeight);
- exit(ERR_TOO_SHORT);
- }
- }
- // We'll make a pretty message by smartly analyzing how many periods we have.
- int noFullPeriod;
- int nQPM=(noQuarterPeriod%4);
- std::string initPattern="Generating wave with a height of %d and ";
- if (noQuarterPeriod < 4)
- {
- noFullPeriod=0;
- initPattern+="\%d/\%d period";
- }
- else if (nQPM)
- {
- noFullPeriod=noQuarterPeriod/4;
- initPattern+="\%d \%d/\%d periods";
- }
- else {
- noFullPeriod=noQuarterPeriod/4;
- initPattern+="\%d period";
- if (noFullPeriod>1) { initPattern+="s";}
- }
- initPattern+="...\n";
- printf(initPattern.c_str(),waveHeight,
- (noFullPeriod)?noFullPeriod:(nQPM==2)?1:nQPM,
- (noFullPeriod)?((nQPM==2)?1:nQPM):(nQPM==2)?2:4,
- (nQPM==2)?2:4);
- Wave sineWave(noQuarterPeriod,waveHeight);
- sineWave.draw();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment