C语言编写计算器
麻烦各位帮我用C语言编一个出题器。就是一开始有一个欢迎语,接着问你要哪一类的(有加减乘除)回答正确会出现一个恭喜你答对了,如果两次都错就出现正确答案。。麻烦了。。帮帮小弟...
麻烦各位帮我用C语言编一个出题器。就是一开始有一个欢迎语,接着问你要哪一类的(有加减乘除)回答正确会出现一个恭喜你答对了,如果两次都错就出现正确答案。。麻烦了。。帮帮小弟
展开
展开全部
#include<stdio.h>
#define NUM 1000;
main(){
int ys,i;
int a,b,c,d;
printf("welcome...\n");
printf("请选择:\n");
printf("1.加法\n");
printf("2.减法\n");
printf("3.乘法\n");
printf("4.除法\n");
scanf("%d",&ys);
srand(time(NULL));
a=rand()%NUM;
b=rand()%NUM;
if(ys==1){
c=a+b;
printf("%d+%d=",a,b);
}
else if(ys==2){
c=a-b;
printf("%d-%d=",a,b);
}
else if(ys==3){
c=a*b;
printf("%d*%d=",a,b);
}
else{
c=a/b;
printf("%d/%d=",a,b);
}
for(i=0;i<2;i++)
{
scanf("%d",&d);
if(d==c){
printf("回答正确\n");
break;
}
if(i==0){
printf("错误\n请重新输入:");
}
}
}
我做的第一版,初步测试运行正常,如果有bug,或者不符合要求可以提出.我再修改
#define NUM 1000;
main(){
int ys,i;
int a,b,c,d;
printf("welcome...\n");
printf("请选择:\n");
printf("1.加法\n");
printf("2.减法\n");
printf("3.乘法\n");
printf("4.除法\n");
scanf("%d",&ys);
srand(time(NULL));
a=rand()%NUM;
b=rand()%NUM;
if(ys==1){
c=a+b;
printf("%d+%d=",a,b);
}
else if(ys==2){
c=a-b;
printf("%d-%d=",a,b);
}
else if(ys==3){
c=a*b;
printf("%d*%d=",a,b);
}
else{
c=a/b;
printf("%d/%d=",a,b);
}
for(i=0;i<2;i++)
{
scanf("%d",&d);
if(d==c){
printf("回答正确\n");
break;
}
if(i==0){
printf("错误\n请重新输入:");
}
}
}
我做的第一版,初步测试运行正常,如果有bug,或者不符合要求可以提出.我再修改
追问
不行啊,嗯它提示有这三个错误。。
c:\documents and settings\zbh\桌面\44.c(13) : warning C4013: 'srand' undefined; assuming extern returning int
c:\documents and settings\zbh\桌面\44.c(13) : warning C4013: 'time' undefined; assuming extern returning int
c:\documents and settings\zbh\桌面\44.c(14) : warning C4013: 'rand' undefined; assuming extern returning int
追答
你用的什么编译器?
你试试在最前面加上 #include应该就没完题了.
展开全部
#include <stdio.h>
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;
link push(link stack,int value)
{
link newnode;
newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}
link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}
int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;
}
int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}
int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}
int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}
void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;
printf("\nPlease input the inorder expression:");
gets(expression);
while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;
link push(link stack,int value)
{
link newnode;
newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}
link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}
int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;
}
int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}
int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}
int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}
void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;
printf("\nPlease input the inorder expression:");
gets(expression);
while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream.h>
void main()
{
int i;
cout<<"i"<<i++<<i--<<endl;
}
这个算不算.哈哈.
void main()
{
int i;
cout<<"i"<<i++<<i--<<endl;
}
这个算不算.哈哈.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |