从两道题看如何读取字符串

二次方程

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
int cba[3],epos;
char str[100];

void Read(int &co,int &eo,int &i){
    if(str[i]==0) {eo=-1; return ;}
    int flag=0;
    if(str[i]=='=') {++i;}
     if(str[i]=='-') {++i;++flag;}
    if(str[i]=='+') ++i;
    if(i>epos) ++flag;
    if(str[i]=='x'){
        co=1;
        if(str[i+1]=='^'){i+=2; eo=str[i]-'0'; ++i;}
        else {i++; eo=1;}
    }
    else{
        co=0;
        while(str[i]>='0'&&str[i]<='9') 
            {co=co*10+str[i]-'0'; ++i;}
        if(str[i]=='x'){ 
            if(str[i+1]=='^'){i+=2; eo=str[i]-'0';++i;}
            else {i++; eo=1;} 
        }
        else {eo=0;++i;}
    }
    if(flag%2){
            co=0-co;
    }
}
int main(){
    while(scanf("%s",str)==1){
        for(int i=0;;++i){
            if(str[i]==0)break;
            if(str[i]=='=') {epos=i; break;}
        }
        int co,eo,i=0;
        memset(cba,0,sizeof(cba));
        while(1){
            Read(co,eo,i);
            if(eo==-1) break;
            cba[eo]+=co;
        }
        int a=cba[2],b=cba[1],c=cba[0];
       // printf("a:%d,b:%d,c:%d\n",a,b,c);
        double delta=b*b-4*a*c;
        if(delta<0) printf("No Solution\n");
        else {
            double x1,x2;
            x1=(0-b-sqrt(delta))/(2*a);
            x2=(0-b+sqrt(delta))/(2*a);
            if(x1>x2) swap(x1,x2);
            printf("%.2f %.2f\n",x1,x2);
        }
    }
    return 0;
}

表达式求解

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int mat[5][5]={
    1,0,0,0,0,
    1,0,0,0,0,
    1,0,0,0,0,
    1,1,1,0,0,
    1,1,1,0,0,
};
char s[1000];

stack<int> op;
stack<double> num;
void getop(bool &isop,int &retn,int &cur){
    if(cur==0&&op.empty()||s[cur]==0){isop=1;retn=0; return ;}
    char ch=s[cur];
    if(ch>='0'&&ch<='9'){
        isop=0;
    }
    else{
        isop=1;
        if(ch=='+') retn=1;
        else if(ch=='-') retn =2;
        else if(ch=='*') retn=3;
        else if(ch=='/') retn = 4;
        cur+=2; 
        return ;
    }
    retn=0;
    while(s[cur]!=' '&&s[cur]!=0){
        retn=retn*10+s[cur++]-'0';
    }
    if(s[cur]==' ') ++cur;
}

int main(){
    while(gets(s)){
        while(op.size()) op.pop();
        while(num.size()) num.pop();
        if(s[0]=='0'&&s[1]==0) break;
        bool isop;
        int retn,cur=0;
        while(1){
            getop(isop,retn,cur);
            if(isop){
                if(op.size()==0||mat[retn][op.top()]) op.push(retn);
                else{
                    while(!mat[retn][op.top()]){
                        int tmpop=op.top(); op.pop();
                        double b=num.top(); num.pop();
                        double a=num.top(); num.pop();
                        double tmpnum;
                        if(tmpop==1) tmpnum=a+b;
                        if(tmpop==2) tmpnum=a-b;
                        if(tmpop==3)tmpnum=a*b;
                        if(tmpop==4) tmpnum=a/b;
                        num.push(tmpnum);
                        //printf("%f\n",tmpnum);

                    }
                     op.push(retn);
                }
            }
            else num.push(retn);
            if(op.size()==2&&op.top()==0) break;
        }
        printf("%.2f\n",num.top());
    }
    re

如何寻找单位读取单元,抽象出公共范式,是关键!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值