c语言的程序填空题?
1. 下列程序的功能是创建单向链表。 #include <stdio.h> #include <alloc.h> struct link { char name[10]; int mark; struct link * next; }; void insert(char *, int); struct link * head = NULL; main() { char name[10]; int mark; struct link * t; while ( 1 ) { scanf("%s %d", name, &mark); if ( strcmp(name, "#") == 0 ) break; ______(1)_______; } for (t=head; ______(2)_______) printf("<%s>: %d\n", t->name, t->mark); } void insert(char * name, int mark) { struct link * p; p = ______(3)_______ ; strcpy(p->name, name); p->mark = mark; ______(4)_______; if ( head != NULL ) ______(5)_______; head = p; } 2. prog.c是带命令行参数的程序。运行prog filename时,将文本文件filename中的内容原样输出;运行prog -u filename时,将文本文件filename中的所有字母以大写方式输出(其余字符原样输出);运行prog -d filename时,只将文本文件filename中的数字输出。 # include <stdio.h> # include <ctype.h> void main(int argc, char * argv[]) { FILE *fp; char * filename, ch; int tag=0; if (argc <2 || argc>3) { printf(“Argument error\n”); return; } if (argc == 3) { if (strcmp(argv[1], “-u”) == 0) tag=1; else if (strcmp(argv[1], “-d”) == 0) tag=2; else {printf(“Option error\n”); return;} ______(6)_______; } else filename = argv[1]; if (______(7)_______) { printf(“Cannot open file\n”); return; } while (______(8)_______ ) { ch = fgetc(fp); if ( tag == 0 ) putchar(ch); else if ( tag == 1 ) ______(9)_______; else if ( ______(10)_______) putchar(ch); } fclose(fp); }
#include #include #include struct link { char name[10]; int mark; struct link * next; }; void insert(char *, int); struct link *head=NULL; void main() { char name[10]; int mark; struct link * t; while (1) { scanf("%s %d", name, &mark); if(strcmp(name, "#") == 0 ) break; insert(name,mark); } for (t=head;t!=NULL;t=t->next) printf(": %d\n", t->name, t->mark); } void insert(char * name, int mark) { struct link *p; p=(struct link *)malloc(sizeof(struct link)); strcpy(p->name,name); p->mark=mark; p->next=NULL; if(head != NULL) p->next=head; head=p; } /***************************/ # include # include # include void main(int argc, char * argv[]) { FILE *fp; char * filename, ch; int tag=0; if (argc 3) { printf("Argument error\n"); return; } if (argc == 3) { if (strcmp(argv[1], "-u") == 0) tag=1; else if (strcmp(argv[1], "-d") == 0) tag=2; else {printf("Option error\n"); return;} filename=argv[2]; } else filename = argv[1]; if ((fp=fopen(filename,"r"))==NULL) { printf("Cannot open file\n"); return; } while (!feof(fp)) { ch = fgetc(fp); if ( tag == 0 ) putchar(ch); else if ( tag == 1 ) putchar(toupper(ch)); else if (ch>='0' &&ch<='9') putchar(ch); } fclose(fp); } 。
答案 1)insert(name,mark) 2)t!=NULL;t=t->next 3)new struct link 4)p->next=NULL 5)p->next=head 6)filename = argv[2] 7)(fp=fopen(filename, "r"))==NULL 8)!feof(fp) 9)putchar(toupper(ch)) 10)ch>='0' && ch<='9'
(1)insert(name,mark) (2)t!=NULL;t=t->next (3)new struct link (4)p->next=NULL (5)p->next=head (6)filename = argv[2] (7)(fp=fopen(filename, "r"))==NULL (8)!feof(fp) (9)putchar(toupper(ch)) (10)ch>='0' && ch<='9'
答:LZ的代码中,双重循环变量的初始值及终值条件有问题。 现修改如下,请参考: #include "stdio.h" main() {int a[20],i,j,n...详情>>
问:挺简单的一C函数,不过我是初学者,暂时还不会写,请好心人多多帮忙!
答:int mystrlen(char *str) { int n = 0; while (*str) { n++; str++;} return n; } voi...详情>>