删除字符串表达式空格以及对包含加减运算符字符串进行计算结果

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 // 指针法
  6 void pointeatspace(char *str)// 对字符串做去除空格处理
  7 {
  8     char *p1 = str;
  9     char *p2 = str;
 10     /*
 11         while (*p1!='\0')
 12         {
 13             *p1 = *p2;
 14             if (*p1!=' ')
 15             {
 16                 p1++;
 17             }
 18             p2++;
 19         }
 20     */
 21 
 22     // 以上while语句可改写为
 23     while ((*p1=*(p2++))!='\0')
 24     {
 25         if (*p1 != ' ')
 26         {
 27             p1++;
 28         }
 29     }
 30 }
 31 
 32 // 下标法
 33 void indexeatspace(char *str)// 对字符串做去除空格处理
 34 {
 35     int i = 0;
 36     int j = 0;
 37     /*
 38         while (str[i]!='\0')
 39         {
 40             str[i] = str[j];
 41             if (str[i]!=' ')
 42             {
 43                 i++;
 44             }
 45             j++;
 46         }
 47     */
 48 
 49     // 以上while语句可改写为
 50     while ((str[i]=str[j++])!='\0')
 51     {
 52         if (str[i]!=' ')
 53         {
 54             i++;
 55         }
 56     }
 57 }
 58 
 59 int isnum(char ch)// 判断是否是数字
 60 {
 61     int is = 0;
 62     if (ch<='9' && ch>='0')
 63     {
 64         is = 1;
 65     }
 66     return is;
 67 }
 68 
 69 double getnum(char *str,int *pindex)
 70 {
 71     double value = 0.0;// 保存读取的结果
 72     int index = *pindex;
 73     while (isnum(*(str+index)))// 循环判断每一个字符是否是数字
 74     {
 75         value = value * 10 + (str[index] - '0');// 字符转整数
 76         index++;// 往前移动
 77     }
 78 
 79     if (*(str+index)=='.')
 80     {
 81         double xiaoshu = 1.0;
 82         while (isnum(*(str+ ++index))) // 循环到小数点后面的非数字
 83         {
 84             xiaoshu /= 10;
 85             value += xiaoshu*(*(str + index) - '0');
 86         }
 87     }
 88     *pindex = index;
 89     return value;
 90 }
 91 
 92 double calc(char *str)
 93 {
 94     double value = 0.0;// 保存计算结果
 95     int index = 0;// 获取字符的位置
 96     value = getnum(str, &index);// 根据位置变化获取每一个字符并且转换为整数
 97 
 98     while (1)
 99     {
100         char ch = *(str + index);// 取出字符
101         index++;// 循环遍历
102         switch (ch)
103         {
104         case '\0':
105         {
106            return value;
107         }
108         case '+':
109         {
110             value += getnum(str, &index);
111             break;
112         }
113 
114         case '-':
115         {
116             value -= getnum(str, &index);
117             break;
118         }
119         default:
120             break;
121         }
122     }
123 }
124 
125 void main()
126 {
127     char str[1024] = { 0 };
128     printf("请输入需要计算的加减字符串表达式:\n");
129     scanf("%[^\n]", str);
130     printf("你需要进行计算的是: %s\n\n",str);
131     //indexeatspace(str);
132     //printf("去除空格后的字符串表达式是: %s\n",str);
133 
134     pointeatspace(str);
135     printf("去除空格后的字符串表达式是: %s\n\n", str);
136 
137     int index = 0;// 表示获取数据的起始位置
138     double value = getnum(str,&index);
139     printf("获取的第一个数据是: %f\n\n",value);
140 
141     printf("表达式计算后的值为: %f\n\n", calc(str));
142     system("pause");
143 }
144 
145 /***************************计算结果*******************/
146 /*
147     
148     请输入需要计算的加减字符串表达式:
149     你需要进行计算的是: 1.1 + 2.2 + 3 + 1 + 4
150 
151     去除空格后的字符串表达式是: 1.1+2.2+3+1+4
152 
153     获取的第一个数据是: 1.100000
154 
155     表达式计算后的值为: 11.300000
156 
157     请按任意键继续. . .
158 
159 */

 

posted on 2015-05-15 15:41  Dragon-wuxl  阅读(313)  评论(0)    收藏  举报

导航