简体   繁体   English

为什么测试用例没有通过这个 c 程序?

[英]Why are test cases not passing in this c program?

Create a function that passes the Item's Name, Quantity, and Price.创建一个传递项目名称、数量和价格的 function。 And function should calculate the total amount (Quantity* Price) and print it.而 function 应该计算总金额(数量*价格)并打印出来。 My Code-我的代码-

#include <stdio.h> 
void displayString(char str[]); 

int main() 
{ 
    int cal(float,int);
    char str[50];
    float pri,c;
    int quan;
    scanf("%s",str);
    scanf("%d",&quan);
    scanf("%f",&pri); 
    c = cal(pri,quan);
    printf("Item name: %s, Price: %.2f, Quantity: %d\n",str,pri,quan);
    printf("Total Amount: %.2f",c);
}

int cal(float x,int y)
{
    float z;
    z = x * y;
    return(z);
}

Test case: output Item name: muruga, Price: 2.50, Quantity: 5测试用例:output 商品名称:muruga,价格:2.50,数量:5
Total Amount: 12.50总量:12.50

My Output Item name: muruga, Price: 2.50, Quantity: 5 Total Amount: 12.00我的 Output 商品名称:muruga 价格:2.50 数量:5 总金额:12.00

The return type of cal function should be float and also function cal prototype should be outside main and it is better to have a printf statement giving an idea about input to program. cal function 的返回类型应该是 float 并且 function cal原型应该在 main 之外并且最好有一个printf语句给出关于程序输入的想法。



#include <stdio.h> 
void displayString(char str[]); 

float cal(float,int);  /*Function prototype should be outside main and
this function should have a return type of float and not int */

int main() 
{ 

    char str[50];
    float pri,c;
    int quan;
    printf("Enter name\n");
    scanf("%s",str);
    printf("Enter Quantity\n" );
    scanf("%d",&quan);
    printf("Enter price\n");
    scanf("%f",&pri); 
    c=cal(pri,quan);
    printf("Item name: %s, Price: %.2f, Quantity: %d\n",str,pri,quan);
    printf("Total Amount: %.2f\n",c);
}

float cal(float x,int y)  //Changed return type
{
    float z;
    z = x * y;
    return(z);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM