| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 527 人关注过本帖
标题:手里有份编程题 自己写出来了 但是有错纠不了~ 求个C语言高手帮解决下~ ...
只看楼主 加入收藏
xiamiqiang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-1-12
收藏
 问题点数:0 回复次数:3 
手里有份编程题 自己写出来了 但是有错纠不了~ 求个C语言高手帮解决下~ 新手初来乍到 谢谢帮忙~
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct        //定义一个结构体
{
    char song[100];
    char singer[10];
    int number;
    char special[10];
    char genre[10];
}KTVSong;

//定义一个静态的常量,记录songs数组中元素的个数,歌曲所在的位置可以用KTVsong里面的number控制。

int menu();                //声明函数
void choose();            //声明函数
void add();                //声明函数
void edt();                //声明函数
void del();                //声明函数
void req();                //声明函数
void bro(KTVSong);        //声明函数

int main()//主函数
{
    KTVSong songs[100];
    choose(menu());

    //用到bro的时候传songs进去

}

int menu()//菜单功能函数
{
    int n;
    printf("\npress any key to enter the menu");
    getch();
    system("cls");
    printf("*********************************************************************\n");
    printf("\t\t\tWELCOME TO KTV SYSTEM\n");
    printf("**********************************MENU*******************************\n");
    printf("\t\t\tINPUT 1 TO ADD     SONGS\n");
    printf("\t\t\tINPUT 2 TO EDIT    SONGS\n");
    printf("\t\t\tINPUT 3 TO DELETE  SONGS\n");
    printf("\t\t\tINPUT 4 TO REQUEST SONGS\n");
    printf("\t\t\tINPUT 5 TO BROWSE  SONGS\n");
    printf("\t\t\tINPUT 6 TO EXIT");
    printf("*********************************************************************\n");
    do
    {
        printf("\n\n\t\t\tENTER YOUR CHOICE(1-6):");
        scanf("%d",&n);
    }while(n < 1 || n > 6);
    return(n);
}

void choose(int i)//选择功能函数
{
    if(i == 1)
        add();
    else if(i == 2)
        edt();
    else if(i==3)
        del();
    else if(i==4)
        req();
    else if(i==5)
        bro();
    else
        exit(0);
}
void add()//添加功能函数
{
    int i = 0;
    char src_address[50] , tar_address[50] , ch;
    KTVSong songs[100];
    FILE *infile , *outfile;

    do
    {
        flushall();

        printf("\nPlease input the singer of the song:");

        gets(songs[i].singer);

        flushall();

        printf("\nPlease input the name of the song:");

        gets(songs[i].song);

        songs[i].number = i;

        flushall();

        printf("\nPlease input the special of the song:");

        gets(songs[i].special);

        flushall();

        printf("\nPlease input the genre of the song:");

        gets(songs[i].genre);

        flushall();

        printf("\nPlease input the address(the lyric of the song,for example D:\\songs\\hello.txt):");

        gets(src_address);

        flushall();

        printf("Please choose the address that your want to save the lyric(for example E:\\songs):");

        gets(tar_address);

        infile = fopen(src_address,"r");
        outfile = fopen(strcat(strcat(strcat(tar_address , "\\"), songs[i].song),".txt"),"w");

        if(!infile || !outfile)
        {
            printf("Save the lyric failed!");
            exit(1);
        }

        while(!feof(infile))
        {
            ch = fgetc(infile);

            if(ch == EOF)
            break;

            fputc(ch , outfile);
        }

        fclose(infile);
        fclose(outfile);
        printf("Lyric added successfully!\n");
        i++;
    }while(menu() == 1);
}

