C 列出一份价格清单,并对其进行归纳和总结

C 列出一份价格清单,并对其进行归纳和总结,c,loops,C,Loops,所以我陷入了一个循环 我不能使用数组 我需要一个数字列表(float/double)(假设第一个数字是正数,代表一个商店商品的价格)如果下一个数字是负数,我需要从总和中减去旧价格,然后加上新价格,即旧价格减去百分比(负数),然后存储在总和中 我在尝试逻辑推理的时候迷路了 非常感谢您的帮助 我用的是C 到目前为止,我得到的是: int main() { float factor = 0, avgPrice = 0, sum = 0, productPrice = 0, temp = 0;

所以我陷入了一个循环

我不能使用数组

我需要一个数字列表(float/double)(假设第一个数字是正数,代表一个商店商品的价格)如果下一个数字是负数,我需要从总和中减去旧价格,然后加上新价格,即旧价格减去百分比(负数),然后存储在总和中

我在尝试逻辑推理的时候迷路了 非常感谢您的帮助

我用的是C

到目前为止,我得到的是:

int main() {
  float factor = 0, avgPrice = 0, sum = 0, productPrice = 0, temp = 0;
  int productCount = 0;
  printf("Enter a positive factor: ");
  scanf("%f", &factor);
  while (factor < 0.0001) {
    printf("Invalid factor. Please enter again: ");
    scanf("%f", &factor);
  }
  printf("Enter the list of products in the shopping cart: ");
  scanf("%f", &productPrice);
  if (productPrice <= 0) {
    printf("Error! Invalid input.");
    return 1;
  }
  sum = productPrice;
  productCount++;
  float sumCopy = 0;
  float priceDeduction = 0;
  temp = productPrice;
  while (scanf("%f", &productPrice) != 0) {
    if (productPrice == 0) {
      break;
    }
    sumCopy = sum;

    if (productPrice < 0) {
      sum -= temp;
      priceDeduction = (temp * productPrice) / 100;
      temp -= priceDeduction;
      sum += temp;
    }
    if (productPrice > 0) {
      productCount++;
      sum += productPrice;
      temp = productPrice;
    }
  }
  printf("Final payment: %.2f", sum);
}
intmain(){
浮动系数=0,平均价格=0,总和=0,产品价格=0,温度=0;
int productCount=0;
printf(“输入一个正系数:”);
scanf(“%f”、&factor);
而(系数<0.0001){
printf(“无效系数。请重新输入:”);
scanf(“%f”、&factor);
}
printf(“在购物车中输入产品列表:”);
scanf(“%f”和产品价格);
if(产品价格0){
productCount++;
总和+=产品价格;
温度=产品价格;
}
}
printf(“最终付款:%.2f”,总额);
}

如果我正确理解了您的问题,那么您希望找到总价。我不明白你们为什么在这段时间里写了一份Scanf声明,这是什么因素。它不在其他任何地方使用

我正在为上述声明撰写一份解决方案

#include <stdio.h>
#include <math.h>
int main() {
   char ch = 'N';
   int p, sp;
   int sum = 0;
   do
   {
      printf("Enter the product price: ");
      scanf("%d", &p);
      if (p > 0)
      {
         //temp = productPrice;
         sum += p;
      }
      else
      {
         printf("Please enter a valid first product price.\n");
         break;
      }
      printf("Enter the second product price: ");
      scanf("%d", &sp);
      if (sp > 0)
      {
         sum += sp;
      }
      else if (sp < 0)
      {
         sum += sp;
     
      }
      printf("\nAre you done with the list of products to buy? enter Y or N ");
      scanf(" %c", &ch);
   }while(ch == 'N');
   printf("The final payment is %d ", sum);
}
#包括
#包括
int main(){
char ch='N';
int p,sp;
整数和=0;
做
{
printf(“输入产品价格:”);
scanf(“%d”和“p”);
如果(p>0)
{
//温度=产品价格;
sum+=p;
}
其他的
{
printf(“请输入有效的首件产品价格。\n”);
打破
}
printf(“输入第二个产品价格:”);
scanf(“%d”和&sp);
如果(sp>0)
{
sum+=sp;
}
否则如果(sp<0)
{
sum+=sp;
}
printf(“\N您是否处理完要购买的产品列表?输入Y或N”);
scanf(“%c”和“ch”);
}而(ch='N');
printf(“最终付款为%d”,金额);
}

首先编辑您的帖子以修复缩进。如果您在(scanf(“%f”,&productPrice)!=0)时陷入此循环,请解释您的想法。参考返回值的分解使用了什么输入?嘿,如果问题的解决方案有用,那么说明它是有用的,或者如果你已经找到了问题的答案,那么请提及它。Stackoverflow就像一本百科全书。许多人使用它,其他人可能也有同样的问题。