回复 20楼 TonyDeng
没有看到讲gets的,很多东西我04年上学时候学的,因为当时候开的这个C语言课程,但是不是专业课,教的不深入,学的也没有那么多,毕业后工作也不是这方面的,很多东西都模糊的印象,现在想重拾起来,重新再认真学学,所以现在写的东西,有时候更多的是我之前的记忆,霍顿的书上我还没看到这部分内容,看到第六章了,不过感觉有的东西也很老的。我用的是C-Free 5.0的编译器
2015-07-20 13:13
2015-07-20 13:20
。。谢谢,很多地方安全是小白,所以可能会闹不少看起来比较白痴或者低级的错误、笑话,各位多见谅。
2015-07-20 13:36
2015-07-20 13:38
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int a,b,c; //存储英文对应的运算数字
char i; //存储运算符
int m,n,d; //用于取余
int ag; //控制再次运算
int j, k; //j对应字符数组下标,k用于循环判断对应下标
const char string[][10]={
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"threeteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
};
char str1[10], str2[10];
printf("这是一个十进制简单的计算器,欢迎使用\n\n");
do
{
system("cls");
printf("请输入 + 选择加法运算\tEnter确认\n\n");
printf("请输入 - 选择减法运算\tEnter确认\n\n");
printf("请输入 * 选择乘法运算\tEnter确认\n\n");
printf("请输入 / 选择除法运算\tEnter确认\n\n");
printf("请输入 %% 选择求余运算\tEnter确认\n\n");
printf("----------------------------------\n\n");
printf("请选择上述五种运算符后Enter,否则输入其他任意键后Enter确认退出\n\n");
scanf("%c",&i);
if(i=='0')
break;
printf("\n");
if(i=='+')
{
fflush(stdin);//清空输入缓冲
printf("请输入第一个数字,然后按Enter\n\n");
gets(str1);
for(j=0;j<10;j++)
{
if(strcmp(str1,string[j])==0)
{
a = j;
break;
}
}
printf("a=%d",a);
printf("\n");
printf("请输入第二个数字,然后按Enter\n\n");
gets(str2);
for(j=0;j<10;j++)
{
if(strcmp(str2,string[j])==0)
{
b = j;
break;
}
}
printf("b=%d",b);
printf("\n");
c = a + b; //直接a+b就行了,不要函数了
printf("\n%s plus %s is %s\n\n",str1, str2, string[c]);
getchar();
printf("请输入1后Enter再次运算,或非1任意键后按Enter退出\n\n");
scanf("%d",&ag);
getchar();
printf("\n");
}
}while(1);
}
2015-07-20 13:42
2015-07-20 13:55
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
// 英文数目对照表,每个字符串的下标即为其对应数值
const char* NumberList[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
// 程序入口函数
// 备注:
// 1.非vc编译器用scanf()/printf()等旧式库函数取代scanf()/printf_s()新式安全型函数
// 2.非vc没有_getch()函数[在conio.h头],自己用别的办法暂停程序
// 3._countof()[在stdlib.h头]是计算一个数组的元素数目,微软扩展
int main(void)
{
char theFirstWord[10]; // 第一个单词
char theSecondWord[10]; // 第二个单词
char operation; // 运算符
int theFirst, theSecond; // 运算数值
double result; // 运算结果
int count;
printf_s("请输入运算表达式(每项之间用空格分隔):\n");
fflush(stdin); // 清空键盘输入缓冲区
count = scanf_s("%s %c %s",
theFirstWord, _countof(theFirstWord),
&operation, 1,
theSecondWord, _countof(theSecondWord)
);
if (count == 3) // scanf()函数族返回成功读取的项数,若不对应即为出错
{
// 翻译第一个单词为数值
for (theFirst = 0; theFirst < _countof(NumberList) && (strcmp(theFirstWord, NumberList[theFirst]) != 0); ++theFirst)
{
;
}
if (theFirst == _countof(NumberList))
{
theFirst = 0;
}
// 翻译第二个单词为数值
for (theSecond = 0; theSecond < _countof(NumberList) && (strcmp(theSecondWord, NumberList[theSecond]) != 0); ++theSecond)
{
;
}
if (theSecond == _countof(NumberList))
{
theSecond = 0;
}
// 进行运算
result = 0;
switch (operation)
{
case '+':
result = theFirst + theSecond;
break;
case '-':
result = theFirst - theSecond;
break;
case '*':
result = theFirst * theSecond;
break;
case '/':
if (theSecond != 0)
{
result = 1.0 * theFirst / theSecond;
}
break;
default:
break;
}
// 输出结果
printf_s("%d %c %d = %.4f\n", theFirst, operation, theSecond, result);
}
else
{
printf_s("输入错误\n");
}
printf_s("\nPress any key to continue...");
_getch();
return EXIT_SUCCESS;
}

2015-07-20 14:04
2015-07-20 14:19

2015-07-20 14:26
2015-07-20 14:31