void edt()//编辑功能函数
{
    bro();
    //属性修改就行。
    //歌词,删掉重新添加一个。(可以把删除、添加歌词做成一个单独的函数);
}
void del()//删除功能函数
{
    char file[100];

    bro();

    printf("Please choose the song'number that your want to delete:");

    //删除,并重新调整songs数组,同时删掉对应的歌词。歌词路径通req()函数中的处理。

    if(remove(file) == 0)
        printf("Removed %s\n",file);
    else
        perror("remove");
}
void req()//点歌功能函数
{
    FILE *infile;

    bro(songs);//传songs进来

    printf("Please choose the song'number that your want to request:");//根据编号点歌,同理可以多些几个函数在这调用。

    //根据选择的条件,找到歌词文件的路径,解决办法:可以在上面的结构体中加一个属性,lyric_address[30]记录歌词存放的路径,方便查询和删除。

    infile = fopen(src_address,"r");
    if(!infile)
        {
            printf("Read the lyric failed!");
            exit(1);
        }

        while(!feof(infile))
        {
            ch = fgetc(infile);

            if(ch == EOF)
            break;

            printf("%s",ch);;
        }

        fclose(infile);
}
void bro(KTVSong) //浏览功能函数
{
    //编号 专辑 歌手 名称(按照编号。。。可以多些几个函数,方便分类查询)
    int i;
    printf("Please choose the condition:\n");
    printf("Query by singer to input 1\n");
    printf("Query by special to input 2\n");
    printf("Query all to input Enter\n");

    while(getch() != 1 && getch() != 2)
    {
        printf("Bad choosen!\n");
        printf("Input the choosen that you want:");
    }

    if(getch() == '\n')//可以单独写成函数,这里调用
    {
        for(i = 0 ; i < KTVSong.lenth ; i++) //KTVSong.lenth用上面定义那个静态常量代替
        {
            printf("*\t%s\t",songs[i].song);
            printf("%s\t",songs[i].singer);
            printf("%s\t",songs[i].number);
            printf("%s\t",songs[i].special);
            printf("%s\n",songs[i].genre);
        }
    }
    else if(getch() == 1) //可以单独写成函数,这里调用
    {
        char singer[30];
        printf("Please input the singer:");
        for(i = 0 ; (singer[i] = getch()) != '\n' ; i++);
        for(i = 0 ; i < KTVSong.lenth ; i++) //KTVSong.lenth用上面定义那个静态常量代替
        {
            if(songs[i].singer.equals(singer))
            {
                printf("*\t%s\t",songs[i].song);
                printf("%s\t",songs[i].number);
                printf("%s\t",songs[i].special);
                printf("%s\n",songs[i].genre);
            }
        }
    }
    else if(getch()==2) //可以单独写成函数,这里调用
    {
        char special[30];
        printf("Please input the special:");
        for(i = 0 ; (special[i] = getch()) != '\n' ; i++);
        for(i = 0 ; i < KTVSong.lenth ; i++)  //KTVSong.lenth用上面定义那个静态常量代替
        {
            if(songs[i].special.equals(special))
            {
                printf("*\t%s\t",songs[i].song);
                printf("%s\t",songs[i].singer);
                printf("%s\t",songs[i].number);
                printf("%s\n",songs[i].genre);
            }
        }
    }
}

搜索更多相关主题的帖子: 初来乍到 C语言 
2010-01-12 14:20
kooyou
Rank: 2
等 级:论坛游民
帖 子:13
专家分:28
注 册:2010-1-12
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct KTVSong
{
    char song[100];
    char singer[10];
    int number;
    char special[10];
    char genre[10];
}songs[100];


int menu();               
void choose();      
void add();            
void edt();            
void del();            
void req();               
void bro();

int main()
{
    choose(menu());


}

int menu()
{
    int n;
    printf("\npress any key to enter the menu");
    getch();
    printf("*********************************************************************\n");
    printf("\t\t\tWELCOME TO KTV SYSTEM\n");
    printf("**********************************MENU*******************************\n");
    printf("\t\t\tINPUT 1 TO ADD     SONGS\n");
    printf("\t\t\tINPUT 2 TO EDIT    SONGS\n");
    printf("\t\t\tINPUT 3 TO DELETE  SONGS\n");
    printf("\t\t\tINPUT 4 TO REQUEST SONGS\n");
    printf("\t\t\tINPUT 5 TO BROWSE  SONGS\n");
    printf("\t\t\tINPUT 6 TO EXIT");
    printf("*********************************************************************\n");
    do
    {
        printf("\n\n\t\t\tENTER YOUR CHOICE(1-6):");
        scanf("%d",&n);
    }
    while(n < 1 || n > 6);
    return(n);
}

