大连普兰店楼盘:那位c语言的高手帮帮忙~!

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 17:05:47
哪个帮我分析以下下面这个程序:用链表做的`1我看不懂啊/?请你把每行后面加上注释好吗?
/* 1、字母转换
* 将字母的大小写转换一下,非字母字符原样输出。
* 要求:不得使用标准库中字母大小写转换函数。
* 样例:
* 一次性输入:
* I like ACM/ICPC.
* Hello World!
* +-* are operators.
* -1 ( 结束标记 )
* 输出:
* i LIKE acm/icpc.
* hELLO wORLD!
* +-* ARE OPERATORS.
*/

#include <stdio.h>
#include <alloc.h>

struct row /* 该结构用于存取输入的一行字符 */
{ char c;
struct row *next;
};

struct column /* 该结构用于处理行数目 */
{ struct row *rhead;
struct column *next;
}*chead = NULL;

int main( void );
int getinput( void );
void putout( void );
void freemem( void );

int main( void )
{
printf(" Please input string:\n\n");
if( getinput() )
printf(" Alloc memory error ! ");
printf(" The result string as follow:\n\n");
putout();
freemem();
getch();
return 0;
}

int getinput( void ) /* 获取输入,内存分配失败时返回 1 */
{ struct row *rtemp1 = NULL,*rtemp2 = NULL;
struct column *ctemp1 = NULL,*ctemp2 = NULL;
char ch;

if( !(chead = ctemp1 = (struct column *)malloc(sizeof(struct column))) )
return 1;
chead -> next = NULL;

while( 1 )
{ rtemp1 = (struct row *)malloc(sizeof(struct row));
rtemp1 -> next = NULL;
if( !rtemp1 )
return 1;
ctemp1 -> rhead = rtemp1;

while( (ch = getche())!= '\r' )
{ rtemp1 -> c = ch;
if( !(rtemp2 = (struct row *)malloc(sizeof(struct row))) )
return 1;
rtemp1 -> next = rtemp2;
rtemp1 = rtemp2;
}
rtemp2 -> next = NULL; /* 行链表的结束标记 */
if( ctemp1 -> rhead -> c == '-' &&
ctemp1 -> rhead -> next -> c == '1' )
{ printf("\n\n");
return 0; /* 输入了结束标记返回主函数 */
}
putchar('\n');
ctemp2 = (struct column *)malloc(sizeof(struct column));
ctemp2 -> next = NULL;
ctemp1 -> next = ctemp2;
ctemp1 = ctemp2;
}
}

void putout( void )
{ struct row *rtemp = NULL;
struct column *ctemp = NULL;

ctemp = chead;
while( ctemp -> next )
{ rtemp = ctemp -> rhead;
while( rtemp -> next ) /* 有一个节点没用 */
{ if( rtemp -> c >= 'a' && rtemp -> c <= 'z')
printf("%c",rtemp -> c - 32 );
else if( rtemp -> c >= 'A' && rtemp -> c <= 'Z' )
printf("%c",rtemp -> c + 32 );
else printf("%c",rtemp -> c);
rtemp = rtemp -> next;
}
putchar('\n');
ctemp = ctemp -> next;
}
}

void freemem( void )
{ struct row *rtemp1 = NULL,*rtemp2 = NULL;
struct column *ctemp1 = NULL,*ctemp2 = NULL;

ctemp1 = chead;
while( ctemp1 )
{ rtemp1 = ctemp1 -> rhead;
while( rtemp1 )
{ rtemp2 = rtemp1;
rtemp1 = rtemp1 -> next;
free( rtemp2 );
}
ctemp2 = ctemp1;
ctemp1 = ctemp1 -> next;
free( ctemp2 );
}
}