POINTERS ON C 读书日记
来源:互联网 发布:淘宝开店怎么卖虚拟 编辑:程序博客网 时间:2024/05/21 09:28
下定决心好好读一下,实现书里的全部代码
// ***************************************************************
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// the book solution:
// 写了一下只是想测一下关于指针和数组头元素的等价性
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// the book solution:
// 写了一下只是想测一下关于指针和数组头元素的等价性
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20
#define MAX_INPUT 1000
#define MAX_INPUT 1000
int read_column_numbers(int *column,int max);
void rearrange(char *output , char const *input ,int n_columns,int const *columns);
void rearrange(char *output , char const *input ,int n_columns,int const *columns);
int main()
{
int n_columns;
int columns[MAX_COLS];
char input[MAX_INPUT];
char output[MAX_INPUT];
{
int n_columns;
int columns[MAX_COLS];
char input[MAX_INPUT];
char output[MAX_INPUT];
n_columns=read_column_numbers(columns,MAX_COLS);
while (gets(input)!=NULL)
{
printf("Original input : %s/n",input);
rearrange(output,input,n_columns,columns);
printf("Rearrange line: %s/n",output);
}
{
printf("Original input : %s/n",input);
rearrange(output,input,n_columns,columns);
printf("Rearrange line: %s/n",output);
}
return EXIT_SUCCESS;
}
}
int read_column_numbers(int *columns,int max)
{
int num=0;
int ch;
{
int num=0;
int ch;
while (num<max && scanf("%d",&columns[num])==1 && columns[num]>=0)
{
num+=1;
}
{
num+=1;
}
if (num%2 !=0)
{
puts("Last column number is not paired.");
exit(EXIT_FAILURE);
}
{
puts("Last column number is not paired.");
exit(EXIT_FAILURE);
}
while ((ch=getchar())!=EOF && ch!='/n')
;
;
return num;
}
}
void rearrange(char *output,char const *input,int n_columns,int const *columns)
{
int col;
int output_col;
int len;
{
int col;
int output_col;
int len;
len=strlen(input);
output_col=0;
output_col=0;
for (col=0;col<n_columns;col+=2)
{
int nchars = columns[col+1]-columns[col]+1;
{
int nchars = columns[col+1]-columns[col]+1;
if (columns[col]>=len || output_col==MAX_INPUT-1)
{
break;
}
if (output_col+nchars>MAX_INPUT-1)
{
nchars=MAX_INPUT-output_col-1;
}
strncpy(output+output_col,input+columns[col],nchars);
output_col+=nchars;
}
{
break;
}
if (output_col+nchars>MAX_INPUT-1)
{
nchars=MAX_INPUT-output_col-1;
}
strncpy(output+output_col,input+columns[col],nchars);
output_col+=nchars;
}
output[output_col]='/0';
}
}
/*
input:
input:
4 9 12 20 -1
abcdefghijklmnopqrstuvwxyz
Hello there, how are you?
I am fine, thanks.
See you!
Bye
Hello there, how are you?
I am fine, thanks.
See you!
Bye
*/
// ***************************************************************
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// my solution:
// 还是没有避免老习惯,用了全局变量省去了麻烦的参数传递
// 现在彻底的该习惯,我不再是在写快速代码
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// my solution:
// 还是没有避免老习惯,用了全局变量省去了麻烦的参数传递
// 现在彻底的该习惯,我不再是在写快速代码
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
int pairs[20][2];
char source[1000];
char out[1000];
int n;
char source[1000];
char out[1000];
int n;
void run()
{
int len=strlen(source);
int out_index=0;
out[0]='/0';
for (int start=0;start<n;start++)
{
int ss=pairs[start][0];
int ee=pairs[start][1];
if(len>(ee-ss+1))
{
for (int j=0;j<ee-ss+1;j++)
{
out[out_index]=source[j+ss];
out_index++;
}
}
}
out[out_index]='/0';
}
{
int len=strlen(source);
int out_index=0;
out[0]='/0';
for (int start=0;start<n;start++)
{
int ss=pairs[start][0];
int ee=pairs[start][1];
if(len>(ee-ss+1))
{
for (int j=0;j<ee-ss+1;j++)
{
out[out_index]=source[j+ss];
out_index++;
}
}
}
out[out_index]='/0';
}
void print()
{
printf("%s/n",out);
}
{
printf("%s/n",out);
}
int main()
{
n=0;
while (1)
{
scanf("%d",&pairs[n][0]);
if (pairs[n][0]<0)
{
break;
}
scanf("%d",&pairs[n][1]);
n++;
}
getchar();
while (gets(source))
{
run();
print();
}
return 0;
}
{
n=0;
while (1)
{
scanf("%d",&pairs[n][0]);
if (pairs[n][0]<0)
{
break;
}
scanf("%d",&pairs[n][1]);
n++;
}
getchar();
while (gets(source))
{
run();
print();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// ***************************************************************
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// my local parameters version :
//
// int mat[3][10];
// 这里多为数组作为参数传递在调用的时候可以:
// func(mat);
//
// 在函数生命时候可以:
// void func(int (*mat)[10]);
// void func(int mat[][10]);
//
//
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
// ***************************************************************
// 1_1 version: 1.0 · date: 04/17/2008
// -------------------------------------------------------------
// my local parameters version :
//
// int mat[3][10];
// 这里多为数组作为参数传递在调用的时候可以:
// func(mat);
//
// 在函数生命时候可以:
// void func(int (*mat)[10]);
// void func(int mat[][10]);
//
//
// -------------------------------------------------------------
// Copyright (C) 2008 - All Rights Reserved
// ***************************************************************
//
// ***************************************************************
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
void run(char *source,char *out,int n,int pairs[][2])
{
int len=strlen(source);
int out_index=0;
out[0]='/0';
for (int start=0;start<n;start++)
{
int ss=pairs[start][0];
int ee=pairs[start][1];
if(len>(ee-ss+1))
{
for (int j=0;j<ee-ss+1;j++)
{
out[out_index]=source[j+ss];
out_index++;
}
}
}
out[out_index]='/0';
}
{
int len=strlen(source);
int out_index=0;
out[0]='/0';
for (int start=0;start<n;start++)
{
int ss=pairs[start][0];
int ee=pairs[start][1];
if(len>(ee-ss+1))
{
for (int j=0;j<ee-ss+1;j++)
{
out[out_index]=source[j+ss];
out_index++;
}
}
}
out[out_index]='/0';
}
void print(char * out)
{
printf("%s/n",out);
}
{
printf("%s/n",out);
}
int main()
{
int pairs[20][2];
char source[1000];
char out[1000];
int n;
n=0;
while (1)
{
scanf("%d",&pairs[n][0]);
if (pairs[n][0]<0)
{
break;
}
scanf("%d",&pairs[n][1]);
n++;
}
getchar();
while (gets(source))
{
run(source,out,n,pairs);
print(out);
}
return 0;
}
{
int pairs[20][2];
char source[1000];
char out[1000];
int n;
n=0;
while (1)
{
scanf("%d",&pairs[n][0]);
if (pairs[n][0]<0)
{
break;
}
scanf("%d",&pairs[n][1]);
n++;
}
getchar();
while (gets(source))
{
run(source,out,n,pairs);
print(out);
}
return 0;
}
- POINTERS ON C 读书日记
- 《Pointers on C》备忘
- Pointers ON C
- POINTERS ON C
- POINTERS ON C (一)
- POINTERS ON C (二)
- <Pointers on C> Chapter 1
- <Pointers on C> Chapter 2
- <Pointers on C> Chapter 4
- 【Pointers On C】struct 详解
- Pointers On C Chapter 9
- Pointers on C——6 Pointers.1
- Pointers on C——6 Pointers.2
- Pointers on C——6 Pointers.3
- Pointers on C——6 Pointers.4
- Pointers on C——6 Pointers.5
- Pointers on C——6 Pointers.6
- Pointers on C——6 Pointers.7
- webwork的property-driven 和model-driven的理解。
- 做一个负责的程序员
- 简历技巧
- 有感于新疆罗布泊发现巨型地下水库 库容量与三峡相当
- 贫困的定义
- POINTERS ON C 读书日记
- csdn 的10大美女!!!!!!!!!!!!
- 星际争霸2对战视频!!
- What Language Do I Use
- 怎样从声音来判断电脑故障
- java笔记
- Asp.Net之枚举类型输出需要类型转换
- From Java to Groovy in a few easy steps
- Groovy Tip 3 如何在if条件语句中判断对象为空