zoukankan      html  css  js  c++  java
  • 计算器(图形界面)

    功能介绍

    绘制出图形界面,支持鼠标点击实现四则运算(可带括号),同时具有清零和撤回操作。另外一种模式,可以绘制一些特定的函数图像。

    代码实现

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include <graphics.h>
      4 #include <windows.h>
      5 #include <conio.h>
      6 #include <string.h>
      7 #include<math.h>
      8 
      9 #define PI 3.1415926
     10 #define N 100
     11 
     12 typedef struct
     13 {
     14     char data[N][N];
     15     int top;
     16 
     17 }stack;
     18 
     19 typedef struct figure
     20 {
     21     char x[N];
     22 }figure;
     23 
     24 const int max = 100;
     25 int q;                  //定义一个全局变量,用来计数结构体数组的数量;
     26 int count = 0;
     27 char str[N] = { '0' };          //字符数组赋初始值;
     28 figure st[N];
     29 char suffix[N][N] = { '0' };
     30 stack sta;
     31 int select_;  //用户选择
     32 
     33 
     34 void gotoxy(int x, int y);
     35 void hidecursor();
     36 void introduce();
     37 void calcul_memu();
     38 void mouse_imfor();
     39 void clear_cal();
     40 void chuli(char ch[], struct figure a[]);
     41 void init(stack *sta);
     42 void suffixtoresult(stack *sta, char suffix[][N], int length);
     43 void inffixtosuffix(stack *sta, struct figure *a, char suffix[][N], int *length);
     44 
     45 void image_menu();
     46 
     47 void function1();
     48 void function2();
     49 void function3();
     50 void function4();
     51 void function5();
     52 void function6();
     53 void function7();
     54 void function8();
     55 void function9();
     56 void function10();
     57 void creatCoordinate();
     58 
     59 int main()
     60 {
     61     introduce();
     62     switch (select_)
     63     {
     64     case 1:
     65     {
     66         initgraph(480, 600);     //设置窗口的大小;
     67         setbkcolor(BLACK);      //设置背景色为黑色;
     68         cleardevice();          //用背景色来清空屏幕;
     69         calcul_memu();
     70         mouse_imfor();
     71         closegraph();
     72         break;
     73     }
     74     case 2:
     75     {
     76         image_menu();
     77         break;
     78     }
     79     default:
     80         break;
     81     }
     82 }
     83 void gotoxy(int x, int y)//函数gotoxy:定义光标位置
     84 {
     85     COORD coord;//坐标系coord 
     86     HANDLE handle;
     87 
     88     coord.X = x;//横坐标X 
     89     coord.Y = y;//纵坐标 y
     90     handle = GetStdHandle(STD_OUTPUT_HANDLE);
     91     SetConsoleCursorPosition(handle, coord);
     92 }//end gotoxy 
     93 
     94 void hidecursor()//函数hidecursor:隐藏光标
     95 {
     96     CONSOLE_CURSOR_INFO curinfo;
     97     HANDLE Out;
     98 
     99     Out = GetStdHandle(STD_OUTPUT_HANDLE);
    100     curinfo.dwSize = 1;//光标百分比厚度:1~100
    101     curinfo.bVisible = 0;//是否可见
    102     SetConsoleCursorInfo(Out, &curinfo);
    103 } //end hidecursor 
    104 
    105 //打印欢迎界面
    106 void introduce()
    107 {
    108     int i;
    109     HANDLE consolehwnd;//创建句柄
    110     consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
    111     SetConsoleTextAttribute(consolehwnd, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    112     //设置为亮白色 
    113 
    114     for (i = 14; i < 39; i++)
    115     {
    116         gotoxy(i * 2, 4);
    117         printf("");
    118     }
    119 
    120     for (i = 5; i < 20; i++)
    121     {
    122         gotoxy(28, i);
    123         printf("");
    124         gotoxy(76, i);
    125         printf("");
    126     }
    127 
    128     gotoxy(45, 5);
    129     printf("欢迎来到计算器!");
    130 
    131     for (i = 15; i < 38; i++)
    132     {
    133         gotoxy(i * 2, 6);
    134         printf("");
    135     }
    136 
    137     gotoxy(45, 7);
    138     printf("计算器控制台");
    139 
    140     for (i = 15; i < 38; i++)
    141     {
    142         gotoxy(i * 2, 8);
    143         printf("");
    144     }
    145 
    146     gotoxy(35, 9);
    147     printf("1.计算 实现加减乘除运算(支持括号)");
    148 
    149     gotoxy(37, 10);
    150     printf("按'C'清屏,按'>'撤退一个");
    151 
    152     gotoxy(35, 11);
    153     printf("2.画图 基本函数类型");
    154     gotoxy(37, 12);
    155     printf("按右键退出");
    156     gotoxy(35, 13);
    157     printf("3.开始使用 按1进入计算,按2进入画图");
    158     gotoxy(37, 14);
    159     printf("按0退出");
    160 
    161 
    162     for (i = 15; i < 38; i++)
    163     {
    164         gotoxy(i * 2, 17);
    165         printf("");
    166     }
    167 
    168     for (i = 14; i < 39; i++)
    169     {
    170         gotoxy(i * 2, 20);
    171         printf("");
    172     }
    173 
    174     gotoxy(51, 19);
    175     scanf_s("%d", &select_);
    176     fflush(stdin);
    177 
    178     getchar();
    179     system("cls");
    180 }
    181 
    182 //打印计算器界面
    183 void calcul_memu()
    184 {
    185     setfillcolor(BLACK);
    186     solidrectangle(0, 100, 480, 600);
    187 
    188     setcolor(DARKGRAY);
    189     line(0, 100, 480, 100);
    190     line(0, 200, 480, 200);
    191     line(0, 300, 480, 300);
    192     line(0, 400, 480, 400);
    193     line(0, 500, 480, 500);
    194     line(120, 100, 120, 600);
    195     line(240, 100, 240, 600);
    196     line(360, 100, 360, 600);
    197 
    198     settextstyle(60, 60, _T("宋体"));
    199     outtextxy(30, 120, _T("("));
    200     outtextxy(150, 120, _T(")"));
    201     outtextxy(390, 120, _T("+"));
    202     outtextxy(30, 220, _T("7"));
    203     outtextxy(150, 220, _T("8"));
    204     outtextxy(270, 220, _T("9"));
    205     outtextxy(390, 220, _T("-"));
    206     outtextxy(30, 320, _T("4"));
    207     outtextxy(150, 320, _T("5"));
    208     outtextxy(270, 320, _T("6"));
    209     outtextxy(390, 320, _T("*"));
    210     outtextxy(30, 420, _T("1"));
    211     outtextxy(150, 420, _T("2"));
    212     outtextxy(270, 420, _T("3"));
    213     outtextxy(390, 420, _T("/"));
    214     outtextxy(30, 520, _T("."));
    215     outtextxy(150, 520, _T("0"));
    216     outtextxy(390, 520, _T("="));
    217     outtextxy(260, 120, _T("<"));
    218     outtextxy(270, 520, _T("C"));
    219 }
    220 
    221 //处理鼠标消息
    222 void mouse_imfor()
    223 {
    224     int length = 0;
    225     int i = -1;
    226     int go = 0;
    227     for (int a = 0; a < N; a++)
    228         strcpy(st[i].x, "0");
    229     MOUSEMSG m;
    230     while (go == 0)
    231     {
    232         setcolor(DARKGRAY);
    233         settextstyle(60, 20, _T("宋体"));
    234         m = GetMouseMsg();
    235         switch (m.uMsg)
    236         {
    237         case WM_LBUTTONDOWN:
    238             if (m.x > 0 && m.x < 120 && m.y>100 && m.y < 200)
    239             {
    240                 ++i; count++;
    241                 str[i] = '(';
    242             }
    243             if (m.x > 120 && m.x < 240 && m.y>100 && m.y < 200)
    244             {
    245                 ++i; count++;
    246                 str[i] = ')';
    247             }
    248             if (m.x > 240 && m.x < 360 && m.y>100 && m.y < 200)
    249             {
    250                 i--;
    251                 count--;
    252                 outtextxy(460 - i * 20, 20, _T(str));
    253                 outtextxy(460 - (i + 1) * 20, 20, _T(" "));     //按'<'键清除后一个,往后退;
    254             }
    255             if (m.x > 360 && m.x < 480 && m.y>100 && m.y < 200)
    256             {
    257                 ++i; count++;
    258                 str[i] = '+';
    259             }
    260             if (m.x > 0 && m.x < 120 && m.y>200 && m.y < 300)
    261             {
    262                 ++i; count++;
    263                 str[i] = '7';
    264             }
    265             if (m.x > 120 && m.x < 240 && m.y>200 && m.y < 300)
    266             {
    267                 ++i; count++;
    268                 str[i] = '8';
    269             }
    270             if (m.x > 240 && m.x < 360 && m.y>200 && m.y < 300)
    271             {
    272                 ++i; count++;
    273                 str[i] = '9';
    274             }
    275             if (m.x > 360 && m.x < 480 && m.y>200 && m.y < 300)
    276             {
    277                 ++i; count++;
    278                 str[i] = '-';
    279             }
    280             if (m.x > 0 && m.x < 120 && m.y>300 && m.y < 400)
    281             {
    282                 ++i; count++;
    283                 str[i] = '4';
    284             }
    285             if (m.x > 120 && m.x < 240 && m.y>300 && m.y < 400)
    286             {
    287                 ++i; count++;
    288                 str[i] = '5';
    289             }
    290             if (m.x > 240 && m.x < 360 && m.y>300 && m.y < 400)
    291             {
    292                 ++i; count++;
    293                 str[i] = '6';
    294             }
    295             if (m.x > 360 && m.x < 480 && m.y>300 && m.y < 400)
    296             {
    297                 ++i; count++;
    298                 str[i] = '*';
    299             }
    300             if (m.x > 0 && m.x < 120 && m.y>400 && m.y < 500)
    301             {
    302                 ++i; count++;
    303                 str[i] = '1';
    304             }
    305             if (m.x > 120 && m.x < 240 && m.y>400 && m.y < 500)
    306             {
    307                 ++i; count++;
    308                 str[i] = '2';
    309             }
    310             if (m.x > 240 && m.x < 360 && m.y>400 && m.y < 500)
    311             {
    312                 ++i; count++;
    313                 str[i] = '3';
    314             }
    315             if (m.x > 360 && m.x < 480 && m.y>400 && m.y < 500)
    316             {
    317                 ++i; count++;
    318                 str[i] = '/';
    319             }
    320             if (m.x > 0 && m.x < 120 && m.y>500 && m.y < 600)
    321             {
    322                 ++i; count++;
    323                 str[i] = '.';
    324             }
    325             if (m.x > 120 && m.x < 240 && m.y>500 && m.y < 600)
    326             {
    327                 ++i; count++;
    328                 str[i] = '0';
    329             }
    330             outtextxy(460 - i * 20, 20, _T(str));
    331             if (m.x > 240 && m.x < 360 && m.y>500 && m.y < 600)
    332             {
    333                 ++i; count++;
    334                 str[i] = 'C';
    335             }
    336             str[i + 1] = '';
    337             if (m.x > 360 && m.x < 480 && m.y>500 && m.y < 600)
    338             {
    339                 ++i; count++;
    340                 str[i] = '=';
    341                 str[i + 1] = '';
    342             }
    343 
    344             Sleep(100); // 延时,降低 CPU 占用率
    345             break;
    346         case WM_RBUTTONUP:
    347             go = 1;
    348             break;
    349         }
    350         if (str[i] == 'C' || str[i] == '=')
    351             break;
    352     } //一次输入结束
    353 
    354     if (str[i] == 'C')
    355     {
    356         setbkcolor(BLACK);
    357         cleardevice();
    358         calcul_memu();
    359         mouse_imfor();
    360     }//按'C'键清空界面;
    361     else if (str[i] == '=')
    362     {
    363         chuli(str, st);
    364         init(&sta);
    365         inffixtosuffix(&sta, st, suffix, &length);
    366         setbkcolor(BLACK);
    367         cleardevice();
    368         calcul_memu();
    369         init(&sta);
    370         suffixtoresult(&sta, suffix, length);
    371 
    372         go = 0;
    373         while (go == 0)
    374         {
    375             m = GetMouseMsg();
    376             switch (m.uMsg)
    377             {
    378             case WM_LBUTTONDOWN:
    379                 if (m.x > 240 && m.x < 360 && m.y>500 && m.y < 600)
    380                 {
    381                     str[i] = 'C';
    382                 }
    383                 str[i + 1] = '';
    384                 break;
    385             case WM_RBUTTONUP:
    386                 go = 1;
    387 
    388                 break;
    389             }
    390             if (str[i] == 'C')
    391                 break;
    392         }
    393         if (str[i] == 'C')
    394         {
    395             setbkcolor(BLACK);
    396             cleardevice();
    397             calcul_memu();
    398             mouse_imfor();
    399         }
    400     }
    401 
    402 }
    403 
    404 void clear_cal()
    405 {
    406     setbkcolor(BLACK);
    407     calcul_memu();
    408 }//初始化界面;
    409 
    410 //处理输入的算术表达式
    411 void chuli(char ch[], struct figure a[])
    412 {
    413     q = 0;
    414     int m = 0, j;
    415     char asd[15] = { '0' };
    416     while (m < count)
    417     {
    418         j = 0;
    419         if (ch[m] == '=')
    420             break;
    421         if (ch[m] == '(' || ch[m] == ')')
    422         {
    423             asd[j] = ch[m];
    424             asd[j + 1] = '';
    425             m++;
    426             strcpy_s(a[q].x, asd);
    427             q++;
    428             continue;
    429         }
    430         if (ch[m] == '+' || ch[m] == '-')
    431         {
    432             if (ch[m - 1] == '+' || ch[m - 1] == '-' || ch[m - 1] == '*' || ch[m - 1] == '/' || m == 0 || ch[m] == '(')
    433             {
    434                 asd[j] = ch[m];
    435                 m++;
    436                 j++;
    437                 while (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != ')')
    438                 {
    439                     asd[j] = ch[m];
    440                     j++;
    441                     m++;
    442                 }
    443                 asd[j] = '';
    444                 strcpy_s(a[q].x, asd);
    445                 q++;
    446             }
    447             else
    448             {
    449                 asd[j] = ch[m];
    450                 asd[j + 1] = '';
    451                 m++;
    452                 strcpy_s(a[q].x, asd);
    453                 q++;
    454             }
    455             continue;
    456         }
    457         if (ch[m] == '*' || ch[m] == '/')
    458         {
    459             asd[j] = ch[m];
    460             asd[j + 1] = '';
    461             m++;
    462             strcpy_s(a[q].x, asd);
    463             q++;
    464             continue;
    465         }
    466         if (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != '('&&ch[m] != ')')
    467         {
    468             while (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != '('&&ch[m] != ')'&&ch[m] != '=')
    469             {
    470                 asd[j] = ch[m];
    471                 j++;
    472                 m++;
    473             }
    474             asd[j] = '';
    475             strcpy_s(a[q].x, asd);
    476             q++;
    477             continue;
    478         }
    479     }
    480 }//将字符串分割出来;
    481 
    482 void init(stack *sta)
    483 {
    484     int i;
    485     for (i = 0; i < max; i++)
    486         strcpy_s(sta->data[i], "0");
    487 
    488     sta->top = -1;
    489 }//初始化栈;
    490 
    491 //中缀表达式变为后缀表达式
    492 void inffixtosuffix(stack *sta, struct figure *a, char suffix[][N], int *length)
    493 {
    494     int i = 0;
    495     int j = 0;
    496     for (i = 0; i < q;)
    497     {
    498         if (strcmp(a[i].x, "(") == 0)
    499         {
    500             sta->top++;
    501             strcpy_s(sta->data[sta->top], a[i].x);
    502             i++;
    503             continue;
    504         }
    505         else if (strcmp(a[i].x, ")") == 0)
    506         {
    507             while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1)
    508             {
    509                 strcpy_s(suffix[j], sta->data[sta->top]);
    510                 sta->top--;
    511                 j++;
    512             }
    513             strcpy_s(sta->data[sta->top], "0");
    514             i++;
    515             sta->top--;
    516             continue;
    517         }
    518         else if (strcmp(a[i].x, "=") == 0)
    519         {
    520             i++;
    521             continue;
    522         }
    523         else if (strcmp(a[i].x, "+") == 0 || strcmp(a[i].x, "-") == 0)
    524         {
    525             while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1)
    526             {
    527                 strcpy_s(suffix[j], sta->data[sta->top]);
    528                 sta->top--;
    529                 j++;
    530             }
    531             sta->top++;
    532             strcpy_s(sta->data[sta->top], a[i].x);
    533             i++;
    534             continue;
    535         }
    536         else if (strcmp(a[i].x, "*") == 0 || strcmp(a[i].x, "/") == 0)
    537         {
    538             while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1 && strcmp(sta->data[sta->top], "+") != 0 && strcmp(sta->data[sta->top], "-") != 0)
    539             {
    540                 strcpy_s(suffix[j], sta->data[sta->top]);
    541                 sta->top--;
    542                 j++;
    543             }
    544             sta->top++;
    545             strcpy_s(sta->data[sta->top], a[i].x);
    546             i++;
    547             continue;
    548         }
    549         else
    550         {
    551             strcpy_s(suffix[j], a[i].x);
    552             j++;
    553             i++;
    554             continue;
    555         }
    556     }
    557     while (sta->top != -1)
    558     {
    559         strcpy_s(suffix[j], sta->data[sta->top]);
    560         sta->top--;
    561         j++;
    562     }
    563     *length = j;
    564 
    565 }
    566 
    567 //计算出后缀表达式的值
    568 void suffixtoresult(stack *sta, char suffix[][N], int length)
    569 {
    570     int i = 0;
    571     double result = 0;
    572     while (i < length)
    573     {
    574         if (strcmp(suffix[i], "+") == 0)
    575         {
    576             printf("
    
    sta=%s
    
    ", sta->data[sta->top]);
    577             printf("
    
    sta=%s
    
    ", sta->data[sta->top - 1]);
    578 
    579             result = atof(sta->data[sta->top - 1]) + atof(sta->data[sta->top]);
    580             sta->top--;
    581             sprintf(sta->data[sta->top], "%.5lf", result);
    582             i++;
    583             continue;
    584         }
    585         else if (strcmp(suffix[i], "-") == 0)
    586         {
    587             result = atof(sta->data[sta->top - 1]) - atof(sta->data[sta->top]);
    588             sta->top--;
    589             sprintf(sta->data[sta->top], "%.8lf", result);
    590             i++;
    591             continue;
    592         }
    593         else if (strcmp(suffix[i], "*") == 0)
    594         {
    595             result = atof(sta->data[sta->top - 1])*atof(sta->data[sta->top]);
    596             sta->top--;
    597             sprintf(sta->data[sta->top], "%.8lf", result);
    598             i++;
    599             continue;
    600         }
    601         else if (strcmp(suffix[i], "/") == 0)
    602         {
    603             result = atof(sta->data[sta->top - 1]) / atof(sta->data[sta->top]);
    604             sta->top--;
    605             sprintf(sta->data[sta->top], "%.8lf", result);
    606             i++;
    607             continue;
    608         }
    609         else
    610         {
    611             sta->top++;
    612             strcpy_s(sta->data[sta->top], suffix[i]);
    613             i++;
    614             continue;
    615         }
    616     }
    617 
    618     setcolor(DARKGRAY);
    619     settextstyle(60, 20, _T("宋体"));
    620     if (sta->top == 0)
    621         outtextxy(480 - strlen(sta->data[sta->top]) * 20, 20, _T(sta->data[sta->top]));
    622     else if (sta->top != 0)
    623         outtextxy(190, 20, _T("ERROR"));
    624 }
    625 
    626 //画图菜单界面
    627 void image_menu()
    628 {
    629     int serial_number, select, marker = 0;
    630     printf("1.一次函数
    ");
    631     printf("2.二次函数
    ");
    632     printf("3.三角函数
    ");
    633     printf("4.幂函数
    ");
    634     printf("5.指数函数
    ");
    635     printf("6.对数函数
    ");
    636     printf("7.圆锥曲线
    ");
    637     printf("8.阿基米德螺线
    ");
    638     printf("9.心形线
    ");
    639     printf("10.玫瑰曲线
    ");
    640     printf("0.退出
    ");
    641     do
    642     {
    643         fflush(stdin);
    644         printf("Please input serial number:");
    645         scanf_s("%d", &serial_number);
    646 
    647 
    648     } while (serial_number < 0 && serial_number>10);
    649 
    650     switch (serial_number)
    651     {
    652     case 0:
    653         break;
    654     case 1:
    655         function1();
    656         marker = 1;
    657         break;
    658     case 2:
    659         function2();
    660         marker = 1;
    661         break;
    662     case 3:
    663         function3();
    664         marker = 1;
    665         break;
    666 
    667     case 4:
    668         function4();
    669         marker = 1;
    670         break;
    671     case 5:
    672         function5();
    673         marker = 1;
    674         break;
    675     case 6:
    676         function6();
    677         marker = 1;
    678         break;
    679     case 7:
    680         function7();
    681         marker = 1;
    682         break;
    683     case 8:
    684         function8();
    685         marker = 1;
    686         break;
    687     case 9:
    688         function9();
    689         marker = 1;
    690         break;
    691     case 10:
    692         function10();
    693         marker = 1;
    694         break;
    695     }
    696 
    697     if (marker == 1)
    698     {
    699         printf("是否继续画图?
    1.是  0.否");
    700         scanf_s("%d", &select);
    701         if (select == 1)
    702         {
    703             marker = 0;
    704             image_menu();
    705         }
    706     }
    707 }
    708 
    709 //建立坐标系
    710 void creatCoordinate()
    711 {
    712     //setbkcolor(WHITE);
    713     initgraph(600, 600);
    714     setorigin(300, 300);
    715 
    716     setlinecolor(RED);
    717     line(-300, 0, 300, 0);
    718     line(0, -300, 0, 300);
    719     settextcolor(RED);
    720     settextstyle(30, 20, _T("宋体"));
    721     outtextxy(280, 5, _T("x"));
    722     outtextxy(5, -300, _T("y"));
    723     setaspectratio(1, -1);
    724 }
    725 
    726 //function1~10是十个画图函数
    727 void function1()
    728 {
    729     float k, b, x, y;
    730     printf("Please input k and b(y=k*x+b) :");
    731     scanf_s("%f%f", &k, &b);
    732     fflush(stdin);
    733 
    734     float sin = k / sqrt(k*k + 1);
    735     float cos = 1 / sqrt(k*k + 1);
    736 
    737     creatCoordinate();
    738 
    739     for (double t = -300; t < 300; t = t + 0.001)
    740     {
    741         x = (int)(t * cos);
    742         y = (int)(t * sin + b);
    743         putpixel(x, y, RED);
    744     }
    745     getchar();
    746     getchar();
    747     closegraph();
    748 
    749 }
    750 
    751 void function2()
    752 {
    753     double x0, y0, a0;
    754     int x, y, k = 10;    //k为放大比例
    755     printf("请输入定点坐标X0,Y0:");
    756     scanf_s("%lf%lf", &x0, &y0);
    757     printf("
    请输入二次项系数:");
    758     scanf_s("%lf", &a0);
    759     fflush(stdin);
    760 
    761     creatCoordinate();
    762     for (double t = -300; t < 300; t = t + 0.001)
    763     {
    764         x = (int)(k*x0 + k * t);
    765         y = (int)(y0 + a0 * t*t);
    766         putpixel(x, y, RED);
    767     }
    768     getchar();
    769     getchar();
    770     closegraph();
    771 }
    772 
    773 
    774 void function3()
    775 {
    776     double a, b, m, n;
    777     int x, y, k = 10;
    778     printf("绘制三角函数图像(y=asin(mx)+bcos(nx))");
    779     printf("请输入a,b,m,n");
    780     scanf_s("%lf%lf%lf%lf", &a, &b, &m, &n);
    781     fflush(stdin);
    782     creatCoordinate();
    783 
    784     for (double i = -10 * PI; i < 10 * PI; i = i + 0.001)
    785     {
    786         y = (int)(k*a*sin(m*i) + k * b * cos(n*i));
    787         x = (int)k*i;
    788         putpixel(x, y, RED);
    789     }
    790 
    791     getchar();
    792     getchar();
    793     closegraph();
    794 }
    795 
    796 void function4()
    797 {
    798     double a;
    799     int x, y, k = 10;
    800     printf("绘制幂函数图像(y=Xa");
    801     printf("请输入a");
    802     scanf_s("%lf", &a);
    803     creatCoordinate();
    804 
    805     for (double i = -300; i < 300; i = i + 0.001)
    806     {
    807         x = (int)(k*i);
    808         y = (int)pow(i, a);
    809         putpixel(x, y, RED);
    810     }
    811 
    812     getchar();
    813     getchar();
    814     closegraph();
    815 }
    816 
    817 void function5()
    818 {
    819     double a;
    820     int x, y, k = 10;
    821     printf("绘制指数函数图像(y=aX(a>0且a!=1)");
    822     printf("请输入a");
    823     scanf_s("%lf", &a);
    824     fflush(stdin);
    825     creatCoordinate();
    826 
    827     for (double i = -300; i < 300; i = i + 0.001)
    828     {
    829         x = (int)(k*i);
    830         y = (int)(pow(a, i));
    831         putpixel(x, y, RED);
    832     }
    833 
    834     getchar();
    835     getchar();
    836     closegraph();
    837 }
    838 
    839 void function6()
    840 {
    841     double a, b, c, d;
    842     int x, y;
    843     printf("绘制对数函数图像(y=alogb(c+dx)");
    844     printf("请输入a,b,c,d(b>0)");
    845     scanf_s("%lf%lf%lf%lf", &a, &b, &c, &d);
    846     fflush(stdin);
    847     creatCoordinate();
    848 
    849     for (double i = -c / d + 0.1; i < 300; i = i + 0.001)
    850     {
    851         x = (int)i;
    852         y = (int)(a * log(c + d * i) / log(b));
    853         putpixel(x, y, RED);
    854     }
    855 
    856     getchar();
    857     getchar();
    858     closegraph();
    859 }
    860 void function7()
    861 {
    862 
    863     int x, y;
    864     double e, p;  //e为离心率,p为焦准距
    865     printf("请输入离心率e,焦准距p");
    866     scanf_s("%lf%lf", &e, &p);
    867     fflush(stdin);
    868     double k = 50;    //调节图形大小
    869 
    870     creatCoordinate();
    871     for (double i = 0; i < 2 * PI; i += 2 * PI / 3600)
    872     {
    873         x = k * e*p / (1 - e * cos(i))*cos(i);
    874         y = k * e*p / (1 - e * cos(i))*sin(i);
    875         putpixel(x, y, RED);
    876     }
    877 
    878     getchar();
    879     getchar();
    880     closegraph();
    881 }
    882 
    883 void function8()
    884 {
    885     creatCoordinate();
    886 
    887     int x, y, k = 10;
    888     int a = 1, b = 5;
    889     for (double i = 0; i < 6 * PI; i = i + 2 * PI / 360)
    890     {
    891         x = (int)k*(a + b * i)*cos(i);
    892         y = -(int)k*(a + b * i)*sin(i);
    893         putpixel(x, y, RED);
    894     }
    895     getchar();
    896     getchar();
    897     closegraph();
    898 }
    899 void function9()
    900 {
    901     creatCoordinate();
    902 
    903     int x, y;
    904     double a = 100;
    905     for (double i = 0; i < 2 * PI; i = i + 2 * PI / 720)
    906     {
    907         x = (int)a * (1 + sin(i))*cos(i);
    908         y = -(int)a * (1 + sin(i))*sin(i);
    909         putpixel(x, y, RED);
    910     }
    911     getchar();
    912     getchar();
    913     closegraph();
    914 }
    915 
    916 void function10()
    917 {
    918     creatCoordinate();
    919     int x, y, a = 200, k = 4;
    920 
    921     for (double i = 0; i < 4 * PI; i = i + 2 * PI / 3600)
    922     {
    923         x = (int)a * sin(PI* i)*cos(i);
    924         y = (int)a * sin(PI* i)*sin(i);
    925 
    926         putpixel(x, y, RED);
    927     }
    928     getchar();
    929     getchar();
    930     closegraph();
    931 }

    注意

    本人使用的是VS2017,上面的代码直接粘贴过去大概率跑不起来,需要做一点修改:

    1、若提示graphics.h不存在,需要先安装EasyX图形库

    2、报错“Lsta、Lstr: 为声明的标识符”,需要在项目属性中将字符集从Unicode改成多字节字符集

    3、sprintf等报错,需要在项目属性中关掉SDL检查

  • 相关阅读:
    ZOJ2402 Lenny's Lucky Lotto List 简单DP
    HDU1024 最大M子段和问题 (单调队列优化)
    HDU2048 HDU2049 组合数系列 错排
    HDU1081 最大字段和 压缩数组(单调队列优化)
    HDU1166 数状数组
    HDU1085 多重背包
    HDU3062
    递归 递推 规律
    【机器学习PAI实战】—— 玩转人工智能之美食推荐
    阿里开源自用 OpenJDK 版本,Java 社区迎来中国力量
  • 原文地址:https://www.cnblogs.com/lfri/p/9890029.html
Copyright © 2011-2022 走看看