1. 首页 > 精选资讯 > 文章页面

用C语言如何实现判断圆括号是否配对(用c语言如何实现判断圆括号是否配对的方法)

C语言怎么判断几个数都相等

大家好,关于用C语言如何实现判断圆括号是否配对很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于c语言男女配对问题的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

本文目录

用C语言如何实现判断圆括号是否配对课程设计题目1:舞伴配对模拟用C语言如何实现判断圆括号是否配对

如果只有圆括号(没有[ ]或{}),不需要构造一个栈。因为用栈实现时,栈里装的都是一模一样的左括号'(',因此我们只需定义一个整型变量来记录栈中元素的个数即可。具体代码如下:

C语言,怎么判定,是不是数字

#include<stdio.h>

int main(void)

{

char input= 0;

int num= 0;/*不用栈,只记录栈中元素的个数,初始化为0*/

while(1== scanf("%c",&input))/*读入字符串,每次读一个字符存入 input中*/

{

if('('== input)

{

++num;/*相当于把左括号压栈*/

}

if(')'== input)

{

--num;/*相当于遇到右括号时弹栈*/

}

if(0> num)

{

printf("括号不匹配\n");

return 0;

}

}

if(0== num)/*读完字符串后判断“栈”是否为空*/

{

printf("括号匹配\n");

}

else

{

printf("括号不匹配\n");

}

return 0;

}

课程设计题目1:舞伴配对模拟

具体算法及相关的类型定义

typedef struct{

char name[20];

char sex;//性别,'F'表示女性,'M'表示男性

}Person;

typedef Person DataType;//将队列中元素的数据类型改为Person

void DancePartner(Person dancer[],int num)

{//结构数组dancer中存放跳舞的男女,num是跳舞的人数。

int i;

Person p;

CirQueue Mdancers,Fdancers;

InitQueue(&Mdancers);//男士队列初始化

InitQueue(&Fdancers);//女士队列初始化

for(i=0;i<num;i++){//依次将跳舞者依其性别入队

p=dancer[i];

if(p.sex=='F')

EnQueue(&Fdancers.p);//排入女队

else

EnQueue(&Mdancers.p);//排入男队

}

printf("The dancing partners are:\n\n");

while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)){

//依次输入男女舞伴名

p=DeQueue(&Fdancers);//女士出队

printf("%s",p.name);//打印出队女士名

p=DeQueue(&Mdancers);//男士出队

printf("%s\n",p.name);//打印出队男士名

}

if(!QueueEmpty(&Fdancers)){//输出女士剩余人数及队头女士的名字

printf("\n There are%d women waitin for the next round.\n",Fdancers.count);

p=QueueFront(&Fdancers);//取队头

printf("%s will be the first to get a partner.\n",p.name);

}else

if(!QueueEmpty(&Mdancers)){//输出男队剩余人数及队头者名字

printf("\n There are%d men waiting for the next round.\n",Mdacers.count);

p=QueueFront(&Mdancers);

printf("%s will be the first to get a partner.\n",p.name);

}

}//DancerPartners

参考程序如下,供大家阅读:

#include<stdio.h>

#include<malloc.h>

#include<string.h>

#define MaxNumber 100

typedef struct{

char name[20];

char sex[4];/*性别,‘F’表示女性,‘M’表示男性*/

}Person;

/*将队列中元素的数据类型改为Person*/

typedef struct

{ Person data[MaxNumber];

int front;

int rear;

}CirQueue;

CirQueue*InitQueue()

{ CirQueue*q;

q=(CirQueue*)malloc(sizeof(CirQueue));

q-> front=q-> rear=0;

return q;

}

int QueueEmpty(CirQueue*q)

{ return(q-> front==q-> rear);

}

int EnQueue(CirQueue*q,Person x)

{ if((q-> rear+1)%MaxNumber==q-> front)

{ printf("\nOverflow!\n");

return 0;

}

q-> data[q-> rear]=x;

q-> rear=(q-> rear+1)%MaxNumber;

return 1;

}

Person DeQueue(CirQueue*q)

{ Person x;

if(q-> front==q-> rear)

{ printf("\nThe queue is empty! Can't delete!\n");

return;

}

x=q-> data[q-> front];

q-> front=(q-> front+1)%MaxNumber;

return x;

}

void DancePartner(Person dancer[],int num)

{/*结构数组dancer中存放跳舞的男女,num是跳舞的人数*/

CirQueue*Mdancers,*Fdancers;

int i, count=0;

Person p;

Mdancers=InitQueue();/*男士队列初始化*/

Fdancers=InitQueue();/*女士队列初始化*/

for(i=0;i<num;i++)/*依次将跳舞者依其性别入队*/

{

p=dancer[i];

if(strcmp(p.sex,"f")==0)

EnQueue(Fdancers,p);/*排入女队*/

else EnQueue(Mdancers,p);/*排入男队*/

}

printf("\nThe dancing partners are:\n");

while(!QueueEmpty(Fdancers)&&!QueueEmpty(Mdancers))

{/*依次输入男女舞伴名*/

count++;

p=DeQueue(Fdancers);/*女士出队*/

printf("%s\t",p.name);/*打印出队女士名*/

p=DeQueue(Mdancers);/*男士出队*/

printf("%s\n",p.name);/*打印出队男士名*/

}

if(!QueueEmpty(Fdancers))/*输出女士剩余人数及队头女士的名字*/

{

printf("\n There are%d women waiting for the next round.\n

",num-2*count);

p=DeQueue(Fdancers);

printf("%s will be the first to get a partner.\n",p.name);

printf("\n");

}

if(!QueueEmpty(Mdancers))/*输出男队剩余人数及队头者名字*/

{

printf("\n There are%d men waiting for the next round.\n

",num-2*count);

p=DeQueue(Mdancers);

printf("%s will be the first to get a partner.\n",p.name);

printf("\n");

}

}

int GetDancersInfo(Person dancer[])

{ int count=0;

Person p;

while(1)

{ printf("Input the sex:\n");

scanf("%s",p.sex);

if(strcmp(p.sex,"0")==0) break;

printf("Input the name:\n");

scanf("%s",p.name);

dancer[count]=p;

count++;

}

return count;

}

void main()

{ int DancersNum;

Person Dancers[MaxNumber];

DancersNum=GetDancersInfo(Dancers);

if(DancersNum!=0) DancePartner(Dancers,DancersNum);

getch();

}

OK,本文到此结束,希望对大家有所帮助。

c语言求助,如何将这的输入作为if的判断条件

联系我们

Q Q:

微信号:

工作日:9:30-18:30,节假日休息

微信