免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1269 | 回复: 3
打印 上一主题 下一主题

新手学《The C Programming language》,自己做课后的习题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-19 16:29 |只看该作者 |倒序浏览
本帖最后由 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;
                }
        }
}

论坛徽章:
0
2 [报告]
发表于 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];
        }
}

论坛徽章:
2
CU十二周年纪念徽章
日期:2013-10-24 15:41:34处女座
日期:2013-12-27 22:22:41
3 [报告]
发表于 2012-06-19 21:09 |只看该作者
以后发代码发在代码记号里,虽然估计没什么人看…………

我也没细看,但是代码形状很好看。如果是刚开始学C语言的话,那真是开了个好头。

论坛徽章:
0
4 [报告]
发表于 2012-06-20 08:26 |只看该作者
恩,谢谢鼓励,请问有什么好的书推荐么,我现在在看《The C Programming language》
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP