[UESTC-信软] iCoding F1 Answer 实验一 计算税金(Tax) 编写一个程序,要求用户输入一个美元数量,然后显示出增加 5% 税率后的相应金额。
输出范例:
Enter an amount: 100.00 with tax added: $105.00
输入的金额为大于 0 的两位数浮点数。
1 2 3 4 5 6 7 8 9 10 #include <stdio.h> int main () { float a; printf ("Enter an amount: " ); scanf ("%f" , &a); a *= 1.05 ; printf ("with tax added: $%.2f" , a); }
计算账单(Bills) 编写⼀个程序,要求用户输出⼀个美元数量,然后显示出如何用最少20美元、10美元、5美元和1美元来付款。
输出范例:
Enter a dollar amount: 93 $20 bills: 4 $10 bills: 1 $5 bills: 0 $1 bills: 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> int main () { int dollar, b, c, d, e; printf ("Enter a dollar amount: " ); scanf ("%d" , &dollar); b = dollar / 20 ; c = (dollar - b * 20 ) / 10 ; d = (dollar - b * 20 - c * 10 ) / 5 ; e = dollar - b * 20 - c * 10 - d * 5 ; printf ("$20 bills: %d\n" , b); printf ("$10 bills: %d\n" , c); printf ("$5 bills: %d\n" , d); printf ("$1 bills: %d\n" , e); }
还贷计算(Loan Balance) 编程计算第一、第二、第三个月还贷后剩余的贷款金额。
输出范例:
Enter amout of loan: 20000.00 Enter interest rate: 6.0 Enter monthly payment: 386.66
Balance remaining after first payment: $19713.34 Balance remaining after second payment: $19425.25 Balance remaining after third payment: $19135.71
提示:每个月的贷款余额减去还款金额后,还需要加上贷款余额与月利率的乘积。月利率的计算方法是把用户输入的利率转换成百分数再除以12。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> float loanCount (float loan, float rate, float payment) { loan = (loan - payment) + loan * rate; return loan; }int main () { float loan, rate, payment; printf ("Enter amout of loan: " ); scanf ("%f" , &loan); printf ("Enter interest rate: " ); scanf ("%f" , &rate); printf ("Enter monthly payment: " ); scanf ("%f" , &payment); rate = rate / 100 / 12 ; loan = loanCount(loan, rate, payment); printf ("Balance remaining after first payment: $%.2f\n" , loan); loan = loanCount(loan, rate, payment); printf ("Balance remaining after second payment: $%.2f\n" , loan); loan = loanCount(loan, rate, payment); printf ("Balance remaining after third payment: $%.2f\n" , loan); }
编写一个程序,以月/日/年(即 mm/dd/yy)的格式接受用户录入的日期信息,并以年月日(即yyyymmdd)的格式将其显示出来。
输出范例:
Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date 20110217
1 2 3 4 5 6 7 8 9 10 #include <stdio.h> int main () { int m, d, y; printf ("Enter a date (mm/dd/yyyy): " ); scanf ("%d/%d/%d" , &m, &d, &y); printf ("You entered the date %04d%02d%02d" , y, m, d); return 0 ; }
分数加法(Fraction Addition) 修改如下程序,使用户可以同时输入两个分数,中间用加号隔开:
输出范例:
Enter two fractions separated by a plus sign: 5/6+3/4 The sum is 38/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> int main (void ) { int num1, denom1, num2, denom2, result_num, result_denom; printf ("Enter two fractions separated by a plus sign: " ); scanf ("%d/%d+%d/%d" , &num1, &denom1, &num2, &denom2); result_num = num1 * denom2 + num2 * denom1; result_denom = denom1 * denom2; printf ("The sum is %d/%d\n" , result_num, result_denom); return 0 ; }
实验二 24小时制-12小时制转换(24-hour to 12-hour) 编写一个程序,要求用户输入 24 小时制的时间,将其转换为 12 小时制的格式。
输出范例:
Enter a 24-hour time: 21:11 Equivalent 12-hour time: 9:11 PM
Enter a 24-hour time: 0:01 Equivalent 12-hour time: 12:01 AM
注意,不要把12:00显示为0:00。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <stdio.h> int main (void ) { int a, b; printf ("Enter a 24-hour time: " ); scanf ("%d:%d" , &a, &b); if (a >= 13 ) { a -= 12 ; printf ("Equivalent 12-hour time: %d:%02d PM" , a, b); return 0 ; } if (a >= 12 ) { printf ("Equivalent 12-hour time: %d:%02d PM" , a, b); } if (a < 1 ) { a += 12 ; printf ("Equivalent 12-hour time: %d:%02d AM" , a, b); return 0 ; } printf ("Equivalent 12-hour time: %d:%02d AM" , a, b); }
风速等级与描述(Wind Speed) 下面是用于测量风力等级的简化版,编写一个程序,要求用户输入风速(海里/小时),然后显示相应的描述。
速率(海里/⼩时) 描述 小于 1 Calm(无风) 1~3 Light air(轻风) 4~27 Breeze(微风) 28~47 Gale(⼤风) 48~63 Storm(暴风) ⼤于63 Hurricane(飓风)
输出范例:
Enter a wind speed: 1
Light air
Enter a wind speed: 27
Breeze
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> int main () { int rate; printf ("Enter a wind speed: " ); scanf ("%d" , &rate); if (rate < 1 ) { printf ("Calm" ); } else if (rate <= 3 ) { printf ("Light air" ); } else if (rate <= 27 ) { printf ("Breeze" ); } else if (rate <= 47 ) { printf ("Gale" ); } else if (rate <= 63 ) { printf ("Storm" ); } else { printf ("Hurricane" ); } return 0 ; }
通用产品代码(UPC, Universal Production Code) 修改如下程序,使其可以检测 UPC 的有效性。在用户输入UPC后,程序将输出 VALID 或 NOT VALID 。
输出范例:
Enter the first (single) digit: 0 Enter first group of five digits: 13800 Enter second group of five digits: 15173 Enter the last (single) digit: 5 VALID
Enter the first (single) digit: 0 Enter first group of five digits: 51500 Enter second group of five digits: 24128 Enter the last (single) digit: 7 NOT VALID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <stdio.h> int main (void ) { int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total, check; printf ("Enter the first (single) digit:" ); scanf ("%1d" , &d); printf ("Enter the first group of five digits:" ); scanf ("%1d%1d%1d%1d%1d" , &i1, &i2, &i3, &i4, &i5); printf ("Enter the second group of five digits:" ); scanf ("%1d%1d%1d%1d%1d" , &j1, &j2, &j3, &j4, &j5); printf ("Enter the last (single) digit: " ); scanf ("%d" , &check); first_sum = d + i2 + i4 + j1 + j3 + j5; second_sum = i1 + i3 + i5 + j2 + j4; total = 3 * first_sum + second_sum; check == 9 - ((total - 1 ) % 10 ) ? printf ("VALID\n" ) : printf ("NOT VALID\n" ); return 0 ; }
将百分制转换为等级制(Centesimal-grade to Letter-grade) 利用switch语句编写一个程序,把用数字表示的成绩转化为字母表示的等级。
使用下面的等级评定规则:A为90100,B为8089,C为7079,D为6069,F为0~59。如果成绩大于100或小于0显示出错信息。
输出范例:
Enter numerical grade: 84 Letter grade: B
Enter numerical grade: -1 Error, grade must be between 0 and 100.
提示:把成绩拆分成 2 个数字,然后使用 switch 语句判定十位上的数字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <stdio.h> int main () { int grade; printf ("Enter numerical grade: " ); scanf ("%d" , &grade); if (grade < 0 || grade > 100 ) { printf ("Error, grade must be between 0 and 100.\n" ); return 0 ; } grade /= 10 ; switch (grade) { case 10 : printf ("Letter grade: A" ); break ; case 9 : printf ("Letter grade: A" ); break ; case 8 : printf ("Letter grade: B" ); break ; case 7 : printf ("Letter grade: C" ); break ; case 6 : printf ("Letter grade: D" ); break ; case 5 : printf ("Letter grade: F" ); break ; case 4 : printf ("Letter grade: F" ); break ; case 3 : printf ("Letter grade: F" ); break ; case 2 : printf ("Letter grade: F" ); break ; case 1 : printf ("Letter grade: F" ); break ; case 0 : printf ("Letter grade: F" ); break ; default : printf ("Error, grade must be between 0 and 100.\n" ); break ; } return 0 ; }
最大公约数(GCD, Greatest Common Divisor) 编写程序,要求用户输入两个整数,然后计算这两个整数的最大公约数(GCD, Greatest Common Divisor)。
输出范例:
Enter two integers: 12 28 Greatest common divisor: 4
Enter two integers:1 9 Greatest common divisor:1
提示:求最大公约数的经典算法 Euclid 算法如下: 分别让变量 m 和 n 存储两个整数。如果 n 为 0,那么停止操作,m 中的值是 GCD ;否则计算 m 除以 n 的余数,把 n 保存到 m 中,并把余数保存到 n 中;重复上述操作,每次都先判断n是否为 0 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> int getGCD (int m, int n) { if (n == 0 ) return m; return getGCD(n, m % n); }int main () { int m, n; printf ("Enter two integers: " ); scanf ("%d %d" , &m, &n); printf ("Greatest common divisor: %d" , getGCD(m, n)); return 0 ; }
股经纪人的佣金(Brokerage) 在下列程序中添加循环,以便用户可以输入多笔交易并且程序可以计算每次的佣金。程序在用户输入的交易额为 0 时终止。
输出范例:
Enter value of trade: 30000 Commission:$166.00
Enter value of trade: 20000 Commission:$144.00
Enter value of trade: 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include <stdio.h> void fff (float value) { float commission; if (value < 2500.00f ) commission = 30.00f + .017 f * value; else if (value < 6250.00f ) commission = 56.00f + .0066 f * value; else if (value < 20000.00f ) commission = 76.00f + .0034 f * value; else if (value < 50000.00f ) commission = 100.00f + .0022 f * value; else if (value < 500000.00f ) commission = 155.00f + .0011 f * value; else commission = 255.00f + .0009 f * value; if (commission < 39.00f ) commission = 39.00f ; printf ("Commission: $%.2f\n\n" , commission); }int main (void ) { float value; printf ("Enter value of trade: " ); while (1 ) { scanf ("%f" , &value); if (!value) break ; fff(value); char c = getchar(); if (c == '\n' ) { break ; } } return 0 ; }
偶数平方(Square of Even Number) 编写程序,提示用户输入一个数 n,然后显示出 1~n 的所有偶数的平方值。
输出范例:
Enter a number: 100 4 16 36 64 100
Enter a number: 50 4 16 36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> #include <math.h> int main () { int n, i = 0 ; printf ("Enter a number: " ); scanf ("%d" , &n); while (i * i < n) { i += 2 ; printf ("%d\n" , i * i); } return 0 ; }
日历(Calendar Month) 编写程序显示单月的日历。用户指定这个月的天数和该月起始日是星期几。
输出范例:
Enter number of days in month: 31
Enter starting day of the week(1=Sun, 7=Sat): 3
日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> void outputCalender (int days, int start) { start %= 8 ; int counts = 1 ; for (int i = 0 ; i < (days + start - 1 ) / 7 + 1 ; i++) { for (int j = 0 ; j < 7 ; j++) { if ((i == 0 && j < start - 1 ) || counts > days) { printf ("\t" ); continue ; } printf ("%d\t" , counts++); } printf ("\n" ); } }int main () { int days, start; scanf ("%d" , &days); scanf ("%d" , &start); outputCalender(days, start); return 0 ; }
实验三 翻译(Translation) 编写程序可以把字母格式的电话号码翻译成数值格式:
Enter phone number: CALLATT
2255288
如果没有电话在身边,参考这里给出的字母在键盘上的对应关系:(2=ABC,3=DEF,4=GHI,5=JKL,6=MNO,7=PQRS,8=TUV,9=WXYZ)原始电话号码中的非字母字符(例如数字或标点符号)保持不变:
Enter phone number: 1-800-COL-LECT1-800-265-5328
可以假设任何用户输入的字母都是大写字母。
输出范例:
Enter phone number: 1-DCKS-A2D-OLED
1-3257-223-6533
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include <stdio.h> #include <ctype.h> int main () { char ch; while ((ch = getchar()) != '\n' ) { switch (ch) { case 'A' : case 'B' : case 'C' : putchar ('2' ); break ; case 'D' : case 'E' : case 'F' : putchar ('3' ); break ; case 'G' : case 'H' : case 'I' : putchar ('4' ); break ; case 'J' : case 'K' : case 'L' : putchar ('5' ); break ; case 'M' : case 'N' : case 'O' : putchar ('6' ); break ; case 'P' : case 'Q' : case 'R' : case 'S' : putchar ('7' ); break ; case 'T' : case 'V' : case 'U' : putchar ('8' ); break ; case 'Y' : case 'W' : case 'X' : case 'Z' : putchar ('9' ); break ; default : putchar (ch); break ; } } return 0 ; }
表达式求值(Expression Evaluation) 编写程序对表达式求值。
Enter an expression: 1+2.5*3 Value of expression: 10.5
表达式中的操作数是浮点数,运算符是+、-、*和/。表达式从左向右求值(所有运算符的优先级都⼀样)。
输出范例:
Enter an expression: 2+3*4-5/2 Value of expression: 7.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> int main () { char ch; float num, num2; scanf ("%f" , &num); while ((ch = getchar()) != '\n' ) { scanf ("%f" , &num2); switch (ch) { case '+' : num += num2; break ; case '-' : num -= num2; break ; case '*' : num *= num2; break ; case '/' : num /= num2; break ; default : break ; } } printf ("%f" , num); return 0 ; }
出现次数(Number Occurrences) 修改如下程序,使其打印⼀份列表,显示出每个数字在数中出现的次数。
输出范例:
Enter a number: 41271092
Digit: 0 1 2 3 4 5 6 7 8 9 Occurrences: 1 2 2 0 1 0 0 1 0 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <stdio.h> int main () { int num; int occ[10 ] = {0 }; printf ("Digit: 0 1 2 3 4 5 6 7 8 9" ); while ((num = getchar()) != '\n' ) { switch (num) { case '0' : occ[0 ]++; break ; case '1' : occ[1 ]++; break ; case '2' : occ[2 ]++; break ; case '3' : occ[3 ]++; break ; case '4' : occ[4 ]++; break ; case '5' : occ[5 ]++; break ; case '6' : occ[6 ]++; break ; case '7' : occ[7 ]++; break ; case '8' : occ[8 ]++; break ; case '9' : occ[9 ]++; break ; default : break ; } } printf ("Occurrences:" ); for (int i = 0 ; i < 10 ; i++) { printf ("%d " , occ[i]); } return 0 ; }
随机步法(Random Walk) 编写程序,生成一种贯穿10×10字符数组(初始时全为字符’.’)的“随机步法”。程序必须随机地从一个元素“走到”另一个元素,每次都向上、向下、向左或向右移动一个元素位置。已访问过的元素按访问顺序用字母A到Z进行标记。
下面是一个输出示例:
A . . . . . . . . . B C D . . . . . . . . F E . . . . . . . H G . . . . . . . . I . . . . . . . . . J . . . . . . . Z . K . . R S T U V Y . L M P Q . . . W X . . N O . . . . . . .
利用srand函数和rand函数产生随机数,然后查看次数除以4的余数。余数一共有4种可能的值(0、1、2和3),指示下一次移动的4种可能方向。在执行移动之前,需要检查两项内容:一是不能走到数组外面,二是不能走到已有字母标记的位置。只要一个条件不满足,就得尝试换一个方向移动。如果4个方向都堵住了,程序就必须终止了。下面是提前结束的一个示例:
A B G H I . . . . . . C F O J K . . . . . D E N M L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
因为Y的4个方向都堵住了,所以没有地方可以放置下一步的Z了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 #include <stdio.h> #include <stdlib.h> #include <time.h> void PrintMap (char Map[10 ][10 ]) { for (int i = 0 ; i < 10 ; i++) { for (int j = 0 ; j < 10 ; j++) { printf ("%c " , Map[i][j]); } printf ("\n" ); } }int SelectToward (int begin[2 ], char Map[10 ][10 ], int toward, int index, int boom) { boom++; char alphabet[26 ] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; switch (toward) { case 0 : if (begin[0 ] - 1 >= 0 && Map[begin[0 ] - 1 ][begin[1 ]] == '.' ) { Map[begin[0 ] - 1 ][begin[1 ]] = alphabet[index]; begin[0 ] -= 1 ; } else { return boom != 4 ? SelectToward(begin, Map, (toward + 1 ) % 4 , index, boom) : 1 ; } break ; case 1 : if (begin[0 ] + 1 < 10 && Map[begin[0 ] + 1 ][begin[1 ]] == '.' ) { Map[begin[0 ] + 1 ][begin[1 ]] = alphabet[index]; begin[0 ] += 1 ; } else { return boom != 4 ? SelectToward(begin, Map, (toward + 1 ) % 4 , index, boom) : 1 ; } break ; case 2 : if (begin[1 ] - 1 >= 0 && Map[begin[0 ]][begin[1 ] - 1 ] == '.' ) { Map[begin[0 ]][begin[1 ] - 1 ] = alphabet[index]; begin[1 ] -= 1 ; } else { return boom != 4 ? SelectToward(begin, Map, (toward + 1 ) % 4 , index, boom) : 1 ; } break ; case 3 : if (begin[1 ] + 1 < 10 && Map[begin[0 ]][begin[1 ] + 1 ] == '.' ) { Map[begin[0 ]][begin[1 ] + 1 ] = alphabet[index]; begin[1 ] += 1 ; } else { return boom != 4 ? SelectToward(begin, Map, (toward + 1 ) % 4 , index, boom) : 1 ; } break ; default : break ; } return 0 ; }void RandomWalk (int begin[2 ], char Map[10 ][10 ]) { time_t t; srand((unsigned )time(&t)); for (int i = 1 ; i < 26 ; i++) { int toward = rand() % 4 ; int result = SelectToward(begin, Map, toward, i, 0 ); if (result) { break ; } } }int main () { char walkMap[10 ][10 ] = {{'.' }}; for (int i = 0 ; i < 10 ; i++) { for (int j = 0 ; j < 10 ; j++) { walkMap[i][j] = '.' ; } } time_t t; srand((unsigned )time(&t)); int begin[2 ] = {0 , 0 }; walkMap[begin[0 ]][begin[1 ]] = 'A' ; RandomWalk(begin, walkMap); PrintMap(walkMap); return 0 ; }
加密(Encryption) 已知的最古老的一种加密技术是凯撒加密(得名于 Julius caesar)。该方法把一条消息中的每个字母用字母表中固定距离之后的那个字母来替代。(如果越过了字母Z,会绕回到字母表的起始位置。例如,如果每个字母都用字母表中两个位置之后的字母代替,那么Y就被替换为A,Z就被替换为B。)
编写程序用凯撒加密方法对消息进行加密。用户输入待加密的消息和移位计数(字母移动的位置数⽬):
Enter message to be encrypted: Go ahead, make my day. Enter shift amount (1-25): 3 Encrypted message: Jr dkhdg, pdnh pb gdb.
注意,当⽤户输⼊26与移位计数的差值时,程序可以对消息进⾏解密:
Enter message to be encrypted: Jr dkhdg, pdnh pb gdb. Enter shift amount (1-25): 23 Encrypted message: Go ahead, make my day
不是字母的那些字符不要改动。此外,加密时不要改变字母的大小写。
输出范例:
Enter message to be encrypted: Hello, world! Enter shift amount (1-25): 3 Encrypted message: Khoor, zruog!
Enter message to be encrypted: Khoor, zruog! Enter shift amount (1-25): 23 Encrypted message: Hello, world!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <stdio.h> #include <string.h> #include <ctype.h> int main () { char str[50 ]; int amounts; fgets(str, sizeof (str), stdin ); fflush(stdin ); scanf ("%d" , &amounts); for (int i = 0 ; i < strlen (str); i++) { if (str[i] > 64 && str[i] < 91 ) { str[i] = (str[i] + amounts) > 90 ? 64 + ((str[i] + amounts) - 90 ) : (str[i] + amounts); } if (str[i] > 96 && str[i] < 123 ) { str[i] = (str[i] + amounts) > 122 ? 96 + ((str[i] + amounts) - 122 ) : (str[i] + amounts); } putchar (str[i]); } return 0 ; }
实验四 栈(Stack) 修改如下程序,使它存储字符而不是整数。
增加 main 函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套是否正确。
输出范例:
Enter parentheses and/or braces: ()({})({})
Parentheses/braces are nested properly
Enter parentheses and/or braces: ((}
Parentheses/braces are NOT nested properly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 char contents[STACK_SIZE];int top = 0 ;void stack_overflow (void ) { printf ("Stack overflow\n" ); exit (EXIT_FAILURE); }void stack_underflow (void ) { printf ("Stack underflow\n" ); exit (EXIT_FAILURE); }void make_empty (void ) { top = 0 ; }bool is_empty (void ) { return top == 0 ; }bool is_full (void ) { return top == STACK_SIZE; }void push (char ch) { if (is_full()) stack_overflow(); else contents[top++] = ch; }char pop (void ) { if (is_empty()) stack_underflow(); else return contents[--top]; return '\0' ; }int main (void ) { char ch; while ((ch = getchar()) != '\n' ) { if (ch == '(' || ch == '[' || ch == '{' ) { push(ch); } else if (ch == ')' || ch == ']' || ch == '}' ) { if (is_empty()) { printf ("Parentheses/braces are NOT nested properly" ); return 0 ; } char a = pop(); if ((ch == ')' && a != '(' ) || (ch == ']' && a != '[' ) || (ch == '}' && a != '{' )) { top++; break ; } } } if (top == 0 ) { printf ("Parentheses/braces are nested properly" ); } else { printf ("Parentheses/braces are NOT nested properly" ); } make_empty(); }
逆序(Reversal) 编写程序读一条消息,然后逆序打印出这条消息。
输出范例:
Enter a message: Don’t get mad, get even. Reversal is: .neve teg ,dam teg t’noD
Enter a message: Hello, world! Reversal is: !dlrow ,olleH
提示:一次读取消息中的一个字符(用getchar函数),并且把这些字符存储在数组中,当数组写满或者读到字符 ‘\n’ 时停止读入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> int main (void ) { char ch, arr[50 ]; int i = 0 ; while ((ch = getchar()) != '\n' ) { arr[i] = ch; i++; } for (int j = i; j > -1 ; j--) { putchar (arr[j]); } return 0 ; }
最大最小单词简略版(The Biggest and Smallest Words) 编写程序找出一组单词中“最小”单词和“最大”单词。
用户输入单词后,程序根据字典顺序决定排在最前面和最后面的单词。
当用户输入4个字母的单词时,程序停止读入。假设所有单词都不超过20个字母。
输出范例1 :
Enter word: dog Enter word: zebra Enter word: rabbit Enter word: catfish Enter word: walrus Enter word: cat Enter word: fish
Smallest word: cat Largest word zebra
输出范例2:
Enter word: computer Enter word: ink Enter word: light Enter word: bookrack Enter word: book
Smallest word: book Largest word: light
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include <stdio.h> #include <string.h> int isMin (char chNew[], char chOld[]) { for (int i = 0 ; i < strlen (chNew); i++) { if (chNew[i] < chOld[i]) return 1 ; if (chNew[i] != chOld[i]) return 0 ; } return 0 ; }int isMax (char chNew[], char chOld[]) { for (int i = 0 ; i < strlen (chNew); i++) { if (chNew[i] > chOld[i]) return 1 ; if (chNew[i] != chOld[i]) return 0 ; } return 1 ; }char *getSmallest (char words[][30 ], int counts) { int record = 0 ; for (int i = 1 ; i < counts; i++) { if (isMin(words[i], words[record])) { record = i; } } return words[record]; }char *getBiggest (char words[][30 ], int counts) { int record = 0 ; for (int i = 1 ; i < counts; i++) { if (isMax(words[i], words[record])) { record = i; } } return words[record]; }int main () { char words[30 ][30 ]; int len = 0 , counts = 0 ; while (len != 4 ) { scanf ("%s" , words[counts++]); len = strlen (words[counts - 1 ]); } printf ("Smallest word: %s\n" , getSmallest(words, counts)); printf ("Largest word: %s" , getBiggest(words, counts)); return 0 ; }
实验五 01 main函数改造。 修改inventory.c (点击下载 )程序,使 inventory 和 num_parts 为 main 函数的局部变量。
本小题只需完成 main 函数的改造即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #define NAME_LEN 25 #define MAX_PARTS 100 struct part { int number; char name[NAME_LEN+1 ]; int on_hand; };int read_line (char str[], int n) ;int find_part (int number, const struct part inv[], int np) ;void insert (struct part inv[], int *np) ;void search (const struct part inv[], int np) ;void update (struct part inv[], int np) ;void print (const struct part inv[], int np) ;
该部分声明已包含在 “lab51.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <stdio.h> #include <stdlib.h> #include "lab51.h" int main () { struct part inventory [MAX_PARTS ]; int number_parts = 0 ; char code; for (;;) { printf ("Enter operation code:" ); scanf ("%c" , &code); while (getchar() != '\n' ) ; switch (code) { case 'i' : insert(inventory, &number_parts); break ; case 's' : search(inventory, number_parts); break ; case 'u' : update(inventory, number_parts); break ; case 'p' : print(inventory, number_parts); break ; case 'q' : return 0 ; default : printf ("Illegal code\n" ); } printf ("\n" ); } }
02 insert 函数改造 修改inventory.c (点击下载 )程序,使 inventory 和 num_parts 为 main 函数的局部变量。
本小题只需完成 insert 函数的改造即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #define NAME_LEN 25 #define MAX_PARTS 100 struct part { int number; char name[NAME_LEN+1 ]; int on_hand; };int read_line (char str[], int n) ;int find_part (int number, const struct part inv[], int np) ;void insert (struct part inv[], int *np) ;void search (const struct part inv[], int np) ;void update (struct part inv[], int np) ;void print (const struct part inv[], int np) ;
该部分声明已包含在 “lab51.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include "lab51.h" #include <stdio.h> #include <stdlib.h> void insert (struct part inv[], int * np) { int part_number; if (*np == MAX_PARTS) { printf ("Database is full; cannot add more parts.\n" ); return ; } printf ("Enter part number:" ); scanf ("%d" , &part_number); if (find_part(part_number, inv, *np) >= 0 ) { printf ("Part already exits.\n" ); return ; } inv[*np].number = part_number; printf ("Enter part name:" ); read_line(inv[*np].name, NAME_LEN); printf ("Enter quantity on hand:" ); scanf ("%d" , &inv[*np].on_hand); (*np)++; }
03 search 函数改造 修改inventory.c (点击下载 )程序,使 inventory 和 num_parts 为 main 函数的局部变量。
本小题只需完成 search 函数的改造即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #define NAME_LEN 25 #define MAX_PARTS 100 struct part { int number; char name[NAME_LEN+1 ]; int on_hand; };int read_line (char str[], int n) ;int find_part (int number, const struct part inv[], int np) ;void insert (struct part inv[], int *np) ;void search (const struct part inv[], int np) ;void update (struct part inv[], int np) ;void print (const struct part inv[], int np) ;
该部分声明已包含在 “lab51.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> #include <stdlib.h> #include "lab51.h" void search (const struct part inv[], int np) { int i, number; printf ("Enter part number: " ); scanf ("%d" , &number); i = find_part(number, inv, np); if (i >= 0 ) { printf ("Part name: %s\n" , inv[i].name); printf ("Quantity on hand: %d\n" , inv[i].on_hand); } else printf ("Part not found.\n" ); }
04 update 函数改造 修改inventory.c (点击下载 )程序,使 inventory 和 num_parts 为 main 函数的局部变量。
本小题只需完成 update 函数的改造即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #define NAME_LEN 25 #define MAX_PARTS 100 struct part { int number; char name[NAME_LEN+1 ]; int on_hand; };int read_line (char str[], int n) ;int find_part (int number, const struct part inv[], int np) ;void insert (struct part inv[], int *np) ;void search (const struct part inv[], int np) ;void update (struct part inv[], int np) ;void print (const struct part inv[], int np) ;
该部分声明已包含在 “lab51.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio.h> #include <stdlib.h> #include "lab51.h" void update (struct part inv[], int np) { int i, number, change; printf ("Enter part number: " ); scanf ("%d" , &number); i = find_part(number, inv, np); if (i >= 0 ) { printf ("Enter change in quantity on hand: " ); scanf ("%d" , &change); inv[i].on_hand += change; } else printf ("Part not found.\n" ); }
05 print 函数改造 修改inventory.c (点击下载 )程序,使 inventory 和 num_parts 为 main 函数的局部变量。
本小题只需完成 print 函数的改造即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #define NAME_LEN 25 #define MAX_PARTS 100 struct part {int number;char name[NAME_LEN+1 ];int on_hand; };int read_line (char str[], int n) ;int find_part (int number, const struct part inv[], int np) ;void insert (struct part inv[], int *np) ;void search (const struct part inv[], int np) ;void update (struct part inv[], int np) ;void print (const struct part inv[], int np) ;
该部分声明已包含在 “lab51.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> #include <stdlib.h> #include "lab51.h" void print (const struct part inv[], int np) { int i; printf ("Part Number Part Name " "Quantity on Hand\n" ); for (i = 0 ; i < np; i++) printf ("%7d %-25s%11d\n" , inv[i].number, inv[i].name, inv[i].on_hand); }
实验六 链表:01-软件界面控制 实现一个数字选项式的启动界面,程序输入数据为(1-8),参考界面如下:
显示所有商品的信息 修改某个商品的信息 插入某个商品的信息 删除某个商品的信息 查找某个商品的信息 商品存盘并退出系统 对商品价格进行排序 (慎用)删除所有内容 其他. 不存盘并退出系统
程序当输入1-8时,执行相关功能,完成后再次显示菜单让用户选择。当输入其它数值时,程序不存盘并退出系统。
本小题只需实现 main 函数即可,相关结构及函数声明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #define GOODS_FILE_NAME "goodsinfo.txt" #define MAX_ID_LEN 30 #define MAX_NAME_LEN 30 #define MAX_PRICE_LEN 30 #define MAX_DISCOUNT_LEN 30 typedef struct { char goods_id[MAX_ID_LEN]; char goods_name[MAX_NAME_LEN]; int goods_price; char goods_discount[MAX_DISCOUNT_LEN]; int goods_amount; int goods_remain; } GoodsInfo;typedef struct node { GoodsInfo data; struct node *next ; } GoodsList; GoodsInfo read_goods_info () ;void init_list (GoodsList **pL) ;void destory_list (GoodsList **pL) ;void destory_list_and_file (GoodsList **pL) ;int save_to_file (GoodsList *L) ;void output_one_item (GoodsList *L) ;void output_all_items (GoodsList *L) ;bool insert_item (GoodsList *L, GoodsInfo item, int choice) ;bool delete_item (GoodsList *L, char * goods_id) ; GoodsList* search_item (GoodsList *L, char * goods_id) ;bool change_item (GoodsList *L, char * goods_id, GoodsInfo new_info) ;void bubble_sort (GoodsList *L) ;int read_line (char str[], int n) ;
该部分声明已包含在 “lab52.h”中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 #include <stdio.h> #include <stdlib.h> #include "lab52.h" int main () { int code; GoodsList *GoodsList; init_list(&GoodsList); char id[MAX_ID_LEN]; char *list = "1.显示所有商品的信息\n2.修改某个商品的信息\n3.插入某个商品的信息\n4.删除某个商品的信息\n5.查找某个商品的信息\n6.商品存盘并退出系统\n7.对商品价格进行排序\n8.(慎用)删除所有内容\n其他.不存盘并退出系统" ; while (1 ) { printf ("%s" , list ); scanf ("%d" , &code); switch (code) { case 1 : output_all_items(GoodsList); break ; case 2 : { read_line(id, MAX_ID_LEN); GoodsInfo newInfo = read_goods_info(); change_item(GoodsList, id, newInfo); break ; } case 3 : { int choice; scanf ("%d" , &choice); GoodsInfo newInfo = read_goods_info(); insert_item(GoodsList, newInfo, choice); break ; } case 4 : { read_line(id, MAX_ID_LEN); delete_item(GoodsList, id); break ; } case 5 : { read_line(id, MAX_ID_LEN); search_item(GoodsList, id); break ; } case 6 : save_to_file(GoodsList); destory_list(&GoodsList); return 0 ; case 7 : bubble_sort(GoodsList); break ; case 8 : destory_list_and_file(&GoodsList); break ; default : destory_list(&GoodsList); return 0 ; } } }
链表:02-初始化 函数原型:void init_list(GoodsList **L)
其中 *L 为指向链表头结点的指针,L为指向链表头结点指针的地址,init_list首先创建链表的头结点,之后读取goodsinfo.txt(示例文件下载) 中的商品信息,并初始化链表,函数执行后,需确保 *L 为指向链表头结点的指针。
init_list 实现商品信息链表的初始化,函数从已有的商品信息文件goodsinfo.txt(示例文件下载) 中读入商品信息,并且分配内存保存至链表中。 为了方便在表头插入和删除结点的操作,经常在表头结点(存储第一个元素的结点)的前面增加一个结点,称之为头结点或表头附加结点。这样原来的表头指针由指向第一个元素的结点改为指向头结点,头结点的数据域为空,头结点的指针域指向第一个元素的结点。
注:使用 fopen 函数打开文件时,文件名必须为 GOODS_FILE_NAME,否则检测不通过。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void init_list (GoodsList **pL) { *pL = (GoodsList *)malloc (sizeof (GoodsList)); (*pL)->next = NULL ; GoodsList *Cur = *pL; GoodsInfo info; FILE *f; f = fopen(GOODS_FILE_NAME, "r" ); if (!f) { perror("open file for reading" ); exit (0 ); } while (fscanf (f, "%s\t%s\t%d\t%s\t%d\t%d\n" , info.goods_id, info.goods_name, &info.goods_price, info.goods_discount, &info.goods_amount, &info.goods_remain) != EOF) { GoodsList *L = (GoodsList *)malloc (sizeof (GoodsList)); L->data = info; L->next = Cur->next; Cur->next = L; Cur = Cur->next; CurrentCnt++; } fclose(f); f = NULL ; }
链表:03-插入 函数原型:bool insert_item(GoodsList *L, GoodsInfo goodsInfo, int choice),
L为指向链表头结点的指针,函数根据choise的值,将goodsInfo插入到链表的指定位置,如果成功插入,函数返回true,如果未插入,函数返回false。
分别实现头插法、尾插法,中间位置插入三种:
用户输入0,将商品信息插入到链表尾部; 用户输入1,将商品信息插入到链表头部; 用户输入其它正整数i,将商品信息插入到链表中间第i号位置, 例如:输入3,应该在第二个节点后插入新节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; bool insert_item (GoodsList *L, GoodsInfo item, int choice) { GoodsList *pL = (GoodsList *)malloc (sizeof (GoodsList)); pL->data = item; GoodsList *Cur = L; switch (choice) { case 0 : { while (Cur->next) Cur = Cur->next; pL->next = NULL ; Cur->next = pL; break ; } case 1 : pL->next = L->next; L->next = pL; break ; default : { int k = 1 ; while (k++ != choice) { if (!Cur->next || choice < 0 ) return false ; Cur = Cur->next; } pL->next = Cur->next; Cur->next = pL; break ; } } CurrentCnt++; return true ; }
链表:04-删除节点 函数原型:bool delete_item(GoodsList L, char goods_id)
其中L为指向链表头结点的指针,goods_id为要删除商品的ID;
如果成功删除,函数返回true,否则返回false。
delete_item 根据商品的ID来删除对应的商品信息,商品查找通过字符串比较的方式,查找到后释放对应指针指向的内存区域,完成删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; bool delete_item (GoodsList *L, char *goods_id) { GoodsList *Cur = L, *Del = Cur->next; while (Del) { if (!strcmp (Del->data.goods_id, goods_id)) { Cur->next = Del->next; free (Del); CurrentCnt--; return true ; } Del = Del->next; Cur = Cur->next; } return false ; }
链表:05-查找 函数原型:GoodsList* search_item(GoodsList L, char goods_id)
其中L为指向链表头结点的指针,goods_id为要查找商品的ID;
如果找到该商品,函数返回该商品对应的结点指针,否则返回 NULL.
根据输入的商品 ID 来查找对应的商品信息,商品 ID 的判断用字符串比较的方式来实现,然后调用格式化显示查找到的商品信息.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; GoodsList *search_item (GoodsList *L, char *goods_id) { GoodsList *Cur = L; while (Cur) { if (!strcmp (Cur->data.goods_id, goods_id)) { return Cur; } Cur = Cur->next; } return NULL ; }
链表:06-修改 函数原型:bool change_item(GoodsList L, char goods_id, GoodsInfo new_info)
其中 L 为指向链表头结点的指针,goods_id 为要修改的商品 ID,new_info 为该商品的最新数据。函数成功修改后返回true,否则返回 false。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; bool change_item (GoodsList *L, char *goods_id, GoodsInfo new_info) { GoodsList *Cur = L; while (Cur) { if (!strcmp (Cur->data.goods_id, goods_id)) { Cur->data = new_info; return true ; } Cur = Cur->next; } return false ; }
链表:07-显示单个节点 函数原型:void output_one_item(GoodsList *p),函数显示结点为 p 的商品信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void output_one_item (GoodsList *L) { printf ("%s" , L->data.goods_id); printf ("%s" , L->data.goods_name); printf ("%d" , L->data.goods_price); printf ("%s" , L->data.goods_discount); printf ("%d" , L->data.goods_amount); printf ("%d" , L->data.goods_remain); }
链表:08-显示所有节点 函数原型:void output_all_items(GoodsList *L),
L为指针链表头结点的指针,output_all_items 调用 output_one_item 函数,将链表中所有的商品信息以格式化的方式打印输出到屏幕上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void output_all_items (GoodsList *L) { GoodsList *Cur = L->next; while (Cur) { output_one_item(Cur); Cur = Cur->next; } }
链表:09-释放链表 函数原型:void destory_list(GoodsList **L),
该函数释放包括头结点在内的所有结点,完成后指向链表头结点的指针为NULL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void destory_list (GoodsList **pL) { GoodsList *Cur = *pL, *Des; while (Cur->next) { Des = Cur->next; Cur->next = Des->next; free (Des); } free (*pL); (*pL) = NULL ; CurrentCnt = 0 ; }
链表:10-释放链表并删除文件 函数原型:void destory_list_and_file(GoodsList **L),
该函数调用destory_list释放包括头结点在内的所有结点,将指向链表头结点的指针为NULL,最后删除存储商品信息的文件goodinfo.txt。
1 2 3 4 5 6 7 8 9 10 11 12 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void destory_list_and_file (GoodsList **pL) { destory_list(pL); remove("goodsinfo.txt" ); }
链表:11-保存文件 函数原型:int save_to_file(GoodsList *L)
将当前链表所有的商品信息保存到文件 goodsinfo.txt 中,其中L为指向链表头结点的指针,保存成功后,该函数返回商品的数量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include "lab52.h" #include <stdio.h> #include <stdlib.h> extern int CurrentCnt; int save_to_file (GoodsList *L) { if (!L) return 0 ; GoodsList *Cur = L->next; FILE *f = fopen("goodsinfo.txt" , "w" ); int counts = 0 ; if (!f) return 0 ; while (Cur) { fprintf (f, "%s\t%s\t%d\t%s\t%d\t%d\n" , Cur->data.goods_id, Cur->data.goods_name, Cur->data.goods_price, Cur->data.goods_discount, Cur->data.goods_amount, Cur->data.goods_remain); Cur = Cur->next; counts++; } fclose(f); f = NULL ; return counts; }
链表:12-排序 函数原型:void bubble_sort(GoodsList *L)
L为指向链表头结点的指针,该函数利用冒泡排序算法,按价格从低到高对商品进行排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; void bubble_sort (GoodsList *L) { GoodsList *Cur = L->next; GoodsInfo temp; for (int i = 0 ; i < CurrentCnt - 1 ; i++) { for (int j = 1 ; j < CurrentCnt - i; j++) { if (Cur->data.goods_price > Cur->next->data.goods_price) { temp = Cur->data; Cur->data = Cur->next->data; Cur->next->data = temp; } Cur = Cur->next; } Cur = L->next; } }
链表:13-读商品信息 函数原型:GoodsInfo read_goods_info()
该函数调用read_line及scanf等函数,按“商品ID、商品名称、商品价格、商品折扣、商品数量、商品剩余数量”的顺序让用户输入,并将这些信息保存到 GoodsInfo 结构体中,函数最后返回该结构体。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> #include <stdlib.h> #include "lab52.h" extern int CurrentCnt; GoodsInfo read_goods_info () { GoodsInfo goods; read_line(goods.goods_id, MAX_ID_LEN); read_line(goods.goods_name, MAX_NAME_LEN); scanf ("%d" , &goods.goods_price); read_line(goods.goods_discount, MAX_DISCOUNT_LEN); scanf ("%d" , &goods.goods_amount); scanf ("%d" , &goods.goods_remain); return goods; }