View difference between Paste ID: bjJeLUqW and 855eX1d8
SHOW: | | - or go back to the newest paste.
1
/**
2-
 SineWaver.cpp - now with a #define!
2+
 SineWaver.cpp - now with a #define and an ERR_CODES enum!
3
4
 This program will draw sine waves that look like this:
5
6
           ,-'`'-,             ,-'`'-,
7
          /       \           /
8
         /         \         /
9
        /           \       /
10
 `'-.-'`             `'-.-'`
11
12
         ,-'`'-,
13
        /       \
14
 `'-.-'`         `'-.-'`
15
16
 The program requires two arguments: how many periods to
17
 draw and the height of the wave. I might program in a
18
 "cosine" option to change whether the wave draws up
19
 or down to begin with. Ooh, and perhaps cross them too!
20
*/
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <iostream>
24
#include <string>
25
26
#define CURVEOFFSET 2
27
28
enum ERR_CODES {
29
 ERR_NO_ARGS  = 1,
30
 ERR_NEG_PERIODS,
31
 ERR_TOO_SHORT,
32
 ERR_PEAK_VALI_LEN_MISMATCH,
33
};
34
35
char upCurve='/';
36
char dnCurve='\\';
37
std::string peakPattern=",-'`'-,";
38
std::string valiPattern="`'-.-'`";
39
40
class Wave
41
{
42
 private:
43
  int qPeriod;
44
  int height;
45
  int waveX;
46
  int waveY;
47
 public:
48
  Wave(int qperFeed, int hiFeed) : qPeriod(qperFeed), height(hiFeed), waveX(0), waveY(hiFeed)
49
  {
50-
    exit(4);
50+
51
52
   // we need to check if the lengths of the peak and valley are equal
53
   int peakLen=peakPattern.length();
54
   int valiLen=valiPattern.length();
55
   if (!(peakLen==valiLen)) {
56
    fprintf(stderr,"Internal error: mismatch between peak and valley string patterns\n");
57
    exit(ERR_PEAK_VALI_LEN_MISMATCH);
58
   }
59
   
60
   // error checking passed. let's find out how wide our curve will be.
61
   for (int i=0;i<qPeriod;i++)
62
   {
63
    switch (i%4)
64
    {
65
     case 0: waveX+=valiLen; break;
66
     case 1: waveX+=(height-CURVEOFFSET); break;
67
     case 2: waveX+=peakLen; break;
68
     case 3: waveX+=(height-CURVEOFFSET); break;
69
    }
70
   }
71
72
   //printf("waveX = %d\nwaveY = %d\n",waveX,waveY);
73
  }
74
75
  void draw()
76
  {
77
   std::string thisRow;
78
   char thisChar;
79
   for (int q=1; q<=waveY; q++)
80
   {
81
    thisRow="";
82
    for (int r=1; r<=waveX; r++)
83
    {
84
     int charPos = r%(peakPattern.length()+2*(height-CURVEOFFSET)+valiPattern.length())+1;
85
     //fprintf(stderr,"%dx%d: %d\n",q,r,charPos);
86
     if (q==1)
87
     {
88
      if ((charPos > valiPattern.length() + (height-CURVEOFFSET)) &&
89
      (charPos <= valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()))
90
       thisChar=peakPattern[(charPos-peakPattern.length()-(height-CURVEOFFSET))-1];
91
      else
92
       thisChar=' ';
93
     }
94
     else if (q==waveY)
95
     {
96
      if (charPos <= valiPattern.length())
97
       thisChar=valiPattern[charPos-1];
98
      else
99
       thisChar=' ';
100
     }
101
     else
102
     {
103
      if ((charPos > valiPattern.length()) && (charPos <= (valiPattern.length() + height-2)) &&
104
      (charPos - valiPattern.length()) == (waveY-q))
105
       thisChar=upCurve;
106
      else if (charPos > (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) &&
107
      (charPos - (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) == (q-1)))
108
       thisChar=dnCurve;
109
      else
110
       thisChar=' ';
111
     }
112
    thisRow+=thisChar;
113
    }
114
    std::cout << thisRow << std::endl;
115
   }
116
  }
117
118
  int GetQPeriod() { return qPeriod; }
119
  int GetHeight() { return height; }
120
};
121
122
int main(int argc, char* argv[])
123
{
124-
  exit(1);
124+
125
 int waveHeight;
126
 if (argc < 3)
127
 {
128
  fprintf(stderr,
129
   "This program requires at least two arguments.\n"
130
   "Usage: SineWaver [#-of-quarter-periods] [wave-height]\n");
131
  exit(ERR_NO_ARGS);
132
 }
133
 else
134-
   exit(2);
134+
135
  noQuarterPeriod = atoi(argv[1]);
136
  if (noQuarterPeriod < 1)
137
  {
138
   fprintf(stderr,
139
    "The number of quarter periods must be a positive number!\n"
140
    "You gave me %d instead.\n",noQuarterPeriod);
141
   exit(ERR_NEG_PERIODS);
142-
   exit(3);
142+
143
  waveHeight = atoi(argv[2]);
144
  if (waveHeight < 2)
145
  {
146
   fprintf(stderr,
147
    "The wave height is too short! The minimum is 2.\n"
148
    "You gave me a height of %d instead.\n",waveHeight);
149
   exit(ERR_TOO_SHORT);
150
  }
151
 }
152
 
153
 // We'll make a pretty message by smartly analyzing how many periods we have.
154
 int noFullPeriod;
155
 int nQPM=(noQuarterPeriod%4);
156
 std::string initPattern="Generating wave with a height of %d and "; 
157
 if (noQuarterPeriod < 4)
158
 {
159
  noFullPeriod=0;
160
  initPattern+="\%d/\%d period";
161
 }
162
 else if (nQPM)
163
 {
164
  noFullPeriod=noQuarterPeriod/4;
165
  initPattern+="\%d \%d/\%d periods";
166
 }
167
 else {
168
  noFullPeriod=noQuarterPeriod/4;
169
  initPattern+="\%d period";
170
  if (noFullPeriod>1) { initPattern+="s";} 
171
 }
172
 initPattern+="...\n";
173
 printf(initPattern.c_str(),waveHeight,
174
   (noFullPeriod)?noFullPeriod:(nQPM==2)?1:nQPM,
175
   (noFullPeriod)?((nQPM==2)?1:nQPM):(nQPM==2)?2:4,
176
   (nQPM==2)?2:4);
177
 Wave sineWave(noQuarterPeriod,waveHeight);
178
 sineWave.draw();
179
180
 return 0;
181
}