佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 941|回复: 4

关于user input storing in ARRAY。

[复制链接]
发表于 29-11-2012 08:14 PM | 显示全部楼层 |阅读模式
我写了一个简单的program。
如果我要store,每次的user‘s input 去array ,然后最后要好像把它display出user 所input的value。
比如说,user 选 1, for 1: 就是user input for length=5; print straight line 5个asterik.
然后 user 可以重选. user 选3, for 3: 就是user input for length=5;print right triangle 5 asterik each side.
在user 要stop input 时, 我要做一个summary to display USER 刚刚input 了的shape。
for example:
User just choose
straight line
DISPLAY the length 5 straight line
triangle
Display the length 5 triangle.
我不会写这个code。。
长辈们,可以教我吗?
谢谢。
  1. #include<stdio.h>/*header function for input and output*/
  2. #include<stdlib.h>/*header function for standard library*/

  3. //prototype for all the function

  4. void DrawFirstAndLastDot(int length);
  5. void DrawDiamondDot(int i);
  6. void line(int length);
  7. void rectangle(int length,int height);
  8. void triangle(int height);
  9. void diamond(int height);
  10. float CalculateArea(float shape,float height,float length);
  11. int shape();

  12. int main(){
  13. //main function
  14.     int i,x,y,z;
  15.     float a,length,height;
  16.     int p=25,q=24,cont_end;
  17.     length = 0;
  18.     height = 0;
  19.     //declaration for every variables
  20.         printf("please enter option 1-4\n");
  21.         shape();
  22.         while (cont_end=getchar())
  23. {

  24.     printf("Enter Y to continue N to end the program:");
  25.     scanf("%c", &cont_end);
  26.     printf("\n");
  27.         {if(cont_end=='N'||cont_end=='n')
  28.             return 0;
  29.             /*end if*/
  30.         else if(cont_end=='Y'||cont_end=='y')
  31.             return shape();
  32.             /*back to the main and continue for the next input*/
  33.     system("pause");
  34.     }

  35. }
  36. }
  37. else
  38.     printf("please the number 1 to proceed\n");
  39.     system("pause");
  40.     return main();
  41. }
  42.     //funtion for line
  43.     void line(int length)
  44.     {
  45.         int i=1;
  46.         while(i<=length){
  47.             printf("* ");
  48.             i++;
  49.         }
  50.     }
  51.     //function for the first * and the last *
  52.     void DrawFirstAndLastDot(int length)
  53.     {
  54.         for(int i=1;i<=length;i++){
  55.             if(i==1 || i==length){
  56.                 printf("* ");
  57.             }else{
  58.                 printf("  ");
  59.         }
  60.     }
  61.     printf("\n");            
  62. }
  63.     //function for rectangle
  64.     void rectangle(int length,int height)
  65.     {
  66.         for(int i=1;i<=height;i++){
  67.             if(i==1 || i==height){
  68.                 line(length);
  69.                 printf("\n");
  70.             }else{
  71.                 DrawFirstAndLastDot(length);
  72.             }
  73.         }
  74.         }
  75.     //funtion for triangle
  76.     void triangle(int height)
  77.     {
  78.         for(int length=1;length<=height;length++){
  79.             if(length==height){
  80.                 line(length);
  81.                 printf("\n");
  82.             }else{
  83.                 DrawFirstAndLastDot(length);
  84.             }
  85.         }
  86.     }

  87.     //function for drawing diamond
  88.     void diamond(int height){
  89.         for (int i = 1; i <= height; i++){
  90.             for (int k = height; k > i; k--){
  91.                 printf("  ");
  92.             }
  93.             DrawDiamondDot(i);
  94.         }
  95.         for (int i = height-1; i > 0; i--){
  96.             for (int k = height; k > i; k--){
  97.                 printf("  ");
  98.             }
  99.             DrawDiamondDot(i);
  100.         }
  101.         printf("\n");
  102.     }
  103.     //function for every * of diamond
  104.     void DrawDiamondDot(int i){
  105.         for (int j =1; j <= i; j++){
  106.             if(j==1||j==i){
  107.                 printf("*   ");

  108.             }else{
  109.                 printf("    ");
  110.                  }
  111.         }
  112.         printf("\n");
  113.     }


  114.     //function for calculating the area of each shape
  115.     float CalculateArea(float shape,float height,float length){
  116.         float a;
  117.         if(shape == 2){
  118.             //Rectangle
  119.             a = length * height;
  120.         }else if(shape == 3){
  121.             //Triangle
  122.             length = height;
  123.             a = length * height / 2;
  124.         }else if(shape == 4){
  125.             //Diamond
  126.             length = height * 2;
  127.             a = length * height;
  128.         }
  129.         return a;
  130.     }

  131.     int shape(){
  132.         int shape,i,x,y,z,option;
  133.         float a,length,height;
  134.         int p=25,q=24,cont_end;
  135.         length = 0;
  136.         height = 0;
  137.         printf("\nEnter your option:");
  138.         scanf("%d", &shape);//choosing the option and proceed to (if)
  139.         printf("\n");
  140.         //single selection statement for user to input their desire number to create a shape

  141.         switch(shape){
  142.         case 1:
  143.             {
  144.         printf("Straight line\n");
  145.         printf("Range of length 1-40\n");
  146.         printf("Enter your length:");
  147.         scanf("%f",&length);
  148.         if(length<=0||length>40)//validation for user to input to some extend
  149.         {
  150.             printf("Invalid input value!");
  151.             printf("\n");
  152.             system("pause");
  153.             return main();
  154.         }
  155.         printf("\n\n");
  156.     }
  157.             line(length);//output for line
  158.             printf("\n");
  159.             break;
  160.         case 2:
  161.             {
  162.         printf("Rectangle\n");
  163.         printf("Range of length and height 1-40\n");
  164.         printf("Enter your length:");
  165.         scanf("%f",&length);
  166.         printf("Enter your height:");
  167.         scanf("%f",&height);
  168.         if(length<=0||height<=0||length>40||height>40)//validation for user to input to some extend
  169.         {
  170.             printf("Invalid input value!");
  171.             printf("\n");
  172.             system("pause");
  173.             return main();
  174.         }
  175.         
  176.         printf("\n\n");
  177.     }
  178.             rectangle(length, height);//output for retangle
  179.             a = CalculateArea(shape,height,length);//call function for calculating area of rectangle
  180.             printf("\n");
  181.             printf("Area of Rectangle=%.0f\n",a);
  182.             break;
  183.         case 3:
  184.             {
  185.         printf("Triangle\n");
  186.         printf("Range of height 1-40\n");
  187.         printf("Enter your height:");
  188.         scanf("%f", &height);
  189.         if(height<=0||height>40)//validation for user to input to some extend
  190.         {
  191.             printf("Invalid input value!");
  192.             printf("\n");
  193.             system("pause");
  194.             return main();
  195.         }
  196.         printf("\n\n");
  197.         
  198.     }
  199.             triangle(height);//output for triangle
  200.             a = CalculateArea(shape,height,length);//call function for calculating area of triangle
  201.             printf("\n");
  202.             printf("Area of Triangle=%.1f\n",a);
  203.             break;
  204.         case 4:
  205.             {
  206.         printf("Diamond\n");
  207.         printf("Range of length 1-19\n");
  208.         printf("Enter your height:");
  209.         scanf("%f", &height);

  210.         if(height<=0||height>=20)//validation for user to input to some extend
  211.         {
  212.             printf("Invalid input value!");
  213.             printf("\n");
  214.             system("pause");
  215.             return main();
  216.         }
  217.         printf("\n\n");
  218.     }
  219.             diamond(height);//output for diamond
  220.             a = CalculateArea(shape,height,length);//call function for calculating area of diamond
  221.             printf("\n");
  222.             printf("Area of Diamond=%.0f\n",a);
  223.             break;
  224.         default:
  225.             printf("Invalid input value!\n");
  226.             system("pause");
  227.             return main();
  228.         }
  229.         printfan");
  230.     }

  231.    
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 29-11-2012 08:40 PM | 显示全部楼层
这个code是你写的吗?
回复

使用道具 举报

发表于 29-11-2012 10:58 PM | 显示全部楼层
你的题目看起来有点乱,
是没有看明白,还是翻译不好吗?

建议先画简单的 flowchart
然后写大概的 pseudo code
最后在把它一行一行变成可执行的程式。
回复

使用道具 举报

 楼主| 发表于 30-11-2012 02:12 AM | 显示全部楼层
对, 是我写的。
你们copy code 去complier , 试一试。
然后你就明白了。
我想要的是,user input后可以知道user 本身build 了几多个shape。
然后当user input N/n, 就会有一个summary of user 刚刚build的shape。。
想问下,你们有什么idea吗?
回复

使用道具 举报

发表于 30-11-2012 07:15 AM | 显示全部楼层
line 42 to line 67

else
...
    printf ...
}


it does not belongs to any function ?
you don't have compile error ?

line 35,
return shape()
this will call shape function and exit ?


line 241, is this a valid function ?
printfan


本帖最后由 flashang 于 30-11-2012 07:23 AM 编辑

评分

参与人数 1人气 +3 收起 理由
cokedreamer + 3 我很赞同

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 9-10-2025 12:43 AM , Processed in 0.152030 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表