- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到了我的函数在太多行上运行的情况。显然有重复因素,我还不确定如何分解它。我试过将相同的代码放入另一个函数中,但它占用的代码行数几乎相同。有更好的分解方法吗?谢谢。
int purchase_meal() {
FILE *fptr;
char meal_choice[5];
char *item;
float price, sumtax, tax;
unsigned int quantity;
float total=0, grand_total=0;
while(strcmp(meal_choice, "-1") != 0) {
printf("Please enter a meal order option and how many orders. Example: C0001 2 \n");
scanf("%s %d", meal_choice, &quantity);
if (meal_choice[0] == '-' && meal_choice[1] == '1') {
print_receipt();
order();
}
else if (meal_choice[0] == 'C' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
fptr = fopen("receipt.txt", "a+"); //opens receipt txt file and appends
if(fptr == NULL) //check whether the file is empty or not
{
printf("File cannot be found\n");
}
else {
switch (meal_choice[4]) {
case '1': // assuming they chose set 1 with code C0001
price = combo1.price;
item = combo1.name;
tax = taxing(combo1.price, quantity);
total = (combo1.price * quantity) + tax;
grand_total += total;
sumtax += tax;
csum.combo_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo1.name, combo1.price);
print_order(quantity, item, price, grand_total);
break;
case '2':
price = combo2.price;
item = combo2.name;
tax = taxing(combo2.price, quantity);
total = (combo2.price * quantity) + tax;
grand_total += total;
sumtax += tax;
csum.combo_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo2.name, combo2.price);
print_order(quantity, item, price, grand_total);
break;
case '3':
price = combo3.price;
item = combo3.name;
tax = taxing(combo3.price, quantity);
total = (combo3.price * quantity) + tax;
grand_total += total;
sumtax += tax;
csum.combo_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo3.name, combo3.price);
print_order(quantity, item, price, grand_total);
break;
case '4':
price = combo4.price;
item = combo4.name;
tax = taxing(combo4.price, quantity);
total = (combo4.price * quantity) + tax;
grand_total += total;
sumtax += tax;
csum.combo_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo4.name, combo4.price);
print_order(quantity, item, price, grand_total);
break;
case '5':
price = combo5.price;
item = combo5.name;
tax = taxing(combo5.price, quantity);
total = (combo5.price * quantity) + tax;
grand_total += total;
sumtax += tax;
csum.combo_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%-15d%-27s%-17.2f\n", quantity, combo5.name, combo5.price);
print_order(quantity, item, price, grand_total);
break;
default:
puts("Invalid meal option!");
}
fclose(fptr); //close the file
}
}
else if (meal_choice[0] == 'A' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
fptr = fopen("receipt.txt", "a"); //opens txt file and READS only
if(fptr == NULL) //check whether the file is empty or not
{
printf("File cannot be found\n");
} else {
switch (meal_choice[4]) {
case '1': // assuming they chose set 1 with code C0001
price = addon1.price;
item = addon1.name;
tax = taxing(addon1.price, quantity);
total = (addon1.price * quantity) + tax;
grand_total += total;
sumtax += tax;
asum.ala_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon1.name, addon1.price);
print_order(quantity, item, price, grand_total);
break;
case '2':
price = addon2.price;
item = addon2.name;
tax = taxing(addon2.price, quantity);
total = (addon2.price * quantity) + tax;
grand_total += total;
sumtax += tax;
asum.ala_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon2.name, addon2.price);
print_order(quantity, item, price, grand_total);
break;
case '3':
price = addon3.price;
item = addon3.name;
tax = taxing(addon3.price, quantity);
total = (addon3.price * quantity) + tax;
grand_total += total;
sumtax += tax;
asum.ala_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon3.name, addon3.price);
print_order(quantity, item, price, grand_total);
break;
case '4':
price = addon4.price;
item = addon4.name;
tax = taxing(addon4.price, quantity);
total = (addon4.price * quantity) + tax;
grand_total += total;
sumtax += tax;
asum.ala_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon4.name, addon4.price);
print_order(quantity, item, price, grand_total);
break;
case '5':
price = addon5.price;
item = addon5.name;
tax = taxing(addon5.price, quantity);
total = (addon5.price * quantity) + tax;
grand_total += total;
sumtax += tax;
asum.ala_trans++;
sum.total = grand_total;
sum.tax = sumtax;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon5.name, addon5.price);
print_order(quantity, item, price, grand_total);
break;
default:
puts("Invalid meal option!");
}
fclose(fptr); //close the file
} //end else for file found
} //end else for menu option A or C
} //end of while loop
return 0;
}
最佳答案
所以这是我所能做的最好的重构,方法是将求和放在 switch 语句的末尾。是的,验证适用于不接受无效的膳食代码,例如 C0001、C0002、C0003、C0004、C0005、A0001、A0002、A0003、A0004、A0005(如果您想知道什么是膳食代码),但是如果用户输入是 float 的,它会导致一个我仍在研究的无限循环。我还添加了信息以退出循环,标记值为 -1。感谢大家的友好反馈。
int purchase_meal() {
FILE *fptr; //flie pointer for receipt text file
char meal_choice[5]; //user input meal code
char *item;
float price;
unsigned int quantity=0;
float total=0, grand_total=0;
while(strcmp(meal_choice, "-1") != 0) {
printf("Please enter a meal order option (-1 to exit). Example: C0001.\n");
scanf("%s", meal_choice);
if (meal_choice[0] == '-' && meal_choice[1] == '1') {
print_receipt();
order();
}
else if (strlen(meal_choice) == 5 && meal_choice[0] == 'C' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0'){
printf("Enter quantity of order. \n");
scanf("%d", &quantity);
fptr = fopen("receipt.txt", "a+"); //opens receipt txt file and appends
if(fptr == NULL) //check whether the file is empty or not
{
printf("File cannot be found\n");
}
else {
switch (meal_choice[4]) {
case '1': // assuming they chose set 1 with code C0001
price = combo1.price;
item = combo1.name;
total = combo1.price * quantity;
grand_total += total;
csum.combo_trans++;
fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo1.name, combo1.price);
print_order(quantity, item, price, grand_total);
break;
case '2':
price = combo2.price;
item = combo2.name;
total = combo2.price * quantity;
grand_total += total;
csum.combo_trans++;
fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo2.name, combo2.price);
print_order(quantity, item, price, grand_total);
break;
case '3':
price = combo3.price;
item = combo3.name;
total = combo3.price * quantity;
grand_total += total;
csum.combo_trans++;
fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo3.name, combo3.price);
print_order(quantity, item, price, grand_total);
break;
case '4':
price = combo4.price;
item = combo4.name;
total = combo4.price * quantity;
grand_total += total;
csum.combo_trans++;
fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo4.name, combo4.price);
print_order(quantity, item, price, grand_total);
break;
case '5':
price = combo5.price;
item = combo5.name;
total = combo5.price * quantity;
grand_total += total;
csum.combo_trans++;
fprintf(fptr, "%-15u%-27s%-17.2f\n", quantity, combo5.name, combo5.price);
print_order(quantity, item, price, grand_total);
break;
default:
total =0;
}
fclose(fptr); //close the file
}
}
else if (strlen(meal_choice) == 5 && meal_choice[0] == 'A' && meal_choice[1] == '0' && meal_choice[2] == '0' && meal_choice[3] == '0') {
printf("Enter quantity of order. \n");
scanf("%d", &quantity);
fptr = fopen("receipt.txt", "a"); //opens txt file and READS only
if(fptr == NULL) //check whether the file is empty or not
{
printf("File cannot be found\n");
} else {
switch (meal_choice[4]) {
case '1': // assuming they chose set 1 with code C0001
price = addon1.price;
item = addon1.name;
total = addon1.price * quantity;
asum.ala_trans++;
grand_total += total;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon1.name, addon1.price);
print_order(quantity, item, price, grand_total);
break;
case '2':
price = addon2.price;
item = addon2.name;
total = addon2.price * quantity;
asum.ala_trans++;
grand_total += total;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon2.name, addon2.price);
print_order(quantity, item, price, grand_total);
break;
case '3':
price = addon3.price;
item = addon3.name;
total = addon3.price * quantity;
asum.ala_trans++;
grand_total += total;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon3.name, addon3.price);
print_order(quantity, item, price, grand_total);
break;
case '4':
price = addon4.price;
item = addon4.name;
total = addon4.price * quantity;
asum.ala_trans++;
grand_total += total;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon4.name, addon4.price);
print_order(quantity, item, price, grand_total);
break;
case '5':
price = addon5.price;
item = addon5.name;
total = addon5.price * quantity;
asum.ala_trans++;
grand_total += total;
fprintf(fptr, "%d\t%s\t\t\t%.2f\n", quantity, addon5.name, addon5.price);
print_order(quantity, item, price, grand_total);
break;
default:
total =0;
}
fclose(fptr); //close the file
} //end else for file found
} else {
quantity = 0; //declare quantity 0 because to prevent dummy data
price = 0; //declare price of certain order 0 because to prevent added into total
total =0; //declare total in order 0 because to prevent added into grand total
printf("Invalid meal code. \n");
} //end else for menu option A or C
sum.total = grand_total;
sum.grand_total += total;
} //end of while loop
return 0;
}
关于c - 分解 C 中的代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46832169/
我正在尝试在 R 中计算任意 N x J 矩阵 S 的投影矩阵 P: P = S (S'S) ^ -1 S' 我一直在尝试使用以下函数来执行此操作: P 概述 solve 基于一般方阵的 LU 分解
所以我有一个包含数千行的非常旧的文件(我猜是手工生成的),我正试图将它们移动到一个 rdb 中,但是这些行没有转换为列的格式/模式。例如,文件中的行如下所示: blah blahsdfas
这实际上只是一个“最佳实践”问题...... 我发现在开发应用程序时,我经常会得到很多 View 。 将这些 View 分解为几个 View 文件是常见的做法吗?换句话说......而不只是有view
使用以下函数foo()作为简单示例,如果可能的话,我想将...中给出的值分配给两个不同的函数。 foo args(mapply) function (FUN, ..., MoreArgs = NUL
正面案例:可以进入列表 groovy> println GroovySystem.version groovy> final data1 = [[99,2] , [100,4]] groovy> d
省略素数计算方法和因式分解方法的详细信息。 为什么要进行因式分解? 它的应用是什么? 最佳答案 哇,这个线程里有这么多争斗。 具有讽刺意味的是,这个问题有一个主要的有效答案。 因式分解实际上在加密/解
术语“分解不良”和“重构”程序是什么意思?你能举一个简单的例子来理解基本的区别吗? 最佳答案 重构是一种通用技术,可以指代许多任务。它通常意味着清理代码、去除冗余、提高代码质量和可读性。 分解不良代码
我以前有,here ,表明 C++ 函数不容易在汇编中表示。现在我有兴趣以一种或另一种方式阅读它们,因为 Callgrind 是 Valgrind 的一部分,在组装时显示它们已损坏。 所以我想要么破坏
最初,我一直在打开并同时阅读两个文件,内容如下: with open(file1, 'r') as R1: with open(file2, 'r') as R2: ### m
我正在尝试摆脱 标签和标签内的内容使用 beatifulsoup。我去看了文档,似乎是一个非常简单的调用函数。有关该功能的更多信息是 here .这是我到目前为止解析的 html 页面的内容...
给定一个 float ,我想将它分成几个部分的总和,每个部分都有给定的位数。例如,给定 3.1415926535 并要求将其分成以 10 为基数的部分,每部分 4 位数字,它将返回 3.141 + 5
我的 JSF 项目被部署为一个 EAR 文件。它还包括一些 war 文件。我需要 EAR 的分解版本(包括分解的内部 WAR)。 有什么工具可以做到吗? 最佳答案 以编程方式还是手动? EAR 和 W
以下函数不使用行透视进行 LU 分解。 R 中是否有一个现有的函数可以使用行数据进行 LU 分解? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 7年前关闭。 Improve this
我正在使用登记数据进行病假研究。从登记册上,我只得到了每个人的病假开始日期和结束日期。但日期并没有逐年分割。例如,对于人 A,只有开始日期 (1-may-2016) 和结束日期 (14-feb-201
我发现以下 R 代码使用 qr 因式分解无法恢复原始矩阵。我不明白为什么。 a <- matrix(runif(180),ncol=6) a[,c(2,4)] <- 0 b <- qr(a) d <-
我正在尝试检测气候数据时间序列中的异常值,其中一些缺失的观测值。在网上搜索我发现了许多可用的方法。其中,STL 分解似乎很有吸引力,因为它去除了趋势和季节性成分并研究了其余部分。阅读 STL: A S
我想使用 javascript 分解数组中的 VIN,可能使用正则表达式,然后使用某种循环... 以下是读取 VIN 的方法: http://forum.cardekho.com/topic/600-
我正在研究 Databricks 示例。数据框的架构如下所示: > parquetDF.printSchema root |-- department: struct (nullable = true
我正在尝试简化我的代码并将其分解为多个文件。例如,我设法做到了: socket.once("disconnect", disconnectSocket); 然后有一个名为 disconnectSock
我是一名优秀的程序员,十分优秀!