版本1和30间的区别 (跳过第29版)
于2006-03-12 15:45:24修订的的版本1
大小: 483
编辑: czk
备注:
于2006-06-09 09:34:27修订的的版本30
大小: 17486
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
= 实验要求 = [[TableOfContents]]
= 实验基本要求 =
行号 7: 行号 8:
= 实验1 =
= 实验运行环境简介 =

== DEV-C++集成开发环境 ==
安装运行
 1. 下载安装程序: http://www.bloodshed.net/dev/devcpp.html
 1. 运行安装程序进行安装
 1. 运行集成开发环境:开始菜单=〉Bloodshed Dev-C++ =〉Dev-C++
工程创建
 1. 在Dev-C++菜单中选择File=>New=>Project
 1. 在弹出对话框中选择Console Application,选中C Project,Name框输入一个工程名字,最后按OK按钮
 1. 选择硬盘上某个路径保存你所建立的工程
程序运行
 1. 选择菜单Execute=>Compile编译程序
 1. 如果编译没有错误,选择Execute=>Run运行程序,得到运行结果
如果程序结果一闪而过,怎么办?
 * 在命令行窗口中运行可执行程序
 * 在代码中添加:system("PAUSE");一行使程序暂停(system函数在stdlib.h中定义)
调试程序
 * 启动调试
     选择菜单Debug=>Debug
 * 设置断点
     将光标移动到需要设置断点的行,选择Debug=>Toggle Breakpoint
 * 单步执行
     菜单Debug=>Next Step和Step Into
 * 查看变量
     菜单Debug=>Add Watch

== Visual Studio.Net 2003集成开发环境 ==
创建工程
 1. 打开Visual Studio.net 2003集成开发环境
 1. 菜单File=>New=>Project
 1. 在弹出的对话框中:Project Types选择Visual C++ Projects,Templates选择Win32 Console Application,Name中输入工程名称,Location选择保存的路径。最后点OK
 1. 在接下来的工程向导中,选择Application Settings,选择Empty Project,最后按Finish
 1. 菜单View=>Solution Explorer
 1. 在Solution Explorer中选中刚才建立的工程
 1. 菜单File=>Add New Item
 1. 在弹出的对话框中:Categories选择Visual C++,Templates选择C++ File,Name中输入文件名,文件要以.c结尾,最后按Open。

编译运行
 1. 菜单Build=>Build Solution
 1. 如果编译通过,选择菜单Debug=>Start Without Debugging

调试
 * 开始调试:菜单Debug=>Start
 * 设置断点:光标移动到要设置断点的地方,按F9
 * 单步执行:Debug菜单下的Step Into和Step Over
 * 查看变量:Debug菜单下的Quick Watch

= 实验1-1 =

== 实验目的 ==
熟悉C语言开发环境,学会
 * 创建工程
 * 编辑代码
 * 编译连接,生成可执行程序
 * 调试代码

== 实验内容 ==
编译运行以下程序,观察程序运行的结果
完成课后作业1-1

{{{#!cplusplus
#include <stdio.h>
main()
{
        printf("hello, world\n");
}
}}}

