发新话题
打印

[TIPS]C学习笔记 发少量谭浩强C程序设计SE课后题答案

我现在看“printf”感觉不习惯,还是习惯“cout <<”。
当初学C语言的时候老师强制让我们保存为".cpp"。
你的笔记注释真够详细的。 [s:17]

TOP

printf cout 是完全不同的两个东西(废话)
但是我要说的和您想的不同:)
在您不了解对象的内部情况的时候可以使用cout比较好 这显示出了C++的优势 所以您只需要接口就可以了...
其实说cout是打印数据和转译其中的内容 并不准确 在C99的定义中 cout的官方解释是:将程序插入输出数据流...

承让了:)
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

两段代码功能一样
复制内容到剪贴板
代码:
#include "stdio.h"

int main(void)
{
    int x,y;
    for(x=0;x<10;x++,printf("\n"))
        for(y=0;y<10;y++)
            printf("X");
        return 0;
}
复制内容到剪贴板
代码:
#include "stdio.h"

int main(void)
{
    int x,y;
    for(x=0;x<10;x++)
    {
        for(y=0;y<10;y++)
        {
            printf("X");
        }
        printf("\n");
    }
        return 0;
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

注释是无锋之刃做的
复制内容到剪贴板
代码:
#include "stdlib.h"
#include "stdio.h"

// 前置声明
void do_heading(char *filename);

// 全局变量
int line=0, page=0;

int main(int argv, char *argc[])
{
    char buffer[256];
    FILE *fp;

    // 如果没有命令行参数,则在控制台上输入
    if(argv<2)
    {
        fprintf(stderr, "\nProper Usage is:");
        fprintf(stderr, "\n\nprint_it filename.ext\n");
        return(1);
    }

// 打开文件出错
if((fp=fopen(argc[1],"r")) == NULL)
{
    // 在标准错误台上输出信息
    fprintf(stderr, "Error opening file, %s!", argc[1]);
    // 结束程序
    return(1);
}

page=0;
line=1;
do_heading(argc[1]);

// 读出256个字符
while(fgets(buffer,256, fp)!=NULL)
{
    // 正好是55个字符的话
    if(line%55 == 0)
        do_heading(argc[1]);
}

    fprintf(stdprn, "\f");
    fclose(fp);
    return 0;
}

void do_heading(char *filename)
{
    // 页数加1
    page++;

        // 如果页数大于1,输出一个控制符
        if(page>1)
            fprintf(stdprn, "\f");

        // 输出一些信息
        fprintf(stdprn, "page: %d, %s\n\n",page, filename);
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

复制内容到剪贴板
代码:
//Program to calculate the product of two numbers.
#include <stdio.h>

int val1, val2, val3;

int product(int x, int y);

int main( void )
{
  //Get the first number
  printf("Enter a number between 1 and 100: ");
  scanf("%d", &val1);

  //Get the second number
  printf("Enter another number between 1 and 100: ");
  scanf("%d", &val2);

  //Calculate and display the product
  val3 = product(val1, val2);
  printf ("%d times %d = %d\n", val1, val2, val3);

  return 0;
}

//Function returns the product of the two values provided
int product(int x, int y)
{
   return (x * y);
}
复制内容到剪贴板
代码:
//Program to calculate the product of two numbers.
#include <stdio.h>


int product(int x, int y);

int main(void)
{
    int a1=0, a2=0, a3=0;

    printf("Please input a number between 1 and 100: ");
    scanf("%d",&a1);

    printf("Please input another number between 1 and 100: ");
    scanf("%d",&a2);

    a3 = product(a1, a2);
    printf("%d times %d is %d\n", a1, a2, a3);

    return 0;
}

int product(int x, int y)
{
    return(x * y);
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

子函数练习
复制内容到剪贴板
代码:
#include <stdio.h>

void display_line(void);

int main(void)
{
    display_line();
    printf("I hate this word.\n");
    display_line();

    return 0;
}

//print asterisk line fucntion
void display_line(void)
{
    int count;
    for(count=0;count<30;count++)
    {
        printf("*");
    }

    printf("\n");
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

strlen
复制内容到剪贴板
代码:
#include <stdio.h>
#include <string.h>

int main(void)
{
    char buffer[256];

    printf("Enter your name and press <Enter>:\n");
    gets( buffer );

    printf("\nYour name has %d character and spaces!", strlen(buffer));

    return 0;
}
复制内容到剪贴板
代码:
#include <stdio.h>
#include <string.h>

int main(void)
{
    char *string = "Fuck you gliet!";
    printf("%d\n", strlen(string));

    return 0;
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

学习到变量和常量存储信息的地方
下面的程序我加了注释 如果回头来自己看不懂是什么功能 那就去死
复制内容到剪贴板
代码:
//demonstrates variables and constants
#include <stdio.h>

//define a constant to convert from pounds to grams
#define GRAMS_PER_POUND 454

//define a constant for the start of the next century
const int TARGET_YEAR=2010;


int main(void)
{
    //declare the needed variables
    long weight_in_grams, weight_in_pounds;
    int year_of_birth, age_in_2010;

    //input data for user
    printf("Enter your weight in pounds: ");
    scanf("%d", &weight_in_grams);
    printf("\nEnter your year of birth: ");
    scanf("%d", &year_of_birth);

    //perform conversions

    weight_in_grams = weight_in_pounds * GRAMS_PER_POUND;
    age_in_2010 = TARGET_YEAR - year_of_birth;

    //display results on the screen

    printf("\nYour weight in grams = %ld", weight_in_grams);
    printf("\nIn 2010 you will be %d years old.\n", age_in_2010);

    return 0;
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

等会后面有个程序要想看懂 就必须要一些前置知识 其中有time和srand两个函数我不是很懂 找了资料
time函数
time_t time(time_t *thetime);
功能是取得系统的时间 需要包含头文件#include <time.h>
thetime:指向time_t变量的可选指针 在这个变量中存放当前的日期和时间 如果是NULL着忽略 代码举例如下
复制内容到剪贴板
代码:
#include <time.h>
#include <stdio.h>
#incude <dos.h>

int main(void)
{
       time_t t;
       time(&t);
       printf("The number of seconds since January 1, 1970 is %ld", t);
       return 0;
}
srand函数 原型
void srand(unsigned seed);
它的功能是 用来初始化随机数发生器 以便让随机数发生器产生可以预测的随即序列(nnd这JB叫什么解释呀)
头文件是#include <stdlib.h>
参数说明
seed是无符号整数 指明随机数序列的最小起点 注意是没有返回值的 下面来个演示程序
复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
  int i;
  time_t t;
  srand((unsigned) time(&t));
  printf("Three random numbers from 0 to 99:");

  for(i=0;i<3;i++)
  printf("%d\n", rand()%100);
  return 0;
}
好 前置知识解决了 下面来看一个猜数字游戏 本来这个游戏是只要求能输入运行查看结果的 很不幸 我读懂了 比第一天有进步 第一天的程序是无锋帮我分析的
复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NO    0
#define YES   1

int main(void)
{
    int guess_value = -1;
    //要猜测的数字
    int number;
    //猜测次数
    int nbr_of_guesses;
    //这里必须要先等于NO 以便下面的while可以继续
    int done = NO;

    printf("\n\nGetting a random number......\n");

    //嘿嘿 上面的那个就是这里了
    srand( (unsigned) time( NULL ) );
    //取随机数字让你猜的那个
    number = rand();

    //猜测次数
    nbr_of_guesses = 0;
    while (done == NO)
    {
        printf("\nPick a number between 0 and %d>", RAND_MAX);
            scanf("%d", &guess_value);

        nbr_of_guesses++;
        //这里就是本单元学的if...else结构 用来判断你猜对没
        if ( number == guess_value)
        {
            done = YES;
        }
        else if(number < guess_value)
        {
            printf("\nYou guessed high!");
        }
        else
        {
            printf("\nYou guessed low!");
        }
    }

    printf("\nCongratulateions! You guessed right in %d Guesses!", nbr_of_guesses);
    printf("\nThe number was %d\n\n",number);


    return 0;
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

简单的递归调用
复制内容到剪贴板
代码:
# include <stdio.h>

unsigned int f, x;
unsigned int factorial(unsigned int a);

int main(void)
{
    puts("Enter an integer value between 1 and 8: ");
    scanf("%d", &x);

    if(x>8||x<1)
    {
        printf("Only values from 1 to 8 are acceptable!");
    }
    else
    {
        f = factorial(x);
        printf("%u factorial equals %u\n", x, f);
    }


    return 0;
}

unsigned int factorial(unsigned int a)
{
    if(a == 1)
        return 1;
    else
    {
        a*=factorial(a-1);
        return a;
    }
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

使用多条return语句的作业
原来的例子
复制内容到剪贴板
代码:
//Demonstrates using multiple return statements in a function.

#include <stdio.h>

int x, y, z;

int larger_of( int a, int b);

int main( void )
{
   puts("Enter two different integer values: ");
   scanf("%d%d", &x, &y);

   z = larger_of(x,y);

   printf("\nThe larger value is %d.", z);

  return 0;
}

int larger_of( int a, int b)
{
   if (a > b)
      return a;
   else
      return b;
}
要求子函数中只包含一条return 如下
复制内容到剪贴板
代码:
//Demonstrates using multiple return statements in a function.

#include <stdio.h>

int x, y, z;

int larger_of( int a, int b);

int main( void )
{
   puts("Enter two different integer values: ");
   scanf("%d,%d", &x, &y);

   z = larger_of(x,y);

   printf("\nThe larger value is %d.", z);

  return 0;
}

int larger_of( int a, int b)
{
   int re;
    re=a>b?a:b;

    return re;
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

求数组里的最大值 最小值 输入0终止接收
复制内容到剪贴板
代码:
#include <stdio.h>

#define MAX 100

int array[MAX];
int count = -1, maximum, minimum, num_entered, temp;

int main(void)
{
    puts("Enter interger values one per line.");
    puts("Enter 0 when finished.");

    //input the values
    do
    {
        scanf("%d", &temp);
        array[++count] = temp;
    }while ( count < (MAX-1) && temp !=0 );

    num_entered = count;

    //find the largest and smallest

    maximum = -32000;
    minimum = 32000;

    for ( count = 0; count <= num_entered && array[count] != 0; count++ )
    {
        if ( array[count] > maximum )
            maximum = array[count];

        if ( array[count] < minimum )
            minimum = array[count];
    }

    printf("\nThe maximum value is %d", maximum);
    printf("\nThe minimum value is %d\n", minimum);

    return 0;
}
qq310926是我唯一用号,除此之外有其他号码号自称邪八冰血封情,则非本人。

TOP

成绩管理系统
复制内容到剪贴板
代码:

#include<stdio.h>      /* head file*/
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<bios.h>
#include<time.h>
#define LEFT  70      /* the cursor&#39;s place */
#define RIGHT  78
#define UP    72
#define DOWN  80
#define ESC  283
#define ENTER  7181
#define F10 17408      /* enter menu */
#define F1 15104       /* help*/
#define len sizeof(Fname)

long a_sta[50];       /*answer stack*/
union key           /*use bioskey*/
{char sym[2];
int  sym2;
}KEY,Choose,ans;
union inkey
{int c;
char ch[2];
}sc,key;
static int bx=45,by=5,ex,ey;    /*cursor&#39;s place*/
FILE *fp,*fp2;             /*FILE pointer*/
char filename[20];
int wrong[50];             /*Wrong answer&#39;s number*/
struct fname              /*student&#39;s information*/
{
char filename[20];
char name[20];
int  level;
int  score;
struct fname *next;
}*message,*head;
typedef struct fname Fname;


void choose_menu(void);       /* used funtion*/
void place(int ,int );
void re(int,int);
void Screen(void);
void SFile(void);
void Stest(void);
void Scheck(void);
void choose_act(void);
char *save(void);
void load(void);
void lv1(int,int,int,int,int,int);
void lv2(int,int,int,int,int,int);
void lv3(void);
int ans1(int,int,int,int,int,int,int,int);
void fangkuai(void);
char *intext(int,int);
void loadfile(void);
Fname *save_name(Fname*);
Fname *load_name(Fname*);
Fname *input(Fname*);
void print_list(Fname*);
Fname *sread(Fname*);
void Sl(void);
void pw(void);
void sename(void);
void selv(void);
void sesc(void);
void menu(void);
void Last(void);
void help(void);

void main()
{int i,gdr=DETECT,gmo;
head=(Fname*)malloc(len);
head=NULL;
  {if((fp2=fopen("wuxi.txt","rb"))!=NULL)  /*information file*/
   head=load_name(head);
   fclose(fp);
  }
initgraph(&gdr,&gmo,"");
menu();            /* the first screen*/
if(key.c==F1)help();
cleardevice();
Screen();
setbkcolor(BLUE);
outtextxy(15,449,"Press F10 into menu");
Choose.sym2=bioskey(0);
while(Choose.sym2!=ESC)
  { if(Choose.sym2==F10)
    choose_menu();
    Choose.sym2=bioskey(0);
   }
closegraph();
}
void choose_menu()    /*choose what you want to choose*/
{setcolor(WHITE);
place(bx,by);
KEY.sym2=bioskey(0);
while(KEY.sym2!=ESC)
  {re(bx,by);
   if(KEY.sym[0]==0)
     switch(KEY.sym[1])
        {case 0x4b: bx=bx-208;if(bx<=0)bx=461;break;
         case 0x4d: bx=bx+208;if(bx>=669)bx=45;break;
         }
   else
     if(KEY.sym2==ENTER)
      switch(bx)
         {case 45:
             Screen();
             SFile();
             place(bx,by);
             choose_act();
             break;
        case 253:
             Screen();
             Stest();
             place(bx,by);
             choose_act();
             break;
        case 461:
             Screen();
             Scheck();
             place(bx,by);
             choose_act();
             break;
         }
    place(bx,by);
    KEY.sym2=bioskey(0);
    Screen();
  }
}
void place(int a0,int a1)             /* cover the son_menu*/
{ setfillstyle(SOLID_FILL,WHITE);
  bar(a0,a1,a0+60,a1+15);
  switch(a0)
    {case 45:
        setcolor(RED);
        outtextxy(50,10,"File");
        setfillstyle(SOLID_FILL,BLUE);
        bar(11,449,620,459);
        setcolor(YELLOW);
        outtextxy(15,449,"Press ENTER into FILE,Press ESC to exit");
        break;
    case 253:setcolor(RED);
         outtextxy(258,10,"Test");
         setfillstyle(SOLID_FILL,BLUE);
         bar(11,449,620,459);
         setcolor(YELLOW);
         outtextxy(15,449,"Press ENTER into LEVEL,Press ESC to exit");
         break;
    case 461:
         setcolor(RED);
         outtextxy(466,10,"Search");
         setfillstyle(SOLID_FILL,BLUE);
         bar(11,449,620,459);
         setcolor(YELLOW);
         outtextxy(15,449,"Press ENTER to SEARCH,Press ESC to exit");
         break;
    }
}
  void re(int a0,int a1)       /*cover the main_menu*/
{ setfillstyle(EMPTY_FILL,RED);
  setcolor(YELLOW);
  bar(a0,a1,a0+80,a1+15);
   outtextxy(50,10,"File");
   outtextxy(258,10,"Test");
   outtextxy(466,10,"Search");
}
void Screen()
{
setfillstyle(SOLID_FILL,BLUE);
bar(1,1,679,479);
setcolor(LIGHTCYAN);
rectangle(10,1,629,25);
rectangle(10,30,629,469);
line(10,439,629,439);
setcolor(YELLOW);
outtextxy(50,10,"File");
outtextxy(258,10,"Test");
outtextxy(466,10,"Search");
}
  void SFile()
{setfillstyle(SOLID_FILL,WHITE);
  bar(15,20,150,180);
  setcolor(BLACK);
  rectangle(20,30,140,170);
  outtextxy(35,50,"Load");
  outtextxy(35,90,"Help");
  outtextxy(35,130,"Exit");
}
  void Stest()
{setfillstyle(SOLID_FILL,WHITE);
  bar(225,20,350,180);
  setcolor(BLACK);
  rectangle(230,30,340,170);
  outtextxy(245,50,"LV1");
  outtextxy(245,90,"LV2");
  outtextxy(245,130,"LV3");
}
  void Scheck()
{setfillstyle(SOLID_FILL,WHITE);
  bar(430,20,545,180);
  setcolor(BLACK);
  rectangle(440,30,535,170);
  outtextxy(455,50,"By name");
  outtextxy(455,90,"By lv");
  outtextxy(455,130,"By score");
}
  void choose_act()
{ Fname *p;
  ex=bx-15;
  ey=by+35;
  Choose.sym2=bioskey(0);
  while(Choose.sym2!=ESC)
    { setfillstyle(SOLID_FILL,WHITE);
    bar(ex,ey,ex+80,ey+20);
    switch(bx)
       { case 45:SFile();break;
        case 253:Stest();break;
        case 461:Scheck();break;
        }
    setcolor(RED);
    if(!Choose.sym[0]){
      switch(Choose.sym[1])
        {case UP:  ey-=40;if(ey<40)ey=120;break;
        case DOWN:ey+=40;if(ey>120)ey=40;break;
        }

     setfillstyle(SOLID_FILL,BLACK);
     bar(ex,ey,ex+80,ey+20);
     switch(ex)
     { case 30:
          switch(ey)
            {
            case 40:outtextxy(35,50,"Load");
                    setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into Open file,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 80:outtextxy(35,90,"Help");
                    setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into Save file,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 120:outtextxy(35,130,"Exit");
                     setfillstyle(SOLID_FILL,BLUE);
                 bar(11,449,620,459);
                     setcolor(YELLOW);
                 outtextxy(15,449,"Press ENTER into Exit dos,Press UP\\DOWN to choose,Press ESC to exit");
                 break;
            }
           break;
       case 238:
          switch(ey)
            {
            case 40:outtextxy(245,50,"LV1");
                setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into LV1,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 80:outtextxy(245,90,"LV2");
                    setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into LV2,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 120:outtextxy(245,130,"LV3");
                     setfillstyle(SOLID_FILL,BLUE);
                 bar(11,449,620,459);
                     setcolor(YELLOW);
                 outtextxy(15,449,"Press ENTER into LV3,Press UP\\DOWN to choose,Press ESC to exit");
                 break;
            }
            break;
       case 446:
          switch(ey)
            {
            case 40:outtextxy(455,50,"By name");
                    setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into Justice,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 80:outtextxy(455,90,"By lv");
                    setfillstyle(SOLID_FILL,BLUE);
                bar(11,449,620,459);
                    setcolor(YELLOW);
                outtextxy(15,449,"Press ENTER into Return test,Press UP\\DOWN to choose,Press ESC to exit");
                break;
            case 120:outtextxy(455,130,"By score");
                 setfillstyle(SOLID_FILL,BLUE);
                 bar(11,449,620,459);
                     setcolor(YELLOW);
                 outtextxy(15,449,"Press ENTER into Exit,Press UP\\DOWN to choose,Press ESC to exit");
                 break;
            }
            break;
         }
        }
      else if(Choose.sym2==ENTER)
        {
         switch(ex)
            {  case 30:
                switch(ey)
                 {case 40: load();break;
                  case 80: help();head=save_name(head);break;
                  case 120:Last();exit(0);break;
                 }
                break;
            case 238:
                 switch(ey)
                  {
                  case 40:head=input(head);p=sread(head);p->level=1;Screen();lv1(20,40,50,125,40,0);p->score+=ans1(95,40,50,125,40,4,0,2);save();head=save_name(head);
                      Screen(); pw();getch();choose_menu();break;
                  case 80:head=input(head);p=sread(head);p->level=2;Screen();lv2(20,40,20,150,70,0);p->score+=ans1(125,40,20,150,70,3,0,5);save();head=save_name(head);
                      Screen(); pw();getch();choose_menu();break;
                  case 120:head=input(head);p=sread(head);p->level=3;Screen();lv3();p->score+=ans1(90,35,20,125,20,4,0,1);p->score+=ans1(125,115,20,150,20,3,20,2);p->score+=ans1(250,215,10,1,20,0,40,4);save();head=save_name(head);
                       Screen();pw();getch();choose_menu();
                  }
                 break;
             case 446:
                switch(ey)
                 {
                  case 40:sename();break;
                  case 80:selv();break;
                  case 120: sesc(); break;
                 }
                break;
            }
        }
     Choose.sym2=bioskey(0);
     }
   Screen();
}
char *save(void)
{
int i, j;
strcpy(filename,sread(head)->filename);
if((fp=fopen(filename,"wb"))==NULL){
   outtext("cannot open this file!");
   choose_menu();
  }
outtextxy(260,449,"waiting....");
  for(i=1;i<=470;i++){
    for(j=1;j<=635;j++){
    fputc(getpixel(j,i),fp);
    }
    }
  fclose(fp);
  setfillstyle(SOLID_FILL,BLUE);
  bar(11,449,620,459);
  outtextxy(260,449,"save over!");
  setfillstyle(SOLID_FILL,BLUE);
  bar(15,471,639,479);
  return filename;
}

void load(void)
{
  char *choice=" ";
  fangkuai();
  outtextxy(210,270,"1.load filename");
  outtextxy(210,300,"2.load file");
  outtextxy(210,330,"  input your choice:");
  strcpy(choice,intext(370,330));
  getch();
  setfillstyle(SOLID_FILL,BLUE);
  bar(200,200,500,400);
  switch(*choice)
   {case &#39;1&#39;: head=load_name(head);print_list(head);break;
    case &#39;2&#39;: loadfile(); break;
    default:  return;
   }
  choose_menu();
}
void loadfile(void)
{ int i,j;
  fangkuai();
  outtextxy(210,300,"input filename:");
  strcpy(filename,intext(350,300));
  if((fp=fopen(filename,"rb"))==NULL){
     outtext("Cannot open this file!");
     setfillstyle(SOLID_FILL,BLUE);
     bar(11,449,620,459);
     choose_menu();
    }
  setfillstyle(SOLID_FILL,BLUE);
  bar(11,449,620,459);
  outtextxy(260,449,"loading.....");
  for(i=1;i<=470;i++){
    for(j=1;j<=635;j++){
     putpixel(j,i,fgetc(fp));
    }
  }
  getch();
  fclose(fp);
}

void lv1(int x,int y,int length,int dx,int dy,int sta)
{
int first,second,mid[4],ans,t,k;
int count;
time_t ti;
char str1[25],str2[25];
int cx=x,cy=y;
srand((unsigned)time(&ti));
for(count=0;count<length;count++)
  {
  first=rand()%100+1;
  second=rand()%100+1;
  if(first<second){t=first;first=second;second=t;}
  ans=rand()%4+1;
  itoa(first,str1,10);
  itoa(second,str2,10);
  strcat(str2,"=");
  switch(ans)
    {case 1: a_sta[count+sta]=first+second; outtextxy(cx+10,cy,str1);outtextxy(cx+35,cy,"+");outtextxy(cx+50,cy,str2);break;
    case 2: a_sta[count+sta]=first-second; outtextxy(cx+10,cy,str1);outtextxy(cx+35,cy,"-");outtextxy(cx+50,cy,str2);break;
    case 3: a_sta[count+sta]=first*second; outtextxy(cx+10,cy,str1);outtextxy(cx+35,cy,"*");outtextxy(cx+50,cy,str2);break;
    case 4: a_sta[count+sta]=first/second; outtextxy(cx+10,cy,str1);outtextxy(cx+35,cy,"/");outtextxy(cx+50,cy,str2);break;
    }
  cx+=dx;
  if(cx>x+4*dx){cy+=dy;cx=x;}
  }
}

void lv2(int x,int y,int length,int dx,int dy,int sta)
{int num;
long a,b,c,d,t,op1,op2;
int cx=x,cy=y;
char str1[25],str2[25],str3[25];
static time_t ti;
srand((unsigned)time(&ti));
for(num=0;num<length;num++)
  {  a=rand()%100+1;
    b=rand()%100+1;
    c=rand()%100+1;
    if(a<b){t=a;a=b;b=t;}
    op1=rand()%4+1;
    itoa(a,str1,10);
    itoa(b,str2,10);
    switch(op1)
     {case 1: d=a+b;outtextxy(cx,cy,str1);outtextxy(cx+25,cy,"+");outtextxy(cx+40,cy,str2);break;
    case 2: d=a-b;outtextxy(cx,cy,str1);outtextxy(cx+25,cy,"-");outtextxy(cx+40,cy,str2);break;
    case 3: d=a*b;outtextxy(cx,cy,str1);outtextxy(cx+25,cy,"*");outtextxy(cx+40,cy,str2);break;
    case 4: d=a/b;outtextxy(cx,cy,str1);outtextxy(cx+25,cy,"/");outtextxy(cx+40,cy,str2);break;
     }
op2=rand()%4+1;
itoa(c,str3,10);
  switch(op2)
  {case 1: a_sta[num+sta]=d+c;outtextxy(cx+65,cy,"+");outtextxy(cx+80,cy,str3);outtextxy(cx+100,cy,"=");break;
  case 2: a_sta[num+sta]=d-c;outtextxy(cx+65,cy,"-");outtextxy(cx+80,cy,str3);outtextxy(cx+100,cy,"=");break;
  case 3: a_sta[num+sta]=d*c;outtextxy(cx+65,cy,"*");outtextxy(cx+80,cy,str3);outtextxy(cx+100,cy,"=");break;
  case 4: a_sta[num+sta]=d/c;outtextxy(cx+65,cy,"/");outtextxy(cx+80,cy,str3);outtextxy(cx+100,cy,"=");break;
  }
  if((op1==1||op1==2)&&(op2==3||op2==4)){outtextxy(cx-5,cy,"(");outtextxy(cx+60,cy,")");}
  cx+=dx; if(cx>x+3*dx){cy+=dy;cx=x;}
  }
}
void lv3()
{long a0;
long n,d,c,t1,t2;
int x=15,y=215;
char str[25];
lv1(15,35,20,125,20,0);
lv2(15,115,20,150,20,20);
for(t1=0;t1<10;t1++)
  {
  c=rand()%2+1;
  if(c==1){a0=rand()%9+1;d=rand()%5+1;n=rand()%99+50;}
    else {a0=rand()%3+1;d=rand()%2+1;n=rand()%12+10;}
  a_sta[t1+40]=0;
  if(c==1)
  for(t2=0;t2<n;t2++)
    {a_sta[t1+40]+=a0;
    ltoa(a0,str,10);
    a0=a0+d;
    if(t2==0||t2==1||t2==2)
     {outtextxy(x,y,str);
    outtextxy(x+15,y,"+");
     }
    if(t2==2)outtextxy(x+25,y,"...");
    if(t2==n-1){outtextxy(x+40,y,"+");outtextxy(x+55,y,str);outtextxy(x+120,y,"=");}
    if(t2==0||t2==1||t2==2)x+=20;
    }
  else
  for(t2=0;t2<n;t2++)
    {a_sta[t1+40]+=a0;
    ltoa(a0,str,10);
    a0=a0*d;
    if(t2==0||t2==1||t2==2)
     {outtextxy(x,y,str);
    outtextxy(x+15,y,"+");
     }
    if(t2==2)outtextxy(x+25,y,"...");
    if(t2==n-1){outtextxy(x+40,y,"+");outtextxy(x+55,y,str);outtextxy(x+120,y,"=");}
    if(t2==0||t2==1||t2==2)x+=20;
    }
  x=15;
  y+=20;
  }

}
int ans1(int x,int y,int length,int dx,int dy,int m,int sta,int ds)
{
int num,ax=x,ay=y,i=0,score=0,j=sta;
long anst=0;
char *ch1,*ch/*,daan[25]*/;
for(num=0;num<length;num++)
  { ch1=" ";
    ch="                                                                                                                                                          ";
  /*  ltoa(a_sta[num+sta],daan,10);*/
    moveto(ax,ay);
  /*  outtextxy(ax,ay,daan);*/ /* you can see the answer*/
    while(ch[i++]!=&#39;\r&#39;)
    {
    ans.sym2=bioskey(0);
    if(ans.sym2==F10)choose_menu();
    ch1[0]=ans.sym[0];
    anst=atol(ch1)+anst*10;
    outtext(ch1);
    ch[i]=ch1[0];
     }
    ch[--i]=&#39;\0&#39;;
    anst/=10;
    if(a_sta[num+sta]<0)anst=-anst;
    if(anst==a_sta[num+sta])score=score+ds;
    else {wrong[j++]=num+sta+1;
        }
    ax+=dx;
    if(ax>x+m*dx){ax=x;ay+=dy;}
    anst=0;
    }
  return score;
}
void pw(void)
{Fname *p;
int i,x=350,y=250;
char str[25],str1[25];
setfillstyle(SOLID_FILL,BLUE);
bar(200,200,500,400);
p=sread(head);
itoa(p->score,str,10);
fangkuai();
setcolor(RED);
outtextxy(210,230,"SCORE:");
outtextxy(350,230,str);
outtextxy(230,250,"WRONG:");
  for(i=0;i<50;i++)
  {if(wrong[i]!=0)
    {itoa(wrong[i],str1,10);
    outtextxy(x,y,str1);
    x+=40;
    if(x>450){y+=20;x=230;}
    }
  }
getch();
setfillstyle(SOLID_FILL,BLUE);
bar(200,200,500,430);
setcolor(YELLOW);
}
void fangkuai(void)
{int i,j;
for(j=200;j<=400;j++)
  for(i=200;i<=500;i++)
  putpixel(i,j,15^getpixel(i,j));
}
char *intext(int x,int y)
{char *ch,*ch1;
int i=0;
moveto(x,y);
ch1=" ";
ch="             ";
  do
   {
    sc.c=bioskey(0);
    ch1[0]=sc.ch[0];
    outtext(ch1);
    ch[i]=ch1[0];
   }while(!(ch[i++]==&#39;\r&#39;));
  ch[--i]=&#39;\0&#39;;
  return(ch);
}

  Fname *save_name(Fname *head)
{
  Fname *info;
  if((fp2=fopen("wuxi.txt","wb"))==NULL)
    {outtext("cannot open this file\n");
     choose_menu();
    }
  else
   info=head;
   while(info!=NULL)
    { if((fp=fopen(info->filename,"rb"))!=NULL)
     fwrite(info,sizeof(Fname),1,fp2);
     info=info->next;
    }
  fclose(fp);
  fclose(fp2);
  return(head);
}
Fname *load_name(Fname *head)
{
  Fname *last,*info;
  if((fp=fopen("wuxi.txt","rb"))==NULL){
   outtext("Cannot open file.\n");
   choose_menu();
      }
  head=(Fname*)malloc(len);
  fread(head,len,1,fp);
  last=head;
  while(!feof(fp))
  { info=(Fname *)malloc(len);
   if(!info){
   printf("Out of Memory");
   }
   if(1!=fread(info,len,1,fp)) break;
   last->next=info;
   last=info;
  }
  fclose(fp);
  return(head);
}
Fname *input(Fname *ht)
{
Fname *p1,*p2,*c;
char com[20];
setfillstyle(SOLID_FILL,BLUE);
bar(200,200,500,400);
fangkuai();
outtextxy(210,210,"input filename:");
  if(ht!=NULL)
  {
   p1=(Fname*)malloc(len);
   p2=(Fname*)malloc(len);
   p2=ht;
   while(p2->next!=NULL)
   p2=p2->next;
   strcpy(com,intext(350,210));
   c=ht;
   while(c!=NULL)
    {
     if(strcmp(c->filename,com)==0){outtext("REPEATED NAME");getch();return input(ht);}
     else c=c->next;
    }
   if((fp=fopen(com,"wb"))==NULL){outtextxy(220,240,"Cannot open this file");getch();return input(ht);}
  else{
   strcpy(p1->filename,com);
   outtextxy(210,350,"Input name:");
   strcpy(p1->name,intext(350,350));
   p1->score=0;
   p1->next=NULL;
   p2->next=p1;
   p2=p1->next;
     }
  }
else
  {ht=(Fname*)malloc(len);
   strcpy(com,intext(350,210));
    if((fp=fopen(com,"wb"))==NULL){outtextxy(220,240,"Cannot open this file");getch();return input(ht);}
  else{
     strcpy(ht->filename,com);
     outtextxy(210,350,"Input name:");
     strcpy(ht->name,intext(350,350));
     ht->score=0;
     ht->next=NULL;
    }
  }
getch();
setfillstyle(SOLID_FILL,BLUE);
bar(200,200,500,400);
return(ht);
}
void print_list(Fname *ht)
{
Fname *p;
int i=60,j=40;
char str[25];
Sl();
p=ht;
settextstyle(0,0,1);
while(p!=NULL)
  {itoa(p->score,str,10);
   outtextxy(i,j,p->filename);
   outtextxy(i+150,j,p->name);
   switch(p->level)
    {case 1: outtextxy(i+300,j,"lv1");break;
     case 2: outtextxy(i+300,j,"lv2");break;
     case 3: outtextxy(i+300,j,"lv3");break;
    }
   outtextxy(i+450,j,str);
   p=p->next;
   j=j+10;
  }
getch();
settextstyle(0,0,0);
Screen();
choose_menu();
}
void Sl(void)
{
setfillstyle(SOLID_FILL,YELLOW);
bar(0,0,639,479);
setlinestyle(0,0,1);
settextstyle(0,0,2);
outtextxy(150,2,"-----INFORMATION----");
outtextxy(130,430,"--PRESS ANY KEY TO RETURN--");
rectangle(50,25,589,419);
settextstyle(0,0,1);
outtextxy(60,30,"FILENAME");
outtextxy(210,30,"NAME");
outtextxy(360,30,"LEVEL");
outtextxy(510,30,"SCORE");
line(50,38,589,38);
setlinestyle(0,0,3);
}
Fname *sread(Fname *head)
{Fname *last,*info;
info=head;
  while(info!=NULL)
  {last=info;
   info=info->next;
  }
  return(last);
}
void sename(void)
{char str1[25],str[25];
int  i=60,j=40;
Fname *p;
fangkuai();
outtextxy(210,300,"input name:");
strcpy(str1,intext(300,300));
Sl();
p=head;
settextstyle(0,0,1);
while(p!=NULL)
  {if(strcmp(str1,p->name)==0)
    {itoa(p->score,str,10);
     outtextxy(i,j,p->filename);
     outtextxy(i+150,j,p->name);
    switch(p->level)
     {case 1: outtextxy(i+300,j,"lv1");break;
      case 2: outtextxy(i+300,j,"lv2");break;
      case 3: outtextxy(i+300,j,"lv3");break;
     }
    outtextxy(i+450,j,str);
    j=j+10;
    }
   p=p->next;
  }
if(j==40)outtextxy(i,j,"CANNOT FIND THE NAME!");
getch();
settextstyle(0,0,0);
Screen();
choose_menu();
}
  void selv(void)
{char str1[25],str[25];
int  i=60,j=40,lv;
Fname *p;
fangkuai();
outtextxy(210,300,"input level:");
strcpy(str1,intext(300,300));
lv=atoi(str1);
Sl();
p=head;
settextstyle(0,0,1);
while(p!=NULL)
  {if(p->level==lv)
    {itoa(p->score,str,10);
     outtextxy(i,j,p->filename);
     outtextxy(i+150,j,p->name);
    switch(p->level)
     {case 1: outtextxy(i+300,j,"lv1");break;
      case 2: outtextxy(i+300,j,"lv2");break;
      case 3: outtextxy(i+300,j,"lv3");break;
     }
    outtextxy(i+450,j,str);
    j=j+10;
    }
   p=p->next;
  }
if(j==40)outtextxy(i,j,"CANNOT FIND THE LEVEL!");
getch();
settextstyle(0,0,0);
Screen();
choose_menu();
}
void sesc(void)
{char str1[25],str[25];
int  i=60,j=40,score;
Fname *p;
fangkuai();
outtextxy(210,300,"input score:");
strcpy(str1,intext(300,300));
score=atoi(str1);
Sl();
p=head;
settextstyle(0,0,1);
while(p!=NULL)
  {if(p->score==score)
    {itoa(p->score,str,10);
     outtextxy(i,j,p->filename);
     outtextxy(i+150,j,p->name);
    switch(p->level)
     {case 1: outtextxy(i+300,j,"lv1");break;
      case 2: outtextxy(i+300,j,"lv2");break;
      case 3: outtextxy(i+300,j,"lv3");break;
     }
    outtextxy(i+450,j,str);
    j=j+10;
    }
   p=p->next;
  }
if(j==40)outtextxy(i,j,"CANNOT FIND THE SCORE!");
getch();
settextstyle(0,0,0);
Screen();
choose_menu();
}
void menu(void)
{char subject[28]="Arithmatic System",
    name[28]="Wu XiaoHui",
    class[28]="Computer 4",
    no[28]="2001374424",
    help[40]="<Press F1 into help>";
time_t ti;
srand((unsigned)time(&ti));
setbkcolor(BLUE);
{setcolor(LIGHTGREEN);
  setlinestyle(SOLID_LINE,0,3);
  rectangle(80,85,530,150);
}
settextstyle(0,0,1);
setcolor(YELLOW);
outtextxy(150,300,"Designer:");
outtextxy(300,300,name);
outtextxy(150,320,"Class:");
outtextxy(300,320,class);
outtextxy(150,340,"Number:");
outtextxy(300,340,no);
outtextxy(200,460,help);
  while(!kbhit())
{
  setcolor(rand()%14+1);
  settextstyle(4,0,6);
  outtextxy(100,90,subject);
  delay(100000);
  }
key.c=bioskey(0);
settextstyle(0,0,1);
}
void Last(void)
{char bye[20]="THANK YOU",
    good[20]="VISIT MY SYSTEM";
int r;
cleardevice();
setbkcolor(BLUE);
settextstyle(1,0,7);
setcolor(YELLOW);
outtextxy(150,80,bye);
outtextxy(50,200,good);
rectangle(20,20,629,469);
sleep(1);
}
void help(void)
{int i,a,b;
setfillstyle(SOLID_FILL,CYAN);
bar(0,0,639,479);
setlinestyle(0,0,1);
settextstyle(0,0,2);
setcolor(RED);
outtextxy(150,2,"-----INFORMATION----");
outtextxy(130,430,"--PRESS ANY KEY TO RETURN--");
rectangle(50,25,589,419);
settextstyle(0,0,1);
outtextxy(100,50,"1.Press F10 ,you can choose the menu.");
outtextxy(100,70,"2.Press \30 or \31 to choose son_menu.");
outtextxy(100,90,"3.Choose File ,you can see the filename or test paper.");
outtextxy(100,110,"4.If you finish the paper ,please waiting for saving.");
outtextxy(100,130,"5.Don&#39;t type any other nonscene,or it will run wrong");
outtextxy(100,150,"6.Something you can see in the menu&#39;s underside");
outtextxy(100,170,"7.Before doing the paper,you must input your imformation.");
setlinestyle(0,0,3);
a=320;
b=320;
while(!kbhit())
  {a=(a++)%639;
  if(a==639)a=321;
  b--;
  if(b==0)b=319;
  for(i=0;i<=479;i++)
    {putpixel(a,i,15^getpixel(a,i));
    putpixel(b,i,15^getpixel(b,i));
    }
  }
settextstyle(0,0,1);
Screen();
choose_menu();
}

TOP

俄罗斯方块简单C语言版
复制内容到剪贴板
代码:
#include<graphics.h>
int score,speed;
int dx=270,dy=151,tag=1;
main()
{
int gd=DETECT,gm;
int press,fag1,fag2,fag3;
int i,j;
char str[10];
initgraph(&gd,&gm,"C:\\turboc2");
score=0;
speed=30000;
cleardevice();
inicrack();
getch();
cleardevice();
screen();
setcolor(RED);
sprintf(str,"%d",score);
outtextxy(92,205,"LEVER 1");
outtextxy(255,120,str);
fag2=1;
while(1)
  {
   if(dy<165)
      {
      fag1=fag2;
      fag2=rand()%7+1;
      setfillstyle(SOLID_FILL,GREEN);
      bar(370,170,398,198);
      switch(fag2)
        {
         case 1:blockshape(370,170,RED,YELLOW,0);break;
         case 2:lineshape(370,170,1,RED,YELLOW,0);break;
         case 3:qishape(370,170,1,RED,YELLOW,0);break;
         case 4:lshape(370,170,1,RED,YELLOW,0);break;
         case 5:tshape(370,170,3,RED,YELLOW,0);break;
         case 6:zshape(370,170,1,RED,YELLOW,0);break;
         case 7:wushape(370,170,2,RED,YELLOW,0);break;
        }
      }
   while(!bioskey(1))
    {
     if(dy<158)
      {
      fag1=fag2;
      fag2=rand()%7+1;
      setfillstyle(SOLID_FILL,GREEN);
      bar(370,170,398,198);
      switch(fag2)
        {
         case 1:blockshape(370,170,RED,YELLOW,0);break;
         case 2:lineshape(370,170,1,RED,YELLOW,0);break;
         case 3:qishape(370,170,1,RED,YELLOW,0);break;
         case 4:lshape(370,170,1,RED,YELLOW,0);break;
         case 5:tshape(370,170,3,RED,YELLOW,0)