首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

自己练手写的一个最简单的加减法的程序,但是算不出结果,求帮助解决办法

2012-04-09 
自己练手写的一个最简单的加减法的程序,但是算不出结果,求帮助C/C++ code#include stdio.h#include std

自己练手写的一个最简单的加减法的程序,但是算不出结果,求帮助

C/C++ code
#include <stdio.h>#include <stdlib.h>int progress(char *str){    int m;    int i;    m=strlen(str);//算出输入字符串的长度        for(i=0;i<m;i++)//进行M-1次循环当循环到M-1次时得到最终答案            {                if(str[i]=='+')                str[i+1]=str[i-1]+str[i+1];                if(str[i]=='-')                str[i+1]=str[i-1]-str[i+1];            }            return str[i+1];}int main(){    char str[81];    gets(str);//读入一串仅包含数字与加减符号的字符串    printf("%d",progress(str));}


[解决办法]
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
float num1,num2,jg;
char sym;
printf("please enter the symbol:\n");
fflush(stdin);
scanf("%c",&sym);
printf("Please enter the first number:\n");
scanf("%f",&num1);
printf("please enter the second number:\n");
scanf("%f",&num2);
switch(sym)
{
case '+':jg=num1+num2;break;
case '-':jg=num1-num2;break;
}
printf("\n%5.2f\n",jg);
return 0;
}


调试通过的

这是最简单的了 你可以自己修改

热点排行
Bad Request.