當前位置:首頁 » 編程語言 » c語言中三位數用算術表達式求解
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言中三位數用算術表達式求解

發布時間: 2022-04-11 15:02:43

㈠ 如何用c語言數據結構的格式實現簡單的算術表達式求值程序

用棧把中綴表達式(輸入的式子)按優先順序轉為後綴表達式(逆波蘭式,即運算符在前,操作數在後),再利用棧變計算邊保存結果用於下一步計算,最後算出式子的答案
以下代碼輸入一個式子(以
=
作為輸入結束標志),輸出結果,負數如-3用0-3表示,支持高位運算
#include
<stdio.h>
#include
<stdlib.h>
#include
<math.h>
#include
<malloc.h>
#define
OK
1
#define
ERROR
-1
typedef
char
SElemType;
typedef
char
Status;
#define
STACK_INIT_SIZE
100000
#define
STACKINCREMENT
2
struct
SqStack
{
SElemType
*base;
SElemType
*top;
int
stacksize;
};
struct
SqStack1
{
int
*base;
int
*top;
int
stacksize;
};
SqStack
OPTR;
SqStack1
OPND;
char
Precede(char
c1,char
c2)
{
if(c1=='+'
||
c1=='-')
{
if(c2=='+'
||
c2=='-'
||
c2==')'
||
c2=='=')
return
'>';
else
return
'<';
}
else
if(c1=='*'
||
c1=='/')
{
if(c2=='(')
return
'<';
else
return
'>';
}
else
if(c1=='(')
{
if(c2==')')
return
'=';
else
return
'<';
}
else
if(c1==')')
return
'>';
else
if(c1=='=')
{
if(c2=='=')
return
'=';
else
return
'<';
}
else
return
'\0';
}
int
In(char
c)
{
if(c=='+'
||
c=='-'
||
c=='*'
||
c=='/'
||
c=='('
||
c==')'
||
c=='=')
return
1;
else
return
0;
}
int
Operrate(int
m,char
b,int
n)
{
switch(b)
{
case
'+':return
m+n;
case
'-':return
m-n;
case
'*':return
m*n;
case
'/':return
m/n;
}
return
0;
}
//操作數
int
InitStack1(SqStack1
&S)
{
S.base=(int
*)malloc(STACK_INIT_SIZE*sizeof(int));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return
OK;
}
int
Push1(SqStack1
&S,int
e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(int
*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+STACKINCREMENT;
}
*S.top++=e;
return
OK;
}
int
Pop1(SqStack1
&S,int
&e)
{
if(S.top==S.base)
return
ERROR;
e=*
--S.top;
return
OK;
}
int
GetTop1(SqStack1
S)
{
if(S.top==S.base)
return
ERROR;
return
*(S.top-1);
}
//算符
int
InitStack(SqStack
&S)
{
S.base=(SElemType
*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return
OK;
}
int
Push(SqStack
&S,SElemType
e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType
*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+STACKINCREMENT;
}
*S.top++=e;
return
OK;
}
int
Pop(SqStack
&S,SElemType
&e)
{
if(S.top==S.base)
return
ERROR;
e=*
--S.top;
return
OK;
}
Status
GetTop(SqStack
S)
{
if(S.top==S.base)
return
ERROR;
return
*(S.top-1);
}
int
Calculate()
{
char
c,theta,p;
int
a,b,i=0,ans,x;
InitStack(OPTR);
Push(OPTR,'=');
InitStack1(OPND);
c=getchar();
while(c!='='
||
GetTop(OPTR)!='=')
{
if(!In(c)
&&
c>='0'
&&
c<='9')
{
Push1(OPND,c-'0');
c=getchar();
while(c>='0'
&&
c<='9')
{
Pop1(OPND,x);
Push1(OPND,x*10+c-'0');
c=getchar();
}
}
else
if(In(c))
{
switch(Precede(GetTop(OPTR),c))
{
case
'<':
Push(OPTR,c);
c=getchar();
break;
case
'=':
Pop(OPTR,p);
c=getchar();
break;
case
'>':
Pop(OPTR,theta);
Pop1(OPND,b);
Pop1(OPND,a);
ans=Operrate(a,theta,b);
Push1(OPND,ans);
break;
}
}
else
{
c=getchar();
}
}
return
GetTop1(OPND);
}
int
main()
{
int
ans;
ans=Calculate();
printf("%d\n",ans);
return
0;
}

