公告

Gentoo交流群:87709706 欢迎您的加入

#451 入门必备 » C 练习实例75 » 2022-08-29 20:44:53

batsom
回复: 0

题目:输入一个整数,并将其反转后输出。

程序分析:无。

 
#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;
 
    printf("输入一个整数: ");
    scanf("%d", &n);
 
    while(n != 0)
    {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }
 
    printf("反转后的整数: %d", reversedNumber);
 
    return 0;
}

#452 入门必备 » C 练习实例74 » 2022-08-29 20:44:23

batsom
回复: 0

题目:连接两个链表。

程序分析:无。

#include <stdlib.h>
#include <stdio.h>
struct list
{
    int data;
    struct list *next;
};
typedef struct list node;
typedef node *link;
link delete_node(link pointer,link tmp)
{
    if (tmp==NULL) /*delete first node*/
        return pointer->next;
    else
    {
        if(tmp->next->next==NULL)/*delete last node*/
            tmp->next=NULL;
        else /*delete the other node*/
            tmp->next=tmp->next->next;
    return pointer;
    }
}
void selection_sort(link pointer,int num)
{
    link tmp,btmp;
    int i,min;
    for(i=0;i<num;i++)
    {
        tmp=pointer;
        min=tmp->data;
        btmp=NULL;
        while(tmp->next)
        {
            if(min>tmp->next->data)
            {
                min=tmp->next->data;
                btmp=tmp;
            }
            tmp=tmp->next;
        }
        printf("\40: %d\n",min);
        pointer=delete_node(pointer,btmp);
    }
}
link create_list(int array[],int num)
{
    link tmp1,tmp2,pointer;
    int i;
    pointer=(link)malloc(sizeof(node));
    pointer->data=array[0];
    tmp1=pointer;
    for(i=1;i<num;i++)
    {
        tmp2=(link)malloc(sizeof(node));
        tmp2->next=NULL;
        tmp2->data=array[i];
        tmp1->next=tmp2;
        tmp1=tmp1->next;
    }
    return pointer;
}
link concatenate(link pointer1,link pointer2)
{
    link tmp;
    tmp=pointer1;
    while(tmp->next)
        tmp=tmp->next;
    tmp->next=pointer2;
    return pointer1;
}
int main(void)
{
    int arr1[]={3,12,8,9,11};
    link ptr;
    ptr=create_list(arr1,5);
    selection_sort(ptr,5);
}

#453 入门必备 » C 练习实例73 » 2022-08-29 20:43:53

batsom
回复: 0

题目:反向输出一个链表。

程序分析:无。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
    int          data;
    struct LNode *next;
}LNode,*LinkList;
 
LinkList CreateList(int n);
void print(LinkList h);
int main()
{
    LinkList Head=NULL;
    int n;
    
    scanf("%d",&n);
    Head=CreateList(n);
    
    printf("刚刚建立的各个链表元素的值为:\n");
    print(Head);
    
    printf("\n\n");
    system("pause");
    return 0;
}
LinkList CreateList(int n)
{
    LinkList L,p,q;
    int i;
    L=(LNode*)malloc(sizeof(LNode));
    if(!L)return 0;
    L->next=NULL;
    q=L;
    for(i=1;i<=n;i++)
    {
        p=(LinkList)malloc(sizeof(LNode));
        printf("请输入第%d个元素的值:",i);
        scanf("%d",&(p->data));
        p->next=NULL;
        q->next=p;
        q=p;
    }
    return L;
}
void print(LinkList h)
{
    LinkList p=h->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
}

#454 入门必备 » C 练习实例72 » 2022-08-29 20:43:22

batsom
回复: 0

题目:创建一个链表。

程序分析:无。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
    int          data;
    struct LNode *next;
}LNode,*LinkList;
 
