查看: 941|回复: 4
|
关于user input storing in ARRAY。
[复制链接]
|
|
我写了一个简单的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。。
长辈们,可以教我吗?
谢谢。- #include<stdio.h>/*header function for input and output*/
- #include<stdlib.h>/*header function for standard library*/
- //prototype for all the function
- void DrawFirstAndLastDot(int length);
- void DrawDiamondDot(int i);
- void line(int length);
- void rectangle(int length,int height);
- void triangle(int height);
- void diamond(int height);
- float CalculateArea(float shape,float height,float length);
- int shape();
- int main(){
- //main function
- int i,x,y,z;
- float a,length,height;
- int p=25,q=24,cont_end;
- length = 0;
- height = 0;
- //declaration for every variables
- printf("please enter option 1-4\n");
- shape();
- while (cont_end=getchar())
- {
- printf("Enter Y to continue N to end the program:");
- scanf("%c", &cont_end);
- printf("\n");
- {if(cont_end=='N'||cont_end=='n')
- return 0;
- /*end if*/
- else if(cont_end=='Y'||cont_end=='y')
- return shape();
- /*back to the main and continue for the next input*/
- system("pause");
- }
- }
- }
- else
- printf("please the number 1 to proceed\n");
- system("pause");
- return main();
- }
- //funtion for line
- void line(int length)
- {
- int i=1;
- while(i<=length){
- printf("* ");
- i++;
- }
- }
- //function for the first * and the last *
- void DrawFirstAndLastDot(int length)
- {
- for(int i=1;i<=length;i++){
- if(i==1 || i==length){
- printf("* ");
- }else{
- printf(" ");
- }
- }
- printf("\n");
- }
- //function for rectangle
- void rectangle(int length,int height)
- {
- for(int i=1;i<=height;i++){
- if(i==1 || i==height){
- line(length);
- printf("\n");
- }else{
- DrawFirstAndLastDot(length);
- }
- }
- }
- //funtion for triangle
- void triangle(int height)
- {
- for(int length=1;length<=height;length++){
- if(length==height){
- line(length);
- printf("\n");
- }else{
- DrawFirstAndLastDot(length);
- }
- }
- }
- //function for drawing diamond
- void diamond(int height){
- for (int i = 1; i <= height; i++){
- for (int k = height; k > i; k--){
- printf(" ");
- }
- DrawDiamondDot(i);
- }
- for (int i = height-1; i > 0; i--){
- for (int k = height; k > i; k--){
- printf(" ");
- }
- DrawDiamondDot(i);
- }
- printf("\n");
- }
- //function for every * of diamond
- void DrawDiamondDot(int i){
- for (int j =1; j <= i; j++){
- if(j==1||j==i){
- printf("* ");
- }else{
- printf(" ");
- }
- }
- printf("\n");
- }
- //function for calculating the area of each shape
- float CalculateArea(float shape,float height,float length){
- float a;
- if(shape == 2){
- //Rectangle
- a = length * height;
- }else if(shape == 3){
- //Triangle
- length = height;
- a = length * height / 2;
- }else if(shape == 4){
- //Diamond
- length = height * 2;
- a = length * height;
- }
- return a;
- }
- int shape(){
- int shape,i,x,y,z,option;
- float a,length,height;
- int p=25,q=24,cont_end;
- length = 0;
- height = 0;
- printf("\nEnter your option:");
- scanf("%d", &shape);//choosing the option and proceed to (if)
- printf("\n");
- //single selection statement for user to input their desire number to create a shape
- switch(shape){
- case 1:
- {
- printf("Straight line\n");
- printf("Range of length 1-40\n");
- printf("Enter your length:");
- scanf("%f",&length);
- if(length<=0||length>40)//validation for user to input to some extend
- {
- printf("Invalid input value!");
- printf("\n");
- system("pause");
- return main();
- }
- printf("\n\n");
- }
- line(length);//output for line
- printf("\n");
- break;
- case 2:
- {
- printf("Rectangle\n");
- printf("Range of length and height 1-40\n");
- printf("Enter your length:");
- scanf("%f",&length);
- printf("Enter your height:");
- scanf("%f",&height);
- if(length<=0||height<=0||length>40||height>40)//validation for user to input to some extend
- {
- printf("Invalid input value!");
- printf("\n");
- system("pause");
- return main();
- }
-
- printf("\n\n");
- }
- rectangle(length, height);//output for retangle
- a = CalculateArea(shape,height,length);//call function for calculating area of rectangle
- printf("\n");
- printf("Area of Rectangle=%.0f\n",a);
- break;
- case 3:
- {
- printf("Triangle\n");
- printf("Range of height 1-40\n");
- printf("Enter your height:");
- scanf("%f", &height);
- if(height<=0||height>40)//validation for user to input to some extend
- {
- printf("Invalid input value!");
- printf("\n");
- system("pause");
- return main();
- }
- printf("\n\n");
-
- }
- triangle(height);//output for triangle
- a = CalculateArea(shape,height,length);//call function for calculating area of triangle
- printf("\n");
- printf("Area of Triangle=%.1f\n",a);
- break;
- case 4:
- {
- printf("Diamond\n");
- printf("Range of length 1-19\n");
- printf("Enter your height:");
- scanf("%f", &height);
- if(height<=0||height>=20)//validation for user to input to some extend
- {
- printf("Invalid input value!");
- printf("\n");
- system("pause");
- return main();
- }
- printf("\n\n");
- }
- diamond(height);//output for diamond
- a = CalculateArea(shape,height,length);//call function for calculating area of diamond
- printf("\n");
- printf("Area of Diamond=%.0f\n",a);
- break;
- default:
- printf("Invalid input value!\n");
- system("pause");
- return main();
- }
- printfan");
- }
-
复制代码 |
|
|
|
|
|
|
|
发表于 29-11-2012 08:40 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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 编辑
|
评分
-
查看全部评分
|
|
|
|
|
|
| |
本周最热论坛帖子
|