㈡ C語言中 怎麼樣進行三位數的四則運算

/* 任意加減乘除表達式,如1+2*3 */
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
void trans(char *exp,char *postexp)
{
struct
{
char data[MaxSize];
int top;
} op;
int i=0;
op.top=-1;
while(*exp!='\0')
{
switch(*exp)
{
case '(':
op.top++;op.data[op.top]=*exp;
exp++;break;
case ')':
while(op.data[op.top]!='(')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top--;exp++;break;
case '+':
case '-':
while(op.top!=-1&&op.data[op.top]!='(')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top++;op.data[op.top]=*exp;exp++;break;
case '*':
case '/':
while(op.data[op.top]=='*'||op.data[op.top]=='/')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top++;op.data[op.top]=*exp;exp++;break;
case ' ':break;
default:
while(*exp>='0'&&*exp<='9')
{
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';
}
}
while(op.top!=-1)
{
postexp[i++]=op.data[op.top];
op.top--;
}
postexp[i]='\0';
}

float compvalue(char *postexp)
{
struct
{
float data[MaxSize];
int top;
} st;
float a,b,c,d;
st.top=-1;
while(*postexp!='\0')
{
switch(*postexp)
{
case '+':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b+a;
st.top++;
st.data[st.top]=c;
break;
case '-':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b-a;
st.top++;
st.data[st.top]=c;
break;
case '*':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b*a;
st.top++;
st.data[st.top]=c;
break;
case '/':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
if(a!=0)
{
c=b/a;
st.top++;
st.data[st.top]=c;
}
else
{
printf("\n\tError of clear zero!\n");
}
break;
default:
d=0;
while(*postexp>='0'&&*postexp<='9')
{
d=10*d+*postexp-'0';
postexp++;
}
st.top++;
st.data[st.top]=d;
break;
}
postexp++;
}
return st.data[st.top];
}

int main()
{
char exp[MaxSize],postexp[MaxSize];
printf("Please input expression: ");
scanf("%s",&exp);
trans(exp,postexp);
printf("%s=%g\n",exp,compvalue(postexp));
return 0;
}

㈢ 用C語言編輸入一個3位的正整數,分別輸出它的個位,十位,百位數字

代碼如下:
#include<stdio.h>
void main()
{
int n,a,b,c;
scanf("%d",&n);
a=n; c=a%10; a/=10; b=a%10; a/=10; a%=10;
printf("%d的個位為%d,十位為%d,百位為%d。\n",n,c,b,a);
}
c語言:
1.簡介:
C語言是一種計算機程序設計語言,它既具有高級語言的特點,又具有匯編語言的特點。它由美國貝爾研究所的D.M.Ritchie於1972年推出,1978年後,C語言已先後被移植到大、中、小及微型機上,它可以作為工作系統設計語言,編寫系統應用程序,也可以作為應用程序設計語言,編寫不依賴計算機硬體的應用程序。它的應用范圍廣泛,具備很強的數據處理能力,不僅僅是在軟體開發上,而且各類科研都需要用到C語言,適於編寫系統軟體,三維,二維圖形和動畫,具體應用比如單片機以及嵌入式系統開發。
2.基本特性
1、高級語言:它是把高級語言的基本結構和語句與低級語言的實用性結合起來的工作單元。
2、結構式語言:結構式語言的顯著特點是代碼及數據的分隔化,即程序的各個部分除了必要的信息交流外彼此獨立。這種結構化方式可使程序層次清晰,便於使用、維護以及調試。C 語言是以函數形式提供給用戶的,這些函數可方便的調用,並具有多種循環、條件語句控製程序流向,從而使程序完全結構化。
4、代碼級別的跨平台:由於標準的存在,使得幾乎同樣的C代碼可用於多種操作系統,如Windows、DOS、UNIX等等;也適用於多種機型。C語言對編寫需要進行硬體操作的場合,優於其它高級語言。
5、使用指針:可以直接進行靠近硬體的操作,但是C的指針操作不做保護,也給它帶來了很多不安全的因素。C++在這方面做了改進,在保留了指針操作的同時又增強了安全性,受到了一些用戶的支持,但是,由於這些改進增加語言的復雜度,也為另一部分所詬病。Java則吸取了C++的教訓,取消了指針操作,也取消了C++改進中一些備受爭議的地方,在安全性和適合性方面均取得良好的效果,但其本身解釋在虛擬機中運行,運行效率低於C++/C。一般而言,C,C++,java被視為同一系的語言,它們長期占據著程序使用榜的前三名。
3.特有特點
1.C語言是一個有結構化程序設計、具有變數作用域(variable scope)以及遞歸功能的過程式語言。
2.C

㈣ C語言編程題目:輸入一個三位數,輸出各個數位上的數字及它們的和。

#include <stdio.h>

void main()

{

int a;

int b,c,d;//各個位數上的數字

printf("請輸入一個三位數:");

scanf("%d",&a);

b=a/100; //百位

c=(a-b*100)/10; //十位

d=a%10; //個位

printf("百位:%d ,十位:%d ,個位%d ",b,c,d);

printf("它們的和是:%d ",b+c+d);

}

㈤ 整數m是一個三位數在c語言中怎麼用關系表達式或邏輯表達式出來

b = int(m/10)%10;
a=10 && a

㈥ c語言程序設計 三位數分解求累加和

#include<stdio.h>

int main()
{
int i,n,sum=0;

scanf("%d",&n);
i=n/10;
while(i!=0)
{
sum+=n%10;
n/=10;
i=n/10;
}
sum+=n;
printf("%d\n",sum);
return 0;
}
求任意數的各位之和

㈦ 在c語言中一個三位數如何運算得到它的十位

一個條件運算符要求有3個操作對象,稱為三目運算符。
其一般形式為:
表達式1?表達式2:表達式
說明
(1)邏輯運算符的優先順序

算術運算符的優先順序

條件運算符的優先順序、賦值運算符的優先順序都高,這是此運算能夠運行的保證
(2)從左至右的運算方式,也是條件之一
(3)因為有(1)、(2)的系統強行規定和運演算法則的限定,保證了運算的次序
(4)綜合之前所說於是有:先求解表達式1
,若為不是0(即為真),則求解表達式2
的值,此時表達式2的值為整個條件表達式的值。若為0(即非真),則求解表達式3的值,此時表達式3的值為整個條件表達式的值。c=(a>b)?a:b
也就是說先判斷a>b是否成立,若成立就把a的值賦給c,否則為b在賦給c。
(5)鑒於以上所述:c=(a>b)?a:b;可改寫為:(a>b)?(c=a):(c=b);

㈧ 用c語言寫出三位數四則運算測試系統

#include<stdlib.h>
#include<stdio.h>
intmain()
{
intselect;
inta,b;
intresult;
loop:
printf("1.加法2.減法3.乘法4.除法 ");
scanf("%d",&select);
if(select==1)
{
printf("從左到右輸入兩個數字,空格鍵隔開 ");
scanf("%d%d",&a,&b);
result=a+b;
printf("計算結果是%d ",result);
gotoloop;
}
elseif(select==2)
{
printf("從左到右輸入兩個數字,空格鍵隔開 ");
scanf("%d%d",&a,&b);

if(a<b)
{
printf("出錯了 ");
gotoloop;
}
else
{
result=a-b;
printf("計算結果是%d ",result);
gotoloop;
}
}
elseif(select==3)
{
printf("從左到右輸入兩個數字,空格鍵隔開 ");
scanf("%d%d",&a,&b);
result=a*b;
printf("計算結果是%d ",result);
gotoloop;
}
elseif(select==4)
{
printf("從左到右輸入兩個數字,空格鍵隔開 ");
scanf("%d%d",&a,&b);
intdiv;
result=a/b;
div=a%b;
printf("計算結果是%d余%d ",result,div);
gotoloop;
}
else
{
printf("error ");
gotoloop;
}
}

㈨ 用C語言編寫程序「算術表達式求值」

#include <stdio.h>
#include <math.h>
enum state
;
int ctoi( char c)
bool isNum( char a)
bool isOp(char op)
{
switch(op)
{
case '+':
return true;
break;
case '-':
return true;
break;
case '*':
return true;
break;
case '/':
return true;
break;
default:
return false;
break;
}
}
bool isDot(char dot)
int checkString( char str[], double *a, double * b, char* op, int num)
{
enum state s = BEGIN;
int a_i = 0;
int b_i = 0;

double num1 = 0;
double num2 = 0;
int pointNum = 0;
for( int i = 0; i < num; ++i)
{
if(str[i] == ' ')continue;
switch(s)
{
case BEGIN:
if(isNum(str[i]))

elses = ERROR;
break;
case P2:
if(isNum(str[i]))

else if(isDot(str[i]))
{
s = P3;
}
else if(isOp(str[i]))
{
*op = str[i];
s = P5;
}
else
s = ERROR;
break;
case P3:
if(isNum(str[i]))
{
num1 = num1 + ctoi(str[i]) * pow(0.1, ++pointNum) ;
s = P4;
}
else
s = ERROR;
break;
case P4:
if(isNum(str[i]))
{
num1 = num1 + ctoi(str[i]) * pow(0.1, ++pointNum);
s = P4;
}
else if(isOp(str[i]))
{
*op = str[i];
s = P5;
}
else
s = ERROR;
break;
case P5:
if(isNum(str[i]))
{
num2 = num2 * 10 + ctoi(str[i]);
s = P6;
}
else
s = ERROR;
break;
case P6:
pointNum = 0;
if(isNum(str[i]))
{
num2 = num2 * 10 + ctoi(str[i]);
s = P6;
}
else if(isDot(str[i]))
{
s = P7;
}
else
s = END;
break;
case P7:
if(isNum(str[i]))
{
num2 = num2 + ctoi(str[i]) * pow(0.1, ++pointNum);
s = P8;
}
else
s = END;
break;
case 8:
if(isNum(str[i]))
{
num2 = num2 + ctoi(str[i]) * pow(0.1, ++pointNum);
s = P8;
}
else if(isOp(str[i]))
{
s = END;
}
else
s = END;
break;
case ERROR:
printf("express error. \n");
break;
}
if (s == END || s == ERROR)
break;
}
if(s==END)
else
}
int main()
{
char op;
double a;
double b;

char string[128] = ;
scanf("%s", &string);
printf("the expression you input is : %s. \n", string);
getchar();

if (-1 == checkString(string, &a, &b, &op, 128))
{
printf("error occur while checking expression. Be sure no space in your expression when input\n");
getchar();
return 0;
}

double result;
switch(op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if(b != 0)
result = a / b;
else
{
printf(" error! %d/%d", a, b);
return -1;
}
break;
default:
printf("undefined expression.\n");
break;
}

printf("%f %c %f = %f\n", a, op, b, result);
return 0;
}

㈩ 在c語言中一個三位數的個位 十位 百位怎麼表示

unsigned int number; /* 代表那個三位數 */

unsigned char ge,shi,; /* 代表提取結果:個,十,百 */

例子:

說明:因為7!=5040>999,所以a,b,c必然小於7。當然也可以把循環條件設置為<=9,讓電腦去處理,不影響結果。

運算結果:

**三位階乘和數有:145 **

#include<stdio.h>
long Fact(int n); /*自定義函數說明*/
void main()
{
int hundred, ten, one, m, n;
printf("三位階乘和數有:");
for (hundred = 1;hundred <= 6;hundred++)
for (ten = 0;ten <= 6;ten++)
for (one = 0;one <= 6;one++)
{
m = hundred * 100 + ten * 10 + one;
n = Fact(hundred) + Fact(ten) + Fact(one);
if (m == n) /*階乘和條件判別*/
printf("%d ", n);
}
}
long Fact(int n)
{
int i;
long s = 1;
for (i = 1;i <= n;i++)
s *= i;
return(s);
}

拓展資料

作用:C語言的運算非常靈活,功能十分豐富,運算種類遠多於其它程序設計語言。在表達式方面較其它程序語言更為簡潔,如自加、自減、逗號運算和三目運算使表達式更為簡單,但初學者往往會覺的這種表達式難讀,關鍵原因就是對運算符和運算順序理解不透不全。

當多種不同運算組成一個運算表達式,即一個運算式中出現多種運算符時,運算的優先順序和結合規則顯得十分重要。在學習中,對此合理進行分類,找出它們與數學中所學到運算之間的不同點之後,記住這些運算也就不困難了。