LinkList CreateList(int n);
void print(LinkList h);
int main()
{
    LinkList Head=NULL;
    int n;
    
    scanf("%d",&n);
    Head=CreateList(n);
    
    printf("刚刚建立的各个链表元素的值为:\n");
    print(Head);
    
    printf("\n\n");
    system("pause");
    return 0;
}
LinkList CreateList(int n)
{
    LinkList L,p,q;
    int i;
    L=(LNode*)malloc(sizeof(LNode));
    if(!L)return 0;
    L->next=NULL;
    q=L;
    for(i=1;i<=n;i++)
    {
        p=(LinkList)malloc(sizeof(LNode));
        printf("请输入第%d个元素的值:",i);
        scanf("%d",&(p->data));
        p->next=NULL;
        q->next=p;
        q=p;
    }
    return L;
}
void print(LinkList h)
{
    LinkList p=h->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
}

#455 入门必备 » C 练习实例71 » 2022-08-29 20:42:51

batsom
回复: 0

题目:编写input()和output()函数输入,输出5个学生的数据记录。

程序分析:无。

程序源代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct{
    char name[20];
    char sex[5];
    int  age;
}Stu;
void input(Stu*stu);
void output(Stu*stu);
int main()
{
    Stu stu[5];
    printf("请输入5个学生的信息:姓名 性别 年龄:\n");
    input(stu);
    printf("5个学生的信息如下:\n姓名  性别  年龄\n");
    output(stu);
    
    system("pause");
    return 0;
}
void input(Stu*stu)
{
    int i;
    for(i=0;i<5;i++)
        scanf("%s%s%d",stu[i].name,stu[i].sex,&(stu[i].age));
}
void output(Stu*stu)
{
    int i;
    for(i=0;i<5;i++)
        printf("%s %s %d\n",stu[i].name,stu[i].sex,stu[i].age);
}

#456 入门必备 » C 练习实例70 » 2022-08-29 20:41:54

batsom
回复: 0

题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度。

程序分析:无。

 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int len;
    char str[20];
    printf("请输入字符串:\n");
    scanf("%s",str);
    len=length(str);
    printf("字符串有 %d 个字符。",len);
}
//求字符串长度  
int length(char *s)  
{  
    int i=0;
    while(*s!='\0')
    {  
        i++;   
        s++;  
    }  
    return i;  
}

#457 入门必备 » C 练习实例69 » 2022-08-29 20:41:17

batsom
回复: 0

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

程序分析:无。

#include <stdio.h>
void main()
{
    int num[50],n,*p,j,loop,i,m,k;
    printf("请输入这一圈人的数量:\n");
    scanf("%d",&n);
    p=num;
    //开始给这些人编号
    for (j=0;j<n;j++)
    {
        *(p+j)=j+1;
    }
    i=0;//i用于计数,即让指针后移
    m=0;//m记录退出圈子的人数
    k=0;//k报数1,2,3
    while(m<n-1)//当退出的人数不大于总人数时,即留下的人数至少是一个人
        //这句不能写成m<n,因为假设有8人,当退出了6人时,此时还是进行人数退出,即m++,
        //这时是7<8,剩下的一个人自己喊1,2,3那么他也就退出了,将不会有输出
    {
        if (*(p+i)!=0)//如果这个人的头上编号不是0就开始报数加1,这里采用的方法是报数为3的人头上编号重置为0
        {
            k++;
        }
        if (k==3)
        {    k=0;    //报数清零,即下一个人从1开始报数
            *(p+i)=0;//将报数为3的人编号重置为0
            m++;    //退出人数加1
        }
        i++;      //指针后移
        if (i==n)//这句很关键,如果到了队尾,就要使指针重新指向对头
            //并且它只能放在i++后面,因为只有i++了才有可能i==n
        {
            i=0;
        }
        
        
    }
    printf("现在剩下的人是:");
    for (loop=0;loop<n;loop++)
    {
        if (num[loop]!=0)
        {
            printf("%2d号\n",num[loop]);
        }
    }
    
}

