当前位置: 代码迷 >> C语言 >> [求助]如何将字符串复制到二维数组里
  详细解决方案

[求助]如何将字符串复制到二维数组里

热度:367   发布时间:2007-08-26 08:53:25.0
[求助]如何将字符串复制到二维数组里


请大家帮忙看看, 就是把最新选择的菜单选项的function的输出部分显示在第3个function里面。。。。
1) Interest Calculator
2) sort line
3) summary
4) exit
--------------------------------
enter option: 2
enter a string: qwe234
new string is: ewq

--------------------------------
enter option: 1
enter amount: 100
enter year: 2
Investment is: 132.25

--------------------------------
enter option: 3 //这个function不知道怎么做,还请大家帮帮我 。。。。

1) new string is: ewq
2) Investment is: 132.25

如果先选1再选2,就显示:
1)Investment is: 132.25
2) new string is: ewq

code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define OUTPUT_LEN 80
#define OUTPUT_MAX 2
#define INTEREST_RATE 15
#define PERCENT 0.01
#define STRLEN 40
#define ONE 1

void initOutputs(char[OUTPUT_MAX][OUTPUT_LEN]);
void interestCalculator(char[OUTPUT_MAX][OUTPUT_LEN]);
void sortLine(char[OUTPUT_MAX][OUTPUT_LEN]);
void summary(char[OUTPUT_MAX][OUTPUT_LEN]);
void readRestOfLine();

int main(void)
{

char lastOutputs[OUTPUT_MAX][OUTPUT_LEN];
char choice;
int done = 0;

initOutputs(lastOutputs);

while(!done)
{
printf("\nMenu:");
printf("\n1) Interest Calculator");
printf("\n2) Sort line");
printf("\n3) Session Summary");
printf("\n4) Exit");
printf("\nEnter option: ");

choice = getchar();

switch(choice)
{
case '1':
interestCalculator(lastOutputs);
break;
case '2':
sortLine(lastOutputs);
break;
case '3':
summary(lastOutputs);
break;
case '4':
exit(0);
break;
default :
break;
}
}

return EXIT_SUCCESS;
}

//这个function是题里本来就有的
void initOutputs(char lastOutputs[OUTPUT_MAX][OUTPUT_LEN])
{
int i;

for (i = 0; i < OUTPUT_MAX; i++)
{
strcpy(lastOutputs[i], "");
}
}

void interestCalculator(char lastOutputs[OUTPUT_MAX][OUTPUT_LEN])//这里只有一个参数也是固定的
{
int investmentAmount, year;
double fixRate, rate, newInvestment;
char string[70] = "Your investment grows to: ";
char str;

printf("1) Interest Calculator\n");
printf("----------------------\n");
printf("Enter amount: ");
scanf("%d", &investmentAmount);

printf("\nEnter year: ");
scanf("%d", &year);

fixRate = (double)(ONE + (INTEREST_RATE*PERCENT));

rate = (double)pow(fixRate, year);
newInvestment = investmentAmount * rate;

printf("\nInvestment is: $%.2lf", newInvestment);

}

void sortLine(char lastOutputs[OUTPUT_MAX][OUTPUT_LEN])
{
char str[STRLEN],str1[STRLEN];
int i, j, k, n, temp, length;

printf("\n2) Sort Line");
printf("\n------------\n");

printf("\nEnter a string: ");
scanf("%s", str);

length = strlen(str);
if(length < STRLEN)
{
for(i = 0; i < length; i++)
{
for(j = i+1; j < length; j++)
{
if(str[i] > str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

for(n = 0,k = 0; k < length; k++)
{
if(isalpha((int)str[k]))
{
str[k] = tolower(str[k]);
str1[n++] = str[k];
str1[n]='\0';
}
}
printf("\nnew string is: %s", str1);
}
}

void summary(char lastOutputs[OUTPUT_MAX][OUTPUT_LEN])
{
//这个function不会。。。。
}

谢谢~~

[此贴子已经被作者于2007-8-26 8:58:35编辑过]

搜索更多相关的解决方案: 字符  

----------------解决方案--------------------------------------------------------
没人可以帮我吗?
谁能给我个思路??拜托~~~~


----------------解决方案--------------------------------------------------------
  相关解决方案