注:如果在你的脑上运行出错,请改一下头文件,你的电脑可能不支持万能头文件。
修改成:#include<iostream> #include<cstdio>
1.普通的a+b
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
char c;
cin>>a>>c>>b;
if(c=='+')
cout<<a+b<<endl;
if(c=='-')
cout<<a-b<<endl;
if(c=='*')
cout<<a*b<<endl;
if(c=='/')
cout<<(double)a/b<<endl;
if(c=='%')
cout<<a%b<<endl;
return 0;
}
2.动态数组的实现
// 程 式 名: DyArray.c
// 程式功能: 动态数组的实现
// 功能描述: 动态数组的创建与使用
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main()
{
int *array = 0, num, i;
printf("please input the number of element: ");
scanf("%d", &num);
// 申请动态数组使用的内存块
array = (int *)malloc(sizeof(int)*num);
if (array == 0) // 内存申请失败,提示退出
{
printf("out of memory,press any key to quit...\n");
exit(0); // 终止程序运行,返回操作系统
}
// 提示输入num个数据
printf("please input %d elements: ", num);
for (i = 0; i < num; i++)
scanf("%d", &array[i]);
// 输出刚输入的num个数据
printf("%d elements are: \n", num);
for (i = 0; i < num; i++)
printf("%d,", array[i]);
printf("\b \n"); // 删除最后一个数字后的分隔符逗号
free(array); // 释放由malloc函数申请的内存块
system("pause");
return 0;
}
3.斐波那锲数列非递归实现
#include<bits/stdc++.h>
using namespace std;
int main(){
int a1=1,a2=1,a,k;
while(true){
printf("请输入你想知道斐波那锲数列的第几项:");
cin >> k;
if(k==1 || k==2) cout << 1 <<endl;
else{
for(int i=3;i<=k;i++){
a=a1+a2;
a1=a2;
a2=a;
}
cout << a << endl;
}
a1=1,a2=1;
}
return 0;
}
4.斐波那锲数列递归实现
#include<bits/stdc++.h>
using namespace std;
int Fib(int n){
if(n==1||n==2) return 1;
else return Fib(n-1)+Fib(n-2);
}
int main(){
int n;
while(true){
printf("请输入你想知道斐波那锲数列的第几项:");
scanf("%d",&n);
printf("%d\n",Fib(n));
}
return 0;
}
5.N的阶乘非递归实现
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int t=n,fact=1;
while(n>1){
fact*=n;
n--;
}
printf("%d的阶乘是%d\n",t,fact);
return 0;
}
5.N的阶乘递归实现
#include<stdio.h>
int Fact(int t){
int sum;
if(t==1) sum=1;
else{
sum=t*Fact(t-1);
}
return sum;
}
int main(){
int n,sum;
while(~scanf("%d",&n)){
sum=Fact(n);
printf("%d\n",sum);
}
return 0;
}
6.进制转化(非递归实现)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn];
int main(){
int n,t;
while(~scanf("%d",&n)){
if(n==0){
printf("0\n");
continue;
}
t=0;
while(n!=0){
a[t]=n%2;
n/=2;
t++;
}
for(int i=t-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
return 0;
}
7.进制转化(递归实现)
#include<bits/stdc++.h>
using namespace std;
int Fib(int i){
int b;
b = i%2;
if(i<2){
printf("%d",b);
}else{
i = i/2;
Fib(i);
printf("%d",b);
}
}
int main(){
int n;
while(~scanf("%d",&n)){
Fib(n);
printf("\n");
}
return 0;
}
8.四则运算
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef char SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)//初始化
{
S.base=(SElemType*)malloc(sizeof(SElemType)*MAXSIZE);
if(!S.base)
exit(OVERFLOW);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)//入栈
{
if(S.top-S.base==S.stacksize)
return ERROR;//栈满
*S.top++=e;//元素e压入栈顶,栈顶指针加1;
return OK;
}
Status Pop(SqStack &S,SElemType &e)//出栈
{
if(S.top==S.base)//栈空
return ERROR;
e=*--S.top;//栈顶指针减一,将栈顶元素赋给e;
return OK;
}
SElemType GetTop(SqStack S)//取栈顶元素
{
if(S.top!=S.base)//栈非空
return *(S.top-1);//返回栈顶元素的值,栈顶指针不变
}
int In(SElemType e)//判断读入字符是否为运算符
{
if(e=='+'||e=='-'||e=='*'||e=='/'||e=='('||e==')'||e=='#')
return 1;//是
else
return 0; //不是
}
SElemType Precede(SElemType a,SElemType b)//比较运算符的优先级
{
SElemType f;
if(a=='+'||a=='-')
{
if(b=='+'||b=='-'||b==')'||b=='#')
f='>';
else if(b=='*'||b=='/'||b=='(')
f='<';
}
else if(a=='*'||a=='/')
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#')
f='>';
else if(b=='(')
f='<';
}
else if(a=='(')
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(')
f='<';
else if(b==')')
f='=';
}
else if(a==')')
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#')
f='>';
}
else if(a=='#')
{
if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(')
f='<';
else if(b=='#')
f='=';
}
return f;
}
SElemType Operate(SElemType a,SElemType theta,SElemType b)//运算
{
SElemType c;
a=a-'0';
b=b-'0';
if(theta=='+')
c=a+b+'0';
else if(theta=='-')
c=a-b+'0';
else if(theta=='*')
c=a*b+'0';
else if(theta=='/')
c=a/b+'0';
return c;
}
int EvaluateExpression()
{
SqStack OPND,OPTR;
char ch,a,b,theta,x;
InitStack(OPND);//寄存操作数和运算结果
InitStack(OPTR);//寄存运算符
Push(OPTR,'#');
ch=getchar();
while(ch!='#'||GetTop(OPTR)!='#')
{
if(!In(ch))
{
Push(OPND,ch);
ch=getchar();
}
else
{
switch(Precede(GetTop(OPTR),ch))
{
case '<':
Push(OPTR,ch);
ch=getchar();
break;
case '>':
Pop(OPTR,theta);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
case '=':
Pop(OPTR,x);
ch=getchar();
break;
}
}
}
return GetTop(OPND)-'0';
}
int main()
{
printf("请输入算术表达式,并以#结束\n");
printf("结果是: %d\n",EvaluateExpression());
return 0;
}