#458 入门必备 » C 练习实例68 » 2022-08-29 20:40:46

batsom
回复: 0

题目:有 n个整数,使其前面各数顺序向后移 m 个位置,最后m个数变成最前面的 m 个数。

程序分析:无。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[20];
    int i,n,offset;
    //输入数组大小和数组内容
    printf("Total numbers?\n");
    scanf("%d",&n);
    printf("Input %d numbers.\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
    //输入滚动偏移量
    printf("Set your offset.\n");
    scanf("%d",&offset);
    printf("Offset is %d.\n",offset);
    //打印滚动前数组
    print_arr(arr,n);
    //滚动数组并打印
    move(arr,n,offset);
    print_arr(arr,n);
}
 
//打印数组
void print_arr(int array[],int n)
{
    int i;
    for(i=0;i<n;++i)
        printf("%4d",array[i]);
    printf("\n");
}
//滚动数组
void move(int array[],int n,int offset)
{
    int *p,*arr_end;
    arr_end=array+n;      //数组最后一个元素的下一个位置
    int last;
    
    //滚动直到偏移量为0
    while(offset)
    {
        last=*(arr_end-1);
        for(p=arr_end-1;p!=array;--p)   //向右滚动一位
            *p=*(p-1);
        *array=last;
        --offset;
    }
}

#459 入门必备 » C 练习实例67 » 2022-08-29 20:40:02

batsom
回复: 0

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

程序分析:谭浩强的书中答案有问题。

#include<stdio.h>
#include<stdlib.h>
 
void fun(int *s,int n)
{
    int i;
    int max=s[0];
    int a=0;
    for(i=0;i<n;i++)
    {
        if(s[i]>max)
        {
            max=s[i];
            a=i;
        }
    }
    s[a]=s[0];
    s[0]=max;
    int j;
    int min=s[n-1];
    int b=n-1;
    for(j=0;j<n;j++)
    {
        if(s[j]<min)
        {
            min=s[j];
            b=j;
        }
    }
    s[b]=s[n-1];
    s[n-1]=min;
}
 
void printf_s(int *s,int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d ",s[i]);
    printf("\n");
}
 
int main()
{
    int s[20];
    int i,n;
    printf("设置数组长度(<20):");
    scanf("%d",&n);
    printf("输入 %d 个元素:\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&s[i]);
    fun(s,n);
    printf_s(s,n);
    return 0;
}

#460 入门必备 » C 练习实例66 » 2022-08-29 20:39:27

batsom
回复: 0

题目:输入3个数a,b,c,按大小顺序输出。

程序分析:利用指针方法。

# include<stdio.h>
 
void swap(int *, int *);
int main(void)
{
    int a, b, c;
    int *p1, *p2, *p3;
    printf("输入 a, b ,c:\n");
    scanf("%d %d %d", &a, &b, &c);
    p1 = &a;
    p2 = &b;
    p3 = &c;
    if(a>b)
        swap(p1, p2);
    if(a>c)
        swap(p1, p3);
    if(b>c)
        swap(p2, p3);
    printf("%d %d %d\n", a, b, c);
}
void swap(int *s1, int *s2)
{
    int t;
    t = *s1; *s1 = *s2; *s2 = t;
}

#461 入门必备 » C 练习实例65 » 2022-08-29 20:38:53

batsom
回复: 0

题目:一个最优美的图案(在TC中实现)。

程序分析:无。

程序源代码:

#include "graphics.h"
#include "math.h"
#include "dos.h"
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "stdarg.h"
#define MAXPTS 15
#define PI 3.1415926
struct PTS {
    int x,y;
};
double AspectRatio=0.85;
void LineToDemo(void)
{
    struct viewporttype vp;
    struct PTS points[MAXPTS];
    int i, j, h, w, xcenter, ycenter;
    int radius, angle, step;
    double rads;
    printf(" MoveTo / LineTo Demonstration" );
    getviewsettings( &vp );
    h = vp.bottom - vp.top;
    w = vp.right - vp.left;
    xcenter = w / 2; /* Determine the center of circle */
    ycenter = h / 2;
    radius = (h - 30) / (AspectRatio * 2);
    step = 360 / MAXPTS; /* Determine # of increments */
    angle = 0; /* Begin at zero degrees */
    for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
        rads = (double)angle * PI / 180.0; /* Convert angle to radians */
        points[i].x = xcenter + (int)( cos(rads) * radius );
        points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
        angle += step; /* Move to next increment */
    }
    circle( xcenter, ycenter, radius ); /* Draw bounding circle */
    for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
        for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
            moveto(points[i].x, points[i].y); /* Move to beginning of cord */
            lineto(points[j].x, points[j].y); /* Draw the cord */
        }
    }
}
int main()
{
    int driver,mode;
    driver=CGA;mode=CGAC0;
    initgraph(&driver,&mode,"");
    setcolor(3);
    setbkcolor(GREEN);
    LineToDemo();
}

