2016-11-27 1 views
0

하나의 그래프에 두 개의 그래프를 표시하기 위해 C++에서 코드를 작성했습니다. 하나의 함수는 sin 함수 여야하고 다른 함수는 cos 함수 여야합니다.C++을 사용하여 sin/cos 그래프 만들기

그래프에 sin 그래프가 필요하고 그래프에 cos 그래프가 필요하지만 함께 표시 할 수 없습니다.

#include <cmath> 
#include <iostream> 
#include <cstdlib> 

using namespace std; 
const float PI = 3.1459265; 
int main() 
{ 
    int size = 80, height = 21; 
    char chart[height][size]; 
    size = 80, height = 21; 
    double cosx[size]; 
    double sinx[size]; 

    { 
     for (int i=0; i<size; i++) 
      cosx[i] = 10*cos(i/4.5); 

     for (int i=0; i<height; i++) 
      for (int j=0; j<size; j++) 
       if (-.01 < 10 - i - round(cosx[j]) && 10 - i - round(cosx[j]) <0.01) 
        chart[i][j] = 'x'; 
       else if (i==height/2) 
        chart[i][j] = '-'; 
       else 
        chart[i][j] = ' '; 

     for (int i=0; i<height; i++) 
      for (int j=0; j<size; j++) 
       cout << chart[i][j]; 

     for (int i=0; i<size; i++) 
      sinx[i] = 10*sin(i/4.5); 

     for (int i=0; i<height; i++) 
      for (int j=0; j<size; j++) 

     if (-.01 < 10 - i - round(sinx[j]) && 10 - i - round(sinx[j]) <0.01) 
      chart[i][j] = 'x'; 
     else if (i==height/2) 
      chart[i][j] = '-'; 
     else 
      chart[i][j] = ' '; 

     for (int i=0; i<height; i++) 
      for (int j=0; j<size; j++) 
       cout << chart[i][j]; 
    } 
} 
+0

차트 라이브러리가 있습니까? 그냥 콘솔에 출력하려고하는 것처럼 보입니다. –

+1

gnuplot http://gnuplot.sourceforge.net/demo_5.0/simple.html 또는 matplotlib http://matplotlib.org/examples/animation/에서 몇 가지 항목을 빠르게 플롯하려는 경우 gnuplot을 살펴볼 수 있습니다. basic_example.html – moof2k

+0

코드를 형식화하려했지만 들여 쓰기가 올바르지 않을 경우 제 편집을 확인해야합니다. 반드시 필요한 것은 아니지만 가독성을 높이고 나중에 수정하기 쉽도록 코드 블록 주위에 중괄호'{..} '를 사용하는 것이 좋습니다. – Tony

답변

0

그래프의 각 행 다음에 줄 바꿈을 인쇄하지 마십시오. 그 다음은 나를 위해 작동

for (int i=0; i<height; i++) { 
    for (int j=0; j<size; j++) { 
     cout << chart[i][j]; 
    } 
    cout << '\n'; 
} 

for (int i=0; i<height; i++) 
    for (int j=0; j<size; j++) 
     cout << chart[i][j]; 

교체합니다.

그러나이 방법은 다소 복잡한 방식으로 수행됩니다. 그것은 나 였다면, 나는 함수 값에 가깝다면 각 좌표를 검사하고 검사하지 않고 함수 값에서 x-es에 대한 좌표를 계산할 것입니다. 예 :

#include <cmath> 
#include <iostream> 
#include <string> 
#include <vector> 

int main() 
{ 
    int size = 80, height = 21; 

    // Start with an empty chart (lots of spaces and a line in the middle) 
    std::vector<std::string> chart(height, std::string(size, ' ')); 
    chart[height/2] = std::string(size, '-'); 

    // Then just put x-es where the function should be plotted 
    for(int i = 0; i < size; ++i) { 
    chart[static_cast<int>(std::round(10 * std::cos(i/4.5) + 10))][i] = 'x'; 
    } 

    // and print the whole shebang 
    for(auto &&s : chart) { 
    std::cout << s << '\n'; 
    } 
}