C语言用指针编写的四则运算

作者&投稿:恭牧 (若有异议请与网页底部的电邮联系)
用简单的c语言编写四则运算题~

#include
#include
#include
int add(int num1,int num2)//加法
{
return num1+num2;
}
int subtraction(int num1,int num2)//减法
{
return num1-num2;
}
int mul(int num1,int num2)//乘法
{
return num1*num2;
}
float divis(int num1,int num2)//除法
{
return ((int)(((float)num1/num2)*100+0.5))/100.0;
}
int main()
{
int type = -1;
int data = -1;
int choice,num1,num2,results;
float div_result;
int right = 0;
int wrong = 0;
char ch;
printf("欢迎进入四则运算题,现在开始爆发你的小宇宙吧!
");
srand((unsigned int)time(NULL));
while(1)
{
printf("请选择运算类型:1.加法 2.减法 3.乘法 4.除法(保留两位小数)
");
scanf("%d",&choice);
printf("请选择位数:1.一位数 2.两位数
");
scanf("%d",&data);
switch (choice)
{
case 1:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d + %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d + %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 2:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d - %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d - %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 3:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d * %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d * %d=__?__
",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 4:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10+1;
printf("%d / %d=__?__
",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100+1;
printf("%d / %d=__?__
",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正确!
");
right++;
}
else
{
printf("回答错误!
");
wrong++;
}
printf("是否继续?y/n
");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
default :
printf("输入错误!
");
break;
}
}
end:
printf("回答正确%d题,回答错误%d题
",right,wrong);
printf("
====================谢谢光临====================!
");
system("pause");
return 0;
}
//注意除法是采用四舍五入的,有什么不明白的再问吧,望采纳!

代码如下:
#include
int main()
{
float fFloat1=.0,fFloat2=.0;
char cOP=NULL;
printf("请输入要进行四则运算表达式:
");
scanf("%f%c%f",&fFloat1,&cOP,&fFloat2);
switch(cOP)
{
case '+':
printf("%f+%f=%f
",fFloat1,fFloat2,fFloat1+fFloat2);
break;
case '-':
printf("%f-%f=%f
",fFloat1,fFloat2,fFloat1-fFloat2);
break;
case '*':
printf("%f*%f=%f
",fFloat1,fFloat2,fFloat1*fFloat2);
break;
case '/':
if(0!=fFloat2)
{
printf("%f/%f=%f
",fFloat1,fFloat2,fFloat1/fFloat2);
}
else
{
printf("error!
");
}
break;
default:
printf("error!
");
break;
}
return 0;
}

扩展资料
switch语句和if语句的区别:
1、大于等于(>=)、小于等于(<=)的判断用if语句,而等于(=)的判断用switch语句。
2、switch语句中的case类似于if…else…else if…else,但是离散值的判断。(离散值的判断自认为是等于情况的判断)。
3、switch一般都可以及用if重写,但是if不一定能用switch重写。
4、不要忘了break.C#中break不写是不行的,除了合并case的情况。
5、case 中的值必须是常量,不能是变量、表达式。
参考资料来源:
百度百科——Switch函数

不知道怎么左对齐,//后面是英文注释///后面是中文注释 给采纳!!

// EX6_08.CPP
// A program to implement a calculator

#include <stdio.h> // For input/output
#include <stdlib.h> // For the exit() function
#include <ctype.h> // For the isdigit() function
#include <string.h> // For the strcpy() function

void eatspaces(char * str); // Function to eliminate blanks
double expr(char * str); // Function evaluating an expression
double term(char * str, int * pindex); // Function analyzing a term
double number(char * str, int * pindex); // Function to recognize a number
char * extract(char * str, int * index); // Function to extract a substring

const int MAX = 80; // Maximum expression length including '\0'

int main(void)
{
char buffer[MAX]; // Input area for expression to be evaluated
char c;
int j,i;

printf("Welcome to your friendly calculator.\n");
printf("Enter an expression, or an empty line to quit.\n");

for(;;)///无开始无终止只有过程,就是不停的循环,保证可以连续输入好几个式子,如先求1+2回车直接3+2再回车,可以多次求
{///式子是一步一步来的,就是说不进行完这个语句是不会进行下一个的
i=0;
scanf("%c",&c); ///捕捉第一个数是c// Read an input line
while(c!='\n')
{
buffer[i++]=c;///,i++是先运算后自加,++i是先自加后运算,所以第一个数放在buffer[0]处
scanf("%c",&c);
}///把式子放在这个数列中,while控制回车时式子结束,此时i是

buffer[i]='\0';///式子最后是一个\o作为标志"\0代表字符数串的结束标志,最后一个在i-1的位置

eatspaces(buffer);///对式子去空格处理 // Remove blanks from input

if(!buffer[0]) // Empty line ends calculator
return 0;///已经去过空格了,buffer[0]应该是有值的,但是没有的话就直接结束,返还0

printf( "\t= %f\n\n",expr(buffer)); ///结果是expr分函数return的结果 // Output value of expression
}
}

// Function to eliminate blanks from a string
void eatspaces(char * str)///预处理,把指向数组的指针设为*str,其中数组中数的地址都是相连的,char数组每个地址差1,int数组每个差2依次类推,其中char型的指针加1就是加一,int型的加一是加二
{
int i=0; // 'Copy to' index to string
int j=0; // 'Copy from' index to string

while((*(str+i) = *(str+j++)) != '\0')///这里是对*(str+i)的一个赋值,没有空格时每次i都加1式子有空格时(str+i)和(str+j++)是相等的,
/// 当有空格时i不加一,就让空格这个地址等于下一个地址,即把式子向前推了一个字符,直到\0 // Loop while character copied is not \0
if(*(str+i) != ' ') // Increment i as long as
i++; // character is not a blank
return;
}

// Function to evaluate an arithmetic expression
double expr(char * str)///真正处理,对加减的分函数
{
double value = 0; ///value是结果,也就是真实值 // Store result here
int index = 0; // Keeps track of current character position

value = term(str, &index);///先解决第一步,因为数字还是char型根本没法算,先把第一个数变成数顺便有()*/都做完了直到看见-+ // Get first term

for(;;) ///保证了多加的情况,后同 // Infinite loop, all exits inside
{
switch(*(str+index++)) ///通过加数组对应的地址时数组一个一个往后推,碰到\0 + -做判断 其他符号输出错误 // Choose action based on current character
{
case '\0': ///一直到最后的标记,输出 // We're at the end of the string
return value; // so return what we have got

case '+': ///先把第一个数变成数然后有()就先()有/*就先/* 最后不都没有了就到-+了然后-+后面的也要先乘除啊,就value += term(str, &index);这样也能让后面的也能变成数,也能让后面的先*/依次类推 // + found so add in the
value += term(str, &index); // next term
break;

case '-': // - found so subtract
value -= term(str, &index); // the next term
break;

default: // If we reach here the string
printf("Arrrgh!*#!! There's an error.\n");///其他符号输出错误因为先进行的*、和()运算,所以再不是-+就一定错了
exit(1);
}
}
}

// Function to get the value of a term
double term(char * str, int * pindex)///对乘除的分函数
{
double value = 0; // Somewhere to accumulate the result

value = number(str, pindex); // Get the first number in the term

// Loop as long as we have a good operator
while((*(str+(*pindex))=='*')||(*(str+(*pindex))=='/'))///进来的是/或者*才继续,不然直接输出了
{

if(*(str+(*pindex))=='*') // If it's multiply,
{
++(*pindex);///是乘先加,就是乘的是后面的
value *= number(str, pindex); ///通过这样,先括号后乘除解决第一步以后的 // multiply by next number
}

if(*(str+(*pindex))=='/') // If it's divide,
{
++(*pindex);
value /= number(str, pindex); // divide by next number
}
}
return value; // We've finished, so return what we've got
}

// Function to recognize a number in a string
double number(char * str, int * pindex)///对(的分函数
{
double value = 0.0; // Store the resulting value

char * psubstr; // Pointer for substring
if(*(str + (*pindex)) == '(') // Start of parentheses
{
++(*pindex);
psubstr = extract(str, pindex);///先看后括号 // Extract substring in brackets
value = expr(psubstr); ///打回来以后再直接用大的顺序进行计算 // Get the value of the substring
return value; // Return substring value
}
///以下是将我们在数组中定义为char的数变为int形式
while(isdigit(*(str+(*pindex)))) ///isdidit是调用的<ctype.h> 这个库函数的一种函数,判断字符是否为阿拉伯数字 // Loop accumulating leading digits
value=10*value + (*(str+(*pindex)++) - 48);///因为ASC码48就是'0',也就是说'0'的值是48,而后依次是'1'到'9'。这样正好是char型的减去48就是它对应的int值
///这样层层推进,直到value是这个数为止(如125就会定义为三个char变量,先判断1是阿拉伯数字,让它乘10加2就成12,再看5是阿拉伯数字,12乘10加5就成了125,变成阿拉伯数字了)

// Not a digit when we get to here

if(*(str+(*pindex))!='.') ///如果没有小数点可以回去了,这里小数点显然无法通过上面的isdigit,就是会有123.45这原来是六个字符(1/2/3/。/4/5)通过上面变成了(123/。/4/5)其中4/5还是char型 // so check for decimal point
return value; // and if not, return value

double factor = 1.0; // Factor for decimal places
while(isdigit(*(str+(++(*pindex)))))///当检测到小数点后面是数的时候就会进行 // Loop as long as we have digits
{
factor *= 0.1; // Decrease factor by factor of 10
value=value + (*(str+(*pindex))-48)*factor;///这里直接第一轮123加0.1*4,第二轮再加0.01*5 // Add decimal place
}

return value; // On loop exit we are done
if(*(str+(*pindex))!='.') // so check for decimal point
return value; ///在此看后面有无小数点,没有就回去,有的话就双小数点,没有返回值发生错误 // and if not, return value

}

// Function to extract a substring between parentheses
// (requires string.h)
char * extract(char * str, int * pindex)///对)的分函数
{
char buffer[MAX]; // Temporary space for substring
char * pstr = NULL; ///代表一个空指针 // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = *pindex;///这个定义是 bufindex直接指向当前所指了 // Save starting value for index
do///多次循环
{
buffer[(*pindex) - bufindex] = *(str + (*pindex));///这相当于重新引进了一套目的是不会混乱
switch(buffer[(*pindex) - bufindex])
{
case ')':///当遇到)时
if(numL == 0)///第一次就代表只有()没有套((()))这种情况,((()))会一层一层的算
{
buffer[(*pindex) - bufindex] = '\0'; ///就直接算()里面的把)当成结束去算 // Replace ')' with '\0'
++(*pindex);
pstr = (char *) malloc((*pindex) - bufindex + 1);///malloc是申请使用((*pindex) - bufindex + 1)大小的空间然后指向这个空间的指针设为pstr
if (!pstr)///指针无效就会运行下面这个
{
printf("Memory allocation failed, program terminated.") ;
exit(1);
}
strcpy(pstr, buffer); /// strcpy也是一个函数复制字符串买就是吧buffer的复制到 pstr中去// Copy substring to new memory
return pstr; ///把()里面的东西直接打回去。 // Return substring in new memory
}
else
numL--; // Reduce count of '(' to be matched
break;
case '(':
numL++; ///说明有((()))这样的结构,用numl记录有几个(,有一个)减一个1 // Increase count of '(' to be // matched
break;
}
} while(*(str + (*pindex)++) != '\0'); ///没有)直接结束会输出这句话 // Loop - don't overrun end of string
printf("Ran off the end of the expression, must be bad input.\n");
exit(1);
}

你想问什么?是查错还是补全函数?问问题能不能专业点


用指针方案编写一个将两个字符串连接起来的函数,并写出能调用该函数的...
include <stdio.h> include <string.h> int main(){ char s1[40],s2[40],s[80];int len1,len2,i;printf("input string1:\\n");gets(s1);printf("input string2:\\n");gets(s2); len1=strlen(s1);len2=strlen(s2);for(i=0;i<len1;i++)s[i]=s1[i];for(i=0;i<len2;i...

C语言题目:用指针的方法,输入五位同学四门课的成绩,并输出没有不及格...
include <stdio.h> int main(){ int *f(int (*p)[4],int n);int sort(int (*p)[4]);int score[5][4];int (*p)[4],i,j,k;p=score;for(j=0;j<5;j++){ k=j+1;printf("请输入第%d个同学的成绩:",k);for(i=0;i<4;i++){ scanf("%d",*(p+j)+i);} printf("...

c语言指针超详细动画演示C语言指针教学
四、总结 指针是C语言的精髓,不能熟练地使用指针,就不算学会了C语言。在教学过程中我采用多种新型教学方法与学习方法的综合运用,能让学生掌握指针编程,编写高质量的程序。从实践看,学生学习指针的兴趣的培养,并不是一朝一夕,一蹴而就的,这需要我们教师不断地摸索,努力地更新传统的教学观念,...

c语言指针
(5)int*(*ptr)[4];\/\/指针的类型是int*(*)[4] 怎么样?找出指针的类型的方法是不是很简单? 指针所指向的类型 当你通过指针来访问指针所指向的内存区时,指针所指向的类型决定了编译器将把那片内存区里的内容当做什么来看待。 从语法上看,你只须把指针声明语句中的指针名字和名字左边的指针声明符*去掉,剩下...

c语言编程题 利用指针编写函数用选择法对整数数组排序(降序)。 求...
include<stdio.h>void Sort(int *a,int n) {int *p,*q,*v,t;for(p = a;p < a + n - 1;++p) {v = p;for(q = p + 1; q < a + n; ++q)if(*v > *q) v = q;if(*p != *v) {t = *p;*p = *v;*v = t;}}}void Show(int *a,int n) {int i;for(...

用指针法编写求 a 数组 n 个元素中的数值最大和最小的函数。 并求一维...
1、#define N 10 include <stdio.h> void main(){ int *num;int min, max;int i;printf("请输入N个数:\\n");for (i = 0; i < N; i++)scanf("%d", num + i);min=*num;max=*num;for (i = 0; i < N; i++){if(min>*num+i)2、#include <stdio.h> define N 10...

一提C语言利用指针做的题目
void strrshif(char* s, int n){ int leng = strlen(s);if(n >= leng)s = "\\0";char * tmp = new char[n + 1];for(int i=0;i<n;i++)tmp[i]=s[i];printf("result:%s", tmp);}

c语言中指针怎么使用?
1、使用场景 使用指针时,必须将它指向一个变量的地址或者为它分配空间方能使用,如下所示:include<stdio.h> include <stdlib.h> int main(int argc, char const *argv[]){ int a[5]={0,1,2,3,4};int *b,*d;int c=2;int *e=a; \/\/e指向a数组首地址 \/\/*b=2; 无法直接初始...

C语言输入a,b,c,和d共4个整数,按先大后小的顺序输出.用指针方法
include "stdio.h"#include "string.h"void compare(int* x,int* y);int main(){ int a,b,c,d; scanf("%d",&a); scanf("%d",&b); scanf("%d",&c); scanf("%d",&d); compare(&a,&b); compare(&a,&c); compare(&a,&d); compare(&b,&c)...

c语言4的个指针问题
1:include<stdio.h> define N 3 int arr[N][N]={1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9};int temp[N][N];void left();void right();void main(){ char c;printf("Input \\"l\\" to turn left, \\"r\\"to turn right \\"Q\\" to quit:\\n");while(1){ c=getch();switch (...

杂多县17015699541: C语言编程题 指针之两位数四则运算 K -
大叔炊益母: #include <stdio.h> int Calc(char *p) {/****** your code start here *****/ int x1 = 0 , x2 = 0; char op = 0; while(*p) { if(*p >= '0' && *p <='9') { if(!op) { x1 = x1*10+*p-'0'; } else { x2 = x2*10+*p-'0'; } } if(*p == '+' || *p == '-' || *p == '*' || *p == '/') op = *p;p++; }if(x1...

杂多县17015699541: c语言,指针函数的程序,能举个简单点的例子吗?越简单越好啊 -
大叔炊益母: #include /*求a+b*/ int plus(int a, int b) { return a + b; }/*求a-b*/ int minus(int a, int b) { return a - b; }/*求a*b*/ int multiply(int a, int b) { return a * b; }/*求a/b*/ int divide(int a, int b) { return a / b; } typedef int (*FUN)(int,int); /*定义基本四则运算的标准...

杂多县17015699541: c语言 指针的四则运算总结怎么写啊,请大家帮帮忙 -
大叔炊益母: #include <stdio.h>#include <math.h>int AddNumber(double *res, double left, double right){ *res = left+right; return 1;}int SubNumber(double *res, double left, double right){ *res = left-right; return 1;}int MulNumber(double *res, double left, double right)...

杂多县17015699541: 用c语言编程简单的四则运算 -
大叔炊益母: 程序里面用随机数,产生两个自然数,然后根据你需要,如果需要加,就循环上面步骤25次,每次结果打印到文件.如果需要减,也循环25次,依此类推.

杂多县17015699541: 这是一条c语言 加减乘除四则运算 程序 在线等急! -
大叔炊益母: 是内存运算吧!不清楚 ,是内存的话,p0应该是指针,0xff可能是内存地址,那个p0=num[12]是指针的运算,p0指向数组num[12],这应该不是加减乘除,x不是乘,*才是乘,那个p0应该是指针,去了*号,

杂多县17015699541: C语言编写程序四则运算法则 -
大叔炊益母: 1234567891011121314151617# include <stdio.h>int main(void){ int a,b,s; char c; scanf("%d%c%d",&a,&c,&b); switch(c) { case '+':s=a+b;break; case '-':s=a-b;break; case '*':s=a*b;break; case '/':s=a/b;break; default:return -1; } printf("%d",s); ...

杂多县17015699541: c语言 指针的四则运算总结怎么写啊,请大家帮帮忙 -
大叔炊益母: #include int AddNumber(double *res, double left, double right) { *res = left+right; return 1; } int SubNumber(double *res, double left, double right) { *res = left-right; return 1; } int MulNumber(double *res, double left, double right) { *res = left*right; return 1; ...

杂多县17015699541: C语言 四则混合运算计算器(用 指针) <高额悬赏> 满意加分 -
大叔炊益母: #include char token[61]; /*存放表达式字符串的数组*/ int n=0; void error(void) /*报告错误函数*/ { printf("ERROR!\n"); exit(1); } void match(char expected) /*检查字符匹配的函数*/ { if(token[n]==expected) token[++n]=getchar(); else error(); } double ...

杂多县17015699541: 如何用指针和数组实现C++的四则运算法则 -
大叔炊益母: 可选中1个或多个下面的关键词,搜索相关资料.也可直接点“搜索资料”搜索整个问题.四则运算指针数组c++

杂多县17015699541: 求高人指点用c语言的指针函数做一个四则混合预算... -
大叔炊益母: 建议你好好看看文件操作的C语言部分.文本文件是最简单的文件.还有汉字的存储都是两个字节的,英文字母都是一个字节,而且ascii码小于128.而汉字的第一字节ascii码大于128.每次从文件中读入80字节,遇到回车换行停止,然后统计该行的信息到指定的存储结构中.重复读取文本行,直到文件结束.最后统计信息.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网