#462 入门必备 » C 练习实例64 » 2022-08-29 20:38:02

batsom
回复: 0

题目:利用ellipse and rectangle 画图(在TC中实现)。

程序分析:无。

程序源代码:

#include "stdio.h"
#include "graphics.h"
#include "conio.h"
main()
{
    int driver=VGA,mode=VGAHI;
    int i,num=15,top=50;
    int left=20,right=50;
    initgraph(&driver,&mode,"");
    for(i=0;i<num;i++)
    {
        ellipse(250,250,0,360,right,left);
        ellipse(250,250,0,360,20,top);
        rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
        right+=5;
        left+=5;
        top+=10;
    }
    getch();
}

#463 入门必备 » C 练习实例63 » 2022-08-29 20:37:23

batsom
回复: 0

题目:画椭圆ellipse(在TC中实现)。

程序分析:无。

程序源代码:

#include "stdio.h"
#include "graphics.h"
#include "conio.h"
int main()
{
    int x=360,y=160,driver=VGA,mode=VGAHI;
    int num=20,i;
    int top,bottom;
    initgraph(&driver,&mode,"");
    top=y-30;
    bottom=y-30;
    for(i=0;i<num;i++)
    {
        ellipse(250,250,0,360,top,bottom);
        top-=5;
        bottom+=5;
    }
    getch();
}

#464 入门必备 » C 练习实例62 » 2022-08-29 20:36:49

batsom
回复: 0

题目:学习putpixel画点,(在TC中实现)。

程序分析:无。

程序源代码:

#include "stdio.h"
#include "graphics.h"
int main()
{
    int i,j,driver=VGA,mode=VGAHI;
    initgraph(&driver,&mode,"");
    setbkcolor(YELLOW);
    for(i=50;i<=230;i+=20)
        for(j=50;j<=230;j++)
            putpixel(i,j,1);
    for(j=50;j<=230;j+=20)
        for(i=50;i<=230;i++)
            putpixel(i,j,1);
}

#465 入门必备 » C 练习实例61 - 杨辉三角形 » 2022-08-29 20:36:18

batsom
回复: 0

题目:打印出杨辉三角形(要求打印出10行)。

程序分析:

结构如下所示:

#include <stdio.h>
 
int main()
{
    int i,j;
    int a[10][10];
    printf("\n");
    for(i=0;i<10;i++) {
        a[i][0]=1;
        a[i][i]=1;
    }
    for(i=2;i<10;i++)
        for(j=1;j<i;j++)
            a[i][j]=a[i-1][j-1]+a[i-1][j];
    for(i=0;i<10;i++) {
        for(j=0;j<=i;j++)
            printf("%5d",a[i][j]);
        printf("\n");
    }
}

#466 入门必备 » C 练习实例60 » 2022-08-29 20:35:41

batsom
回复: 0

题目:画图,综合例子2。(在TC中实现)。