void choose(int i)
{
    if(i==1)
        add();
    else if(i==2)
        edt();
    else if(i==3)
        del();
    else if(i==4)
        req();
    else if(i==5)
        bro();
    else
        exit(0);
}
void add()
{
    int i = 0;
    char src_address[50] , tar_address[50] , ch;
    FILE *infile , *outfile;

    do
    {
        flushall();

        printf("\nPlease input the singer of the song:");

        gets(songs[i].singer);

        flushall();

        printf("\nPlease input the name of the song:");

        gets(songs[i].song);

        songs[i].number = i;

        flushall();

        printf("\nPlease input the special of the song:");

        gets(songs[i].special);

        flushall();

        printf("\nPlease input the genre of the song:");

        gets(songs[i].genre);

        flushall();

        printf("\nPlease input the address(the lyric of the song,for example D:\\songs\\hello.txt):");

        gets(src_address);

        flushall();

        printf("Please choose the address that your want to save the lyric(for example E:\\songs):");

        gets(tar_address);

        infile=fopen(src_address,"r");
        outfile=fopen(strcat(strcat(strcat(tar_address , "\\"), songs[i].song),".txt"),"w");

        if(!infile || !outfile)
        {
            printf("Save the lyric failed!");
            exit(1);
        }

        while(!feof(infile))
        {
            ch= fgetc(infile);

            if(ch == EOF)
            break;

            fputc(ch , outfile);
        }

        fclose(infile);
        fclose(outfile);
        printf("Lyric added successfully!\n");
        i++;
    }
    while(menu() == 1);
}

void edt()
{
    bro();
  
}
void del()
{
    char file[100];

    bro();

    printf("Please choose the song'number that your want to delete:");


    if(remove(file) == 0)
        printf("Removed %s\n",file);
    else
        perror("remove");
}
void req()
{
    FILE *infile;
    char src_address[50],ch;
    bro();

    printf("Please choose the song'number that your want to request:");
    gets(src_address);
    infile = fopen(src_address,"r");
    if(!infile)
        {
            printf("Read the lyric failed!");
            exit(1);
        }

        while(!feof(infile))
        {
            ch=fgetc(infile);

            if(ch == EOF)
            break;

            printf("%s",ch);;
        }

        fclose(infile);
}
void bro()
{
  
    int i;
    printf("Please choose the condition:\n");
    printf("Query by singer to input 1\n");
    printf("Query by special to input 2\n");
    printf("Query all to input Enter\n");

    while(getch() != 1 && getch() != 2)
    {
        printf("Bad choosen!\n");
        printf("Input the choosen that you want:");
    }

    if(getch()== '\n')
        for(i=0;i<strlen(songs);i++)
        {
            printf("*\t%s\t",songs[i].song);
            printf("%s\t",songs[i].singer);
            printf("%s\t",songs[i].number);
            printf("%s\t",songs[i].special);
            printf("%s\n",songs[i].genre);
        }
    else if(getch()==1)
    {
        char singer[30];
        printf("Please input the singer:");
        for(i = 0 ; (singer[i] = getch()) != '\n' ; i++);
        for(i = 0 ; i < strlen(songs) ; i++)
        {
            if(songs[i].singer==songs[i].singer)
            {
                printf("*\t%s\t",songs[i].song);
                printf("%s\t",songs[i].number);
                printf("%s\t",songs[i].special);
                printf("%s\n",songs[i].genre);
            }
        }
    }
    else if(getch()==2)
    {
        char special[30];
        printf("Please input the special:");
        for(i = 0 ; (special[i] = getch()) != '\n' ; i++);
        for(i = 0 ; i < strlen(songs) ; i++)
            if(songs[i].special==songs[i].singer)
            {
                printf("*\t%s\t",songs[i].song);
                printf("%s\t",songs[i].singer);
                printf("%s\t",songs[i].number);
                printf("%s\n",songs[i].genre);
            }
        }

}




运行没错,不过就发现功能有点混乱~希望对你有用~新手,试试的~
2010-01-12 19:58
xiamiqiang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-1-12
收藏
得分:0 
回复 2楼 kooyou
嗯~  麻烦您了   谢谢帮忙哈
2010-01-13 10:09
kooyou
Rank: 2
等 级:论坛游民
帖 子:13
专家分:28
注 册:2010-1-12
收藏
得分:0 
可以结贴吗?
2010-01-13 13:28
快速回复:手里有份编程题 自己写出来了 但是有错纠不了~ 求个C语言高手帮解决 ...
数据加载中...
 
   



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

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