c - 如何在没有graphics.h的情况下用C生成水平正弦文本?

标签 c trigonometry math.h

我正在尝试编写一个用 C 语言生成正弦文本的程序。我尝试了下面的代码并得到了一个垂直正弦波。能不能做成水平的?我是 C 编程新手,所以如果可以的话,请用最简单的方式解释一下代码。谢谢!

#include <stdio.h>
#include <math.h>


int main()
{
    // declarations
    int x;
    char y[80];
    char str[] = "This is a test string";
    // setting entire array to spaces
    for(x = 0; x < 79; x++)
        y[x] = ' ';
    y[79] = '\0';

    // print 20 lines, for one period (2 pi radians)
    for(x = 0; str[x]!='\0'; x++)
    {
        y[40 + (int) (40 * sin(M_PI * (float) x /10))] = str[x];
        printf("%s\n", y);
        y[40 + (int) (40 * sin(M_PI * (float) x / 10))] = ' ';
    }

    // exit
    return 0;
} 

输出:

                                   T                                      
                                                    h                          
                                                               i               
                                                                        s      


                                                                              s

                                                               a               

                                        t                                      
                            e                                                  
                 s                                                             
        t                                                                      

s                                                                              
  t                                                                            
        r                                                                      
                 i                                                             
                            n                                                  
                                        g               

是否可以在不改变现有代码的情况下使波浪水平?谢谢你!

最佳答案

我为你的问题写了一个答案,它有效,也许不完全如你所期望的,但它应该给你足够的工作空间

注意以下几点:

  • 写入控制台\文件后,很难返回和更改打印值
  • 在打印任何内容之前,您必须定义所需的输出矩阵并准备整个输出

我的示例背后的逻辑是这样的:

  1. 创建一个矩阵 (80*22)
  2. 用空格填充矩阵
  3. 按列填充矩阵
  4. 按字符打印整个矩阵;
<小时/>
#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    // declarations
    int col, row;
    char str[] = "This is a test string";

    /* define the screen for print (80 width * 22 length)*/
    char printout[80][22];

    // setting entire matrix to spaces
    for (row=0; row < 22 ; row++)
    {
        for(col = 0; col < 80; col++)
            printout[col][row] = ' ';
    }

    /* fill in the columns modulo the string to allow continuous output */
    for(col = 0; col < 80 ; col++)
    {
        printout[col][10 + (int) (10 * sin(M_PI * (float) col /10))] = str[( col % strlen(str) )];
    }

    /* printout the entire matrix formatted */
    for (row = 0 ; row < 22 ; row++) {
        for (col = 0 ; col < 80 ; col++) {
            printf("%c", printout[col][row]);
        }
        printf("\n");
    }
    // exit
    return 0;
} 

此代码中有很多需要纠正的地方 - 行应该考虑字符串的大小,您应该将其解析为 string 而不是 char 等。 但它确实给了你想要的东西,并且可能会帮助你继续......

               s                                       t                   s    
                t                 t s                 s                   e t   
             t   r               s   t               e   s               t      


            s     i             e     r             t     t                   s 

           e       n           t       i                   r           a       t


T         t         g                   n         a         i                   


 h                   T       a           g                   n       s          

  i     a             h                   T     s             g     i           


   s                   i   s               h   i               T                
      s                 s i                 i                   h s             
     i                                       s                   i              

关于c - 如何在没有graphics.h的情况下用C生成水平正弦文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213910/

相关文章:

algorithm - 计算序列的余弦

c - 将 float 结果保存到第三位,C 中不四舍五入

c - pow 函数在 Code::Blocks IDE 中无法正常工作

c - 包含 math.h 后出现 abs 'implicit declaration...' 错误

c - 无法将 char 字符串插入 char 数组

c - 如何修复 strcat 函数中的段错误?

binary - 浮点余弦

c - 如何比较表格

c - 使用 scanf 解析字符串时的奇怪行为

Java 射弹角度