{{{#!cplusplus
#include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
main ()
{
    int fahr, celsius;
    int lower, upper, step;
    lower = 0;
    upper = 300;
    step = 20;
    fahr = lower;
    while ( fahr <= upper) {
        celsius = 5 * (fahr - 32) / 9;
        printf( "%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}
}}}
{{{#!cplusplus
#include <stdio.h>
/* print Fahrenheit-Celsius table*/
main()
{
    int fahr;

    for (fahr = 0; fahr <= 300; fahr = fahr + 20)
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
}}}

{{{1-1 Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.
}}}

= 实验1-2 =
== 实验目的 ==
 * 掌握C语言变量、循环的基本概念
 * 掌握基本的算术运算

== 实验内容 ==
完成课后作业1-4,1-5
 * Write a program to print the corresponding Celsius to Fahrenheit table.
 * Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.

= 实验2-1 =
== 实验目的 ==
行号 20: 行号 133:
== 参考程序 ==
{{{#!cplusplus
void strinsert(char *s, int i, char *t) {
   int tlen = strlen(t);
   int slen = strlen(s);
   int j;

   for(j = tlen; j >= i; j--)
      s[j+tlen] = s[j];
   for(j = 0; j < tlen; j++)
      s[j+i] = t[j];
}
}}}

= 实验2-2 =
== 实验目的 ==
== 实验内容 ==
写两个函数
 * strcrop(s, i, j) 取字符串s中第i个到第j个字符
 * strdel(s, i, j) 将字符串s中第i个到第j个字符删除
for example:
{{{#!cplusplus
char s[20]="hello world";
char t[20]="hello world";
strcrop(s, 1, 8);
strdel(t, 1, 8);
printf("%s\n%s\n", s, t);
}}}
output:
{{{
ello wor
hld
}}}

== 参考程序 ==
{{{#!cplusplus
void strcrop(char *s, int i, int j) {
    int k;
    for(k = i; k <=j; k++)
        s[k-i] = s[k];
    s[k] = '\0';
}
void strdel(char *s, int i, int j) {
    j++;
    while(s[j] != '\0')
        s[i++] = s[j++]
    s[i] = '\0';
}
}}}

= 实验2-3 =
== 实验目的 ==
熟悉指针数组的使用
== 实验内容 ==
写一个函数weekday(y,m,d),求取y年m月d日是星期几,返回星期几的名字。例如:
{{{#!cplusplus
printf("%s", weekday(2006,3,6));
}}}输出
{{{Monday
}}}

= 实验2-4 =
== 实验目的 ==
掌握多维数组的使用
== 实验内容 ==
写一个函数{{{void inverse(int matrix[N][N])}}},将矩阵matrix转置。比如
{{{#!cplusplus
#define N 3
int m[N][N] = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9}
};
inverse(m);
}}}得到结果:
{{{
m =
1 4 7
2 5 8
3 6 9
}}}
== 参考程序 ==
{{{#!cplusplus
void inverse(int matrix[N][N]) {
    int i, j;
    for(i = 0; i < N; i++) {
        for(j = 0; j < i; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
}
}}}

= 实验2-5 =
== 实验目的 ==
掌握命令行参数的获取和使用

== 实验内容 ==

修改1-20题的程序,使其可以使用命令行参数来指定tab终止位的宽度。比如
{{{
detab -10
}}}
指定tab终止位为每10个字符一个。(这里,detab为程序的名字,-10为参数。)

= 实验2-6 =
== 实验目的 ==
练习函数指针的使用方法

== 实验内容 ==
写一个函数strfilter,删除字符串中的满足一定条件的字符
{{{#!cplusplus
void strfilter(char s[], int (*fun)(int) );
int filter (int c){
   return !islower(c) && !isuper(c);
}
main() {
   char str1[]="Hello, world";
   char str2[]="Hello, world";
   char str3[]="Hello, world";
   strfilter(str1, isupper);/*ello, world*/
   strfilter(str2, islower);/*H, */
   strfilter(str3, filter);/*Helloworld*/
   printf("%s\n%s\n%s\n", str1, str2, str3);
}
}}}
== 参考程序 ==
{{{#!cplusplus
void strfilter(char *s, int (*fun)(int c)) {
    char *t = s;
    while(*s !='\0') {
        if(!(*fun)(c)) {
            *t = *s;
            t++;
        }
        s++;
    }
}
}}}

= 实验2-7 =

== 实验目的 ==
练习结构体的简单使用

== 实验内容 ==
 * write a function to calculate distance between two points.
 * write a function to calculate the area of a rect.

== 参考程序 ==
{{{#!cplusplus
struct point {
    int x, y;
};
struct rect {
    struct point p1, p2;
};
double distance(struct point p1, struct point p2) {
    return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
int area(struct rect r) {
    return (r.p1.x - r.p2.x) * (r.p1.y - r.p2.y);
}
}}}

= 实验2-8 =
== 实验目的 ==

掌握结构体的进一步使用

== 实验内容 ==

Write a program that prints the distinct words in its input sorted into increasing order of frequency of occurrence. The words with frequency lower than 3 are omitted. Make 3 a parameter that can be set from the command line.

== 参考程序 ==

{{{#!cplusplus
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define MAXWORD 100
#define MAX 1000

struct key {
   char word[MAXWORD];
   int count;
} keytab[MAX];
int size = 0;

int getword(char *, int);
int binsearch(char *, struct key *, int);
int comp(struct key w1, struct key w2);
void qsort2(struct key v[], int left, int right,
          int (*comp)(struct key, struct key));
void swap(struct key v[], int i, int j);

/* count C keywords */
main(int argc, char *argv[])
{
    int n;
    char word[MAXWORD];
    
    int least_count = 3;
   
    /*read parameter from command line*/
    if(argc>1) {
        least_count = atoi(argv[1]);
    }

    /*read and count words*/
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((n = binsearch(word, keytab, size)) >= 0)
                keytab[n].count++;
            else {
                strcpy(keytab[size].word, word);
                keytab[size].count = 1;
                size++;
            }
    /*sort*/
    qsort2(keytab, 0, size-1, &comp);

    /*output*/
    for (n = 0; n < size; n++)
       if (keytab[n].count >=least_count )
           printf("%4d %s\n", keytab[n].count, keytab[n].word);
    system("pause");
    return 0;
}

/* qsort: sort v[left]...v[right] into increasing order */
void qsort2(struct key v[], int left, int right,
          int (*comp)(struct key, struct key))
{
   int i, last;

   if (left >= right) /* do nothing if array contains */
       return; /* fewer than two elements */
   swap(v, left, (left + right)/2);
   last = left;
   for (i = left+1; i <= right; i++)
       if ((*comp)(v[i], v[left]) < 0)
           swap(v, ++last, i);
   swap(v, left, last);
   qsort2(v, left, last-1, comp);
   qsort2(v, last+1, right, comp);
}

int comp(struct key w1, struct key w2) {
    return w1.count - w2.count;
}

void swap(struct key v[], int i, int j) {
    struct key temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

/* binsearch: find word in tab[0]...tab[n-1] */
int binsearch(char *word, struct key tab[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        if( strcmp( word, tab[i].word)==0)
            return i;
    return -1;
}

#define BUFSIZE 100

char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
      return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */ {
      if (bufp >= BUFSIZE)
            printf("ungetch: too many characters\n");
      else
            buf[bufp++] = c;
}

/* getword: get next word or character from input */
int getword(char *word, int lim)
{
   int c;
   char *w = word;

   while (isspace(c = getch()))
       ;
   if (c != EOF)
       *w++ = c;
   if (!isalpha(c)) {
       *w = '\0';
       return c;
   }
   for ( ; --lim > 0; w++)
       if (!isalnum(*w = getch())) {
           ungetch(*w);
           break;
       }
   *w = '\0';
   return word[0];
}

}}}


= 实验2-9 =

== 实验目的 ==

掌握结构体数组的使用

== 实验内容 ==

按要求完成ZJU Online Judge第2104题:http://acm.zju.edu.cn/show_problem.php?pid=2104


== 参考程序 ==
{{{#!cplusplus
#include <stdio.h>

struct balloon {
    char color[16];
    int count;
};

int main() {
    while(1) {
        int n, i;
        int found;
        struct balloon balloons[1000];
        int size = 0;
        char color[16];
        int max, max_i;
         
        getint(&n);
        if(n==0) break;
        for(i = 0; i < n; i++) {
            getword(color, 16);
            if((found = search(color, balloons, size)) >=0)
                balloons[found].count++;
            else {
                strcpy(balloons[size].color, color);
                balloons[size].count = 1;
                size++;
            }
        }
        
        max_i = -1;
        max = 0;
        for(i = 0; i < size; i++) {
            if(balloons[i].count > max) {
                max = balloons[i].count;
                max_i = i;
            }
        }
        printf("%s\n", balloons[max_i].color);
    }
    return 0;
}

/* search: find word in tab[0]...tab[n-1] */
int search(char *word, struct balloon tab[], int n)
{
    int i;
    for(i = 0; i < n; i++)
        if(strcmp(tab[i].color, word)==0)
            return i;
    return -1;
}

#define BUFSIZE 100

char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
      return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */ {
      if (bufp >= BUFSIZE)
            printf("ungetch: too many characters\n");
      else
            buf[bufp++] = c;
}

/* getword: get next word or character from input */
int getword(char *word, int lim)
{
   int c;
   char *w = word;

   while (isspace(c = getch()))
       ;
   if (c != EOF)
       *w++ = c;
   for ( ; --lim > 0; w++)
       if (isspace(*w = getch())) {
           ungetch(*w);
           break;
       }
   *w = '\0';
   return word[0];
}


int getint(int *pn)
{
   int c, sign;

   while (isspace(c = getch())) /* skip white space */
       ;
   if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
       ungetch(c); /* it is not a number */
       return 0;
   }
   sign = (c == '-') ? -1 : 1;
   if (c == '+' || c == '-')
       c = getch();
   for (*pn = 0; isdigit(c); c = getch())
       *pn = 10 * *pn + (c - '0');
   *pn *= sign;
   if (c != EOF)
       ungetch(c);
   return c;
}

}}}


= 实验2-10 =

== 实验目的 ==

掌握自引用结构体的使用

== 实验内容 ==

统计输入中不同单词的个数。使用链表实现。


== 参考程序 ==
{{{#!cplusplus
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXWORD 1000
struct node {
    char *word;
    int count;
    struct node *next;
};
struct node*addlist(struct node *head, char *w);
char *strdup(char *s);
void listprint(struct node *p);

main() {
    struct node *head = NULL;
    char word[MAXWORD];
    while(getword(word, MAXWORD)!=EOF)
        if(isalpha(word[0]))
            head = addlist(head, word);
    listprint(head);
    system("pause");
}

void listprint(struct node *p) {
    while(p!=NULL) {
        printf("%4d %s\n", p->count, p->word);
        p = p->next;
    }
}

struct node*addlist(struct node *head, char *w){
    int cond;
    struct node *p = head, *pre = NULL;
    while(p!=NULL) {
        cond=strcmp(w,p->word);
        if(cond == 0) {
            p->count ++;
            return head;
        } else if(cond > 0) {
            pre = p; p = p->next;
        } else
            break;
    }
    p =(struct node*)malloc(sizeof(struct node));
    p->word = strdup(w);
    p->count = 1;
    if(pre == NULL) {
        p->next = NULL;
        head = p;
    } else {
        p->next = pre->next;
        pre->next = p;
    }
    return head;
}

char *strdup(char *s) {
    char *p = (char *)malloc(strlen(s)+1);
    strcpy(p, s);
    return p;
}

#define BUFSIZE 100

char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */ {
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

int getword(char *word, int lim)
{
    int c;
    char *w = word;

    while (isspace(c = getch()))
        ;
    if (c != EOF)
        *w++ = c;
    if (!isalpha(c)) {
        *w = '\0';
        return c;
    }
    for ( ; --lim > 0; w++)
        if (!isalnum(*w = getch())) {
            ungetch(*w);
            break;
        }
    *w = '\0';
    return word[0];
}

}}}

= 实验2-11 =

== 实验目的 ==

掌握C语言的输入输出

== 实验内容 ==

Exercise 7-1. Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0].

== 参考程序 ==

{{{#!cplusplus
#include <stdio.h>
#include <ctype.h>
main(int argc, char * argv[]) {
    int c;
    int (*convert)(int);
    
    convert = (strstr(argv[0],"tolower")!=NULL) ? tolower : toupper;
    while((c=getchar())!=EOF)
        putchar(convert(c));
}
}}}

= 实验2-12 =
== 实验目的 ==

掌握带变长参数列表的函数

== 实验内容 ==

编写一个函数max(n, s1,s2,s3,...,sn),计算求字符串s1,s2,s3...sn中最大的一个,n为参数个数

== 参考程序 ==

{{{#!cplusplus
#include <stdarg.h>

char *max(int n, ...) {
    char *maxstring = NULL;
    char * curr;
    va_list ap;
    
    va_start(ap, n);
    while(n-- > 0) {
        curr = va_arg(ap, char *);
        if(maxstring == NULL || strcmp(curr, maxstring) >0)
            maxstring = curr;
    }
    return maxstring;
}
}}}

= 实验考试 =

TableOfContents

实验基本要求

  • 实验前对实验内容做好预习
  • 有效利用实验时间,不做与实验无关的活动
  • 独立完成实验内容
  • 按时提交实验报告

实验运行环境简介

1. DEV-C++集成开发环境

安装运行

  1. 下载安装程序: http://www.bloodshed.net/dev/devcpp.html

  2. 运行安装程序进行安装
  3. 运行集成开发环境:开始菜单=〉Bloodshed Dev-C++ =〉Dev-C++

工程创建

  1. 在Dev-C++菜单中选择File=>New=>Project

  2. 在弹出对话框中选择Console Application,选中C Project,Name框输入一个工程名字,最后按OK按钮
  3. 选择硬盘上某个路径保存你所建立的工程

程序运行

  1. 选择菜单Execute=>Compile编译程序

  2. 如果编译没有错误,选择Execute=>Run运行程序,得到运行结果

如果程序结果一闪而过,怎么办?

  • 在命令行窗口中运行可执行程序
  • 在代码中添加:system("PAUSE");一行使程序暂停(system函数在stdlib.h中定义)

调试程序

  • 启动调试
    • 选择菜单Debug=>Debug

  • 设置断点
    • 将光标移动到需要设置断点的行,选择Debug=>Toggle Breakpoint

  • 单步执行
    • 菜单Debug=>Next Step和Step Into

  • 查看变量
    • 菜单Debug=>Add Watch

2. Visual Studio.Net 2003集成开发环境

创建工程

  1. 打开Visual Studio.net 2003集成开发环境
  2. 菜单File=>New=>Project

  3. 在弹出的对话框中:Project Types选择Visual C++ Projects,Templates选择Win32 Console Application,Name中输入工程名称,Location选择保存的路径。最后点OK
  4. 在接下来的工程向导中,选择Application Settings,选择Empty Project,最后按Finish
  5. 菜单View=>Solution Explorer

  6. 在Solution Explorer中选中刚才建立的工程
  7. 菜单File=>Add New Item

  8. 在弹出的对话框中:Categories选择Visual C++,Templates选择C++ File,Name中输入文件名,文件要以.c结尾,最后按Open。

编译运行

  1. 菜单Build=>Build Solution

  2. 如果编译通过,选择菜单Debug=>Start Without Debugging

调试

  • 开始调试:菜单Debug=>Start

  • 设置断点:光标移动到要设置断点的地方,按F9
  • 单步执行:Debug菜单下的Step Into和Step Over
  • 查看变量:Debug菜单下的Quick Watch

实验1-1

1. 实验目的

熟悉C语言开发环境,学会

  • 创建工程
  • 编辑代码
  • 编译连接,生成可执行程序
  • 调试代码

2. 实验内容

编译运行以下程序,观察程序运行的结果 完成课后作业1-1

   1 #include <stdio.h>
   2 main() 
   3 {
   4         printf("hello, world\n");
   5 }

   1 #include <stdio.h>
   2 /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
   3 main ()
   4 {
   5     int fahr,  celsius;
   6     int lower, upper, step;
   7     lower = 0;
   8     upper = 300;
   9     step = 20;
  10     fahr = lower;
  11     while ( fahr <= upper) {
  12         celsius = 5 * (fahr - 32) / 9;
  13         printf( "%d\t%d\n", fahr, celsius);
  14         fahr = fahr + step;
  15     } 
  16 }

   1 #include <stdio.h>
   2 /* print Fahrenheit-Celsius table*/
   3 main()
   4 {
   5     int fahr;
   6 
   7     for (fahr = 0; fahr <= 300; fahr = fahr + 20)
   8         printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
   9 }

{{{1-1 Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get. }}}

实验1-2

1. 实验目的

  • 掌握C语言变量、循环的基本概念
  • 掌握基本的算术运算

2. 实验内容

完成课后作业1-4,1-5

  • Write a program to print the corresponding Celsius to Fahrenheit table.
  • Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.

实验2-1

1. 实验目的

2. 实验内容

写一个函数strinsert(s, n, t)在字符串s的第n个字符后面插入字符串t。比如:

   1 char s[100] = "write everywhere";
   2 char t[] = "once, run ";
   3 strinsert(s, 6, t);
   4 printf("%s", s); 

输出: {{{write once, run everywhere }}}

3. 参考程序

   1 void strinsert(char *s, int i, char *t) {
   2    int tlen = strlen(t);
   3    int slen = strlen(s);
   4    int j;
   5 
   6    for(j = tlen; j >= i; j--)
   7       s[j+tlen] = s[j];
   8    for(j = 0; j < tlen; j++)
   9       s[j+i] = t[j];
  10 }

实验2-2

1. 实验目的

2. 实验内容

写两个函数

  • strcrop(s, i, j) 取字符串s中第i个到第j个字符
  • strdel(s, i, j) 将字符串s中第i个到第j个字符删除

for example:

   1 char s[20]="hello world";
   2 char t[20]="hello world";
   3 strcrop(s, 1, 8);
   4 strdel(t, 1, 8);
   5 printf("%s\n%s\n", s, t); 

output:

ello wor
hld 

3. 参考程序

   1 void strcrop(char *s, int i, int j) {
   2     int k;
   3     for(k = i; k <=j; k++)
   4         s[k-i] = s[k];
   5     s[k] = '\0';
   6 }
   7 void strdel(char *s, int i, int j) {
   8     j++;
   9     while(s[j] != '\0')
  10         s[i++] = s[j++] 
  11     s[i] = '\0';
  12 }

实验2-3

1. 实验目的

熟悉指针数组的使用

2. 实验内容

写一个函数weekday(y,m,d),求取y年m月d日是星期几,返回星期几的名字。例如:

   1 printf("%s", weekday(2006,3,6));

输出

C语言课程实验 (2008-02-23 15:36:56由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.