程序分析:无。

程序源代码:

#include "graphics.h"
#define LEFT 0
#define TOP 0
#define RIGHT 639
#define BOTTOM 479
#define LINES 400
#define MAXCOLOR 15
int main()
{
    int driver,mode,error;
    int x1,y1;
    int x2,y2;
    int dx1,dy1,dx2,dy2,i=1;
    int count=0;
    int color=0;
    driver=VGA;
    mode=VGAHI;
    initgraph(&driver,&mode,"");
    x1=x2=y1=y2=10;
    dx1=dy1=2;
    dx2=dy2=3;
    while(!kbhit())
    {
        line(x1,y1,x2,y2);
        x1+=dx1;y1+=dy1;
        x2+=dx2;y2+dy2;
        if(x1<=LEFT||x1>=RIGHT)
            dx1=-dx1;
        if(y1<=TOP||y1>=BOTTOM)
            dy1=-dy1;
        if(x2<=LEFT||x2>=RIGHT)
            dx2=-dx2;
        if(y2<=TOP||y2>=BOTTOM)
                dy2=-dy2;
        if(++count>LINES)
        {
            setcolor(color);
            color=(color>=MAXCOLOR)?0:++color;
        }
    }
    closegraph();
}

#467 入门必备 » C 练习实例59 » 2022-08-29 20:34:27

batsom
回复: 0

题目:画图,综合例子。(在TC中实现)。

程序分析:无。

程序源代码:

# define PAI 3.1415926
# define B 0.809
# include "graphics.h"
#include "math.h"
int main()
{
    int i,j,k,x0,y0,x,y,driver,mode;
    float a;
    driver=CGA;mode=CGAC0;
    initgraph(&driver,&mode,"");
    setcolor(3);
    setbkcolor(GREEN);
    x0=150;y0=100;
    circle(x0,y0,10);
    circle(x0,y0,20);
    circle(x0,y0,50);
    for(i=0;i<16;i++)
    {
        a=(2*PAI/16)*i;
        x=ceil(x0+48*cos(a));
        y=ceil(y0+48*sin(a)*B);
        setcolor(2); line(x0,y0,x,y);
    }
    setcolor(3);circle(x0,y0,60);
    /* Make 0 time normal size letters */
    settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
    outtextxy(10,170,"press a key");
    getch();
    setfillstyle(HATCH_FILL,YELLOW);
    floodfill(202,100,WHITE);
    getch();
    for(k=0;k<=500;k++)
    {
        setcolor(3);
        for(i=0;i<=16;i++)
        {
            a=(2*PAI/16)*i+(2*PAI/180)*k;
            x=ceil(x0+48*cos(a));
            y=ceil(y0+48+sin(a)*B);
            setcolor(2); line(x0,y0,x,y);
        }
        for(j=1;j<=50;j++)
        {
            a=(2*PAI/16)*i+(2*PAI/180)*k-1;
            x=ceil(x0+48*cos(a));
            y=ceil(y0+48*sin(a)*B);
            line(x0,y0,x,y);
        }
    }
    restorecrtmode();
}

#468 入门必备 » C 练习实例58 » 2022-08-29 20:33:30

batsom
回复: 0

题目:学用rectangle画方形。(在TC中实现)。

程序分析:无。

程序源代码:

#include "graphics.h"
int main()
{
    int x0,y0,y1,x1,driver,mode,i;
    driver=VGA;mode=VGAHI;
    initgraph(&driver,&mode,"");
    setbkcolor(YELLOW);
    x0=263;y0=263;y1=275;x1=275;
    for(i=0;i<=18;i++)
    {
        setcolor(1);
        rectangle(x0,y0,x1,y1);
        x0=x0-5;
        y0=y0-5;
        x1=x1+5;
        y1=y1+5;
    }
    settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
    outtextxy(150,40,"How beautiful it is!");
    line(130,60,480,60);
    setcolor(2);
    circle(269,269,137);
}

