| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 826 人关注过本帖
标题:刚学c语言,向高手求教如何达到程序要求(menu菜单问题)
只看楼主 加入收藏
linledi
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-10-31
收藏
 问题点数:0 回复次数:0 
刚学c语言,向高手求教如何达到程序要求(menu菜单问题)
要求实现一菜单,
当选择d,为输入地点名,输入时间,通过时间计算矿井深度,询问是否再次输入时间重新计算,返回计算的深度值,只有最后一次计算数据被存储(最多可以有100组输入数据可以存储在程序内存)

当选择s,提示输入要存储的文件名,若文件不存在,提示再次输入一次,输入后新建文件,若文件存在,则覆盖文件,存储的数组(parallel array)为地点名和计算的深度(地点名为string数组,深度为double数组)
当选择l,提示输入要读取的文件名,文件不存在,再次提示输入一次,若文件存在,则读取文件,并返回文件中pit的总数值
当选择p,则在屏幕显示当前缓存内的所有数据,并且按如下格式
        Pit Location          Pit Depth
          ele 01                -3.78
          ele 02                -6.78
只先显示15行数据,按下enter再显示余下数据,并显示pit的总数
只有当选择q的时候,才退出程序

且要求要有一module为逐行读取文件用fgets()函数,且移除the trailing '\n'(个人觉得应该包含在load里面)

下面是自己初步写的程序,不懂怎么改进了,希望各位大大帮忙指出错误以及如何完全实现要求,尤其是下划线部分

程序代码:
#include <stdio.h>
#include <stdbool.h>
#define SIZE 100
double Depth(double seconds2Bottom);
bool yesNo(const char prompt[]);
int getPitData(char location[SIZE], double depth[SIZE], double toBottom);
void save(char location[SIZE], double depth[SIZE], int numdata);
int load(char location[SIZE], double depth[SIZE], int numdata);
bool readline(int maxLen);
void print(char location[SIZE], double depth[SIZE], int numdata);

int main(void)
{
    /*  Declare and initialize variables.  */
    char selection, location[SIZE],str;
    const char prompt;
    double toBottom, depth[SIZE];
    int numdata, maxLen =100;
    while (selection != 'Q', 'q')
    {
        fflush(stdin);
        printf("\n\
               (D)epth: Enter pit location. Compute depth\n\
               (S)ave: Write pits date to file\n\
               (L)oad: Read mine shaft information from file\n\
               (P)rint: Display mine shafr information\n\
               (Q)uit: Exit program\n");
        printf("\nEnter Choice (D/S/L/P/Q):");
        selection = getchar();
        fflush(stdin);
        switch (selection)
        {
        case 'Q':
        case 'q':
            return 0;
            break;
        case 'D':
        case 'd':
            getPitData(location, depth ,toBottom);
            break;
        case 'S':
        case 's':
            save(location,depth,numdata);
            break;
        case 'L':
        case 'l':
            load(location,depth,numdata);
            break;
        case 'P':
        case 'p':
            print(location,depth,numdata);
            break;
        default:
            printf("Invalid selection. Please try again.\n");
        } /*end of switch*/
    } /*end of while*/
    return 0;
}
double Depth(double seconds2Bottom)
{
    /* Acceleration of gravity */
    const double G = 9.81;
    return -0.5 * G * seconds2Bottom * seconds2Bottom;
}
/* Display a question and returns true if the answer is 'y' or 'Y' */
bool yesNo(const char prompt[])
{
    char answer;
    printf("\n%s (Y/N)? ", prompt);
    answer = getchar();
    fflush(stdin);
    return (answer == 'y' || answer == 'Y');
}
int getPitData(char location[SIZE], double depth[SIZE], double toBottom)
{
    printf("\nEnter mine shaft location or name: ");
    scanf("%s", &location);

    do
    {

        printf("\nEnter time to bottom: ");
        scanf("%lf", &toBottom);
        fflush(stdin);
        printf("\nShaft depth is %.2f m\n", Depth(toBottom));
    }
    while ( yesNo("Do you want to take another measure of the same shaft"));
    return Depth(toBottom);
}

void save(char location[SIZE], double depth[SIZE], int numdata)
{
FILE *fp;
char filename[SIZE];
printf("Please enter the file name to save pitdata to: ");
scanf("%s", &filename);
fflush(stdin);
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("Error opening %s for writing \n", filename);
printf("Please enter the file name to save pitdata to: ");
scanf("%s", &filename);
fflush(stdin);
fp = fopen(filename, "w");
}
else
{
    for(numdata=0;numdata<SIZE;numdata++)
    {
    fwrite(stdin, sizeof(location),1,fp);
    }

}
fclose(fp);
}
bool readline(int maxLen)
{
    char filename[SIZE],str[maxLen];
    FILE *stream;
    stream = fopen(filename,"r+");
    if(!stream)
    return(false);
    else
    {
        while(fgets(str,maxLen,stream))
        printf("%s", str);
    }
    return(true);
}
int load(char location[SIZE], double depth[SIZE], int numdata)
{
    FILE *fp;
    char filename[SIZE],str;
    printf("\nEnter name of file to load pit data from: ");
    scanf("%s", &filename);
    fp = fopen(filename, "r");
    if(!fp)
    {
       printf("\nEnter name of file to load pit data from: ");
       scanf("%s", &filename);
       return;
    }
    else
    {
        if (readline(str))
        printf("%s\n", str);
        printf("wo");

    }
fclose(fp);
return numdata;
}
void print(char location[SIZE], double depth[SIZE], int numdata)
{
    printf("\nPit Location    Pit Depth\n");
    printf("%c %6.2f\n", &location, &depth);
    fflush(stdin);
}
搜索更多相关主题的帖子: c语言 menu 菜单 
2008-10-31 00:50
快速回复:刚学c语言,向高手求教如何达到程序要求(menu菜单问题)
数据加载中...
 
   



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

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