Chinaunix

标题: 新手学《The C Programming language》,自己做课后的习题 [打印本页]

作者: xiaowh00    时间: 2012-06-19 16:29
标题: 新手学《The C Programming language》,自己做课后的习题
本帖最后由 xiaowh00 于 2012-06-19 17:02 编辑

Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters
完成了第一个函数 把不可见的tab和换行转换成\t和\n,可以从键盘输入或者文件输入

#include <stdio.h>
#define MAX 10000
/* author: xiaowh00 */
/* date: 2012-6-19 */
/*
Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters
*/

void escape(char [],char []);
int main(int argc,char *argv[])
{
        FILE *fp;
        char s[MAX],t[MAX];
        int i,j,c;
        i=0;
        if(argc==1)
        {
                while((c=getchar())!=EOF)
                        t[i++]=c;
                t='\0';
                escape(s,t);
                printf("%s\n",s);
        }
        else
        {
                j=0;
                while(++j<argc)
                {
                        if((fp=fopen(argv[j],"r"))==NULL)
                                printf("Cant not open the file %s\n",argv[j]);
                        c=fgetc(fp);
                        while(c!=EOF)
                        {
                                t[i++]=c;
                                c=fgetc(fp);
                        }
                        t='\0';
                        escape(s,t);
                        printf("File %s result:>>>\n\n%s\n",argv[j],s);
                        fclose(fp);
                }
        }
}

void escape(char s[],char t[])
{
        int i,j;
        for(i=0,j=0;t!='\0';i++)
        {
                switch(t)
                {
                        case '\n': s[j++]='\\';s[j++]='n';break;
                        case '\t': s[j++]='\\';s[j++]='t';break;
                        default: s[j++]=t;break;
                }
        }
}
作者: xiaowh00    时间: 2012-06-19 17:01
第二个函数,将显示的\n和\t转换成实际的换行和tab

#include <stdio.h>
#define MAX 10000
/*
Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters
*/
/* author: xiaowh00 */
/* date: 2012-6-19 */

void epacse(char [],char []);
int main(int argc,char *argv[])
{
        FILE *fp;
        char s[MAX],t[MAX];
        int i,j,c;
        i=0;
        if(argc==1)
        {
                while((c=getchar())!=EOF)
                        t[i++]=c;
                t[i]='\0';
                epacse(s,t);
                printf("%s\n",s);
        }
        else
        {
                j=0;
                while(++j<argc)
                {
                        if((fp=fopen(argv[j],"r"))==NULL)
                                printf("Cant not open the file %s\n",argv[j]);
                        c=fgetc(fp);
                        while(c!=EOF)
                        {
                                t[i++]=c;
                                c=fgetc(fp);
                        }
                        t[i]='\0';
                        epacse(s,t);
                        printf("File %s result:>>>\n\n%s\n",argv[j],s);
                        fclose(fp);
                }
        }
}

void epacse(char s[],char t[])
{
        int i,j;
        for(i=0,j=0;t[i]!='\0';i++)
        {
                if(t[i]=='\\')
                {
                        switch(t[i+1])
                        {
                                case 'n': s[j++]='\n';i++;break;
                                case 't': s[j++]='\t';i++;break;
                                default: s[j++]=t[i];break;
                        }
                }
                else
                        s[j++]=t[i];
        }
}
作者: tempname2    时间: 2012-06-19 21:09
以后发代码发在代码记号里,虽然估计没什么人看…………

我也没细看,但是代码形状很好看。如果是刚开始学C语言的话,那真是开了个好头。
作者: xiaowh00    时间: 2012-06-20 08:26
恩,谢谢鼓励,请问有什么好的书推荐么,我现在在看《The C Programming language》




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2