#469 入门必备 » C 练习实例57 » 2022-08-29 20:32:59

batsom
回复: 0

题目:画图,学用line画直线(在TC中实现)。

程序分析:无。

程序源代码:

#include "graphics.h"
int main()
{
    int driver,mode,i;
    float x0,y0,y1,x1;
    float j=12,k;
    driver=VGA;mode=VGAHI;
    initgraph(&driver,&mode,"");
    setbkcolor(GREEN);
    x0=263;y0=263;y1=275;x1=275;
    for(i=0;i<=18;i++)
    {
        setcolor(5);
        line(x0,y0,x0,y1);
        x0=x0-5;
        y0=y0-5;   
        x1=x1+5;   
        y1=y1+5;   
        j=j+10;
    }
}

#470 入门必备 » C 练习实例56 » 2022-08-29 20:32:09

batsom
回复: 0

题目:画图,学用circle画圆形。

程序分析:无。

#include <graphics.h> //VC6.0中是不能运行的,要在Turbo2.0/3.0中  
int main()   
{  
    int driver,mode,i;   
    float j=1,k=1;   
    driver=VGA;  
    mode=VGAHI;   
    initgraph(&driver,&mode,"");   
    setbkcolor(YELLOW);   
    for(i=0;i<=25;i++)   
    {   
        setcolor(8);   
        circle(310,250,k);   
        k=k+j;   
    j=j+0.3;   
    }   
    return 0;  
}

#471 入门必备 » C 练习实例55 » 2022-08-29 20:31:30

batsom
回复: 0

题目:学习使用按位取反~。

程序分析:~0=-1; ~1=-2;

程序源代码:

#include <stdio.h>
int main()
{
    int a,b;
    a=234;
    b=~a;
    printf("a 的按位取反值为(十进制) %d \n",b);
    a=~a;
    printf("a 的按位取反值为(十六进制) %x \n",a);
    return 0;
}

#472 入门必备 » C 练习实例 54 » 2022-08-29 20:31:00

batsom
回复: 0

题目:取一个整数 a 从右端开始的 4~7 位。

程序分析:可以这样考虑:

(1)先使 a 右移 4 位。

(2)设置一个低 4 位全为 1,其余全为 0 的数,可用~(~0<<4)

(3)将上面二者进行 & 运算。

#include <stdio.h>
int main()
{
    unsigned a,b,c,d;
    printf("请输入整数:\n");
    scanf("%o",&a);
    b=a>>4;
    c=~(~0<<4);
    d=b&c;
    printf("%o\n%o\n",a,d);
    return 0;
}

#473 入门必备 » C 练习实例53 » 2022-08-29 20:30:29

batsom
回复: 0

题目:学习使用按位异或 ^。

程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0 。

程序源代码:


#include <stdio.h>
int main()
{
    int a,b;
    a=077;
    b=a^3;
    printf("b 的值为 %d \n",b);
    b^=7;
    printf("b 的值为 %d \n",b);
    return 0;
}

#474 入门必备 » C 练习实例52 » 2022-08-29 20:29:44

batsom
回复: 0

题目:学习使用按位或 |。

程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1 。

程序源代码:

#include<stdio.h>
int main()
{
    int a,b;
    a=077;
    b=a|3;
    printf("b 的值为 %d \n",b);
    b|=7;
    printf("b 的值为 %d \n",b);
    return 0;
}

#475 入门必备 » C 练习实例51 » 2022-08-29 20:29:13

batsom
回复: 0

题目:学习使用按位与 &。

程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 。

程序源代码:

#include <stdio.h>
int main()
{
    int a,b;
    a=077;
    b=a&3;
    printf("a & b(decimal) 为 %d \n",b);
    b&=7;
    printf("a & b(decimal) 为 %d \n",b);
    return 0;
}

页脚

Powered by FluxBB

本站由XREA提供空间支持