| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1516 人关注过本帖
标题:C语言,如何多维数组输出时,无值的地方显示空白?
取消只看楼主 加入收藏
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:11 
C语言,如何多维数组输出时,无值的地方显示空白?
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册



如两个附件里的图片所示,我希望能够写一个程序让他显示出来的时候如图所示,这是一个模拟的火车票预定系统其中一部分的节选,然后并且当用户选择定某一张车票后,对应地方的值要被减掉,然后输出后入图二所示。

具体题目是程序一开始:

用户被告知三个选项
1、购买车票:
2、查询余票:
2、退出

当用户选择1时:
{首先显示一个列表:

序号   站名
1      站1
2      站2
3      站3
.      .
.      .
.      .
然后进行一系列程序用户购买车票成功(略过因为这一部分没有问题)
返回主菜单。


然后如果用户输入:2
就显示表格(如附件1所示)
如果之前用户已经买过票,就显示进行过步骤一之后的表格(附件二所示)



我现在的问题就是不知道如何让结果显示的和图片一样。

我想用数组,就是赋值之后整体一行打印出来,但是如何让那些没有值的地方就空着,而且打印出来的结果不是进凑在一起的而是有间隔的啊???


(大一初学者,请大神帮帮忙喔)
搜索更多相关主题的帖子: 火车票预定 C语言 如图所示 图片 如何 
2015-10-20 14:15
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 3楼 TonyDeng
错了,是二维数组。。

然后不知道怎么写,,
2015-10-20 16:28
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 5楼 TonyDeng
大神版主,我才学一个星期多,所以没太明白您说的让我看看数据结构和数据文件是什么意思,数据结构和数据文件指的是。。。- -!!
2015-10-20 16:44
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 9楼 TonyDeng
//6515767 zy15767 AiLi
#include<stdio.h>
#include<stdlib.h>
#define trainsta 9
int main(void)
{
int a;
do
{
printf("1£©Book ticket(s):/n");
printf("2£©List available tickets:/n");
printf("3) Quit/n");
printf("Enter option:  /n");

scanf("%d",&a);
if (a==1)
{
    int b,c,d;
    char distance[trainsta][9]={0};
    distance[0][1]=155,distance[0][5]=380,
    distance[1][0]=155,distance[1][3]=210,distance[1][4]=180,distance[1][7]=180,distance[1][8]=280;
    distance[2][3]=95,distance[2][4]=90;
    distance[3][1]=210,distance[3][2]=95,distance[3][8]=130;
    distance[4][1]=180,distance[4][2]=90;
    distance[5][0]=380,distance[5][6]=610;
    distance[6][5]=610,distance[6][7]=235;
    distance[7][1]=180,distance[7][6]=235;
    distance[8][1]=280,distance[8][3]=130;

char tickets[trainsta][9]={-1};
    tickets[0][1]=10,tickets[0][5]=10,
    tickets[1][0]=10,tickets[1][3]=10,tickets[1][4]=10,tickets[1][7]=10,tickets[1][8]=10;
    tickets[2][3]=10,tickets[2][4]=10;
    tickets[3][1]=10,tickets[3][2]=10,tickets[3][8]=10;
    tickets[4][1]=10,tickets[4][2]=10;
    tickets[5][0]=10,tickets[5][6]=10;
    tickets[6][5]=10,tickets[6][7]=10;
    tickets[7][1]=10,tickets[7][6]=10;
    tickets[8][1]=10,tickets[8][3]=10;


    char stations[9][8]={"Ningbo","Hangzhou","Suzhou","Changzhou","Shanghai","Taizhou","Wenzhou","Jinhua","Nanjing""null"};
    printf("Num/tStation/n");
    int i;
    for(i=0;i<10;i++)
    {
        printf("%d/t%s/n",i+1,station[i]);
    }
    printf("Origin station: /n");
    scanf("%d",&b);
    if (b<1)
    {
        printf("Invalid station number./n");
    }
    else if (b>9)
    {printf("Invalid station number./n");
    }
    printf("Destination station number:/n");
    scanf("%d",&c);
     if (c<1)
    {
        printf("Invalid station number./n");
    }
    else if (c>9)
    {printf("Invalid station number./n");
    }
    else if (c==b)
    {
        printf("No train between %s and %s/n",stations[c-1],stations[c-1]);
    }
    else if(distance[b][c]==0)
    {
        printf("No train between %s and %s/n",stations[b-1],stations[c-1]);
    }
    printf("Number of tickets:  /n");
    scanf("%d",&d);
    if (d<0)
    {
        printf("Invalid number of tickets./n");
    }
    else if (d>tickets[b-1][c-1])
    {
        printf("Not enough tickets available (%d requested,%d remaining)./n",d,tickets[b-1][c-1]);
    }
    if(distance[b-1][c-1]>500)
    {
        printf("Tickets booked, total cost is %d RMB./n",300*d);
    }
    else if(distance[b-1][c-1]<500&&distance[b-1][c-1]>250)
    {
        printf("Tickets booked, total cost is %d RMB./n",200*d);
    }
2015-10-20 21:30
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 9楼 TonyDeng
大神版主,This is the program written by me.

my school's computer don't have the 中文输入法 so i only can reply you in english.

when i ran my program, there are many many error and warning

i have tried to fix some of them.

and this is the best and final version that i can do.


but there is still many errors, i totally have no idea about those.  ppppppplease    help me
2015-10-20 21:37
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
zhe ge ti de  wan zheng ban ben jiu shi zhege!!!!


Task

You have to write a program that will act as a ticket booking system for trains. The user will enter the origin and destination train stations then the system should work out if there is a direct train between the two stations. If there is, it should check to see if there are enough free seats left on that train. If there are, the seats should be reserved for that booking and should display the cost of the tickets. The program can also show the remaining tickets for each available route. The program will be driven by a simple menu system.

The stations and available trains are shown in the following table. The origin station is on the left and the destination station is on the top. If there is a number in a cell, that is the distance in km that the train has to travel. If there is no number then that means there is no train running on that route.

Ningbo    Hangzhou    Suzhou    Changzhou    Shanghai    Taizhou    Wenzhou    Jinhua    Nanjing
Ningbo        155                380            
Hangzhou    155            210    180            180    280
Suzhou                95    90               
Changzhou        210    95                        130
Shanghai        180    90                        
Taizhou    380                        610        
Wenzhou                        610        235   
Jinhua        180                    235        
Nanjing        280        130                    
Stations are numbered, from 1, in the order they appear in the rows of the table.

When the program starts, the user should be shown a menu of the available actions and prompted to enter one:
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option:
      
If the user enters option 1, the program should print a list of all the station numbers and their names, then print a prompt asking the user to enter the origin station number, then a prompt asking the user to enter the destination station number. The station list should have a header with Num and Station column names, separated with a tab. The rows should be the station number and then the full station name, separated with a tab. See the examples below for the exact formatting.

After prompting the user for the origin station, you should check the input is a number that appears in the list. If not, you should print the error message "Invalid station number." and go back to the main menu. The same check should happen after the destination station number has been entered. If there is no train from the origin to the destination then the program should display "No train between X and Y." where X is the origin station name and Y is the destination station name, and return to the main menu.

Next, the program should ask the user for the number of tickets required. You should check the input is greater than zero, otherwise display the error message "Invalid number of tickets." and go back to the main menu. After the required number of tickets has been validated, the program should check if at least that number of tickets is still available for that journey. When the program starts, all train routes have 10 tickets available each (your program should not make any attempt to remember how many tickets are available between runs; always start with 10 when the program runs). If there are not enough tickets left display the error message "Not enough tickets available (X requested, Y reminaing)." where X is the number of tickets the user entered and Y is the number of tickets actually available.

Finally, if there are enough tickets available the program should reduce the number of available tickets by the amount the user requested and print a message containing the cost of the tickets. The cost is calculated in the following manner — the cost of a single ticket is 300 RMB for a journey over 500km, 200 RMB for a journey over 250km, and 100 RMB otherwise; the total cost is the single ticket price multiplied by the number of tickets. The message displayed should be "Tickets booked, total cost is X RMB." where X is the total cost calculated above. After this the program should return to the main menu.

If the user enters option 2 at the main menu then the program should print a table showing the number of tickets remaining for each train route. The table should have the names of the stations along the top row, then each row showing the origin station at the beginning of the row and the appropriate number of remaining tickets of each destination station at each cell across the row. Cells should be tab-separated. If no route exists between two stations then nothing should be shown in the cell. In order to maintain the layout of the table, the station names are limited to 6 characters. If the station name is longer then it should be truncated to 6 characters. See the examples for a demonstration of the exact layout. After the table has been printed the program should return to the main menu.

If the user enters option 3 at the main menu then the program should exit with no further output.

If the user enters a different number at the main menu then the program should print "Invalid option!" and return to the main menu.

See the examples below for the rest of the specification of the required text and layout of the prompts and messages. Your program should look exactly the same as the examples for the values entered in them, including spacing, capitalisation, and punctuation.

Example input/output

Example 1:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
    Ningbo    Hangzh    Suzhou    Changz    Shangh    Taizho    Wenzho    Jinhua    Nanjin   
Ningbo        10                10               
Hangzh    10            10    10            10    10   
Suzhou                10    10                    
Changz        10    10                        10   
Shangh        10    10                           
Taizho    10                        10            
Wenzho                        10        10        
Jinhua        10                    10            
Nanjin        10        10                        
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num    Station
1    Ningbo
2    Hangzhou
3    Suzhou
4    Changzhou
5    Shanghai
6    Taizhou
7    Wenzhou
8    Jinhua
9    Nanjing
Origin station number: 3
Destination station number: 5
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
    Ningbo    Hangzh    Suzhou    Changz    Shangh    Taizho    Wenzho    Jinhua    Nanjin   
Ningbo        10                10               
Hangzh    10            10    10            10    10   
Suzhou                10    6                    
Changz        10    10                        10   
Shangh        10    10                           
Taizho    10                        10            
Wenzho                        10        10        
Jinhua        10                    10            
Nanjin        10        10                        
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$
Example 2:
[zlizpd3@cs-linux ex2]$ ./trainbooking
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 5
Invalid option!
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
    Ningbo    Hangzh    Suzhou    Changz    Shangh    Taizho    Wenzho    Jinhua    Nanjin   
Ningbo        10                10               
Hangzh    10            10    10            10    10   
Suzhou                10    10                    
Changz        10    10                        10   
Shangh        10    10                           
Taizho    10                        10            
Wenzho                        10        10        
Jinhua        10                    10            
Nanjin        10        10                        
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num    Station
1    Ningbo
2    Hangzhou
3    Suzhou
4    Changzhou
5    Shanghai
6    Taizhou
7    Wenzhou
8    Jinhua
9    Nanjing
Origin station number: 1
Destination station number: 1
No train between Ningbo and Ningbo.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num    Station
1    Ningbo
2    Hangzhou
3    Suzhou
4    Changzhou
5    Shanghai
6    Taizhou
7    Wenzhou
8    Jinhua
9    Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 12
Not enough tickets available (12 requested, 10 remaining).
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num    Station
1    Ningbo
2    Hangzhou
3    Suzhou
4    Changzhou
5    Shanghai
6    Taizhou
7    Wenzhou
8    Jinhua
9    Nanjing
Origin station number: 1
Destination station number: 2
Number of tickets: 4
Tickets booked, total cost is 400 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
    Ningbo    Hangzh    Suzhou    Changz    Shangh    Taizho    Wenzho    Jinhua    Nanjin   
Ningbo        6                10               
Hangzh    10            10    10            10    10   
Suzhou                10    10                    
Changz        10    10                        10   
Shangh        10    10                           
Taizho    10                        10            
Wenzho                        10        10        
Jinhua        10                    10            
Nanjin        10        10                        
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 1
Num    Station
1    Ningbo
2    Hangzhou
3    Suzhou
4    Changzhou
5    Shanghai
6    Taizhou
7    Wenzhou
8    Jinhua
9    Nanjing
Origin station number: 5
Destination station number: 3
Number of tickets: 1
Tickets booked, total cost is 100 RMB.
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 2
    Ningbo    Hangzh    Suzhou    Changz    Shangh    Taizho    Wenzho    Jinhua    Nanjin   
Ningbo        6                10               
Hangzh    10            10    10            10    10   
Suzhou                10    10                    
Changz        10    10                        10   
Shangh        10    9                           
Taizho    10                        10            
Wenzho                        10        10        
Jinhua        10                    10            
Nanjin        10        10                        
1) Book ticket(s)
2) List available tickets
3) Quit
Enter option: 3
[zlizpd3@cs-linux ex2]$
2015-10-20 21:40
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 13楼 TonyDeng
wo我明天要交作业le,,,,,zhe ge shi 我 men de 作业, 虽然 新的学期 has been started for a quite long time, but i was one of them who transferred out major(转换专业) in to computer science
2015-10-20 21:48
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 13楼 TonyDeng
这是我们的作业,da shen!!!看在我  所有的chinese都来自  百度翻译 的份上,please help me
2015-10-20 21:51
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 13楼 TonyDeng
我不期望它可以现在很完美的,我只是希望它  的错误   能解决  可以编译成功,,我可以知道how,以及为什么我犯了错误..
2015-10-20 21:56
eileenliai
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2015-10-20
收藏
得分:0 
回复 17楼 边小白
but c is very fundamental , so we have to learn this.
2015-10-20 21:57
快速回复:C语言,如何多维数组输出时,无值的地方显示空白?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023822 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved